Created
July 6, 2018 02:33
-
-
Save hwshim0810/ed1ca013a4ca62633d266814ba50070c to your computer and use it in GitHub Desktop.
Restframework custom timestamp field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.utils import timezone | |
from rest_framework import serializers | |
class DateToTimestampField(serializers.Field): | |
def to_representation(self, value): | |
epoch = timezone.datetime(1970, 1, 1) | |
value = timezone.datetime.fromordinal(value.toordinal()) | |
return int((value - epoch).total_seconds()) * 1000 | |
class DatetimeToTimestampField(serializers.Field): | |
def to_representation(self, value): | |
epoch = timezone.datetime(1970, 1, 1) | |
return int((value - epoch).total_seconds()) * 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment