0

Need a ListBox/Dropdown with pytz common_timezones name each difference for 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>

I'm feeding that list with: 'timezones': pytz.common_timezones, 'TIME_ZONE': request.session['django_timezone'] on render to response...

but I'm getting only a list of names.. I needed a list of names the difference of each time zone for UTC..

example:

America/XYZ UTC-xxxx/UTC-xxxx+1

Europe/XPTO UTC+xxxx

Edited: I would be happy getting the present known offsets for each timezone like showed here http://www.timeanddate.com/worldclock/) in this moment (this instant) and if possible with one/two last years spawn (the possibilities like: http://www.timeanddate.com/library/abbreviations/timezones/).

Thanks in advance for any help given!

  • What do you mean by "the difference"? It varies over time for most time zones. Do you mean the offset at the current instant in time? All possible offsets across history? All possible offsets within some time period (e.g. year 2000 to year 2030)? – Jon Skeet Jan 30 '13 at 18:53
  • You're right because of DST.. right?! In the current instant would be enough.. it would be nice for the last two years... BTW, now that you answered and you are WHO you are at stackoverflow noticed you some days ago (checked your overwhelming profile), I would be happy just having an answer from a King :P – samthgreat_pt Jan 30 '13 at 19:05
  • Well, DST and time zones just fundamentally changing over time. I can't look now, but if no-one else has answered later when I get a bit more time, I'll have a go. I'm not a Python person, mind you... – Jon Skeet Jan 30 '13 at 20:22
  • Please edit your question and clarify what you are asking, as @JonSkeet indicated. – rantanplan Jan 30 '13 at 20:25

2 Answers2

0

I came to this one:

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

of course I will further manipulate these two methods in order to accomplish the desired output from my input context.

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>
catherine
  • 22,492
  • 12
  • 61
  • 85