将时间戳从一个时区转换为另一个时区
wtftz的Python项目详细描述
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.075608print(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.860947print(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:46print(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.644934print(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
从壳里用!
$ date && ./wtftz "`date`" pst && ./wtftz "`date`" utc Mon Dec 1023:43:06 PST 20122012-12-10 23:43:06 2012-12-11 07:43:06 $ date +%s && ./wtftz "`date +%s`" utc 13552117472012-12-10 23:42:27
wtftz还可以处理自由文本字符串
print(wtftz.convert_free("1355236920 to est"))# 2012-12-11 01:42:00print(wtftz.convert_free("2012-12-10T18:31:29.214653-08:00 to est"))# 2012-12-10 21:31:29.214653print(wtftz.convert_free("2012-12-10T18:31:29.214653 from pst to est"))# 2012-12-10 21:31:29.214653print(wtftz.convert_free("2012-12-10T18:31:29.214653 from utc to est"))# 2012-12-10 13:31:29.214653
开发
欢迎提出问题和请求!
我想把常用时区名称的列表扩展到包括外文 (对我来说)时区和使自由文本解析器更智能。
测试
测试很重要。没有请求将不被接受。
python -m unittest discover
自述文件
在添加新行为时,应使用示例更新此自述文件到 确保文件格式正确,请检查:
pip install docutils
pip install pygments
python setup.py --long-description | rst2html.py > output.html
请确保文件已解析且外观良好