Skip to content

Conversation

@ianthomas23
Copy link
Collaborator

Fixes #1312.

This replaces the functions datetime.utcnow and datetime.utcfromtimestamp which are deprecated in Python 3.12. Following the CPython issue (python/cpython#103857) and related article (https://blog.ganssle.io/articles/2019/11/utcnow.html) they are replaced as follows:

datetime.utcnow() -> datetime.now(tz=timezone.utc) and
datetime.utcfromtimestamp(x) -> datetime.fromtimestamp(x, tz=timezone.utc)

The following example

import fsspec

mem = fsspec.filesystem("memory")
mem.touch("/file")
print(repr(mem.created("/file")))

used to give

datetime.datetime(2023, 7, 21, 10, 38, 3, 924824)

whereas after this PR it gives

datetime.datetime(2023, 7, 21, 10, 38, 3, 924824, tzinfo=datetime.timezone.utc)

so previously datetimes returned by fsspec were timezone-naive whereas now they are timezone aware at UTC. This is more correct than it used to be as such created datetimes are derived from UNIX timestamps which are seconds since the epoch at UTC so it is correct to label them as UTC (as in the article linked above) rather than as timezone naive. But some users/libraries may be making assumptions about the timezone which are no longer true.

It would be possible to change this PR to drop the UTC timezone to make such datetimes timezone-naive, which more closely follows the old behaviour but is less correct in the long term, so I prefer the solution here.

@martindurant martindurant merged commit e64f0a1 into fsspec:master Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

datetime.utcnow() is deprecated

2 participants