datetime seems to be broken WRT timezones (even when you add them)

Python python at bladeshadow.org
Mon Feb 10 19:16:00 EST 2020


On Tue, Feb 11, 2020 at 11:04:28AM +1100, Chris Angelico wrote:
> On Tue, Feb 11, 2020 at 10:42 AM Python <python at bladeshadow.org> wrote:
> > Now, you can instantiate a datetime.datetime object with the times you
> > want, and pass an instance of this class as the tzinfo argument to the
> > constructor.  Also no problem:
> 
> Rather than try to create your own GMT() object, have you considered
> using datetime.timezone.utc ? I tested it in your examples and it
> works just fine.

Not here it doesn't:

>>> import datetime
>>> dt = datetime.datetime(2020, 1, 31, 1, 30, 45, 987654, datetime.timezone.utc)
>>> print(dt)
2020-01-31 01:30:45.987654+00:00
>>> print(dt.strftime("%s"))
1580452245

That's the same erroneous result from my example.  The correct value
is again 1580434245.  Unless you've done something subtlely
different...

Also it's only available on Python3, which may not be the end of the
world, but the tool I'm working on is--for the moment at
least--expected to work on both python2.7 and 3.  But it's irrelevant
if it doesn't give the correct result, which seems to still be the
case.



More information about the Python-list mailing list