[18.0][IMP] queue_job: avoid deprecation warning about datetime.datetime.utcnow() in _odoo_now()#813
Conversation
|
Hi @guewen, |
amh-mw
left a comment
There was a problem hiding this comment.
Given https://www.odoo.com/documentation/18.0/administration/on_premise/source.html#python
Odoo [18] requires Python 3.10 or later to run.
I went through the Python documentation for versions 3.10 through 3.13 and the guidance on replacing utcnow is consistent:
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 the current time in UTC is by calling datetime.now(timezone.utc).
So shouldn't the change be
- dt = datetime.datetime.utcnow()
+ dt = datetime.datetime.now(timezone.utc)
Yes but if I do that, the returned |
Yet |
|
What do you think of this: |
|
Yes, that sounds better. |
8ed15a5 to
22c9da3
Compare
|
I checked that That said, so does |
The current return type of |
queue_job/jobrunner/runner.py
Outdated
| # Need naive datetime | ||
| dt = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) |
There was a problem hiding this comment.
I agree with @sbidoul
We can remove the funciton _datetime_to_epoch and directly return:
| # Need naive datetime | |
| dt = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) | |
| return time.time() |
I tested localy and in fact, _odoo_now() returns a float:
>>> dt = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
>>> (dt - datetime.datetime(1970, 1, 1)).total_seconds()
1757943419.659445
>>> time.time()
1757943426.0511284
>>>
There was a problem hiding this comment.
I agree with @sbidoul
We can remove the funciton
_datetime_to_epochand directly return:I tested localy and in fact,
_odoo_now()returns afloat:>>> dt = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) >>> (dt - datetime.datetime(1970, 1, 1)).total_seconds() 1757943419.659445 >>> time.time() 1757943426.0511284 >>>
As discussed with @acsonefho, I have done the refactor of the function _odoo_now().
22c9da3 to
95ebb0f
Compare
| def _odoo_now(): | ||
| dt = datetime.datetime.utcnow() | ||
| return _datetime_to_epoch(dt) | ||
| return time.time() |
There was a problem hiding this comment.
I'd keep the comment that says it must do the same as the postgres equivalent, though.
|
This PR has the |
datetime.datetime.utcnow() is now deprecated and should be replaced by datetime.datetime.now() (optional TZ parameter). As the original _odoo_now() doesn't contain the Timezone, the parameter datetime.UTC is not added into this improvement
95ebb0f to
26e94fc
Compare
|
/ocabot merge patch |
|
Hey, thanks for contributing! Proceeding to merge this for you. |
|
It looks like something changed on |
|
Congratulations, your PR was merged at 0a2be50. Thanks a lot for contributing to OCA. ❤️ |
datetime.datetime.utcnow()is now deprecated and should be replaced bydatetime.datetime.now()(optional TZ parameter).As the original
_odoo_now()doesn't contain the Timezone, the parameterdatetime.UTCis not added into this improvement to stay anaive datetime.The purpose of
_odoo_now()is to have the current datetime in UTC timezone but stayingnaive datetime.https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow
https://docs.python.org/3/whatsnew/3.12.html#:~:text=datetime%3A%20datetime,gh%2D103857.)
Original warning:
