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

Chris Angelico rosuav at gmail.com
Mon Feb 10 19:04:28 EST 2020


On Tue, Feb 11, 2020 at 10:42 AM Python <python at bladeshadow.org> wrote:
>
> As best I can tell, Python has no means to make use of the system's
> timezone info.  In order to make datetime "timezone aware", you need
> to manually create a subclass of datetime.tzinfo, whose methods return
> the correct values for the timezone you care about.  In the general
> case, this is hard, but since the timezone my dates are in is always
> GMT, it's no problem:
>
> class GMT(datetime.tzinfo):
>     def utcoffset(self, dt):
>         return datetime.timedelta(hours=0)
>     def dst(self, dt):
>         return datetime.timedelta(minutes=0)
>     def tzname(self, dt):
>         return "GMT"
>
> 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.

ChrisA


More information about the Python-list mailing list