Skip to content

EPOCH apply timezone - #774

Closed
shn7798 wants to merge 1 commit into
celery:masterfrom
shn7798:master
Closed

shn7798 wants to merge 1 commit into
celery:masterfrom
shn7798:master

Conversation

@shn7798

@shn7798 shn7798 commented Jul 27, 2017

Copy link
Copy Markdown

celery task ETA calc with this function, but it only works when timezone is 'UTC'

@georgepsarakis

Copy link
Copy Markdown
Contributor

Before merging this we need to consider the discussion in celery/celery#4160 .

Comment thread kombu/async/timer.py
d = d.replace(tzinfo=default_timezone)
diff = _time() - time()
return max((d - EPOCH).total_seconds() - diff, 0)
epoch = datetime.utcfromtimestamp(0).replace(tzinfo=default_timezone)

@rtpg rtpg Jul 28, 2017 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe that the timezone replacement here is proper. This won't convert the time to the new timezone, but simply replace the internal timezone.

So 9 AM UTC will "become" 9 AM JST.

Likewise, the epoch here might be transformed into "epoch + 9 hours".

what you can go for instead is default_timezone.normalize(datetime.utcfromtimestamp(0)) (you might need to manually add the UTC timezone to the datetime first though)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch make sure the (d - epoch).total_seconds() is calcing in same timezone

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


In [22]: from pytz import UTC

In [23]: from pytz.reference import Eastern

In [24]: from datetime import datetime

In [25]: datetime.utcfromtimestamp(0).replace(tzinfo=Eastern) - datetime.utcfrom
    ...: timestamp(0).replace(tzinfo=UTC)
Out[25]: datetime.timedelta(0, 18000)

replaceing here means that epoch is no longer the the "proper" epoch.

The whole point of storing timezone info in datetimes is to do math without worrying about timezones... so here if you need timezone-aware datetimes, you could just do epoch = datetime.utcfromtimestamp(0).replace(tzinfo=UTC) to get the "timezone-aware" epoch.

@georgepsarakis

Copy link
Copy Markdown
Contributor

@shn7798 can we close this now?

@auvipy auvipy closed this Jan 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants