0

I'm trying to get add a datetime to my form error message. Unfortunately it is rendered in UTC (my TIME_ZONE), but should be in the current timezone.

def clean(self):
    # print(get_current_timezone()) > Europe/Berlin
    cleaned_data = super(LegForm, self).clean()
    departure_scheduled = cleaned_data.get('departure_scheduled')
    # check departure in journey
    if departure_scheduled < self.instance.journey.start:
        journey_start = localize(self.instance.journey.start)
        # print(self.instance.journey.start.tzinfo) > UTC
        self.add_error(
            'departure_scheduled',
            _('Error Message (%(start)s).') % {'start': journey_start}
        )

print(get_current_timezone()) returns Europe/Berlin

print(self.instance.journey.start.tzinfo) returns UTC

The current timezone is activated. Is there any equal to localize() to convert the datetime object to the current timezone?

T. Christiansen
  • 1,036
  • 2
  • 19
  • 34

1 Answers1

1

I could use:

journey_start = localize(self.instance.journey.start.astimezone(get_current_timezone()))

But it feels like fighting against django's timezone support.

UPDATE:

Thank's to Kevin Christopher Henry comment, here's the django tool I was looking for:

from django.utils import timezone
journey_start = timezone.localtime(self.instance.journey.start)

https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#usage

T. Christiansen
  • 1,036
  • 2
  • 19
  • 34