0

I'm using Django and Python 3.7. I'm parsing some strings into dates using this logic ...

created_on = datetime.strptime(created_on_txt, '%Y-%m-%dT%H:%M:%S+00:00')
print(created_on.tzinfo)

How do I incorporate the fact that the time zone I want to be interpreted for the string shoudl be UTC? When I print out the ".tzinfo," it reads "None." An example of something I'm parsing is

2019-04-08T17:03:00+00:00
Dave
  • 15,639
  • 133
  • 442
  • 830
  • 2
    I think parsing `+00:00` directly will cause `strptime` to ignore it. Try playing with either `%z` or `%Z` format specifiers: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior – quiet_laika Apr 08 '19 at 17:40
  • @quiet_laika, yup it was "%z" – Dave Apr 08 '19 at 17:55

1 Answers1

0

Python 3.7, lucky you:

created_on = datetime.fromisoformat('2019-04-08T17:03:00+00:00')
created_on.tzinfo
>>> datetime.timezone.utc

If you insist on using strptime():

created_on = datetime.strptime('2019-04-08T17:03:00+00:00', '%Y-%m-%dT%H:%M:%S%z')
created_on.tzinfo
>>> datetime.timezone.utc

This uses the %z directive.

Pierre Monico
  • 982
  • 8
  • 16