return Locale.parse(locale).time_formats[format]
-def get_timezone_gmt(datetime=None, width='long', locale=LC_TIME):
+def get_timezone_gmt(datetime=None, width='long', locale=LC_TIME, return_z=False):
"""Return the timezone associated with the given `datetime` object formatted
as string indicating the offset from GMT.
>>> dt = datetime(2007, 4, 1, 15, 30)
>>> get_timezone_gmt(dt, locale='en')
u'GMT+00:00'
-
+ >>> get_timezone_gmt(dt, locale='en', return_z=True)
+ 'Z'
+ >>> get_timezone_gmt(dt, locale='en', width='iso8601_short')
+ u'+00'
>>> tz = get_timezone('America/Los_Angeles')
>>> dt = tz.localize(datetime(2007, 4, 1, 15, 30))
>>> get_timezone_gmt(dt, locale='en')
u'GMT-07:00'
>>> get_timezone_gmt(dt, 'short', locale='en')
u'-0700'
+ >>> get_timezone_gmt(dt, locale='en', width='iso8601_short')
+ u'-07'
The long format depends on the locale, for example in France the acronym
UTC string is used instead of GMT:
:param datetime: the ``datetime`` object; if `None`, the current date and
time in UTC is used
- :param width: either "long" or "short"
+ :param width: either "long" or "short" or "iso8601" or "iso8601_short"
:param locale: the `Locale` object, or a locale string
+ :param return_z: True or False; Function returns indicator "Z"
+ when local time offset is 0
"""
datetime = _ensure_datetime_tzinfo(_get_datetime(datetime))
locale = Locale.parse(locale)
offset = datetime.tzinfo.utcoffset(datetime)
seconds = offset.days * 24 * 60 * 60 + offset.seconds
hours, seconds = divmod(seconds, 3600)
- if width == 'short':
+ if return_z and hours == 0 and seconds == 0:
+ return 'Z'
+ elif seconds == 0 and width == 'iso8601_short':
+ return u'%+03d' % hours
+ elif width == 'short' or width == 'iso8601_short':
pattern = u'%+03d%02d'
+ elif width == 'iso8601':
+ pattern = u'%+03d:%02d'
else:
pattern = locale.zone_formats['gmt'] % '%+03d:%02d'
return pattern % (hours, seconds // 60)
-def get_timezone_location(dt_or_tzinfo=None, locale=LC_TIME):
+def get_timezone_location(dt_or_tzinfo=None, locale=LC_TIME, return_city=False):
u"""Return a representation of the given timezone using "location format".
The result depends on both the local display name of the country and the
>>> tz = get_timezone('America/St_Johns')
>>> print(get_timezone_location(tz, locale='de_DE'))
Kanada (St. John’s) Zeit
+ >>> print(get_timezone_location(tz, locale='en'))
+ Canada (St. John’s) Time
+ >>> print(get_timezone_location(tz, locale='en', return_city=True))
+ St. John’s
>>> tz = get_timezone('America/Mexico_City')
>>> get_timezone_location(tz, locale='de_DE')
u'Mexiko (Mexiko-Stadt) Zeit'
the timezone; if `None`, the current date and time in
UTC is assumed
:param locale: the `Locale` object, or a locale string
+ :param return_city: True or False, if True then return exemplar city (location)
+ for the time zone
:return: the localized timezone name using location format
+
"""
dt, tzinfo = _get_dt_and_tzinfo(dt_or_tzinfo)
locale = Locale.parse(locale)
if territory not in locale.territories:
territory = 'ZZ' # invalid/unknown
territory_name = locale.territories[territory]
- if territory and len(get_global('territory_zones').get(territory, [])) == 1:
+ if not return_city and territory and len(get_global('territory_zones').get(territory, [])) == 1:
return region_format % (territory_name)
# Otherwise, include the city in the output
else:
city_name = zone.replace('_', ' ')
+ if return_city:
+ return city_name
return region_format % (fallback_format % {
'0': city_name,
'1': territory_name
def get_timezone_name(dt_or_tzinfo=None, width='long', uncommon=False,
- locale=LC_TIME, zone_variant=None):
+ locale=LC_TIME, zone_variant=None, return_zone=False):
r"""Return the localized display name for the given timezone. The timezone
may be specified using a ``datetime`` or `tzinfo` object.
>>> dt = time(15, 30, tzinfo=get_timezone('America/Los_Angeles'))
>>> get_timezone_name(dt, locale='en_US')
u'Pacific Standard Time'
+ >>> get_timezone_name(dt, locale='en_US', return_zone=True)
+ 'America/Los_Angeles'
>>> get_timezone_name(dt, width='short', locale='en_US')
u'PST'
values are valid: ``'generic'``, ``'daylight'`` and
``'standard'``.
:param locale: the `Locale` object, or a locale string
+ :param return_zone: True or False. If true then function
+ returns long time zone ID
"""
dt, tzinfo = _get_dt_and_tzinfo(dt_or_tzinfo)
locale = Locale.parse(locale)
# Get the canonical time-zone code
zone = get_global('zone_aliases').get(zone, zone)
-
+ if return_zone:
+ return zone
info = locale.time_zones.get(zone, {})
# Try explicitly translated zone names first
if width in info:
def test_get_timezone_gmt():
dt = datetime(2007, 4, 1, 15, 30)
assert dates.get_timezone_gmt(dt, locale='en') == u'GMT+00:00'
-
+ assert dates.get_timezone_gmt(dt, locale='en', return_z=True) == 'Z'
+ assert dates.get_timezone_gmt(dt, locale='en', width='iso8601_short') == u'+00'
tz = timezone('America/Los_Angeles')
dt = tz.localize(datetime(2007, 4, 1, 15, 30))
assert dates.get_timezone_gmt(dt, locale='en') == u'GMT-07:00'
assert dates.get_timezone_gmt(dt, 'short', locale='en') == u'-0700'
-
+ assert dates.get_timezone_gmt(dt, locale='en', width='iso8601_short') == u'-07'
assert dates.get_timezone_gmt(dt, 'long', locale='fr_FR') == u'UTC-07:00'
tz = timezone('America/St_Johns')
assert (dates.get_timezone_location(tz, locale='de_DE') ==
u"Kanada (St. John\u2019s) Zeit")
+ assert (dates.get_timezone_location(tz, locale='en') ==
+ u'Canada (St. John’s) Time')
+ assert (dates.get_timezone_location(tz, locale='en', return_city=True) ==
+ u'St. John’s')
+
tz = timezone('America/Mexico_City')
assert (dates.get_timezone_location(tz, locale='de_DE') ==
u'Mexiko (Mexiko-Stadt) Zeit')
dt = time(15, 30, tzinfo=timezone('America/Los_Angeles'))
assert (dates.get_timezone_name(dt, locale='en_US') ==
u'Pacific Standard Time')
+ assert (dates.get_timezone_name(dt, locale='en_US', return_zone=True) ==
+ u'America/Los_Angeles')
assert dates.get_timezone_name(dt, width='short', locale='en_US') == u'PST'
tz = timezone('America/Los_Angeles')