用户指定的日期时间
我需要从用户输入中解析日期/时间字符串,并根据 datetime.strptime()
字符串中不可用的 timzeone 信息转换为 UTC(有什么建议吗?)。有没有一种简单的方法可以做到这一点?
理想情况下,在谷歌应用程序引擎上,如果可能的话,我也想从浏览器中使用 tzinfo 获取当地时间。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在搜索类似信息时,我遇到了一个演示应用程序引擎应用(包含源代码),它演示了如何在一种与您所要求的类似的方式。但不幸的是,您需要为要转换的每个时区创建自定义
tzinfo
类(上面链接的演示应用程序中的说明/代码)。如果您需要能够处理任何时区和/或想要采取简单的路线,我建议使用 pytz< /a> 模块。但是,请记住,pytz 是一个相当庞大的模块,您必须将其上传到 GAE 实例。
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.