From 4be51959042405c809024ee76fc8f4650cdffbdd Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 12 Dec 2024 14:58:13 -0800 Subject: [PATCH] Fixes for datetime UTC warnings in Python Refs: - https://github.com/simonw/llm/issues/672 --- python/utc-warning-fix.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 python/utc-warning-fix.md diff --git a/python/utc-warning-fix.md b/python/utc-warning-fix.md new file mode 100644 index 000000000..67a039623 --- /dev/null +++ b/python/utc-warning-fix.md @@ -0,0 +1,35 @@ +# Fixes for datetime UTC warnings in Python + +I was getting the following warning for one of my Python test suites: + +> `DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).` + +I also saw a deprecation warning elsewhere for my usage of `datetime.datetime.utcfromtimestamp(iso_timestamp_string)`. + +## datetime.datetime.now(datetime.timezone.utc) + +I tried switching to `datetime.UTC` for the first warning, but that caused my tests to fail on versions of Python prior to 3.11. I support all versions of Python that have not yet hit EOL for support, which is currently [Python 3.9 and higher](https://devguide.python.org/versions/). + +According to [the documentation](https://docs.python.org/3/library/datetime.html#datetime.UTC) `datetime.UTC` was added in Python 3.11 as an alias for `datetime.timezone.utc` - so I switched my usage of `datetime.utcnow()` to the following instead: + +```python +import datetime + +utcnow = datetime.datetime.now(datetime.timezone.utc) +``` +These tests now passed for Python 3.9+. + +## datetime.datetime.fromtimestamp(s, datetime.timezone.utc) + +I found another hint [in the docs](https://docs.python.org/3/library/datetime.html#datetime.datetime.utcfromtimestamp) for replacing the now deprecated `datetime.datetime.utcfromtimestamp`: + +> **Warning**: Because naive `datetime` objects are treated by many `datetime` methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing a specific timestamp in UTC is by calling `datetime.fromtimestamp(timestamp, tz=timezone.utc)`. + +Again, `timezone.utc` was the right fix here to keep things working in pre-3.11 versions: + +```python +import python + +utc_datetime = datetime.datetime.fromtimestamp(created_string, datetime.timezone.utc) +``` +Here's [the commit](https://github.com/simonw/llm/commit/571f4b2a4da52ad127061b7fa953562f6ba6aeb0) where I fixed both of these warnings for my LLM project.