更好的Python日期时间显示?

发布于 2024-10-16 12:23:28 字数 204 浏览 5 评论 0原文

我正在使用 babel 和 pytz 来获取时区。然而,对于美国的大部分地区,它映射到下拉框中没有那么有用的东西:

“America/New_York”显示“Eastern Time”, “America/Nipigon”还显示“东部时间”。

有什么方法可以进行此转换以添加城市信息吗?其他时区似乎还可以,例如“亚洲/雅加达”转换为“印度尼西亚(雅加达)时间”

I'm using babel and pytz to get the time zones. However, for most of America, it maps to something not as helpful in a dropdown box:

"America/New_York" displays "Eastern Time",
"America/Nipigon" also displays "Eastern Time".

Is there some way to do this conversion to add city info? other timezones seems okay, like "Asia/Jakarta" converts to "Indonesia (Jakarta) Time"

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

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

发布评论

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

评论(1

甜宝宝 2024-10-23 12:23:28

适用于我的 Babel 0.9.5 和 pytz 2010b。

py.tz

#!/usr/bin/env python

import pytz
import babel.dates

tz = pytz.timezone('America/New_York')
print babel.dates.get_timezone_location(tz)

输出

$ python tz.py 
United States (New York) Time

你是如何运行它的?什么版本?


如果您坚持使用现有的版本,那么为什么不只使用大陆/城市条目呢?

这是您的起点。它决定了大陆和城市,因此您可以根据需要设置其格式。

tzs.py

#!/usr/bin/env python

import pytz
import babel.dates
import re

country_timezones = {}
for (country, tzlist) in pytz.country_timezones.iteritems():
    country_name = pytz.country_names[country]
    cities = []
    for timezone in tzlist:
        # remove continent
        city = re.sub(r'^[^/]*/', r'', timezone)
        # Argentina has an extra "Argentina/" on my system (pytz 2010b)
        city = re.sub(country_name + '/', '', city)
        # Indiana and North Dakota have different rules by country
        # change Indiana/Location to Location, Indiana
        city = re.sub(r'^([^/]*)/(.*)', r'\2, \1', city)
        # change underscores to spaces
        city = re.sub(r'_', r' ', city)
        cities.append(city)  
    country_timezones[country_name] = cities

for country in sorted(country_timezones):
    print country
    for city in sorted(country_timezones[country]):
        print "\t%s" % (city)

输出

Aaland Islands
        Mariehamn
Afghanistan
        Kabul
...
Indonesia
        Jakarta
        Jayapura
        Makassar
        Pontianak
...
United States
        Adak
        Anchorage
        Boise
        Center, North Dakota
        Chicago
        Denver
        Detroit

Works for me with Babel 0.9.5 and pytz 2010b.

py.tz

#!/usr/bin/env python

import pytz
import babel.dates

tz = pytz.timezone('America/New_York')
print babel.dates.get_timezone_location(tz)

output

$ python tz.py 
United States (New York) Time

How are you running it? What versions?


If you are stuck with the versions you have, then why not only use the Continent/City entries?

Here's a starting point for you. It determines both the continent and the city, so you can format it however you want.

tzs.py

#!/usr/bin/env python

import pytz
import babel.dates
import re

country_timezones = {}
for (country, tzlist) in pytz.country_timezones.iteritems():
    country_name = pytz.country_names[country]
    cities = []
    for timezone in tzlist:
        # remove continent
        city = re.sub(r'^[^/]*/', r'', timezone)
        # Argentina has an extra "Argentina/" on my system (pytz 2010b)
        city = re.sub(country_name + '/', '', city)
        # Indiana and North Dakota have different rules by country
        # change Indiana/Location to Location, Indiana
        city = re.sub(r'^([^/]*)/(.*)', r'\2, \1', city)
        # change underscores to spaces
        city = re.sub(r'_', r' ', city)
        cities.append(city)  
    country_timezones[country_name] = cities

for country in sorted(country_timezones):
    print country
    for city in sorted(country_timezones[country]):
        print "\t%s" % (city)

output

Aaland Islands
        Mariehamn
Afghanistan
        Kabul
...
Indonesia
        Jakarta
        Jayapura
        Makassar
        Pontianak
...
United States
        Adak
        Anchorage
        Boise
        Center, North Dakota
        Chicago
        Denver
        Detroit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文