Python:将自由文本转换为日期
假设在同一(以色列)时区同时输入文本,以下自由文本行是等效的:
Wed Sep 9 16:26:57 IDT 2009
2009-09-09 16:26:57
16:26:57
September 9th, 16:26:57
是否有一个 python 模块可以将所有这些文本日期转换为(相同的)datetime.datetime< /代码> 实例?
我想在命令行工具中使用它,该工具将获取自由文本日期和时间作为参数,并返回不同时区的等效日期和时间,例如:
~$wdate 16:00 Israel
Israel: 16:00
San Francisco: 06:00
UTC: 13:00
或:
~$wdate 18:00 SanFran
San Francisco 18:00:22
Israel: 01:00:22 (Day after)
UTC: 22:00:22
有什么想法吗?
谢谢,
乌迪
Assuming the text is typed at the same time in the same (Israeli) timezone, The following free text lines are equivalent:
Wed Sep 9 16:26:57 IDT 2009
2009-09-09 16:26:57
16:26:57
September 9th, 16:26:57
Is there a python module that would convert all these text-dates to an (identical) datetime.datetime
instance?
I would like to use it in a command-line tool that would get a freetext date and time as an argument, and return the equivalent date and time in different time zones, e.g.:
~$wdate 16:00 Israel
Israel: 16:00
San Francisco: 06:00
UTC: 13:00
or:
~$wdate 18:00 SanFran
San Francisco 18:00:22
Israel: 01:00:22 (Day after)
UTC: 22:00:22
Any Ideas?
Thanks,
Udi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
python-dateutil 包听起来会很有帮助。您的示例仅使用简单的 HH:MM 时间戳和(神奇地缩短的)城市标识符,但它似乎也能够处理更复杂的格式,例如问题前面的格式。
The python-dateutil package sounds like it would be helpful. Your examples only use simple HH:MM timestamps with a (magically shortened) city identifier, but it seems able to handle more complicated formats like those earlier in the question, too.
parsedatetime 似乎是一个非常适合此特定任务的模块。
parsedatetime seems to be a very capable module for this specific task.
你可以
time.strptime
喜欢this :
它将返回一个 struct_time 值(更多信息请参见 python 文档页面)。
You could you
time.strptime
Like this :
It will return a struct_time value (more info on the python doc page).