[Datetime-SIG] Datetime arithmetic proposal

Tim Peters tim.peters at gmail.com
Wed Jul 29 04:45:49 CEST 2015


[Chris Barker]
>> 1) timespan since an epoch: -- i.e seconds since Jan 1, 1970 00:00. This is
>> how the datetime object keeps it internally.

[Alexander Belopolsky <alexander.belopolsky at gmail.com>]
> No, it is not.  You are probably confusing stdlib datetime with
> numpy.datetime64.
> The stdlib datetime keeps broken down year, month, day, hour, minute, second
> and microsecond values.

It's interesting (well, maybe to someone ;-) ) to note that this
division into named attributes is simply for efficiency in extracting
commonly accessed values.  Conceptually, it's really just storing the
integer number of microseconds since the start of "year number 1" of
the proleptic Gregorian calendar.  All those named fields can be
viewed as just representing that integer in a very messy mixed-radix
notation.

That's not just academic.  For example, if you stare at the Python
code for the implementation of datetime + timedelta
(datetime.__add__), you'll see that it converts the datetime _to_ a
timedelta first (which is another way of representing an integer
number of microseconds).  Of course that conversion has to be done "by
hand" since it makes no sense in the user-visible model.  But,
internally,datetimes, dates, times, and timedeltas are all sometimes
viewed as just complicated ways of spelling "some number of
microseconds".

It's cute ;-)


More information about the Datetime-SIG mailing list