0

需要一个带有 pytz common_timezones 的 ListBox/Dropdown 命名每个 UTC 差异。

<select style="cursor:pointer; min-width: 300px;" name="timezone">
    {% for tz in timezones %}
        <option value="{{ tz }}"{% if tz == TIME_ZONE %} selected="selected"{% endif %}>{{ tz }}</option>
    {% endfor %}
</select>

我正在为该列表提供: 'timezones': pytz.common_timezones, 'TIME_ZONE': request.session['django_timezone'] on render to response ...

但我只得到一个名称列表..我需要一个名称列表,每个时区的 UTC 差异..

例子:

美国/XYZ UTC-xxxx/UTC-xxxx+1

欧洲/XPTO UTC+xxxx

已编辑:我很乐意在这一刻(这一刻)获得每个时区的当前已知偏移量,如此处所示http://www.timeanddate.com/worldclock/),如果可能的话,去年一/两年产卵(可能性像:http ://www.timeanddate.com/library/abbreviations/timezones/ )。

提前感谢您提供的任何帮助!

4

2 回答 2

0

我来到了这个:

def get_timezones_with_gmt_diff(timezone_string_list = None):
    """
    Converts a datetime object into string.
    """
    if timezone_string_list:
        timezone_list = timezone_string_list
    else:
        timezone_list = pytz.common_timezones

    datetime1 = datetime.strptime("10-07-%s 00:00:00" % datetime.utcnow().year, "%d-%m-%Y %H:%M:%S")
    datetime2 = datetime.strptime("10-12-%s 00:00:00" % datetime.utcnow().year, "%d-%m-%Y %H:%M:%S")

    result_dict = {}
    for time_zone in timezone_list:
        result_dict[time_zone] = [Date.timedelta_in_seconds(pytz.timezone(time_zone).localize(datetime1).tzinfo._utcoffset)/3600,
                                  Date.timedelta_in_seconds(pytz.timezone(time_zone).localize(datetime2).tzinfo._utcoffset)/3600,
                                  time_zone.replace("/", " - ").replace("_", " "),
                                  Date.timedelta_in_seconds(pytz.timezone(time_zone).localize(datetime1).tzinfo._utcoffset)/3600 !=
                                  Date.timedelta_in_seconds(pytz.timezone(time_zone).localize(datetime2).tzinfo._utcoffset)/3600]

    sorted_result_dict = OrderedDict(sorted(result_dict.items(), key=lambda t: t[1][2]))
    return sorted_result_dict

def get_timezones_for_listbox(time_zones_dict):
    list = []
    for time_zone_tuple in time_zones_dict:
        # Has different DST/Summer times:
        if time_zones_dict[time_zone_tuple][3]:
            new_item = "%s      UCT %s/%s" % (str(time_zones_dict[time_zone_tuple][2]), str(time_zones_dict[time_zone_tuple][0]), str(time_zones_dict[time_zone_tuple][1]))
        # Does not change datetime:
        else:
            new_item = "%s      UCT %s" % (str(time_zones_dict[time_zone_tuple][2]), str(time_zones_dict[time_zone_tuple][0]))
        list.append(new_item)
    return list

def timedelta_in_seconds(duration):
    days, seconds = duration.days, duration.seconds
    return days * 24 * 60 * 60 + seconds

当然,我将进一步操作这两种方法,以便从我的输入上下文中完成所需的输出。

于 2013-01-31T16:07:52.607 回答
0
 {% load tz %}

 <select style="cursor:pointer; min-width: 300px;" name="timezone">
     {% for tz in timezones %}
    <option value="{{ tz }}"{% if tz == TIME_ZONE %} selected="selected"{% endif %}>
        {{ tz }} - {{tz|utc}}
    </option>
    {% endfor %}
 </select>
于 2013-01-31T18:02:38.360 回答