DEV Community

Cover image for How to return DateTime in different time zones from Django Rest Framework
Juan Benitez
Juan Benitez

Posted on • Updated on

How to return DateTime in different time zones from Django Rest Framework

This is how we can return DateTimes with dynamic time zones from DRF.

So, early this week I got a new ticket where I had to create new endpoints for our API, a few endpoints with DRF were not a big deal I said, this was until I realized that the DateTime values in the response from these endpoints needed to be converted from UTC to other time zones.

We always store and return our DateTime values in UTC, and we convert them to the time zone of the user at display time, this can be in the browser using JS. But this time was different, the API needed to return the values in a specific time zone.

We have something like this.


class Venue(models.Model):
    time_zone = models.CharField(max_length=128, default='UTC')
    ...

class Event(models.Model):
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    venue = models.ForeignKey(Venue, on_delete=models.CASCADE)
    ...
Enter fullscreen mode Exit fullscreen mode

Each Event is happening in a Venue, this venue is placed in some city, so we need to store the time zone used for that venue. Since we want to sell tickets or just promote some events we need to return the DateTime values in the venue time zone so we don't confuse the customers and other resellers.

The first solution I thought of was the serializer field DateTimeField, it accepts an attribute called default_timezone, this is a pytz.timezone representing the timezone. If not specified and the USE_TZ setting is enabled, this defaults to the current timezone. If USE_TZ is disabled, then datetime objects will be naive.

class EventSerializer(serializers.ModelSerializer):
    start_time = serializers.DateTimeField(default_timezone=pytz.timezone('America/Bogota'))
    end_time = serializers.DateTimeField(default_timezone=pytz.timezone('America/Bogota'))
    ...
Enter fullscreen mode Exit fullscreen mode

This works, if the start_time in UTC is 2021-09-02T10:00:00 in the resultant JSON will be displayed as 2021-09-02T05:00:00-05:00. But it didn't just work for me because I don't know the time zones beforehand and also they will be different, so I needed to do this at runtime, my solution was to override the to_representation() method from the serializer.

class EventSerializer(serializers.ModelSerializer):
    def to_representation(self, instance):
        self.fields['start_time'] = serializers.DateTimeField(default_timezone=pytz.timezone(instance.venue.time_zone))
        self.fields['end_time'] = serializers.DateTimeField(default_timezone=pytz.timezone(instance.venue.time_zone))
        return super().to_representation(instance)
    ...
Enter fullscreen mode Exit fullscreen mode

to_representation is run for each instance that is going to be returned by the serializer, so basically what we are doing is overriding the start_time and end_time declaration for each instance, this is for specifying that we want to use a DateTimeField and we want to pass it a default_timezone which is going to be taken from the venue that is linked to the instance.

This helped me to do what I needed and I hope that this can help you as well.

You can follow me on Twitter and GitHub to be up to date with all my projects and content.

Top comments (1)

Collapse
 
devanghingu profile image
Devang Hingu

i wondering that why DRF not support default timezone setting from setting at serializer level