2
基于此代码,我如何获得给定日期的UTC偏移量(以秒为单位)。 Get UTC offset from time zone name in pythonPython - 以秒为单位获取UTC偏移量
目前,我有这样的代码:
Python 2.7.3 (default, Jul 3 2012, 19:58:39)
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytz
>>> import datetime
>>> offset = datetime.datetime.now(pytz.timezone('US/Pacific')).strftime('%z')
>>> offset
'-0700'
>>> int(offset)*3600
-2520000
>>> int(offset)*360
-252000
>>>
例如,美国/太平洋有时是-800小时(可能是军用时间:d),但我需要它是在几秒钟内,例如:-25200 。
我只是乘以360?听起来不正确,但谁知道。
TIA
不会'.total_seconds()'这里帮助? :-) –
@MartijnPieters,是的,尤其是如果偏移量的秒数小数:) –
Ach,调用'int()'并且完成它; timedeltas可以处理毫秒,'.total_seconds()'反映了这一点。 :-P –