Timezone for datetime.date objects

Chris Angelico rosuav at gmail.com
Mon Feb 28 17:55:33 EST 2022


On Tue, 1 Mar 2022 at 09:28, Morten W. Petersen <morphex at gmail.com> wrote:
>
> Hi Chris, Cameron.
>
> Well, let's say I specify the datetime 2022-02-22 02:02 (AM). I think everyone could agree that it also means 2022-02-22 02:02:00:00, to 2022-02-22 02:02:59:59.
>

Not sure how many :59s you want there :) I'm going to assume you mean
"02:02:00" to "02:02:59".

> And I think the same applies for a date. If the pipes are clogged and I can't take (give) a shit, a shower or do anything else involving fluids, I can just leave the keys under the doormat, and agree a date with the plumber, and go off to a friend of relatives' place for a couple of days while waiting for the plumber to do the necessary work.
>

That is one of the fundamental differences between humans and
computers. Humans are very VERY sloppy with time descriptions. With
computers, it's much better to be clear about time ranges; a time does
not imply a specific window size. (And autistic humans are more like
computers.)

> Usually that would imply that the plumber visits from 07:00 to 15:00 on the given date, with an implicit timezone. It could also mean that the plumber shows up at 01:00 at night and fixes it, or at 18:00 in the evening.
>

Around here, that means the plumber might visit between 09:00 and
17:00, but also might not. Humans are sloppy.

> If a newspaper talks about new years celebrations, and specifically talks about what happens on the 1st of January, this could mean at 00:01, or a later dinner party at 20:00. But the celebration that starts at midnight, doesn't start at the same moment all over the world.  So context, or location and implicit timezone does matter.
>

Yes, it matters... and you can't depend on newspapers to get these
things correct, because humans are sloppy. For instance, a natural
disaster in some other place in the world might be reported with the
timezone where it happened ("the hurricane that hit New Orleans on
Thursday"), but also might use the timezone where the newspaper is
being published ("last night, a hurricane devastated New Orleans"). So
the implicit timezone matters, but is also very unclear.

> I was also thinking of specifying some range objects for my needs, as that makes sense from what I've worked with earlier, this makes sense for example for a month or a year (or even decade) as well.
>
> But the point is that a date is not just a flat, one-dimensional object.
>

If your needs require a range, then use a range (or a pair of
datetimes). A date does not imply a range.

Side point: It's actually not that uncommon for a day to start or end
at some time other than midnight. For instance, here's the timetable
for one of Melbourne's railway lines:

https://d309ul1fvo6zfp.cloudfront.net/1645676503802/train-4-2022-02-28-2022-03-02.pdf

The last outbound train on a Friday night (scroll down to page 8)
departs at 1:20AM from Dandenong Station and arrives at 1:34AM at
Cranbourne Station. It's the same train that departed Westall Station
at 1:03AM. Whichever way you measure it, that service most definitely
runs entirely after midnight, but it counts as a Friday service, not a
Saturday one. So if I were to ask you "how many services run on a
Friday?", you should count this one, regardless of what your phone
says the day is. The best way to define the day would be 3AM to 3AM.

But that's the railways. Here's a NightRider bus timetable:

https://d309ul1fvo6zfp.cloudfront.net/1645676503802/bus-15131-2021-10-29-2022-12-31.pdf

How many services does THIS route run on a Saturday? Four or five
(depending on direction). It would be best to define THIS day from
midnight, or maybe even noon. The definition is context-sensitive, so
if you need a range, *be explicit*. It's okay for the range to be
implicit to your users, but make it explicit in your code:

day = datetime.datetime(y, m, d, 4, 0, 0), datetime.timedelta(days=1)

Python doesn't have a datetimerange object, so I'd use either a
datetime,timedelta pair, or a tuple of two datetimes. In some
contexts, it might be safe to let the timedelta be implicit, but at
very least, it's worth being clear that "this day" means "starting at
4AM on this day", or whatever it be.

Even when you describe a minute, it is most definitely not 100% clear
whether it means 02:02:00 to 02:03:00 (inclusive/exclusive), 02:02:00
to 02:02:01 (inc/exc), or the exact instant at 02:02:00. All three are
valid meanings, and a full timerange is the only way to be clear.

ChrisA


More information about the Python-list mailing list