From: Sachin Paliwal Date: Fri, 5 Feb 2016 15:39:34 +0000 (+0530) Subject: dates: Add Features in get timezone functions X-Git-Tag: 2.3.1~20^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=949e4cb402b6290245b88051c00577b96ae4787f;p=thirdparty%2Fbabel.git dates: Add Features in get timezone functions Added argument 'return_z' and two values 'iso8601' and 'iso8601_short' in argument width in get_timezone_gmt(), 'return_city' argument is add in get_timezone_location() and 'return_zone' in get_timezone_name() so we can implement iso8601 timezone patterns. --- diff --git a/babel/dates.py b/babel/dates.py index b41945bc..6d6c36aa 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -370,20 +370,25 @@ def get_time_format(format='medium', locale=LC_TIME): 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: @@ -395,8 +400,10 @@ def get_timezone_gmt(datetime=None, width='long', locale=LC_TIME): :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) @@ -404,14 +411,20 @@ def get_timezone_gmt(datetime=None, width='long', locale=LC_TIME): 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 @@ -420,6 +433,10 @@ def get_timezone_location(dt_or_tzinfo=None, locale=LC_TIME): >>> 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' @@ -437,7 +454,10 @@ def get_timezone_location(dt_or_tzinfo=None, locale=LC_TIME): 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) @@ -459,7 +479,7 @@ def get_timezone_location(dt_or_tzinfo=None, locale=LC_TIME): 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 @@ -476,6 +496,8 @@ def get_timezone_location(dt_or_tzinfo=None, locale=LC_TIME): else: city_name = zone.replace('_', ' ') + if return_city: + return city_name return region_format % (fallback_format % { '0': city_name, '1': territory_name @@ -483,13 +505,15 @@ def get_timezone_location(dt_or_tzinfo=None, locale=LC_TIME): 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' @@ -548,6 +572,8 @@ def get_timezone_name(dt_or_tzinfo=None, width='long', uncommon=False, 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) @@ -572,7 +598,8 @@ def get_timezone_name(dt_or_tzinfo=None, width='long', uncommon=False, # 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: diff --git a/tests/test_dates.py b/tests/test_dates.py index e93fa401..0d3fe546 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -393,12 +393,13 @@ def test_get_time_format(): 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' @@ -406,6 +407,11 @@ def test_get_timezone_location(): 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') @@ -419,6 +425,8 @@ def test_get_timezone_name(): 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')