Exemplo n.º 1
0
 def test_transition_backward(self):
     tz_names = ['Europe/London']
     before = datetime(2021, 10, 31, 0, 59, 59, 999999, tzinfo=pytz.utc)
     after = datetime(2021, 10, 31, 1, 0, 0, 0, tzinfo=pytz.utc)
     self.assertEqual(with_gmt_offset(tz_names, now=before),
                      [('Europe/London', 'GMT+01:00 Europe/London')])
     self.assertEqual(with_gmt_offset(tz_names, now=after),
                      [('Europe/London', 'GMT+00:00 Europe/London')])
Exemplo n.º 2
0
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('coerce', coerce_to_pytz)
        kwargs.setdefault('empty_value', None)

        if 'choices' in kwargs:
            values, displays = zip(*kwargs['choices'])
        else:
            values = pytz.common_timezones
            displays = None

        choices_display = kwargs.pop('choices_display', None)
        if choices_display == 'WITH_GMT_OFFSET':
            choices = with_gmt_offset(values)
        elif choices_display == 'STANDARD':
            choices = standard(values)
        elif choices_display is None:
            choices = zip(values, displays) if displays else standard(values)
        else:
            raise ValueError(
                "Unrecognized value for kwarg 'choices_display' of '"
                + choices_display + "'"
            )

        kwargs['choices'] = choices
        super(TimeZoneFormField, self).__init__(*args, **kwargs)
Exemplo n.º 3
0
 def test_in_northern_winter(self):
     now = datetime(2020, 1, 15)
     result = with_gmt_offset(self.tz_names, now=now)
     self.assertEqual(result, [
         ('America/Los_Angeles', 'GMT-08:00 America/Los Angeles'),
         ('Canada/Newfoundland', 'GMT-03:30 Canada/Newfoundland'),
         ('America/Santiago', 'GMT-03:00 America/Santiago'),
         ('Europe/London', 'GMT+00:00 Europe/London'),
     ])
Exemplo n.º 4
0
 def test_using_timezone_names(self):
     result = with_gmt_offset(self.tz_names)
     self.assertEqual(result, [
         ('America/Argentina/Buenos_Aires',
          'GMT-03:00 America/Argentina/Buenos Aires'),
         ('Asia/Qatar', 'GMT+03:00 Asia/Qatar'),
         ('Asia/Kolkata', 'GMT+05:30 Asia/Kolkata'),
         ('Asia/Kathmandu', 'GMT+05:45 Asia/Kathmandu'),
     ])
Exemplo n.º 5
0
 def test_in_northern_summer(self):
     now = datetime(2020, 7, 15, tzinfo=pytz.utc)
     result = with_gmt_offset(self.tz_names, now=now)
     self.assertEqual(result, [
         ('America/Los_Angeles', 'GMT-07:00 America/Los Angeles'),
         ('America/Santiago', 'GMT-04:00 America/Santiago'),
         ('Canada/Newfoundland', 'GMT-02:30 Canada/Newfoundland'),
         ('Europe/London', 'GMT+01:00 Europe/London'),
     ])
Exemplo n.º 6
0
    def __init__(self, *args, **kwargs):
        # allow some use of positional args up until the args we customize
        # https://github.com/mfogel/django-timezone-field/issues/42
        # https://github.com/django/django/blob/1.11.11/django/db/models/fields/__init__.py#L145
        if len(args) > 3:
            raise ValueError('Cannot specify max_length by positional arg')
        kwargs.setdefault('max_length', self.default_max_length)

        if 'choices' in kwargs:
            values, displays = zip(*kwargs['choices'])
            # Choices can be specified in two forms: either
            # [<pytz.timezone>, <str>] or [<str>, <str>]
            #
            # The [<pytz.timezone>, <str>] format is the one we actually
            # store the choices in memory because of
            # https://github.com/mfogel/django-timezone-field/issues/24
            #
            # The [<str>, <str>] format is supported because since django
            # can't deconstruct pytz.timezone objects, migration files must
            # use an alternate format. Representing the timezones as strings
            # is the obvious choice.
            if not is_pytz_instance(values[0]):
                values = [pytz.timezone(v) for v in values]
        else:
            values = self.default_tzs
            displays = None

        choices_display = kwargs.pop('choices_display', None)
        if choices_display == 'WITH_GMT_OFFSET':
            choices = with_gmt_offset(values)
        elif choices_display == 'STANDARD':
            choices = standard(values)
        elif choices_display is None:
            choices = zip(values, displays) if displays else standard(values)
        else:
            raise ValueError(
                "Unrecognized value for kwarg 'choices_display' of '" +
                choices_display + "'")

        # 'display_GMT_offset' is deprecated, use 'choices_display' instead
        if kwargs.pop('display_GMT_offset', False):
            choices = add_gmt_offset_to_choices(choices)

        kwargs['choices'] = choices
        super(TimeZoneField, self).__init__(*args, **kwargs)