Ada*_*tan 55 python time timezone datetime utc
我想将日志文件中的UTC时间戳与本地时间戳进行比较.在创建本地datetime对象时,我使用如下内容:
>>> local_time=datetime.datetime(2010, 4, 27, 12, 0, 0, 0,
tzinfo=pytz.timezone('Israel'))
Run Code Online (Sandbox Code Playgroud)
我想找到一个自动工具来取代tzinfo=pytz.timezone('Israel')当前的本地时区.
有任何想法吗?
vbe*_*bem 73
在Python 3.x中,本地时区可能会像这样:
import datetime
LOCAL_TIMEZONE = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
Run Code Online (Sandbox Code Playgroud)
这是一个棘手datetime的代码使用.
jfs*_*jfs 28
to compare UTC timestamps from a log file with local timestamps.
It is hard to find out Olson TZ name for a local timezone in a portable manner. Fortunately, you don't need it to perform the comparison.
tzlocal module returns a pytz timezone corresponding to the local timezone:
from datetime import datetime
import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal
tz = get_localzone()
local_dt = tz.localize(datetime(2010, 4, 27, 12, 0, 0, 0), is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc) #NOTE: utc.normalize() is unnecessary here
Run Code Online (Sandbox Code Playgroud)
Unlike other solutions presented so far the above code avoids the following issues:
dateutil) fail to take that into accountNote: to get timezone-aware datetime object from a naive datetime object, you should use*:
local_dt = tz.localize(datetime(2010, 4, 27, 12, 0, 0, 0), is_dst=None)
Run Code Online (Sandbox Code Playgroud)
instead of:
#XXX fails for some timezones
local_dt = datetime(2010, 4, 27, 12, 0, 0, 0, tzinfo=tz)
Run Code Online (Sandbox Code Playgroud)
*is_dst=None forces an exception if given local time is ambiguous or non-existent.
If you are certain that all local timestamps use the same (current) utc offset for the local timezone then you could perform the comparison using only stdlib:
# convert a naive datetime object that represents time in local timezone to epoch time
timestamp1 = (datetime(2010, 4, 27, 12, 0, 0, 0) - datetime.fromtimestamp(0)).total_seconds()
# convert a naive datetime object that represents time in UTC to epoch time
timestamp2 = (datetime(2010, 4, 27, 9, 0) - datetime.utcfromtimestamp(0)).total_seconds()
Run Code Online (Sandbox Code Playgroud)
timestamp1 and timestamp2 can be compared directly.
Note:
timestamp1 formula works only if the UTC offset at epoch (datetime.fromtimestamp(0)) is the same as nowfromtimestamp() creates a naive datetime object in the current local timezoneutcfromtimestamp() creates a naive datetime object in UTC.Dav*_*d L 14
我问自己一样,我在1中找到了答案:
看一下8.1.7节:格式"%z"(小写,Z大写也返回时区,但不是4位格式,但是以时区缩写的形式,如[3]中所示) strftime返回"+/- 4DIGIT"形式,这是电子邮件标题中的标准(参见RFC 2822的3.3节,参见[2],它废弃了指定电子邮件标题时区的其他方法).
因此,如果您想要这种格式的时区,请使用:
time.strftime("%z")
Run Code Online (Sandbox Code Playgroud)
[1] http://docs.python.org/2/library/datetime.html
[2] http://tools.ietf.org/html/rfc2822#section-3.3
[3]时区缩写:http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations,仅供参考.
以下似乎适用于 3.7+,使用标准库:
from datetime import timedelta
from datetime import timezone
import time
def currenttz():
if time.daylight:
return timezone(timedelta(seconds=-time.altzone),time.tzname[1])
else:
return timezone(timedelta(seconds=-time.timezone),time.tzname[0])
Run Code Online (Sandbox Code Playgroud)
要创建包含以色列当地时区的 ISO 表示形式的 ISO格式字符串( +04:00):
>>> datetime.now(datetime.now().astimezone().tzinfo).isoformat()
'2021-09-07T01:02.030042+04:00'
Run Code Online (Sandbox Code Playgroud)
这将创建一个“时区感知”日期对象,该对象将与 UTC 或本地时间中的任何其他日期时间对象进行适当的比较。但是,如果您像我一样在同一时间在旧金山的服务器上运行它,则时区 ISO 表示(以及日期/时间字符串本身)将会改变:
>>> datetime.now(datetime.now().astimezone().tzinfo).isoformat()
'2021-09-06T14:01:02.030042-07:00'
Run Code Online (Sandbox Code Playgroud)
两种情况下的对象datetime都是相互兼容的。因此,如果减去它们,时间增量将为 0:
>>> (datetime.fromisoformat('2021-09-06T14:01:02.030042-07:00') -
... datetime.fromisoformat('2021-09-07T01:01:02.030042+04:00'))
datetime.timedelta(0)
Run Code Online (Sandbox Code Playgroud)
首先得到pytz和tzlocal模块
pip install pytz tzlocal
Run Code Online (Sandbox Code Playgroud)
然后
from tzlocal import get_localzone
local = get_localzone()
Run Code Online (Sandbox Code Playgroud)
然后你可以做的事情
from datetime import datetime
print(datetime.now(local))
Run Code Online (Sandbox Code Playgroud)
这是一种仅使用标准库获取本地时区的方法,(仅适用于 *nix 环境):
>>> '/'.join(os.path.realpath('/etc/localtime').split('/')[-2:])
'Australia/Sydney'
Run Code Online (Sandbox Code Playgroud)
您可以使用它来创建pytz时区:
>>> import pytz
>>> my_tz_name = '/'.join(os.path.realpath('/etc/localtime').split('/')[-2:])
>>> my_tz = pytz.timezone(my_tz_name)
>>> my_tz
<DstTzInfo 'Australia/Sydney' LMT+10:05:00 STD>
Run Code Online (Sandbox Code Playgroud)
...然后您可以将其应用于 a datetime:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2014, 9, 3, 9, 23, 24, 139059)
>>> now.replace(tzinfo=my_tz)
>>> now
datetime.datetime(2014, 9, 3, 9, 23, 24, 139059, tzinfo=<DstTzInfo 'Australia/Sydney' LMT+10:05:00 STD>)
Run Code Online (Sandbox Code Playgroud)
这是@vbem 解决方案的更简洁版本:
from datetime import datetime as dt
dt.utcnow().astimezone().tzinfo
Run Code Online (Sandbox Code Playgroud)
唯一实质性的区别是我datetime.datetime.now(datetime.timezone.utc)用datetime.datetime.utcnow(). 为简洁起见,我也别名datetime.datetime为dt.
出于我的目的,我想要以秒为单位的 UTC 偏移量。这是它的样子:
dt.utcnow().astimezone().utcoffset().total_seconds()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76464 次 |
| 最近记录: |