Pre-Pre-PEP: The datetime.timedeltacal class

Peter J. Holzer hjp-python at hjp.at
Sun Apr 17 10:34:01 EDT 2022


On 2022-04-17 10:15:54 +0200, Peter J. Holzer wrote:
> On 2022-04-17 06:08:54 +1000, Chris Angelico wrote:
> > On Sun, 17 Apr 2022 at 03:37, Peter J. Holzer <hjp-python at hjp.at> wrote:
> > > Therefore a new class (provisionally called timedeltacal, because it is
> > > calendaric, not absolute) should be added to datetime:
> > >
> > > Internally it stores months, days, seconds and microseconds as ints.
> > >
> > > The seconds and microseconds split is mostly for compatibility with
> > > datetime and timedelta. We could store seconds as a float instead.
> > >
> > > We don't store minutes since leap seconds aren't usually represented in
> > > "computer time", so they are unlikely to be useful in a timedeltacal
> > > object.
> > >
> > > Days are stored since they aren't a fixed multiple of any smaller unit.
> > > Months are stored since they aren't a fixed multiple of any smaller unit.
> > >
> > > Hours, weeks and years aren't stored since they are always 60 minutes, 7
> > > days and 12 months respectively.
> > 
> > It sounds like you're planning for annual DST changes, but what about
> > other shifts? What about when a location adopts standard time, which
> > could change their UTC offset (yes, I'm aware that most places adopted
> > standard time before UTC was a thing, but we still usually call it a
> > UTC offset rather than messing with GMT-UTC changeover) by an
> > arbitrary amount, even minutes?
> 
> Yes, I think you are right. I first thought it wouldn't matter because
> you'd have to look it up in the database anyway, but that's a
> non-sequitur. Clearly, when you cities switched from local times to
> timezones, one hour had to be longer or shorter than 3600 seconds.
> Similarily if India decided to switch to a whole-hour offset, then they
> would have one hour of 30 or 90 minutes.

Thinking about it some more (while writing test cases) I've changed my
mind again. I think "1 hour" should always be 3600 seconds (disregarding
leap seconds), not "the time until the hour hand has advanced by 30° and
the minute hand is in the same position". If I sit at a bar at 00:30 on
the last Sunday in March, and I continue to sit there for 2 hours, then
it's 03:30 when I get out of the bar, not 02:30. 02:30 doesn't even
exist.

That might be considered an inconsistency, but I think it "does the
right thing" in most cases.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20220417/e40b0883/attachment.sig>


More information about the Python-list mailing list