Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetime.now() in Django application goes bad

I've had some problems with a Django application after I deployed it. I use a Apache + mod-wsgi on a ubuntu server. A while after I reboot the server the time goes foobar, it's wrong by around -10 hours. I made a Django view that looks like:

def servertime():
  return HttpResponse( datetime.now() )

and after I reboot the server and check the url that shows that view it first looks alright. Then at one point it sometimes gives the correct time and sometimes not and later it gives the wrong time always. The server time is corect though.

Any clues? I've googled it without luck.

like image 931
Nixarn Avatar asked May 19 '09 16:05

Nixarn


People also ask

How do I use datetime in Django?

First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value. Now, we can either assign this method to a variable or we can directly use this method wherever we required the datetime value.

What is Delta time Django?

Python timedelta class. The timedelta is a class in datetime module that represents duration. The delta means average of difference and so the duration expresses the difference between two date, datetime or time instances.


5 Answers

Can I see your urls.py as well?

Similar behaviors stumped me once before...

What it turned out to be was the way that my urls.py called the view. Python ran the datetime.now() once and stored that for future calls, never really calling it again. This is why django devs had to implement the ability to pass a function, not a function call, to a model's default value, because it would take the first call of the function and use that until python is restarted.

Your behavior sounds like the first time is correct because its the first time the view was called. It was incorrect at times because it got that same date again. Then it was randomly correct again because your apache probably started another worker process for it, and the craziness probably happens when you get bounced in between which process was handling the request.

like image 54
phillc Avatar answered Sep 30 '22 08:09

phillc


I found that putting wsgi in daemon mode works. Not sure why, but it did. Seems like some of the newly created processes gets the time screwed up.

like image 23
Nixarn Avatar answered Oct 01 '22 08:10

Nixarn


datetime.now() is probably being evaluated once, when your class is instantiated. Try removing the parenthesis so that the function datetime.now is returned and THEN evaluated. I had a similar issue with setting default values for my DateTimeFields and wrote up my solution here.

like image 45
David Avatar answered Oct 02 '22 08:10

David


Maybe the server is evaluating the datetime.now() at server start, try making it lazy through a template or use a variable in your view.

Take a look at this blog post.

like image 30
Xbito Avatar answered Sep 30 '22 08:09

Xbito


Django sets the system time zone based on your settings variable TIME_ZONE. This may lead to all kinds of confusion when running multiple Django instances with different TIME_ZONE settings.

This is what Django does:

os.environ['TZ'] = self.TIME_ZONE

The above answer:

"I found that putting wsgi in daemon mode works"

does not work for me...

I think I'm going with not using django's built in TIME_ZONE anymore.

like image 27
Klaas van Schelven Avatar answered Oct 03 '22 08:10

Klaas van Schelven