Repository navigation
Conversation
time.timezone/time.altzone are seconds west of UTC. utcoffset() converted them to whole hours with `// 3600`, which floor-divides: for a fractional-hour, east-of-UTC zone (India/Sri Lanka/Iran at UTC+5:30, seconds = -19800), this rounds the magnitude up (-19800 // 3600 == -6) instead of truncating to the correct -5. West-of-UTC fractional offsets are unaffected, since floor and truncation agree for positive dividends -- this is a sign asymmetry, not a general precision limitation. utcoffset() feeds events/dispatcher.py (attached to every worker event) and events/receiver.py's adjust_timestamp() (cross-timezone event timestamp correction), so a worker in one of these locales skews its reported offset by a full hour. Different bug from LocalTimezone.fromutc() (celery#10517/celery#10519), which lost the sign entirely via timedelta.seconds; this one only mis-rounds the magnitude for fractional hours via `//`. LocalTimezone.__repr__ a few lines above already truncates the equivalent seconds-to-hours conversion -- this matches that existing pattern instead of floor-dividing. Added regression tests pinning both the fractional-hour case and confirming whole-hour zones are unaffected.
|
@MehrazRumman you can review this |
|
OK @auvipy ! |
MehrazRumman
left a comment
There was a problem hiding this comment.
The sign-asymmetry analysis is right, but I don't think truncation is the correct target — it's a lateral move for :30 zones and a regression for :45 ones.
time.timezone / 3600 for UTC+5:30 is exactly -5.5. Floor gives -6, truncation gives -5; both are 30 minutes wrong, just in opposite directions. So calling -5 "correct" isn't quite right — neither value is. And for the quarter-hour zones truncation is strictly worse:
| zone | time.timezone |
true | floor (main) | trunc (PR) | floor err | PR err |
|---|---|---|---|---|---|---|
| India +5:30 | -19800 | -5.50 | -6 | -5 | 30m | 30m |
| Iran +3:30 | -12600 | -3.50 | -4 | -3 | 30m | 30m |
| Nepal +5:45 | -20700 | -5.75 | -6 | -5 | 15m | 45m |
| Chatham +12:45 | -45900 | -12.75 | -13 | -12 | 15m | 45m |
| Eucla +8:45 | -31500 | -8.75 | -9 | -8 | 15m | 45m |
Nepal, Chatham and Eucla get 30 minutes worse under this patch, and no listed zone gets better.
The rounding itself is what's unnecessary. utcoffset() is already annotated -> float, and its only consumer is
def adjust_timestamp(ts, offset, here=utcoffset):
return ts - (offset - here()) * 3600which is pure arithmetic. I grepped the tree: the value is placed in the event dict in events/dispatcher.py and read back through itemgetter in events/receiver.py, and nothing formats it as an int. So just dropping the rounding:
if localtime().tm_isdst:
return time.altzone / 3600
return time.timezone / 3600makes adjust_timestamp() exact for every zone, whole-hour and fractional alike, instead of trading one 30-minute error for another. Whole-hour zones are unaffected (18000 / 3600 == 5.0). adjust_timestamp's offset: int annotation would want widening to float, and a mixed old/new fleet still computes correctly since both sides are just doing arithmetic.
One note on the justification: LocalTimezone.__repr__ truncates because it formats with :+03d, which requires an int for display. That's not really a precedent for the arithmetic path.
As written, test_utcoffset_fractional_hour_offset pins -5 for India, which would lock in the 30-minute error and make the exact fix a test change later. Worth retargeting the test to -5.5 and covering Nepal too, since :45 is where the two roundings actually diverge.
Review on the previous commit (truncate instead of floor) showed truncation is not actually a fix: for a :30 zone like India it is 30 minutes off, same as floor, just in the other direction, and for a :45 zone (Nepal +5:45, Chatham/Eucla +8:45) truncation is 45 minutes off versus floor's 15 -- strictly worse, not better. The rounding itself was never needed. utcoffset()'s only consumer, adjust_timestamp(), does plain float arithmetic, and the value's only other use is passed through the event dict in events/dispatcher.py and read back via itemgetter in events/receiver.py -- nothing requires an int. Returning time.timezone/altzone / 3600 directly (no floor, no truncation) is exact for every zone, fractional or whole-hour alike. adjust_timestamp()'s offset parameter is widened from int to float to match, since it now receives fractional values. Tests updated: the India case now asserts the exact -5.5 instead of pinning the previously-"fixed" but still-wrong -5, and a new test covers a :45 zone (Nepal, -5.75), which is where floor and truncation actually diverge in how wrong they are.
|
thanks you're right that truncation just trades one wrong direction for another, and is worse for the Updated |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10625 +/- ##
=======================================
Coverage 89.13% 89.14%
=======================================
Files 153 153
Lines 20284 20284
Branches 2403 2403
=======================================
+ Hits 18080 18082 +2
+ Misses 1898 1897 -1
+ Partials 306 305 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
@MehrazRumman the fix from your review is in (8bdb29b) and CI is green across the board now. Let me know if there's anything else you'd like changed. |
GangEunzzang
left a comment
There was a problem hiding this comment.
@bodapatisaikrishna @auvipy I tried this out locally — it's a real bug, and this fix looks right to me.
One small correction to the description: west-of-UTC fractional zones aren't really fine on main either. Floor and truncation happen to agree there, but the result is still not exact:
| zone | main |
here |
|---|---|---|
| UTC+5:30 India | 30 min off | exact |
| UTC+5:45 Nepal | 15 min off | exact |
| UTC-3:30 Newfoundland | 30 min off | exact |
| UTC-9:30 Marquesas | 30 min off | exact |
That's the offset a UTC receiver ends up applying to timestamp through adjust_timestamp.
One other thing I noticed: whole-hour zones now send 5.0 instead of 5. It doesn't affect the arithmetic, but it is visible to anything else consuming the event stream.
Nothing blocking from me. LGTM!
|
Good catch, thanks you're right that the description overstated things. West-of-UTC fractional zones were never actually exact on On the |
|
Looks good to me too, nothing to add, I could find no impact on changes on the crontab remaining_estimate conducted in parallel. |
Description
celery.utils.time.utcoffset()computes the local UTC offset in whole hoursfrom
time.timezone/time.altzone(seconds west of UTC) using floordivision (
// 3600). For any fractional-hour, east-of-UTC timezone(India, Sri Lanka, Iran, Afghanistan, Myanmar, Nepal, parts of Australia, all
at a 30- or 45-minute offset), this rounds the magnitude up instead of
truncating:
That's a full-hour error for east-of-UTC zones. West-of-UTC fractional zones
don't hit this specific floor-vs-truncation asymmetry (floor and truncation
agree for positive dividends), but they're not correct on
maineither —they're still truncated to a whole hour, which is 15-30 minutes off from the
real offset.
mainis only exact for whole-hour zones.This PR fixes both: instead of rounding to a whole hour at all,
utcoffset()now returns the exact fractional offset via true division:
utcoffset()feedscelery/events/dispatcher.py(attached to every workerevent) and
celery/events/receiver.py'sadjust_timestamp()(used tonormalize event timestamps across workers in different timezones), and
adjust_timestamp()does pure arithmetic on this value, so keeping it exactrather than rounded avoids skewing cross-timezone timestamp correction for
any fractional-hour zone, east or west.
Whole-hour zones now get a
floatback (e.g.5.0instead of5) ratherthan an
int. This doesn't change the arithmetic anywhereutcoffset()isconsumed, and the function was already annotated
-> float, so this bringsthe implementation in line with the type it already claimed to return.
This is a different function from
LocalTimezone.fromutc()(fixed in #10519for a related but distinct negative-offset bug; that one lost the sign
entirely via
timedelta.seconds, this one mis-rounds the magnitude forfractional hours via
//).LocalTimezone.__repr__a few lines above thefunction I'm fixing already reports the equivalent seconds-to-hours
conversion without floor-dividing (
int(x.total_seconds() / 3600)truncatestoward zero rather than flooring); this fix goes one step further for
utcoffset()and skips rounding entirely.Testing
Added
test_utcoffset_whole_hour_unaffected(confirms no behavior change forordinary whole-hour zones, aside from the
int→floattype), plustest_utcoffset_fractional_hour_offsetandtest_utcoffset_quarter_hour_offset(regression tests for 30- and 45-minuteoffsets, covering both STD and DST branches) to
t/unit/utils/test_time.py.Confirmed the new fractional-hour tests fail on current
mainand pass withthis fix.
flake8andisort --check-onlyclean on both changed files.