如何获取当前时区对应的tz_info对象?

发布于 2024-08-10 08:07:10 字数 123 浏览 2 评论 0原文

python(或pytz)中是否有跨平台函数返回与计算机上当前设置的时区相对应的tzinfo对象?

不能依赖环境变量,因为它们不是跨平台的

Is there a cross-platform function in python (or pytz) that returns a tzinfo object corresponding to the timezone currently set on the computer?

environment variables cannot be counted on as they are not cross-platform

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

静赏你的温柔 2024-08-17 08:07:10
>>> import datetime
>>> today = datetime.datetime.now()
>>> insummer = datetime.datetime(2009,8,15,10,0,0)
>>> from pytz import reference
>>> localtime = reference.LocalTimezone()
>>> localtime.tzname(today)
'PST'
>>> localtime.tzname(insummer)
'PDT'
>>> 
>>> import datetime
>>> today = datetime.datetime.now()
>>> insummer = datetime.datetime(2009,8,15,10,0,0)
>>> from pytz import reference
>>> localtime = reference.LocalTimezone()
>>> localtime.tzname(today)
'PST'
>>> localtime.tzname(insummer)
'PDT'
>>> 
踏月而来 2024-08-17 08:07:10

Python 3.7:

import datetime

datetime.datetime.now().astimezone().tzinfo

Python 3.7:

import datetime

datetime.datetime.now().astimezone().tzinfo
眼泪也成诗 2024-08-17 08:07:10

返回 pytz 时区的 tzlocal 模块 适用于* nix 和 win32:

from datetime import datetime
from tzlocal import get_localzone # $ pip install tzlocal

# get local timezone    
local_tz = get_localzone() 


print local_tz.localize(datetime(2012, 1, 15))
# -> 2012-01-15 00:00:00+04:00 # current utc offset
print local_tz.localize(datetime(2000, 1, 15))
# -> 2000-01-15 00:00:00+03:00 # past utc offset (note: +03 instead of +04)
print local_tz.localize(datetime(2000, 6, 15))
# -> 2000-06-15 00:00:00+04:00 # changes to utc offset due to DST

注意:它考虑了 DST 和非 DST utc 偏移量变化。

tzlocal module that returns pytz timezones works on *nix and win32:

from datetime import datetime
from tzlocal import get_localzone # $ pip install tzlocal

# get local timezone    
local_tz = get_localzone() 


print local_tz.localize(datetime(2012, 1, 15))
# -> 2012-01-15 00:00:00+04:00 # current utc offset
print local_tz.localize(datetime(2000, 1, 15))
# -> 2000-01-15 00:00:00+03:00 # past utc offset (note: +03 instead of +04)
print local_tz.localize(datetime(2000, 6, 15))
# -> 2000-06-15 00:00:00+04:00 # changes to utc offset due to DST

Note: it takes into account both DST and non-DST utc offset changes.

冷情 2024-08-17 08:07:10

time.timezone 返回当前时区偏移量。还有一个 datetime.tzinfo,如果您需要更复杂的结构。

time.timezone returns current timezone offset. there is also a datetime.tzinfo, if you need more complicated structure.

木森分化 2024-08-17 08:07:10

以下代码片段返回不同时区的时间,无论服务器上配置的时区如何。

# pip install pytz tzlocal

from tzlocal import get_localzone
from datetime import datetime
from pytz import timezone

local_tz = get_localzone()
local_datetime = datetime.now(local_tz)

zurich_tz = timezone('Europe/Zurich')
zurich_datetime = zurich_tz.normalize(local_datetime.astimezone(zurich_tz))

This following code snippet returns time in a different timezone irrespective of the timezone configured on the server.

# pip install pytz tzlocal

from tzlocal import get_localzone
from datetime import datetime
from pytz import timezone

local_tz = get_localzone()
local_datetime = datetime.now(local_tz)

zurich_tz = timezone('Europe/Zurich')
zurich_datetime = zurich_tz.normalize(local_datetime.astimezone(zurich_tz))
唱一曲作罢 2024-08-17 08:07:10

我自己没有使用过它,但是 dateutil.tz.tzlocal() 应该可以解决问题。

http://labix.org/python-dateutil#head-50221b5226c3ccb97daa06ea7d9abf0533ec0310

I have not used it myself, but dateutil.tz.tzlocal() should do the trick.

http://labix.org/python-dateutil#head-50221b5226c3ccb97daa06ea7d9abf0533ec0310

淑女气质 2024-08-17 08:07:10

也许可以尝试:

import time

print time.tzname #or time.tzname[time.daylight]

Maybe try:

import time

print time.tzname #or time.tzname[time.daylight]

不再让梦枯萎 2024-08-17 08:07:10

我问自己同样的问题,我在 [1] 中找到了答案:

看看第 8.1.7 节:格式“%z”(小写,Z 大写也返回时区,但不在 4 -digit 格式,但采用时区缩写的形式,如 [3] 中)的 strftime 返回电子邮件标头中标准的“+/- 4DIGIT”形式(请参阅 RFC 2822 的第 3.3 节,请参阅 [2],该格式已废弃)指定电子邮件标头时区的其他方法)。

因此,如果您希望时区采用这种格式,请使用:

time.strftime("%z")

[1] http:// docs.python.org/2/library/datetime.html

[2] https://www.rfc-editor.org/rfc/rfc2822#section-3.3

[3] 时区缩写:http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations ,仅供参考。

I was asking the same to myself, and I found the answer in [1]:

Take a look at section 8.1.7: the format "%z" (lowercase, the Z uppercase returns also the time zone, but not in the 4-digit format, but in the form of timezone abbreviations, like in [3]) of strftime returns the form "+/- 4DIGIT" that is standard in email headers (see section 3.3 of RFC 2822, see [2], which obsoletes the other ways of specifying the timezone for email headers).

So, if you want your timezone in this format, use:

time.strftime("%z")

[1] http://docs.python.org/2/library/datetime.html

[2] https://www.rfc-editor.org/rfc/rfc2822#section-3.3

[3] Timezone abbreviations: http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations , only for reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文