Skip to content

Conversation

ttak-apphelix
Copy link
Contributor

@ttak-apphelix ttak-apphelix commented Aug 25, 2025

Description

pytz got deprecated in Django 4.0 && has been completely removed in Django 5.0.
Django 4.2 had provided USE_DEPRECATED_PYTZ flag for pytz support which has now been completely removed in Django 5.0 as well.
Django now uses zoneinfo by default and datetime module use this under the hood now instead of pytz

Difference between datetime.timezone and zoneinfo

datetime.timezone and the zoneinfo package are both related to handling time zones in Python, but they serve slightly different purposes and have different use cases.

datetime.timezone

This is part of the standard library in Python.
It provides a simple way to represent a fixed offset from UTC (Coordinated Universal Time).
It doesn't have information about daylight saving time (DST) or historical changes in time zones.
It's suitable for scenarios where you only need to work with a constant offset, and historical changes in time zones are not important.

zoneinfo

The zoneinfo package is introduced in Python 3.9 as part of PEP 615.
It provides a more comprehensive and accurate way to handle time zones by including historical changes, daylight saving time transitions, and more.
It uses the IANA Time Zone Database, which is regularly updated to reflect changes in time zones around the world.
This package is suitable for applications that require precise handling of time zones, especially when dealing with historical dates.

Original Issue link:
#33980

Related PR:
#37148

Since it involves multiple files changes and as suggested in the PR planning to break that PR into multiple small PR.

Smaller PR link:

@ttak-apphelix ttak-apphelix requested review from a team as code owners August 25, 2025 09:30
@ttak-apphelix ttak-apphelix removed request for a team August 25, 2025 09:43
@@ -41,7 +65,7 @@ def get_display_time_zone(time_zone_name):

:param time_zone_name (str): Name of Pytz time zone
"""
time_zone = timezone(time_zone_name)
time_zone = ZoneInfo(time_zone_name)

Choose a reason for hiding this comment

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

Do we need to wrap this in a flag as well ?

@@ -93,7 +93,7 @@ def get_seconds_since_epoch(date_time):
"""
if date_time is None:
return None
epoch = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)

Choose a reason for hiding this comment

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

Can we add a note in PR that utcfromtimestamp is being deprecated and thus we are changing it ?

Only to be done if it's being done across the platform.

Otherwise this can be ignored.

@@ -189,7 +189,7 @@ def get_start_end_date(cadence_type):
start_date = end_date - datetime.timedelta(days=1, minutes=15)
if cadence_type == EmailCadence.WEEKLY:
start_date = start_date - datetime.timedelta(days=6)
return utc.localize(start_date), utc.localize(end_date)
return start_date.replace(tzinfo=get_utc_timezone()), end_date.replace(tzinfo=get_utc_timezone())

Choose a reason for hiding this comment

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

Can we check if output of both these things is same ?

@@ -205,7 +205,7 @@ def get_time_ago(datetime_obj):
"""
Returns time_ago for datetime instance
"""
current_date = utc.localize(datetime.datetime.today())
current_date = datetime.datetime.now(get_utc_timezone())

Choose a reason for hiding this comment

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

Can we check if output for both of these is same ?

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.

2 participants