1

I need to parse a date/time string from user input, and convert to UTC based on timzeone info not available in the string for datetime.strptime() (any suggestions?). Is there a straightforward way of doing this?

Ideally, on google app engine i'd like to grab local time with tzinfo from the browser if possible also.

timezone_string = "GMT-0800"
fields = ("eventstartmonth","eventstartday", "eventstartyear", "eventstarttimehour", "eventstarttimeampm") 
date_string = '_'.join(map(lambda x: self.request.get(x), fields))
# date_string = "01_11_2000_1:35_PM"

dt = datetime.datetime.strptime(date_string, "%m_%d_%Y_%I:%M_%p")
# how to convert dt into a tz-aware datetime, and then to UTC 
Kyle
  • 355
  • 1
  • 4
  • 13
  • possible duplicate of [On GAE, how may I show a date according to right client TimeZone ?](http://stackoverflow.com/questions/868708/on-gae-how-may-i-show-a-date-according-to-right-client-timezone) – msw Sep 11 '10 at 22:21
  • close, this has some relevant information (thank you) but not entirely. I could use a little more 'hand-holding' with tzinfo strategies – Kyle Sep 11 '10 at 22:30
  • 1
    it baffles me that python does not have a solid implementation for handling timezones – Kyle Sep 12 '10 at 00:48
  • It seems to me that you're over-speccing the problem a bit. You don't need to worry about time zones if you can just get the proper offset to GMT. The `timezone_string` you provide in the example contains this offset as a number, all you have to do is create a `timedelta` from it: http://stackoverflow.com/questions/4044863/get-the-gmt-time-given-date-and-utc-offset-in-python – Mark Ransom Nov 02 '10 at 18:07

1 Answers1

0

While searching for similar information I came across a demo app engine app (with source included) that demonstrates how to convert timezones in a way that's similar to what you've requested. Unfortunately, though, you'll need to create custom tzinfo classes (explanation/code in the demo app linked above) for each timezone you'll be converting.

If you need to be able to handle any timezone and/or want to take the easy route, I'd recommend using the pytz module. However, keep in mind, pytz is a rather bulky module that you'd have to upload to your GAE instance.

Josh
  • 2,158
  • 18
  • 22