Custom Day Spans with Arrow

Arrow is a wonderful library for python that makes the tedium of date and time conversions a relative joy when compared with the alternatives. I was recently tasked with a project that required splitting up time between given start and end dates in chunks of n-many days each, starting at an arbitrary point in time. At first, I figured this would be a perfect use of Arrow’s range and span functions.

I first tried span_range, but quickly realized that this would round the start datetime back to the beginning of the day our point of time is in:

Alright, not ideal, let’s try just range and I’ll offset the end myself, easy enough:

Well, that looks better, but range doesn’t accept strings like “5 days”. So to do arbitrary day ranges, I would have to compress this list. At this point, I took a step back from the functions I had set out to use and looked back at the rest of the Arrow API and figured out a pretty easy way to use a function I’ve already been leveraging to do the job, replace.

Hey it works! And it’s readable! It pays to read the docs.

Tags: ,


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s