You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Timezone calculations in app.now() are incorrect #4160
def now(self):
"""Return the current time and date as a datetime."""
from datetime import datetime
return datetime.utcnow().replace(tzinfo=self.timezone)
This is just semantically wrong. This takes the UTC timezone-aware time and replaces the timezone, without actually ensuring it's the same point in time.
So 9 AM UTC becomes 9 AM JTC, even though 9 AM UTC is 6 PM JTC
The "proper" way of doing this is self.timezone.normalize(datetime.utcnow())
This bug breaks things like crontab for celery beat, for example, since the schedules get shifted around. And it's the wrong time too
@rtpg Based on your suggestion, I think this might be the correct implementation:
def now(self):
"""Return the current time and date as a datetime."""
from celery.utils.time import to_utc
from datetime import datetime
now_in_utc = to_utc(datetime.utcnow())
return self.timezone.normalize(now_in_utc)
one detail (I'm not sure how it applies here, but it feels relevant) is whether self.timezone differs from the system timezone.
For example if self.timezone is UTC+1 but the system timezone is UTC+9
If those differs, then datetime.utcnow() will return the current timestamp (probably) by subtracting 9 hours from the now, but then self.timezone.normalize(now_in_utc) will shift it forward by only 1 hour?
Though..... maybe the system timezone is "always right". This might be ramblings based off of imprecise understanding of this
@rtpg I think it is actually the other way round, the system clock counts in UTC (Universal Coordinated Time) and when you set the system timezone, it changes the representation only. Thus UTC is an absolute measure of time, whereas timezones are relative offsets to UTC.
Activity
[-]Timezone calculaitons are incorrect[/-][+]Timezone calculations in app.now() are incorrect[/+]georgepsarakis commentedon Jul 26, 2017
This may be caused by the changes in this Pull Request.
georgepsarakis commentedon Jul 26, 2017
Perhaps also related: #4145
georgepsarakis commentedon Jul 26, 2017
@rtpg Based on your suggestion, I think this might be the correct implementation:
I need to test it some more though.
rtpg commentedon Jul 27, 2017
one detail (I'm not sure how it applies here, but it feels relevant) is whether
self.timezonediffers from the system timezone.For example if self.timezone is UTC+1 but the system timezone is UTC+9
If those differs, then
datetime.utcnow()will return the current timestamp (probably) by subtracting 9 hours from thenow, but thenself.timezone.normalize(now_in_utc)will shift it forward by only 1 hour?Though..... maybe the system timezone is "always right". This might be ramblings based off of imprecise understanding of this
georgepsarakis commentedon Jul 27, 2017
@rtpg I think it is actually the other way round, the system clock counts in UTC (Universal Coordinated Time) and when you set the system timezone, it changes the representation only. Thus UTC is an absolute measure of time, whereas timezones are relative offsets to UTC.