DEV Community

Simon Green
Simon Green

Posted on

1

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay