DEV Community

Simon Green
Simon Green

Posted on

Quater-imaginary Business Date

Weekly Challenge 178

Challenge, My solutions

Task 1: Quater-imaginary Base

Task

Write a script to convert a given number (base 10) to quater-imaginary base number and vice-versa.

My solution

For this task, I chose to only do the first half of the challenge, that is take a base 10 numbers and covert it to a base -4 number. The general principle is take the number and divide it by -4, where the remainder makes up the solution. I had a few challenges when the resulting division was a negative number, but believe I've got that sorted.

Examples

$ ./ch-1.py 4
10300

$ ./ch-1.py 15
10103
Enter fullscreen mode Exit fullscreen mode

Task 2: Business Date

Task

You are given $timestamp (date with time) and $duration in hours.

Write a script to find the time that occurs $duration business hours after $timestamp. For the sake of this task, let us assume the working hours is 9am to 6pm, Monday to Friday. Please ignore timezone too.

My solution

Time maths is never easy, at least I don't need to worry about timezones, although that wouldn't really be an issue as most (all?) countries change their clocks outside business hours. The excpetion would be Samoa (and later Tokelau) who skipped a whole day on Friday 30/12/2011.

There are a couple of ways to tackle this task, and I think the biggest challenge is handling a span that covers a weekend. I may have over complicated my solution, let's see what other Team PWC members come up with.

I start by converting the time input with strptime. For Python, this is part of the datetime module, and the DateTime module for Perl. I also convert the hours provided into minutes. I also check that the start time is between 9am and 6pm on a weekday.

Next I wind the clock back to 9am Monday. I add the number of working minutes we wound the clock back to the minutes variable (days x 540, hours after 9am x 60 and minutes). We now know how many minutes after 9am Monday we need to add. I split this into days and minutes.

For every five whole days, we need to take 2 extra days for the weekend. Finally I use date math taken the 9am Monday time, and adding the required number of days and minutes to find the end time.

Examples

$ ./ch-2.py "2022-08-01 10:30" 4
2022-08-01 14:30

$ ./ch-2.py "2022-08-01 17:00" 3.5
2022-08-02 11:30
Enter fullscreen mode Exit fullscreen mode

Top comments (0)