menu

Questions & Answers

datetime.datetime.now() is five minutes off

I'm currently developing a django backend. I use auto_now_add=True in my model to populate a start point on create(). I use datetime.datetime.now() to add an endpoint on update(). The entire code for that is datetime.datetime.now().replace(tzinfo=pytz.timezone(settings.TIME_ZONE).

Here's an example for an instance that was created at 2023-01-21T19:26:04.561888Z and updated only seconds later.

enter image description here

As you can see, the endpoint populated on update() is somehow before the startpoint, which is not intended behavior.

Thank you

Comments:
2023-01-22 00:55:05
Some additional information: When printing, the result looks like this 2023-01-21 20:40:57.840847+01:05. I have absolutely no idea why the timezone is set as +01:05
2023-01-22 00:55:05
The comments here might be interesting. The conclusion there (and in other answers) seems to be to avoid pytz if you can.
2023-01-22 00:55:05
What is your settings.TIME_ZONE value?
2023-01-22 00:55:05
It's Europe/Vienna
Answers(0) :