问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/3692869/user-specified-date-time