python - datetime.now() returns wrong day in Django application -
my django application (nginx, uwsgi stack) requests current day calling datetime.now():
def get_drinks(request, drink_type): current_day = datetime.today().strftime("%w") current_day_string = datetime.today().strftime("%a")
and show current day on website. bizarre reason, returning friday, instead of thursday (today). server time is:
server:~$ date thu apr 9 18:51:02 pdt 2015
and when run datetime.now() in python shell, thursday well:
>>> import datetime >>> datetime.datetime.today().strftime("%a") 'thursday'
what's issue here?
datetime.datetime.now()
not aware of timezone gets current time in system's local timezone. should set time_zone
america/los_angeles
(pst/pdt) , use_tz
true
in settings.py file date values created within application timezone aware long import django.utils.timezone
.
please see timezone documentation more information.
time_zone = "america/los_angeles" use_tz = true
>>> django.utils import timezone >>> print timezone.now() datetime.datetime(2015, 4, 10, 2, 17, 10, 839067, tzinfo=<utc>) >>> print timezone.localtime(timezone.now()) datetime.datetime(2015, 4, 9, 19, 17, 10, 839067, tzinfo=<dsttzinfo 'america/los_angeles' pdt-1 day, 17:00:00 dst>)