import time
from rest_framework import serializers


class TimeStampField(serializers.Field):
    def to_internal_value(self, data):
        pass

    def to_representation(self, value):
        return int(time.mktime(value.timetuple()))

import time
from rest_framework import serializers


class TimeStampField(serializers.Field):
    def to_internal_value(self, data):
        pass

    def to_representation(self, value):
        try:
            return int(time.mktime(value.timetuple()))
        except OverflowError:
            return 0

Fix exception on dates older then 1970
Fix exception on dates older then 1970

  import time
  from rest_framework import serializers
  
  
  class TimeStampField(serializers.Field):
      def to_internal_value(self, data):
          pass
  
      def to_representation(self, value):
+         try:
-         return int(time.mktime(value.timetuple()))
+             return int(time.mktime(value.timetuple()))
+         except OverflowError:
+             return 0
  
Fix exception on dates older then 1970
## Code Before:
import time
from rest_framework import serializers


class TimeStampField(serializers.Field):
    def to_internal_value(self, data):
        pass

    def to_representation(self, value):
        return int(time.mktime(value.timetuple()))

## Instruction:
Fix exception on dates older then 1970
## Code After:
import time
from rest_framework import serializers


class TimeStampField(serializers.Field):
    def to_internal_value(self, data):
        pass

    def to_representation(self, value):
        try:
            return int(time.mktime(value.timetuple()))
        except OverflowError:
            return 0
