Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why datetime.now() and datetime.today() show time in UTC and not local time on my PC?

datetime.now() and datetime.today() return time in UTC on my computer even though the documentation says they should return local time.

Here's the script I ran:

#!/usr/bin/python

import time
import datetime

if __name__ == "__main__":
   print(datetime.datetime.now())
   print(datetime.datetime.today())
   print(datetime.datetime.fromtimestamp(time.time()))

and here's the output:

2017-11-29 22:47:35.339914
2017-11-29 22:47:35.340399
2017-11-29 22:47:35.340399

The output of running date right after it is:

Wed, Nov 29, 2017  3:47:43 PM

Why is my installation returning time in UTC?
What can I do get those functions to return local time?

PS We are in MST, which is UTC-7.

PS 2 I realize there are methods to convert a UTC time to local time, such as those explained in Convert a python UTC datetime to a local datetime using only python standard library?. However, I am trying to understand the cause of the fundamental problem and not looking for a method to patch the problem in my own code.


In response to comment by @jwodder:

The output of executing

print(time.altzone)
print(time.timezone)
print(time.tzname)

is:

-3600
0
('Ame', 'ric')
like image 926
R Sahu Avatar asked Nov 29 '17 22:11

R Sahu


People also ask

Does DateTime now return local time?

The property Now of the DateTime class returns the current date and time of the machine running the code, expressed in the computer's local time. The property UtcNow of the DateTime class returns the current date and time of the machine running the code, expressed in UTC format.

Does DateTime now return UTC time?

Get current time in UTC format using datetime. For that, you can use the datetime module's now() function. In the now() function, you can pass the timezone information, and it returns the current time in that timezone. It returned the timezone aware datetime object, which contains the current time in the UTC timezone.

What timezone does DateTime now return?

Now and Culture/Timezone specific and C#: Making sure DateTime. Now returns a GMT + 1 time which seem relevant, but no pointers as to the cause of "DateTime. Now not corresponding to machine settings".

What does DateTime DateTime NOW () do?

now() function Return the current local date and time, which is defined under datetime module.


1 Answers

I think there is some odd behavior going on with things using the glibc time and timezone libraries on Windows. I started noticing this behavior in both python and emacs recently.

The best thing to do is probably to set TZ to the "ugly" version described in the best answer as that seems to fix issues in python and emacs and works correctly in cygwin as well.

Another workaround I tried is to make sure the TZ environment variable is NOT set. The following illustrates the problem around 10:18 New York time in python. I get similar results using either cygwin or CMD on Windows. Emacs illustrates the same problem using the current-time-string function suggesting it is a glibc issue or at least some library which both python and emacs are using. Interestingly the date command in cygwin gives the correct result with TZ set to "America/New York" but incorrect if TZ is unset.

Summary: some things (e.g., python, emacs) on Windows seem to not accept "America/New_York" for TZ while some things (e.g., cygwin) do accept it. Using something like EST+05EDT,M4.1.0,M10.5.0 for eastern time (or appropriate ugly equivalent) works.

$ echo $TZ
America/New_York

$ python -i
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2018, 5, 14, 15, 38, 6, 174073)
>>> datetime.datetime.now()
datetime.datetime(2018, 5, 14, 15, 38, 57, 141708)
>>> quit()

$ export TZ=

$ python -i
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2018, 5, 14, 10, 38, 41, 102117)
like image 141
oxer Avatar answered Oct 18 '22 18:10

oxer