Repository navigation
Fix task ETA issues when timezone is defined in configuration - #3867
Conversation
…#3867) * Fix wrong task ETA when using timezone setting * Optimization: reduce polling interval in test cases * Test cases for App.now, App.uses_utc_timezone * Fix PEP8 and pydocstyle issues
| """Return the current time and date as a datetime.""" | ||
| return self.loader.now(utc=self.conf.enable_utc) | ||
| from datetime import datetime | ||
| return datetime.utcnow().replace(tzinfo=self.timezone) |
There was a problem hiding this comment.
I think this is wrong. This will not return the current time in self.timezone. Let's say for example that self.timezone is +8 and it is currently 12:00 UTC. This method will return 12:00 +8, which is the same point in time as 04:00 UTC – not 12:00 UTC.
Note that this current implementation might even return nonsensical times, like 2017-03-25 02:30 for a Europe/Berlin timezone – a time that doesn't exist.
The correct way to get the current time in a local timezone is datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(self.timezone). See the same functionality in django and the pytz docs.
But you can't just make a function that used to return utc times return times from timezones that observe DST. If the caller code is not aware of that, things might break. Example: You return 2017-03-24 03:30 Europe/Berlin. The caller adds 24h to this and then checks if the time is before 04:00. Adding 24h crosses a DST boundary, so the above +24h is actually 2017-03-25 04:30. But if you add 24h to a 2017-03-24 03:30 Europe/Berlin datetime object, you get 2017-03-25 03:30 Europe/Berlin [NON-DST]. You need to call timezone.normalize on that to turn that into a sensible datetime object – see the pytz docs.
Hope this helps.
There was a problem hiding this comment.
@Chronial would you mind sending a pr with your suggestions?
There was a problem hiding this comment.
Providing a correct implementation using timezones is not easy. Since I moved away from using celery (its broken timzone handling beeing one of the reasons ^^), I unfortunately do not have the time to provide a full PR for this.
Just wanted to report this bug, since I stumbled upon it while browsing.
There was a problem hiding this comment.
@Chronial you are right, this has already been corrected by other contributors in master/latest release.
There was a problem hiding this comment.
Ah, sorry for digging up this old PR. I came here via the mention on #4006 and didn't realize how old this PR was.
There was a problem hiding this comment.
No problem @Chronial thanks for the feedback.
Description
App.nowto correctly return the timezone-aware current date/time, according to the configured timezone or UTC.Fixes #3753.