Why datetime.now() is not returning correct value

1,136 views
Skip to first unread message

mfarees...@gmail.com

unread,
Mar 25, 2016, 2:24:31 AM3/25/16
to web2py-users
Hi.
I am in Asia/Karachi timezone and am testing my application for US/Pacific timezone. Here is the scenario:
The system time that I've set is,
    2016-03-24 18:02:58
    and the timezone is US/Pacific

When I use the Python interpreter to print the datetime, I get 
>>> print datetime.datetime.now()
2016-03-24 18:02:58.814000
>>> print datetime.datetime.utcnow()
2016-03-25 01:02:58.814000

Which is correct. But when I use my web2py application to print the same I get,
print datetime.datetime.now()       # 2016-03-25 06:02:58.814000
print datetime.datetime.utcnow()  # 2016-03-25 01:02:58.814000

So, the problem is that the two datetime.now() results do not match. For some reason, the application is returning a datetime which is 12 hours ahead of what it should actually return. I'm facing this issue on a Windows machine; but on Linux Mint, I'm getting correct results.
This seems like its a tricky one, who's up for a challenge? : )

Niphlod

unread,
Mar 25, 2016, 5:13:30 AM3/25/16
to web2py-users
how do you run the web2py application ? utc times match perfectly, it's just the timezone that is not matching... environmental variables ?!

mfarees...@gmail.com

unread,
Mar 25, 2016, 5:42:39 AM3/25/16
to web2py-users
I start the web2py server using this command
    python web2py.py
The I go to localhost:8000/app_name
I haven't set any timezone value in the environment variable, so I guess it should be default. However, I have set the system time and timezone from the bottom right clock.

Leonel Câmara

unread,
Mar 25, 2016, 7:09:58 AM3/25/16
to web2py-users
When I use the Python interpreter to print the datetime, I get 
>>> print datetime.datetime.now()
2016-03-24 18:02:58.814000
>>> print datetime.datetime.utcnow()
2016-03-25 01:02:58.814000

How is this correct if your timezone is not UTC?

mfarees...@gmail.com

unread,
Mar 25, 2016, 7:23:28 AM3/25/16
to web2py-users
Pacific Time (US & Canada (UTC -08:00) is the selected timezone. When it's 6:00 PM in LA, its 1:00 AM (of the next day) UTC. There's a 7 hour difference between LA and UTC (since DayLight Saving is active right now). Hence, the results quoted are correct.

Leonel Câmara

unread,
Mar 25, 2016, 7:42:25 AM3/25/16
to web2py-users
But that shows a 9 hour difference.

mfarees...@gmail.com

unread,
Mar 25, 2016, 8:14:52 AM3/25/16
to web2py-users
No, it doesn't. 6:00 PM + 7 hrs = 1:00 AM (of next day).

Leonel Câmara

unread,
Mar 25, 2016, 9:03:49 AM3/25/16
to web2py-users
Nevermind I clearly needed more coffee.

This is weird. Why would calling it in web2py be different. I can't replicate this. Is it possible web2py was launched before you changed the timezone?.

mfarees...@gmail.com

unread,
Mar 25, 2016, 9:10:53 AM3/25/16
to web2py-users
Yes, that's exactly what happened. I did not restart web2py after changing the timezone and I don't want to do that too. I want the correct time dynamically, without having to restart the server.

mfarees...@gmail.com

unread,
Mar 25, 2016, 9:17:04 AM3/25/16
to web2py-users
Here is some more info on this. The datetime module does not return the correct time if the timezone is changed, unless the Python instance is restarted.
 http://bugs.python.org/issue17627

Leonel Câmara

unread,
Mar 25, 2016, 9:56:39 AM3/25/16
to web2py-users
Humm if you don't mind installing python for windows extensions. You could try:

import platform
import datetime
if platform.system() == 'Windows':
    from win32timezone import TimeZoneInfo, TimeZoneDefinition
    localnow = datetime.datetime.now(TimeZoneInfo(TimeZoneDefinition.current()[1]))
else:
    localnow = datetime.datetime.now()
print localnow


Reply all
Reply to author
Forward
0 new messages