跳转到主要内容

将时间戳从一种时区转换为另一种时区

项目描述

WTF时区?!

我无法在脑海中将UTC转换为PST,谷歌也无法为我做到。

每个时区转换网站都充满了垃圾邮件和想象中最差的用户体验。

wtftz假设您正在尝试转换UTC时间戳

print(wtftz.convert("2012-12-10T18:31:29.214653", "pst"))
# 2012-12-10 10:31:29.214653

除非您不是

注意此iso格式字符串中嵌入的时区。

print(wtftz.convert("2012-12-10T18:31:29.214653-08:00", "pst"))
# 2012-12-10 18:31:29.214653

wtftz了解一些时区的常见缩写

print(wtftz.convert("1355236920", "est"))
# 2012-12-11 01:42:00
# Sorry, Australia! EST is most commonly used for US/Eastern!

wtftz返回无tzinfo的时间戳,因为Python的时区是损坏的!

print(wtftz.convert(datetime.datetime.now(), from_tz="pst", to_tz="utc"))
# 2012-12-11 06:45:04.075608
print(wtftz.convert(datetime.datetime.now(), from_tz="pst", to_tz="eastern"))
# 2012-12-11 01:45:18.343536

wtftz知道您不知道当前时间是PST还是PDT

但它会做正确的事,在适当的时候使用PST或PDT

print(wtftz.convert(datetime.datetime.now(), "pst"))
# 2012-12-11 06:48:39.860947
print(wtftz.convert(datetime.datetime.now(), "pdt"))
# 2012-12-11 06:48:39.860947

wtftz知道您与具有非标准时间戳格式的系统一起工作

print(wtftz.convert('2012/10/7 12:25:46', 'pst'))
# 2012-10-07 19:25:46
print(wtftz.convert('7 October 2012 12:25:46', 'pst'))
# 2012-10-07 19:25:46

wtftz也将接受正确的时间区名称

print(wtftz.convert(datetime.datetime.now(), "US/Pacific"))
# 2012-12-10 15:04:03.644934
print(wtftz.convert(datetime.datetime.now(), "America/Chicago"))
# 2012-12-10 17:04:03.650494

但它不能处理一切

print(wtftz.convert('2012:10:7:12:25:46', 'pst'))
# ...
# ValueError: Cannot parse timestamp 2012:10:7:12:25:46

从shell中使用它!

$ date && ./wtftz "`date`" pst && ./wtftz "`date`" utc
Mon Dec 10 23:43:06 PST 2012
2012-12-10 23:43:06
2012-12-11 07:43:06
$ date +%s && ./wtftz "`date +%s`" utc
1355211747
2012-12-10 23:42:27

wtftz还可以处理纯文本字符串

print(wtftz.convert_free("1355236920 to est"))
# 2012-12-11 01:42:00
print(wtftz.convert_free("2012-12-10T18:31:29.214653-08:00 to est"))
# 2012-12-10 21:31:29.214653
print(wtftz.convert_free("2012-12-10T18:31:29.214653 from pst to est"))
# 2012-12-10 21:31:29.214653
print(wtftz.convert_free("2012-12-10T18:31:29.214653 from utc to est"))
# 2012-12-10 13:31:29.214653

安装

wtftz在cheese shop中,所以只需

pip install wtftz

开发

欢迎问题和拉取请求!

我希望扩展常用时区名称列表,包括对我来说是外国的时区,并使免费文本解析器更智能。

测试

测试很重要。没有测试的拉取请求将不予接受。

python -m unittest discover

README

此README应随着新行为的添加而更新示例。为了确保文件格式正确,请检查它

pip install docutils
pip install pygments
python setup.py --long-description | rst2html.py > output.html

确保文件可以解析并看起来良好。

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页