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: datetime.utcfromtimestamp() shoud have option for create tz aware datetime
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: belopolsky Nosy List: akira, belopolsky, methane, python-dev, r.david.murray
Priority: normal Keywords: patch

Created on 2014-11-04 08:44 by methane, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue22791-utcfromtimestamp-aware.diff akira, 2014-11-09 22:39 show how to get aware utc datetime, make sure the docs do not lie review
Messages (10)
msg230592 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2014-11-04 08:44
In [1]: import datetime

In [2]: datetime.datetime.utcfromtimestamp(0)
Out[2]: datetime.datetime(1970, 1, 1, 0, 0)

In [3]: datetime.datetime.utcfromtimestamp(0).replace(tzinfo=datetime.timezone.utc)
Out[3]: datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)

datetime.utcfromtimestamp() returns naive datetime.
But some methods assumes naive datetime is localtime. (e.g. datetime.timestamp()).
This is big pitfall for many Pythonistas.

We can't change default behavior for backward compatibility.
How about adding `aware` keyword-only option?

>>> datetime.datetime.utcfromtimestamp(0, aware=True)
datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
msg230632 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-11-04 15:51
I don't find the keyword to be superior to the 'replace' spelling, myself.  I think the replace spelling makes it clearer what the intent is.
msg230636 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2014-11-04 16:37
This is not a spelling issue.

When people writing code converting between unixtime and datetime,
they should find `.timestamp()` and `.utcfromtimestamp()`.
But they may not awake about `.replace(tzinfo=datetime.timezone.utc)` is very important.

Since `fromtimestamp` takes `tz` as optional 2nd argument,
I feel adding 2nd `aware` option to `utcfromtimestamp` is good for symmetry.


If `datetime.datetime.utcfromtimestamp(ts).replace(tzinfo=datetime.timezone.utc)`
is clearer than `datetime.datetime.utcfromtimestamp(ts, aware=True)`,
docstring of `utcfromtimestamp` should notice about how `replace()` important:

"""
Note that it returns **naive** (tz=None) datetime.  Since naive datetime is
treated as localtime in most functions, you may have to call `.replace(tzinfo=datetime.datetime.utc)` before using it.
"""
msg230643 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-11-04 17:17
Yes, I agree that a doc note would be appropriate.  timezone is relatively new, originally there was no way to produce aware datetimes from the datetime module without writing your own tzinfo class.  Now that there is, there are may be more than one place in the datetime docs that should be enhanced.
msg230882 - (view) Author: Akira Li (akira) * Date: 2014-11-09 03:42
>>> from datetime import datetime, timezone
  >>> datetime.fromtimestamp(0, timezone.utc)
  datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)

already works and it is documented [1]

[1] https://docs.python.org/3/library/datetime.html#datetime.datetime.fromtimestamp

Or it can be written as:

  >>> epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)
  >>> aware_utc = epoch + timedelta(seconds=posix_timestamp)
msg230883 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-11-09 03:56
I personally wish we could deprecate utcfromtimestamp.  With timezone.utc in stdlib and being a singleton there is no reason to put UTC time in naive datetime instances.
msg230884 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2014-11-09 04:22
akira:

It seems cleaner than utcfromtimestamp().replace().
I think utcfromtimestamp() should have note about it.

"""
Note that it returns **naive** (tz=None) datetime.  Naive datetime is
treated as localtime in most functions.
If you want to create aware datetime, use `.fromtimestamp(ts, tz=timezone.utc)` instead.
"""

And I want to add this note to docstring too, since I read docstring before
document.
msg230919 - (view) Author: Akira Li (akira) * Date: 2014-11-09 22:39
I agree the documentation should nudge towards aware
datetime objects.

I've attached a documentation patch as an example.
msg236849 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2015-02-27 22:02
I like Akira's documentation patch.  Will apply unless hear objections from documentation folks.
msg236974 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-03-01 19:52
New changeset 4a590c66070d by Alexander Belopolsky in branch 'default':
Closes issue #22791: Improved datetime from timestamp methods documentation.
https://hg.python.org/cpython/rev/4a590c66070d
History
Date User Action Args
2022-04-11 14:58:09adminsetgithub: 66980
2015-03-01 19:53:17belopolskysetstatus: open -> closed
resolution: fixed
stage: resolved
2015-03-01 19:52:18python-devsetnosy: + python-dev
messages: + msg236974
2015-02-27 22:02:16belopolskysetassignee: belopolsky
messages: + msg236849
components: + Documentation, - Library (Lib)
2014-11-09 22:39:55akirasetfiles: + issue22791-utcfromtimestamp-aware.diff
keywords: + patch
messages: + msg230919
2014-11-09 04:22:26methanesetmessages: + msg230884
2014-11-09 03:57:00belopolskysetmessages: + msg230883
2014-11-09 03:42:12akirasetnosy: + akira
messages: + msg230882
2014-11-04 17:17:27r.david.murraysetmessages: + msg230643
2014-11-04 16:37:54methanesetmessages: + msg230636
2014-11-04 15:51:41r.david.murraysetnosy: + r.david.murray
messages: + msg230632
2014-11-04 11:08:38pitrousetnosy: + belopolsky
2014-11-04 08:44:40methanecreate