I'm trying to create some kind of validation for dates.
I have these kinds of dates for my API request.
"date_from": "2020-11-20T20:11:00Z",
"date_to": "2020-11-20T20:12:50Z"
date.timestamp() = 1605903600.0
datetime.utcnow().timestamp() = 1605896932.580989
Let's say the current time is 2020-11-20 20:13:00, and I'm still able to create some objects by providing the older time (2020-11-20T20:11:00Z) than the current.
I need to check if the date_from is not less than the current time, so I have tried to:
def validate_date_from(self, date):
if date.timestamp() <= datetime.utcnow().timestamp():
raise serializers.ValidationError("Meeting could not start earlier than the current time")
return date
But I'm still able to create the objects via API, so that means the validation method isn't working as it should.
Secondly, I have this code snippet (below) it should check if the reservation is currently happening, so basically it checks between the start and the end date and if the current time is between the start and the end date, we can know that meeting is active, but unfortunately, it's not working as well.
if parse(reservation['date_from']).timestamp() >= datetime.now().timestamp() <= parse(reservation['date_to']).timestamp():
raise serializers.ValidationError("Room is in-use")
parse(reservation['date_from']).timestamp() = 1605903060.0
datetime.now().timestamp() = 1605896745.853667
parse(reservation['date_to']).timestamp() = 1605903170.0