0

I'm trying to convert a time (including timezone) to just a time with everything in UTC. When i have the timezone GMT it works fine, but soon as i use PDT or PST i get an error. Am i going about this the right way or is there a better way to do this?

>>> datetime.datetime.strptime("10:54:02 PM GMT", "%I:%M:%S %p %Z").strftime('%H:%M:%S')
'22:54:02'

I've seen pytz might be helpful to me but not sure how to use it within the date string. Any help is appreciated.

  • Check this: http://stackoverflow.com/questions/1398674/python-display-the-time-in-a-different-time-zone – sk11 Jul 30 '14 at 10:03

1 Answers1

1

If you only need to parse PST/PDT, you don't need pytz, as they are, by definition, always 8/7 hours away from UTC. You can simply interpret the data as a naive datetime (i.e.: exclude the timezone from the string you pass into strptime), then subtract 7 or 8 hours, depending on the timezone.

If you need to parse more timezones, or simply want to have the code ready for it, the link sk11 mentioned should be enough to get you started.

loopbackbee
  • 21,962
  • 10
  • 62
  • 97