开发者

User specified date time

开发者 https://www.devze.com 2023-01-16 06:46 出处:网络
I need to parse a date/time string from user input, and convert to UTC based on timze开发者_开发百科one info not available in the string for datetime.strptime() (any suggestions?). Is there a straight

I need to parse a date/time string from user input, and convert to UTC based on timze开发者_开发百科one 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 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号