This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: time.time() returns local time instead of UTC
Type: behavior Stage: resolved
Components: Documentation, Library (Lib) Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: belopolsky, cmcqueen1975, docs@python, dwsarber, ezio.melotti, lemburg, maksbotan, pitrou, python-dev, r.david.murray
Priority: normal Keywords: easy, patch

Created on 2011-08-16 07:52 by maksbotan, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
time_doc_fix.patch dwsarber, 2012-03-15 05:47 review
Messages (14)
msg142167 - (view) Author: Maxim Koltsov (maksbotan) Date: 2011-08-16 07:52
Python docs (http://docs.python.org/library/time.html#time.time) say that time.time() function should return UTC timestamp, but actually i get local one:
>>> time.mktime(time.gmtime()), time.time(), time.mktime(time.localtime())
(1313466499.0, 1313480899.384221, 1313480899.0)
As you can see, the result of second statement is equal to result of the third, while it must be equal to result of the first. Checked on 2.7 and 3.1. My OS is Gentoo/Linux, timezone-info is the latest version (2011h).
msg142168 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2011-08-16 08:11
Maxim Koltsov wrote:
> 
> New submission from Maxim Koltsov <kolmax94@gmail.com>:
> 
> Python docs (http://docs.python.org/library/time.html#time.time) say that time.time() function should return UTC timestamp, but actually i get local one:
>>>> time.mktime(time.gmtime()), time.time(), time.mktime(time.localtime())
> (1313466499.0, 1313480899.384221, 1313480899.0)
> As you can see, the result of second statement is equal to result of the third, while it must be equal to result of the first. Checked on 2.7 and 3.1. My OS is Gentoo/Linux, timezone-info is the latest version (2011h).

The description in the docs is a bit misleading.

time.time() returns the local time in number of seconds since the epoch
(1970-01-01 00:00:0O UTC).

UTC refers to the epoch, not the timezone used by time.time().
msg142169 - (view) Author: Maxim Koltsov (maksbotan) Date: 2011-08-16 08:14
Then docs must be fixed. By the way, help(time.time) correctly says about localtime.
msg142171 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-08-16 08:25
Would dropping 'in UTC' from
"""
Return the time as a floating point number expressed in seconds since the epoch, in UTC.
"""
be an acceptable solution?  AFAIK there are no "non-UTC epochs".
msg142172 - (view) Author: Maxim Koltsov (maksbotan) Date: 2011-08-16 08:27
Maybe add some words about local timezone?
msg142176 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-08-16 09:17
"""
Return the local time as a floating point number expressed in seconds since the epoch.
"""
msg142179 - (view) Author: Maxim Koltsov (maksbotan) Date: 2011-08-16 09:37
Seems OK to me.
msg142243 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2011-08-17 00:42
> Return the local time as a floating point number
> expressed in seconds since the epoch.

No.  Seconds since the epoch is neither local nor UTC.  It is just an elapsed number of seconds since an agreed upon time called the "epoch".  

I would say: "Return the current time as a floating point number representing the number of seconds elapsed since the epoch."  Suggestions for a shorter formulation that would not change the meaning are welcome.  Maybe "Return the number of seconds elapsed since the epoch as a floating point number."
msg155862 - (view) Author: Dylan Sarber (dwsarber) Date: 2012-03-15 05:47
I patched this one up quickly. One has been changed using belopolsky's recommendation, which already reads well.
msg155867 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-03-15 07:10
New changeset 1559a82a3529 by R David Murray in branch '2.7':
#12758: removing confusing mention of UTC from time.time description
http://hg.python.org/cpython/rev/1559a82a3529

New changeset 5615d6b91b53 by R David Murray in branch '3.2':
#12758: removing confusing mention of UTC from time.time description
http://hg.python.org/cpython/rev/5615d6b91b53

New changeset f18767bb66ba by R David Murray in branch 'default':
Merge #12758: removing confusing mention of UTC from time.time description
http://hg.python.org/cpython/rev/f18767bb66ba
msg155869 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-03-15 07:12
Thanks, Dylan.
msg180109 - (view) Author: Craig McQueen (cmcqueen1975) Date: 2013-01-16 21:43
Alexander Belopolsky wrote:
> No.  Seconds since the epoch is neither local nor UTC.  It is just
> an elapsed number of seconds since an agreed upon time called the
> "epoch".

This statement just seems wrong. And I have just been confused by the current documentation, hence finding this issue. In what timezone is the "epoch"? It makes a difference. It seems with the current behaviour, the "epoch" is _in the local timezone_. So I reckon the documentation is unclear, because the way I read it, I interpretted it to mean UTC. I think it does need to state "in local time".

However, what I'd really prefer is a new function that returns the seconds since the epoch in UTC.
msg180110 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-01-16 21:52
> It makes a difference. It seems with the current behaviour, the
> "epoch" is _in the local timezone_.

No it isn't. Two different machines:

$ LANG=C date
Wed Jan 16 21:47:03 UTC 2013
$ python -c "import time; print(time.time())"
1358372827.5

$ LANG=C date
Wed Jan 16 22:47:21 CET 2013
$ python -c "import time; print(time.time())"
1358372848.2

time.time() *is* timezone-independent.

Now to your question:

> However, what I'd really prefer is a new function that returns the
> seconds since the epoch in UTC.

>>> epoch = datetime(1970, 1, 1)
>>> (datetime.utcnow() - epoch).total_seconds()
1358372978.448235
>>> time.time()
1358372980.176238
msg180113 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-16 22:09
On linux/posix, the epoch is *defined* to be 1970, 1, 1 in UTC.  Python just uses whatever the OS defines the epoch to be, as far as I know.
History
Date User Action Args
2022-04-11 14:57:20adminsetgithub: 56967
2013-01-16 22:09:15r.david.murraysetmessages: + msg180113
2013-01-16 21:53:00pitrousetnosy: + pitrou
messages: + msg180110
2013-01-16 21:43:44cmcqueen1975setnosy: + cmcqueen1975
messages: + msg180109
2012-03-15 07:12:08r.david.murraysetstatus: open -> closed

type: behavior

nosy: + r.david.murray
messages: + msg155869
resolution: fixed
stage: needs patch -> resolved
2012-03-15 07:10:58python-devsetnosy: + python-dev
messages: + msg155867
2012-03-15 05:47:00dwsarbersetfiles: + time_doc_fix.patch

nosy: + dwsarber
messages: + msg155862

keywords: + patch
2011-08-17 00:42:02belopolskysetmessages: + msg142243
2011-08-16 09:37:17maksbotansetmessages: + msg142179
2011-08-16 09:17:01ezio.melottisetnosy: + belopolsky
messages: + msg142176
2011-08-16 08:27:32maksbotansetmessages: + msg142172
2011-08-16 08:25:53ezio.melottisetversions: + Python 3.2, Python 3.3, - Python 3.1
nosy: + ezio.melotti

messages: + msg142171

stage: needs patch
2011-08-16 08:17:33lemburgsetkeywords: + easy
2011-08-16 08:17:19lemburgsetassignee: docs@python

components: + Documentation, Library (Lib)
nosy: + docs@python
2011-08-16 08:14:52maksbotansetmessages: + msg142169
2011-08-16 08:11:48lemburgsetnosy: + lemburg
messages: + msg142168
2011-08-16 07:52:03maksbotancreate