Ejemplo n.º 1
0
def test_get_timezone_gmt():
    dt = datetime(2007, 4, 1, 15, 30)
    assert dates.get_timezone_gmt(dt, locale='en') == u'GMT+00: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, 'long', locale='fr_FR') == u'UTC-07:00'
Ejemplo n.º 2
0
def test_get_timezone_gmt():
    dt = datetime(2007, 4, 1, 15, 30)
    assert dates.get_timezone_gmt(dt, locale='en') == u'GMT+00: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, 'long', locale='fr_FR') == u'UTC-07:00'
Ejemplo n.º 3
0
def test_get_timezone_gmt():
    dt = datetime(2007, 4, 1, 15, 30)
    assert dates.get_timezone_gmt(dt, locale='en') == 'GMT+00:00'

    tz = timezone('America/Los_Angeles')
    dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
    assert dates.get_timezone_gmt(dt, locale='en') == 'GMT-08:00'
    assert dates.get_timezone_gmt(dt, 'short', locale='en') == '-0800'

    assert dates.get_timezone_gmt(dt, 'long', locale='fr_FR') == 'UTC-08:00'
Ejemplo n.º 4
0
    def get_timezone_gmt(self, datetime=None, width='long', return_z=False):
        """Format the timezone associated with the given datetime

        In:
          - ``datetime`` -- the `datetime`
          - ``width`` -- 'long', 'short', 'iso8601' or 'iso8601_short'
          - ``return_z`` -- Have indicator 'Z' when local time offset is 0 to be included?

        Returns:
          - the formatted timezone
        """
        return dates.get_timezone_gmt(datetime, width, self, return_z)
Ejemplo n.º 5
0
def _timestamp_localized_timezone(
        tz: LocalizedTimezone,
        dt: datetime.datetime) -> TimestampedLocalizedTimezone:
    localized_dt = tz.tzinfo.localize(dt)
    offset = get_timezone_gmt(localized_dt, locale=tz.locale)
    return TimestampedLocalizedTimezone(
        id=tz.id,
        name=tz.name,
        offset=offset,
        aliases=tz.aliases,
        sort_key=(localized_dt.utcoffset(), tz.name_sort_key),
    )
Ejemplo n.º 6
0
def timezones_choices():
    """Timezones values and their labels for current locale.

    :return: an iterable of `(code, label)`, code being a timezone code and label
    the timezone name in current locale.
    """
    utcnow = pytz.utc.localize(datetime.utcnow())
    locale = _get_locale()
    for tz in sorted(pytz.common_timezones):
        tz = get_timezone(tz)
        now = tz.normalize(utcnow.astimezone(tz))
        label = '({}) {}'.format(get_timezone_gmt(now, locale=locale), tz.zone)
        yield (tz, label)  # get_timezone_name(tz, locale=locale))
Ejemplo n.º 7
0
def timezones_choices():
    """Timezones values and their labels for current locale.

    :return: an iterable of `(code, label)`, code being a timezone code and label
    the timezone name in current locale.
    """
    utcnow = pytz.utc.localize(datetime.utcnow())
    locale = _get_locale()
    for tz in sorted(pytz.common_timezones):
        tz = get_timezone(tz)
        now = tz.normalize(utcnow.astimezone(tz))
        label = "({}) {}".format(get_timezone_gmt(now, locale=locale), tz.zone)
        yield (tz, label)  # get_timezone_name(tz, locale=locale))
Ejemplo n.º 8
0
    def get_timezone_gmt(self, dt=None, width='long'):
        """Return the timezone associated with the given ``dt`` datetime object, formatted as string indicating the offset from GMT

        >>> dt = datetime.datetime(2007, 4, 1, 15, 30)
        >>> Locale('en').get_timezone_gmt(dt)
        u'GMT+00:00'

        In:
          - ``datetime`` -- a ``datetime`` object; if ``None``, the current
            date and time in UTC is used
          - ``width`` -- either 'long' or 'short'

        Return:
          - The timezone
        """
        return dates.get_timezone_gmt(dt, width, self)
Ejemplo n.º 9
0
def name(tz, dt=None):
    if not dt:
        dt = datetime.now()
    return "{} {}".format(
        get_timezone_gmt(get_timezone(tz).localize(dt), width='iso8601_short'),
        tz)