获取当地时区的Olson TZ名称?(Get the Olson TZ name for the local timezone?)

编程入门 行业动态 更新时间:2024-10-24 02:02:16
获取当地时区的Olson TZ名称?(Get the Olson TZ name for the local timezone?)

如何获得与C localtime通话给出的值相对应的Olson时区名称 (如Australia/Sydney )?

这是通过TZ重写的值,通过符号链接/etc/localtime ,或者在时间相关的系统配置文件中设置一个TIMEZONE变量。

How do I get the Olson timezone name (such as Australia/Sydney) corresponding to the value given by C's localtime call?

This is the value overridden via TZ, by symlinking /etc/localtime, or setting a TIMEZONE variable in time-related system configuration files.

最满意答案

我认为最好的方法是通过所有的pytz时区,并检查哪一个匹配当地时区,每个pytz时区对象包含关于utcoffset和tzname的信息,如CDT,EST,可以从time.timezone/altzone和time.tzname获得与本地时间相同的信息time.tzname ,我认为这足以正确匹配pytz数据库中的本地时区,例如

import time import pytz import datetime local_names = [] if time.daylight: local_offset = time.altzone localtz = time.tzname[1] else: local_offset = time.timezone localtz = time.tzname[0] local_offset = datetime.timedelta(seconds=-local_offset) for name in pytz.all_timezones: timezone = pytz.timezone(name) if not hasattr(timezone, '_tzinfos'): continue#skip, if some timezone doesn't have info # go thru tzinfo and see if short name like EDT and offset matches for (utcoffset, daylight, tzname), _ in timezone._tzinfos.iteritems(): if utcoffset == local_offset and tzname == localtz: local_names.append(name) print local_names

输出:

美国/巴伊亚比德拉斯,美国/巴伊亚比德拉斯,美国/伯利兹,美国/剑桥,美国/坎昆,美洲/芝加哥,美洲/奇瓦瓦州,美洲/印度尼西亚/诺克斯,美洲/印第安纳州/美洲/印第安纳州/美洲/印第安纳州/美洲/印第安纳州/美洲/印第安纳州/美洲/印第安纳州/威尼斯人,美洲/印第安纳州/维门多族,美洲/印第安纳州/维门多族,美洲/印第安纳州/ ,“美洲/印第安纳波利斯”,“美国/肯塔基州”,“美国/肯塔基州/路易斯维尔”,“美国/肯塔基州/路易斯维尔”,“美国/肯塔基州/蒙蒂塞洛”,“America / Knox_IN”,“America / Louisville”美洲/路易斯维尔“,”美洲/马那瓜“,”美洲/马塔莫罗斯“,”美洲/美洲“,”美洲/梅里达“,”美国/墨西哥“,”美洲/蒙特雷“,”美洲/北达科他州/美洲/北达科他州/中心','美洲/北达科他州/ New_Salem','America / Ojinaga','America / Pangnirtung','Am “美国/瑞典”,“美国/特古西加尔巴”,“美洲/温尼伯”,“CST6CDT”,“加拿大/中部”,“墨西哥/ ,“美国/中部”,“美国/东印第安纳州”,“美国/印第安纳州 - 斯塔克”]

在生产中,您可以预先创建这样的映射,并保存它,而不是总是迭代。

更改时区后的测试脚本:

$ export TZ ='澳大利亚/悉尼' $ python get_tz_names.py [澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/澳大利亚/新南威尔斯州“,”澳大利亚/昆士兰州“,”澳大利亚/悉尼“,”澳大利亚/塔斯马尼亚“,”澳大利亚/维多利亚州“

I think best bet is to go thru all pytz timezones and check which one matches local timezone, each pytz timezone object contains info about utcoffset and tzname like CDT, EST, same info about local time can be obtained from time.timezone/altzone and time.tzname, and I think that is enough to correctly match local timezone in pytz database e.g.

import time import pytz import datetime local_names = [] if time.daylight: local_offset = time.altzone localtz = time.tzname[1] else: local_offset = time.timezone localtz = time.tzname[0] local_offset = datetime.timedelta(seconds=-local_offset) for name in pytz.all_timezones: timezone = pytz.timezone(name) if not hasattr(timezone, '_tzinfos'): continue#skip, if some timezone doesn't have info # go thru tzinfo and see if short name like EDT and offset matches for (utcoffset, daylight, tzname), _ in timezone._tzinfos.iteritems(): if utcoffset == local_offset and tzname == localtz: local_names.append(name) print local_names

output:

['America/Atikokan', 'America/Bahia_Banderas', 'America/Bahia_Banderas', 'America/Belize', 'America/Cambridge_Bay', 'America/Cancun', 'America/Chicago', 'America/Chihuahua', 'America/Coral_Harbour', 'America/Costa_Rica', 'America/El_Salvador', 'America/Fort_Wayne', 'America/Guatemala', 'America/Indiana/Indianapolis', 'America/Indiana/Knox', 'America/Indiana/Marengo', 'America/Indiana/Marengo', 'America/Indiana/Petersburg', 'America/Indiana/Tell_City', 'America/Indiana/Vevay', 'America/Indiana/Vincennes', 'America/Indiana/Winamac', 'America/Indianapolis', 'America/Iqaluit', 'America/Kentucky/Louisville', 'America/Kentucky/Louisville', 'America/Kentucky/Monticello', 'America/Knox_IN', 'America/Louisville', 'America/Louisville', 'America/Managua', 'America/Matamoros', 'America/Menominee', 'America/Merida', 'America/Mexico_City', 'America/Monterrey', 'America/North_Dakota/Beulah', 'America/North_Dakota/Center', 'America/North_Dakota/New_Salem', 'America/Ojinaga', 'America/Pangnirtung', 'America/Rainy_River', 'America/Rankin_Inlet', 'America/Resolute', 'America/Resolute', 'America/Tegucigalpa', 'America/Winnipeg', 'CST6CDT', 'Canada/Central', 'Mexico/General', 'US/Central', 'US/East-Indiana', 'US/Indiana-Starke']

In production you can create such a mapping beforehand and save it instead of iterating always.

Testing script after changing timezone:

$ export TZ='Australia/Sydney' $ python get_tz_names.py ['Antarctica/Macquarie', 'Australia/ACT', 'Australia/Brisbane', 'Australia/Canberra', 'Australia/Currie', 'Australia/Hobart', 'Australia/Lindeman', 'Australia/Melbourne', 'Australia/NSW', 'Australia/Queensland', 'Australia/Sydney', 'Australia/Tasmania', 'Australia/Victoria']

更多推荐

本文发布于:2023-04-29 07:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时区   名称   Olson   TZ   local

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!