arrow-py / arrow

🏹 Better dates & times for Python
https://arrow.readthedocs.io
Apache License 2.0
8.73k stars 684 forks source link

#1178: changes to address datetime.utcnow deprecation warning #1182

Closed csessh closed 3 months ago

csessh commented 3 months ago

Pull Request Checklist

Thank you for taking the time to improve Arrow! Before submitting your pull request, please check all appropriate boxes:

If you have any questions about your code changes or any of the points above, please submit your questions along with the pull request and we will try our best to help!

Description of Changes

Python has raised deprecation warning for the use of datetime.datetime.utcnow(). The suggestion is to use datetime.datetime.now(datetime.UTC) or similar.

This PR addresses just by swapping out all datetime utcnow() calls. I don't think there's anything in documentation to be updated.

With the example raised in #1178 with humanize():

Python 3.12:

Python 3.12.4 (main, Jun  6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> time = arrow.utcnow()
>>> time.humanize()
'just now'
>>> 

No more deprecation warning.

This also works with other python versions, like python 3.11:

Python 3.11.9 (main, Apr  2 2024, 08:25:04) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> time = arrow.utcnow()
>>> time.humanize()
'just now'
>>> time
<Arrow [2024-08-07T02:20:54.376543+00:00]>
>>> 

One thing to note as I setup virtualenv for this: Despite being listed in requirements.txt, python-dateutil wasn't installed with make build. I had to manually install it.

-> % pip install python-dateutil
Looking in indexes: https://tdo:****@artifactory.sie.sony.com/artifactory/api/pypi/cgei-pypi-release-virtual/simple
Collecting python-dateutil
  Using cached https://artifactory.sie.sony.com/artifactory/api/pypi/cgei-pypi-release-virtual/packages/packages/ec/57/56b9bcc3c9c6a792fcbaf139543cee77261f3651ca9da0c93f5c1221264b/python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)
Collecting six>=1.5 (from python-dateutil)
  Using cached https://artifactory.sie.sony.com/artifactory/api/pypi/cgei-pypi-release-virtual/packages/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, python-dateutil
Successfully installed python-dateutil-2.9.0.post0 six-1.16.0

But that's a different thing to this PR.

Closes https://github.com/arrow-py/arrow/issues/1178

csessh commented 3 months ago

@jadchaar Please review this PR