1

我创建了这个函数来获得一个带有偏移量的公共时区的二元组。

[(item, datetime.datetime.now(pytz.timezone(item)).strftime('%z') + " " + item) for item in pytz.common_timezones]

我从函数中得到了类似的东西

[('Africa/Abidjan', '+0000 Africa/Abidjan'), ('Africa/Accra', '+0000 Africa/Accra'), ('Africa/Addis_Ababa', '+0300 Africa/Addis_Ababa'), ('Africa/Algiers', '+0100 Africa/Algiers'), ('Africa/Asmara', '+0300 Africa/Asmara'), ('Africa/Bamako', '+0000 Africa/Bamako'), ('Africa/Bangui', '+0100 Africa/Bangui'), ('Africa/Banjul', '+0000 Africa/Banjul'), ('Africa/Bissau', '+0000 Africa/Bissau'), ('Africa/Blantyre', '+0200 Africa/Blantyre'), ('Africa/Brazzaville', '+0100 Africa/Brazzaville'), ('Africa/Bujumbura', '+0200 Africa/Bujumbura'), ('Africa/Cairo', '+0200 Africa/Cairo'), ('Africa/Casablanca', '+0100 Africa/Casablanca'), ('Africa/Ceuta', '+0200 Africa/Ceuta'), ('Africa/Conakry', '+0000 Africa/Conakry'), ('Africa/Dakar', '+0000 Africa/Dakar'), ('Africa/Dar_es_Salaam', '+0300 Africa/Dar_es_Salaam'), ('Africa/Djibouti', '+0300 Africa/Djibouti'), ('Africa/Douala', '+0100 Africa/Douala'), ('Africa/El_Aaiun', '+0000 Africa/El_Aaiun'), ('Africa/Freetown', '+0000 Africa/Freetown'), ('Africa/Gaborone', '+0200 Africa/Gaborone'), ('Africa/Harare', '+0200 Africa/Harare'), ('Africa/Johannesburg', '+0200 Africa/Johannesburg'), ('Africa/Juba', '+0300 Africa/Juba'), ('Africa/Kampala', '+0300 Africa/Kampala'), ('Africa/Khartoum', '+0300 Africa/Khartoum'), ('Africa/Kigali', '+0200 Africa/Kigali'), ('Africa/Kinshasa', '+0100 Africa/Kinshasa'), ('Africa/Lagos', '+0100 Africa/Lagos'), ('Africa/Libreville', '+0100 Africa/Libreville'), ('Africa/Lome', '+0000 Africa/Lome'), ('Africa/Luanda', '+0100 Africa/Luanda'), ('Africa/Lubumbashi', '+0200 Africa/Lubumbashi'), ('Africa/Lusaka', '+0200 Africa/Lusaka'), (

如何按偏移量对其进行排序?目前是否按 ASC 排序

4

4 回答 4

5

我不确定我是否能正确理解你,但你可以试试这个:

>>> tz = [(item, datetime.datetime.now(pytz.timezone(item)).strftime('%z') + " " + item) for item in pytz.common_timezones]
>>> sorted(tz, key=lambda x: int(x[1].split()[0]))
[('Pacific/Midway', '-1100 Pacific/Midway'), ('Pacific/Niue', '-1100 Pacific/Niue'), ('Pacific/Pago_Pago', '-1100 Pacific/Pago_Pago'), ('Pacific/Honolulu', '-1000 Pacific/Honolulu'), ...
于 2013-08-29T14:35:17.133 回答
1

试试这个:

tzs = [('Africa/Abidjan', '+0000 Africa/Abidjan'), ('Africa/Accra', '+0000 Africa/Accra'), ('Africa/Addis_Ababa', '+0300 Africa/Addis_Ababa'), ('Africa/Algiers', '+0100 Africa/Algiers'), ('Africa/Asmara', '+0300 Africa/Asmara'), ('Africa/Bamako', '+0000 Africa/Bamako'), ('Africa/Bangui', '+0100 Africa/Bangui'), ('Africa/Banjul', '+0000 Africa/Banjul'), ('Africa/Bissau', '+0000 Africa/Bissau'), ('Africa/Blantyre', '+0200 Africa/Blantyre'), ('Africa/Brazzaville', '+0100 Africa/Brazzaville'), ('Africa/Bujumbura', '+0200 Africa/Bujumbura'), ('Africa/Cairo', '+0200 Africa/Cairo'), ('Africa/Casablanca', '+0100 Africa/Casablanca'), ('Africa/Ceuta', '+0200 Africa/Ceuta'), ('Africa/Conakry', '+0000 Africa/Conakry'), ('Africa/Dakar', '+0000 Africa/Dakar'), ('Africa/Dar_es_Salaam', '+0300 Africa/Dar_es_Salaam'), ('Africa/Djibouti', '+0300 Africa/Djibouti'), ('Africa/Douala', '+0100 Africa/Douala'), ('Africa/El_Aaiun', '+0000 Africa/El_Aaiun'), ('Africa/Freetown', '+0000 Africa/Freetown'), ('Africa/Gaborone', '+0200 Africa/Gaborone'), ('Africa/Harare', '+0200 Africa/Harare'), ('Africa/Johannesburg', '+0200 Africa/Johannesburg'), ('Africa/Juba', '+0300 Africa/Juba'), ('Africa/Kampala', '+0300 Africa/Kampala'), ('Africa/Khartoum', '+0300 Africa/Khartoum'), ('Africa/Kigali', '+0200 Africa/Kigali'), ('Africa/Kinshasa', '+0100 Africa/Kinshasa'), ('Africa/Lagos', '+0100 Africa/Lagos'), ('Africa/Libreville', '+0100 Africa/Libreville'), ('Africa/Lome', '+0000 Africa/Lome'), ('Africa/Luanda', '+0100 Africa/Luanda'), ('Africa/Lubumbashi', '+0200 Africa/Lubumbashi'), ('Africa/Lusaka', '+0200 Africa/Lusaka')]

pre_sort_tzs = []

for tz in tzs:
    pre_sort_tzs.append([int(tz[1][0:5], 10), (tz)])

sorted_tzs = []

for tz in sorted(pre_sort_tzs):
    sorted_tzs.append(tz[1])

print sorted_tzs

输出:

[('Africa/Abidjan', '+0000 Africa/Abidjan'), ('Africa/Accra', '+0000 Africa/Accra'), ('Africa/Bamako', '+0000 Africa/Bamako'), ('Africa/Banjul', '+0000 Africa/Banjul'), ('Africa/Bissau', '+0000 Africa/Bissau'), ('Africa/Conakry', '+0000 Africa/Conakry'), ('Africa/Dakar', '+0000 Africa/Dakar'), ('Africa/El_Aaiun', '+0000 Africa/El_Aaiun'), ('Africa/Freetown', '+0000 Africa/Freetown'), ('Africa/Lome', '+0000 Africa/Lome'), ('Africa/Algiers', '+0100 Africa/Algiers'), ('Africa/Bangui', '+0100 Africa/Bangui'), ('Africa/Brazzaville', '+0100 Africa/Brazzaville'), ('Africa/Casablanca', '+0100 Africa/Casablanca'), ('Africa/Douala', '+0100 Africa/Douala'), ('Africa/Kinshasa', '+0100 Africa/Kinshasa'), ('Africa/Lagos', '+0100 Africa/Lagos'), ('Africa/Libreville', '+0100 Africa/Libreville'), ('Africa/Luanda', '+0100 Africa/Luanda'), ('Africa/Blantyre', '+0200 Africa/Blantyre'), ('Africa/Bujumbura', '+0200 Africa/Bujumbura'), ('Africa/Cairo', '+0200 Africa/Cairo'), ('Africa/Ceuta', '+0200 Africa/Ceuta'), ('Africa/Gaborone', '+0200 Africa/Gaborone'), ('Africa/Harare', '+0200 Africa/Harare'), ('Africa/Johannesburg', '+0200 Africa/Johannesburg'), ('Africa/Kigali', '+0200 Africa/Kigali'), ('Africa/Lubumbashi', '+0200 Africa/Lubumbashi'), ('Africa/Lusaka', '+0200 Africa/Lusaka'), ('Africa/Addis_Ababa', '+0300 Africa/Addis_Ababa'), ('Africa/Asmara', '+0300Africa/Asmara'), ('Africa/Dar_es_Salaam', '+0300 Africa/Dar_es_Salaam'), ('Africa/Djibouti', '+0300 Africa/Djibouti'), ('Africa/Juba', '+0300 Africa/Juba'), ('Africa/Kampala', '+0300 Africa/Kampala'), ('Africa/Khartoum', '+0300 Africa/Khartoum')]

较短的版本:

tzs = [...]

pre_sort_tzs = map(lambda x: [int(x[1][0:5], 10), (x)], tzs)
sorted_tzs = map(lambda x: x[1], sorted(pre_sort_tzs))

print sorted_tzs
于 2013-08-29T14:24:57.993 回答
0

我在这个项目中找到了答案

https://github.com/brosner/django-timezones/blob/master/timezones/zones.py

这正是我想要的。

我导入了 PRETTY_TIMEZONE_CHOICES 并且这个选择的顺序是正确的。

于 2013-08-29T14:25:12.450 回答
0

对于那些喜欢熊猫方式的人:

from pytz import timezone,common_timezones as tz_names
from datetime import datetime as dt

numeric_offset = [int(dt.now(timezone(tz)).strftime('%z')) for tz in tz_names]
pd.Series(numeric_offset,index=tz_names).sort_values()

输出:

Pacific/Pago_Pago       -1100
Pacific/Midway          -1100
Pacific/Niue            -1100
US/Hawaii               -1000
Pacific/Honolulu        -1000
Pacific/Tahiti          -1000
Pacific/Rarotonga       -1000
America/Adak            -1000
Pacific/Marquesas        -930
America/Sitka            -900
America/Anchorage        -900
Pacific/Gambier          -900
America/Nome             -900
US/Alaska                -900
America/Yakutat          -900
America/Metlakatla       -900
America/Juneau           -900
America/Vancouver        -800
America/Whitehorse       -800
Canada/Pacific           -800
America/Los_Angeles      -800
America/Tijuana          -800
US/Pacific               -800
Pacific/Pitcairn         -800
America/Dawson           -800
America/Hermosillo       -700
America/Fort_Nelson      -700
America/Cambridge_Bay    -700
America/Mazatlan         -700
America/Edmonton         -700
                         ... 
Asia/Srednekolymsk       1100
Pacific/Pohnpei          1100
Australia/Sydney         1100
Pacific/Kosrae           1100
Australia/Hobart         1100
Pacific/Guadalcanal      1100
Pacific/Efate            1100
Asia/Sakhalin            1100
Australia/Lord_Howe      1100
Australia/Melbourne      1100
Asia/Magadan             1100
Pacific/Bougainville     1100
Pacific/Wallis           1200
Pacific/Wake             1200
Pacific/Tarawa           1200
Asia/Kamchatka           1200
Pacific/Nauru            1200
Pacific/Majuro           1200
Pacific/Kwajalein        1200
Asia/Anadyr              1200
Pacific/Funafuti         1200
Antarctica/McMurdo       1300
Pacific/Tongatapu        1300
Pacific/Enderbury        1300
Pacific/Fiji             1300
Pacific/Auckland         1300
Pacific/Fakaofo          1300
Pacific/Chatham          1345
Pacific/Apia             1400
Pacific/Kiritimati       1400
Length: 439, dtype: int64
于 2019-01-10T19:06:34.917 回答