#35857: django.utils.timesince.timesince incorrectly handles daylight saving
time
-----------------------------------+------------------------------------
Reporter: Frank Sauerburger | Owner: (none)
Type: Bug | Status: new
Component: Utilities | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Comment (by rohan yadav):
hey,
I've implemented several changes to improve handling of timezone-aware
datetimes, particularly around DST transitions. Key improvements include
ensuring naive datetimes are converted to aware ones using make_aware,
normalizing timezone info for both d and now, and adjusting the comparison
logic to account for DST transitions. Could you please review the code and
let me know if there are any further improvements or edge cases that I
might have missed?
Your feedback would be greatly appreciated!
{{{
# If d is naive (has no timezone info), make it aware using the
current time zone
if d.tzinfo is None:
d = make_aware(d) # Convert naive datetime to aware
if now and now.tzinfo is None: # If now is naive, make it aware
now = make_aware(now)
# Compared datetimes must be in the same time zone.
if not now:
now = datetime.datetime.now(d.tzinfo if is_aware(d) else None)
elif is_aware(now) and is_aware(d):
now = now.astimezone(d.tzinfo)
if reversed:
d, now = now, d
if d.tzinfo is not None and now.tzinfo is not None:
delta = now.astimezone(d.tzinfo) - d
else:
delta = now - d
# Normalize times to handle DST transitions
if d.tzinfo is not None:
d = d.tzinfo.normalize(d)
if now.tzinfo is not None:
now = now.tzinfo.normalize(now)
}}}
{{{
remaining_time = (now.astimezone(d.tzinfo) - pivot).total_seconds()
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35857#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/0107019326709ae3-e49b2436-953c-4548-aa44-58de99e102c4-000000%40eu-central-1.amazonses.com.