Example #1
0
def test_no_inherit_metazone_formatting():
    # See: https://github.com/python-babel/babel/issues/428
    tz = pytz.timezone('America/Los_Angeles')
    t = tz.localize(datetime(2016, 1, 6, 7))
    assert dates.format_time(t, format='long', locale='en_US') == "7:00:00 AM PST"
    assert dates.format_time(t, format='long', locale='en_GB') == "07:00:00 Pacific Standard Time"
    assert dates.get_timezone_name(t, width='short', locale='en_US') == "PST"
    assert dates.get_timezone_name(t, width='short', locale='en_GB') == "Pacific Standard Time"
Example #2
0
def test_no_inherit_metazone_formatting():
    # See: https://github.com/python-babel/babel/issues/428
    tz = pytz.timezone('America/Los_Angeles')
    t = tz.localize(datetime(2016, 1, 6, 7))
    assert dates.format_time(t, format='long', locale='en_US') == "7:00:00 AM PST"
    assert dates.format_time(t, format='long', locale='en_GB') == "07:00:00 Pacific Standard Time"
    assert dates.get_timezone_name(t, width='short', locale='en_US') == "PST"
    assert dates.get_timezone_name(t, width='short', locale='en_GB') == "Pacific Standard Time"
Example #3
0
def test_get_timezone_name_misc(timezone_getter):
    localnow = datetime.utcnow().replace(tzinfo=timezone_getter('UTC')).astimezone(dates.LOCALTZ)
    assert (dates.get_timezone_name(None, locale='en_US') ==
            dates.get_timezone_name(localnow, locale='en_US'))

    assert (dates.get_timezone_name('Europe/Berlin', locale='en_US') == "Central European Time")

    assert (dates.get_timezone_name(1400000000, locale='en_US', width='short') == "Unknown Region (UTC) Time")
    assert (dates.get_timezone_name(time(16, 20), locale='en_US', width='short') == "UTC")
Example #4
0
def convert_timezones():
    timezones = {}
    found = set()

    today = datetime.utcnow()

    for zone_name in sorted(pytz.all_timezones):
        tzinfo = dates.get_timezone(zone_name)
        if tzinfo is None:
            continue
        short_name = zone_name

        try:
            transition = dates.get_next_timezone_transition(tzinfo, today)
        except TypeError:
            continue
        if transition is None:
            key = tzinfo.tzname(today)
            has_dst = False
            name = dates.get_timezone_name(tzinfo)
        else:
            from_tz = transition.from_tz
            to_tz = transition.to_tz
            from_tzinfo = transition.from_tzinfo
            to_tzinfo = transition.to_tzinfo
            if transition.from_tzinfo.localize(today).dst():
                dst_tz = from_tz
                std_tz = to_tz
                dst_tzinfo = from_tzinfo
                std_tzinfo = to_tzinfo
            else:
                dst_tz = to_tz
                std_tz = from_tz
                dst_tzinfo = to_tzinfo
                std_tzinfo = from_tzinfo
            key = '%s/%s' % (std_tz, dst_tz)
            name = dates.get_timezone_name(std_tzinfo, zone_variation='generic')

        if name in found:
            continue
        found.add(name)

        timezones[short_name] = {
            'short': key,
            'name': name
        }

    with open('timezones.json', 'w') as f:
        json.dump({'timezones': timezones}, f, indent=2)
Example #5
0
    def get_timezone_location(self, dt_or_tzinfo):
        """Returns a representation of the given timezone using "location
        format".

        The result depends on both the local display name of the country and
        the city assocaited with the time zone::

            >>> from pytz import timezone
            >>> tz = timezone('America/St_Johns')
            >>> get_timezone_location(tz, locale='de_DE')
            u"Kanada (St. John's)"
            >>> tz = timezone('America/Mexico_City')
            >>> get_timezone_location(tz, locale='de_DE')
            u'Mexiko (Mexiko-Stadt)'

        If the timezone is associated with a country that uses only a single
        timezone, just the localized country name is returned::

            >>> tz = timezone('Europe/Berlin')
            >>> get_timezone_name(tz, locale='de_DE')
            u'Deutschland'

        :param dt_or_tzinfo:
            The ``datetime`` or ``tzinfo`` object that determines
            the timezone; if None, the current date and time in UTC is assumed.
        :returns:
            The localized timezone name using location format.
        """
        return dates.get_timezone_name(dt_or_tzinfo, locale=self.locale)
Example #6
0
def get_translated_dict():
    from babel.dates import Locale, get_timezone, get_timezone_name

    translated_dict = {}
    locale = Locale.parse(frappe.local.lang, sep="-")

    # timezones
    for tz in get_all_timezones():
        timezone_name = get_timezone_name(get_timezone(tz),
                                          locale=locale,
                                          width="short")
        if timezone_name:
            translated_dict[tz] = timezone_name + " - " + tz

    # country names && currencies
    for country, info in get_all().items():
        country_name = locale.territories.get((info.get("code") or "").upper())
        if country_name:
            translated_dict[country] = country_name

        currency = info.get("currency")
        currency_name = locale.currencies.get(currency)
        if currency_name:
            translated_dict[currency] = currency_name

    return translated_dict
Example #7
0
    def get_timezone_location(self, dt_or_tzinfo):
        """Returns a representation of the given timezone using "location
        format".

        The result depends on both the local display name of the country and
        the city assocaited with the time zone::

            >>> from pytz import timezone
            >>> tz = timezone('America/St_Johns')
            >>> get_timezone_location(tz, locale='de_DE')
            u"Kanada (St. John's)"
            >>> tz = timezone('America/Mexico_City')
            >>> get_timezone_location(tz, locale='de_DE')
            u'Mexiko (Mexiko-Stadt)'

        If the timezone is associated with a country that uses only a single
        timezone, just the localized country name is returned::

            >>> tz = timezone('Europe/Berlin')
            >>> get_timezone_name(tz, locale='de_DE')
            u'Deutschland'

        :param dt_or_tzinfo:
            The ``datetime`` or ``tzinfo`` object that determines
            the timezone; if None, the current date and time in UTC is assumed.
        :returns:
            The localized timezone name using location format.
        """
        return dates.get_timezone_name(dt_or_tzinfo, locale=self.locale)
Example #8
0
    def get_timezone_name(self, dt_or_timezone=None, width='long', uncommon=False):
        """Return the localized display name for the given timezone

        In:
          - ``dt_or_timezone`` -- the timezone, specified using a ``datetime`` or ``tzinfo`` object
          - ``width`` --
          - ``uncommon`` --

        >>> from pytz import timezone
        >>> dt = datetime.time(15, 30, tzinfo=timezone('America/Los_Angeles'))
        >>> Locale('en', 'US').get_timezone_name(dt)
        u'Pacific Standard Time'
        >>> Locale('en', 'US').get_timezone_name(dt, width='short')
        u'PST'

        In:
          - ``dt_or_tzinfo`` -- the ``datetime`` or ``tzinfo`` object that determines
            the timezone; if a ``tzinfo`` object is used, the resulting display
            name will be generic, i.e. independent of daylight savings time;
            if ``None``, the current date in UTC is assumed
          - ``width`` -- either 'long' or 'short'
          - ``uncommon`` -- whether even uncommon timezone abbreviations should be used

        Return:
          - the localized timezone name
        """
        return dates.get_timezone_name(dt_or_timezone, width, uncommon, self)
Example #9
0
 def get(self, request, *args, **kwargs):
     try:
         # build dictionary of timezone: translated city name
         locale = get_tz_locale()
         timezones = [{'zone': tz, 'name': get_timezone_name(tz, locale=locale)}
                      for tz in pytz.all_timezones]
         return Response(timezones, status=status.HTTP_200_OK)
     except Exception as ex:
         return Response({'error': ex.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #10
0
def test_no_inherit_metazone_marker_never_in_output(locale):
    # See: https://github.com/python-babel/babel/issues/428
    tz = pytz.timezone('America/Los_Angeles')
    t = tz.localize(datetime(2016, 1, 6, 7))
    assert NO_INHERITANCE_MARKER not in dates.format_time(t,
                                                          format='long',
                                                          locale=locale)
    assert NO_INHERITANCE_MARKER not in dates.get_timezone_name(t,
                                                                width='short',
                                                                locale=locale)
Example #11
0
def get_timezones():
    user = get_user()
    lang = user.get('language',
                    flask.current_app.config.get('DEFAULT_LANGUAGE',
                                                 'en')).replace('-', '_')
    return [{
        'id': tz,
        'name': dates.get_timezone_name(tz, locale=lang),
        'location': dates.get_timezone_location(tz, locale=lang),
    } for tz in pytz.common_timezones]
Example #12
0
def get_timezones():
    user = get_user()
    lang = user.get("language",
                    flask.current_app.config.get("DEFAULT_LANGUAGE",
                                                 "en")).replace("-", "_")
    return [{
        "id": tz,
        "name": dates.get_timezone_name(tz, locale=lang),
        "location": dates.get_timezone_location(tz, locale=lang),
    } for tz in pytz.common_timezones]
Example #13
0
def test_get_timezone_location():
    tz = timezone('America/St_Johns')
    assert (dates.get_timezone_location(tz, locale='de_DE') ==
            u"Kanada (St. John's) Zeit")
    tz = timezone('America/Mexico_City')
    assert (dates.get_timezone_location(tz, locale='de_DE') ==
            u'Mexiko (Mexiko-Stadt) Zeit')

    tz = timezone('Europe/Berlin')
    assert (dates.get_timezone_name(tz, locale='de_DE') ==
            u'Mitteleurop\xe4ische Zeit')
Example #14
0
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")
    tz = timezone('America/Mexico_City')
    assert (dates.get_timezone_location(
        tz, locale='de_DE') == u'Mexiko (Mexiko-Stadt) Zeit')

    tz = timezone('Europe/Berlin')
    assert (dates.get_timezone_name(
        tz, locale='de_DE') == u'Mitteleurop\xe4ische Zeit')
Example #15
0
File: base.py Project: UfSoft/oil
 def _build_timezones(self, locale=None):
     log.debug('UPDATING TZs for %r', h.get_lang()[0])
     tz_list = []
     for tzname in common_timezones:
         try:
             tz = {}
             tz['value'] = tzname
             tz['name'] = get_timezone_name(timezone(tzname), locale=locale)
             tz_list.append(tz)
         except KeyError:
             pass
     tz_list.sort(lambda x,y: cmp(x['value'], y['value']))
     return tz_list
Example #16
0
def get_timezones():
    from babel.dates import get_timezone, get_timezone_name, Locale
    from frappe.utils.momentjs import get_all_timezones

    translated_dict = {}
    locale = Locale.parse(frappe.local.lang, sep="-")

    for tz in get_all_timezones():
        timezone_name = get_timezone_name(get_timezone(tz),
                                          locale=locale,
                                          width='short')
        if timezone_name:
            translated_dict[tz] = timezone_name + ' - ' + tz

    return translated_dict
Example #17
0
    def get_timezone_name(self, dt_or_timezone=None, width='long', uncommon=False, zone_variant=None, return_zone=False):
        """Return the localized display name for the given timezone

        In:
          - ``dt_or_tzinfo`` -- the ``datetime`` or ``tzinfo`` object that determines
            the timezone; if a ``tzinfo`` object is used, the resulting display
            name will be generic, i.e. independent of daylight savings time;
            if ``None``, the current date in UTC is assumed
          - ``width`` -- either 'long' or 'short'
          - ``uncommon`` -- whether even uncommon timezone abbreviations should be used
          - ``zone_variant`` -- defines the zone variation to return
          - ``return_zone`` -- long time zone ID?

        Return:
          - the localized timezone name
        """
        return dates.get_timezone_name(dt_or_timezone, width, uncommon, self, zone_variant, return_zone)
def with_timezone(original, evt, locale, user_tz: tzinfo = None):
    """
        The timezone concatenation might be incorrect in some cases, but there
        seems not to be a format string for this purpose.
    """
    dt = evt.start_localized
    if user_tz:
        dt = evt.start.astimezone(user_tz)
    """
        This is just a hack to prevent an ugly display like "Unknown Region
        (GMT) Time". It should be replaced with something better.
    """
    if dt.tzname() == 'GMT':
        return original + ' GMT'
    elif dt.tzname() == 'UTC':
        return original + ' UTC'

    return original + ' ' + get_timezone_name(
        dt.tzinfo, width='short', locale=locale)
def build_time_zone_choices(pays=None):
    timezones = pytz.country_timezones[pays] if pays else pytz.common_timezones
    result = []
    now = datetime.datetime.now()
    for tzname in timezones:
        tz = pytz.timezone(tzname)
        fr_name = get_timezone_name(tz, locale='fr_FR')
        try:
            offset = tz.utcoffset(now)
        except (AmbiguousTimeError, NonExistentTimeError):
            # oups. On est en train de changer d'heure. Ça devrait être fini
            # demain
            offset = tz.utcoffset(now + datetime.timedelta(days=1))
        seconds = offset.seconds + offset.days * 86400
        (hours, minutes) = divmod(seconds // 60, 60)
        offset_str = 'UTC%+d:%d' % (hours, minutes) \
                if minutes else 'UTC%+d' % hours
        result.append((seconds, tzname, '%s - %s' % (offset_str, fr_name)))
    result.sort()
    return [(x[1], x[2]) for x in result]
Example #20
0
def get_translated_dict():
	from babel.dates import get_timezone, get_timezone_name, Locale

	translated_dict = {}
	locale = Locale.parse(frappe.local.lang, sep="-")

	# timezones
	for tz in get_all_timezones():
		timezone_name = get_timezone_name(get_timezone(tz), locale=locale, width='short')
		if timezone_name:
			translated_dict[tz] = timezone_name + ' - ' + tz

	# country names && currencies
	for country, info in get_all().items():
		country_name = locale.territories.get((info.get("code") or "").upper())
		if country_name:
			translated_dict[country] = country_name

		currency = info.get("currency")
		currency_name = locale.currencies.get(currency)
		if currency_name:
			translated_dict[currency] = currency_name

	return translated_dict
Example #21
0
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, width='short', locale='en_US') == u'PST'

    tz = timezone('America/Los_Angeles')
    assert dates.get_timezone_name(tz, locale='en_US') == u'Pacific Time'
    assert dates.get_timezone_name(tz, 'short', locale='en_US') == u'PT'

    tz = timezone('Europe/Berlin')
    assert (dates.get_timezone_name(
        tz, locale='de_DE') == u'Mitteleurop\xe4ische Zeit')
    assert (dates.get_timezone_name(
        tz, locale='pt_BR') == u'Hor\xe1rio da Europa Central')

    tz = timezone('America/St_Johns')
    assert dates.get_timezone_name(tz, locale='de_DE') == u'Neufundland-Zeit'

    tz = timezone('America/Los_Angeles')
    assert dates.get_timezone_name(tz,
                                   locale='en',
                                   width='short',
                                   zone_variant='generic') == u'PT'
    assert dates.get_timezone_name(tz,
                                   locale='en',
                                   width='short',
                                   zone_variant='standard') == u'PST'
    assert dates.get_timezone_name(tz,
                                   locale='en',
                                   width='short',
                                   zone_variant='daylight') == u'PDT'
    assert dates.get_timezone_name(tz,
                                   locale='en',
                                   width='long',
                                   zone_variant='generic') == u'Pacific Time'
    assert dates.get_timezone_name(
        tz, locale='en', width='long',
        zone_variant='standard') == u'Pacific Standard Time'
    assert dates.get_timezone_name(
        tz, locale='en', width='long',
        zone_variant='daylight') == u'Pacific Daylight Time'
 def tz_name(self):
     return get_timezone_name(self.start_localized)
Example #23
0
 def mktz(self, tzlist, loc):
     return [(tz, get_timezone_name(timezone(tz), locale=loc))
             for tz in tzlist]
Example #24
0
def test_no_inherit_metazone_marker_never_in_output(locale):
    # See: https://github.com/python-babel/babel/issues/428
    tz = pytz.timezone('America/Los_Angeles')
    t = tz.localize(datetime(2016, 1, 6, 7))
    assert NO_INHERITANCE_MARKER not in dates.format_time(t, format='long', locale=locale)
    assert NO_INHERITANCE_MARKER not in dates.get_timezone_name(t, width='short', locale=locale)
Example #25
0
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')
    assert dates.get_timezone_name(tz, locale='en_US') == u'Pacific Time'
    assert dates.get_timezone_name(tz, 'short', locale='en_US') == u'PT'

    tz = timezone('Europe/Berlin')
    assert (dates.get_timezone_name(tz, locale='de_DE') ==
            u'Mitteleurop\xe4ische Zeit')
    assert (dates.get_timezone_name(tz, locale='pt_BR') ==
            u'Hor\xe1rio da Europa Central')

    tz = timezone('America/St_Johns')
    assert dates.get_timezone_name(tz, locale='de_DE') == u'Neufundland-Zeit'

    tz = timezone('America/Los_Angeles')
    assert dates.get_timezone_name(tz, locale='en', width='short',
                                   zone_variant='generic') == u'PT'
    assert dates.get_timezone_name(tz, locale='en', width='short',
                                   zone_variant='standard') == u'PST'
    assert dates.get_timezone_name(tz, locale='en', width='short',
                                   zone_variant='daylight') == u'PDT'
    assert dates.get_timezone_name(tz, locale='en', width='long',
                                   zone_variant='generic') == u'Pacific Time'
    assert dates.get_timezone_name(tz, locale='en', width='long',
                                   zone_variant='standard') == u'Pacific Standard Time'
    assert dates.get_timezone_name(tz, locale='en', width='long',
                                   zone_variant='daylight') == u'Pacific Daylight Time'

    localnow = datetime.utcnow().replace(tzinfo=timezone('UTC')).astimezone(dates.LOCALTZ)
    assert (dates.get_timezone_name(None, locale='en_US') ==
            dates.get_timezone_name(localnow, locale='en_US'))

    assert (dates.get_timezone_name('Europe/Berlin', locale='en_US') == "Central European Time")

    assert (dates.get_timezone_name(1400000000, locale='en_US', width='short') == "Unknown Region (UTC) Time")
    assert (dates.get_timezone_name(time(16, 20), locale='en_US', width='short') == "UTC")
Example #26
0
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, width='short', locale='en_US') == u'PST'

    tz = timezone('America/Los_Angeles')
    assert dates.get_timezone_name(tz, locale='en_US') == u'Pacific Time'
    assert dates.get_timezone_name(tz, 'short', locale='en_US') == u'PT'

    tz = timezone('Europe/Berlin')
    assert (dates.get_timezone_name(tz, locale='de_DE') ==
            u'Mitteleurop\xe4ische Zeit')
    assert (dates.get_timezone_name(tz, locale='pt_BR') ==
            u'Hor\xe1rio da Europa Central')

    tz = timezone('America/St_Johns')
    assert dates.get_timezone_name(tz, locale='de_DE') == u'Neufundland-Zeit'

    tz = timezone('America/Los_Angeles')
    assert dates.get_timezone_name(tz, locale='en', width='short',
                                   zone_variant='generic') == u'PT'
    assert dates.get_timezone_name(tz, locale='en', width='short',
                                   zone_variant='standard') == u'PST'
    assert dates.get_timezone_name(tz, locale='en', width='short',
                                   zone_variant='daylight') == u'PDT'
    assert dates.get_timezone_name(tz, locale='en', width='long',
                                   zone_variant='generic') == u'Pacific Time'
    assert dates.get_timezone_name(tz, locale='en', width='long',
                                   zone_variant='standard') == u'Pacific Standard Time'
    assert dates.get_timezone_name(tz, locale='en', width='long',
                                   zone_variant='daylight') == u'Pacific Daylight Time'
Example #27
0
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')
    assert dates.get_timezone_name(tz, locale='en_US') == u'Pacific Time'
    assert dates.get_timezone_name(tz, 'short', locale='en_US') == u'PT'

    tz = timezone('Europe/Berlin')
    assert (dates.get_timezone_name(tz, locale='de_DE') ==
            u'Mitteleurop\xe4ische Zeit')
    assert (dates.get_timezone_name(tz, locale='pt_BR') ==
            u'Hor\xe1rio da Europa Central')

    tz = timezone('America/St_Johns')
    assert dates.get_timezone_name(tz, locale='de_DE') == u'Neufundland-Zeit'

    tz = timezone('America/Los_Angeles')
    assert dates.get_timezone_name(tz, locale='en', width='short',
                                   zone_variant='generic') == u'PT'
    assert dates.get_timezone_name(tz, locale='en', width='short',
                                   zone_variant='standard') == u'PST'
    assert dates.get_timezone_name(tz, locale='en', width='short',
                                   zone_variant='daylight') == u'PDT'
    assert dates.get_timezone_name(tz, locale='en', width='long',
                                   zone_variant='generic') == u'Pacific Time'
    assert dates.get_timezone_name(tz, locale='en', width='long',
                                   zone_variant='standard') == u'Pacific Standard Time'
    assert dates.get_timezone_name(tz, locale='en', width='long',
                                   zone_variant='daylight') == u'Pacific Daylight Time'

    localnow = datetime.utcnow().replace(tzinfo=timezone('UTC')).astimezone(dates.LOCALTZ)
    assert (dates.get_timezone_name(None, locale='en_US') ==
            dates.get_timezone_name(localnow, locale='en_US'))

    assert (dates.get_timezone_name('Europe/Berlin', locale='en_US') == "Central European Time")

    assert (dates.get_timezone_name(1400000000, locale='en_US', width='short') == "Unknown Region (UTC) Time")
    assert (dates.get_timezone_name(time(16, 20), locale='en_US', width='short') == "UTC")
Example #28
0
def test_get_timezone_name_tzinfo(timezone_getter, tzname, params, expected):
    tz = timezone_getter(tzname)
    assert dates.get_timezone_name(tz, **params) == expected
Example #29
0
def test_get_timezone_name_time_pytz(timezone_getter, tzname, params, expected):
    """pytz (by design) can't determine if the time is in DST or not,
    so it will always return Standard time"""
    dt = time(15, 30, tzinfo=timezone_getter(tzname))
    assert dates.get_timezone_name(dt, **params) == expected
Example #30
0
>>> t = get_next_timezone_transition('Europe/Vienna', datetime(2011, 3, 2))
>>> t
<TimezoneTransition CET -> CEST (2011-03-27 01:00:00)>
>>> t.from_offset
3600.0
>>> t.to_offset
7200.0
>>> t.from_tz
'CET'
>>> t.to_tz
'CEST'

>>> from babel.dates import LOCALTZ, get_timezone_name
>>> LOCALTZ
<DstTzInfo 'Europe/Vienna' CET+1:00:00 STD>
>>> get_timezone_name(LOCALTZ)
u'Central European Time'


#Localized Time-zone Names
>>> from babel import Locale
>>> from babel.dates import get_timezone_name, get_timezone
>>> tz = get_timezone('Europe/Berlin')
>>> get_timezone_name(tz, locale=Locale.parse('pt_PT'))
u'Hora da Europa Central'

>>> from datetime import datetime

>>> dt = tz.localize(datetime(2007, 8, 15))
>>> get_timezone_name(dt, locale=Locale.parse('de_DE'))
u'Mitteleurop\xe4ische Sommerzeit'