Uh oh!
There was an error while loading. Please reload this page.
[18.0][IMP] queue_job: avoid deprecation warning about datetime.datetime.utcnow() in _odoo_now() - #813
Conversation
OCA-git-bot
commented
Aug 12, 2025
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)acsonefho
commented
Aug 12, 2025
Yes but if I do that, the returned |
sbidoul
commented
Aug 14, 2025
Yet |
acsonefho
commented
Aug 14, 2025
What do you think of this: |
sbidoul
commented
Aug 14, 2025
Yes, that sounds better. |
8ed15a5 to
22c9da3CompareI checked that That said, so does |
acsonefho
commented
Aug 18, 2025
The current return type of |
| # 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) | |
| returntime.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
95ebb0fCompare| 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.
OCA-git-bot
commented
Sep 17, 2025
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
26e94fcCompareguewen
commented
Sep 18, 2025
/ocabot merge patch |
OCA-git-bot
commented
Sep 18, 2025
Hey, thanks for contributing! Proceeding to merge this for you. |
OCA-git-bot
commented
Sep 18, 2025
It looks like something changed on |
Uh oh!
There was an error while loading. Please reload this page.
OCA-git-bot
commented
Sep 18, 2025
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:
