Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Fixed Offset Timezones: Use zoneinfo for Accurate DST Handling #24

Open
ShreyTiwari opened this issue Oct 13, 2024 · 2 comments
Open

Comments

@ShreyTiwari
Copy link

Hi there! 👋

I noticed that the codebase uses fixed offset timezones when creating timezone-aware datetime objects. This can lead to issues with daylight saving time (DST) changes, causing inconsistencies and potential software failures. Can we consider a more dynamic approach to handling time zones?

CodeQL Alerts:

Here are the specific instances CodeQL flagged:

  1. Location:
    timestr = datetime.now(timezone(timedelta(hours=-6))).strftime("%Y%m%d-%H%M%S-%f") # US Central time

Explanation

Issue:

  • Fixed offset timezones, such as timezone(timedelta(hours=-6)), do not account for daylight saving time changes. This can lead to inaccurate times during DST transitions, which may cause software errors or inconsistencies.

Potential Solution:

  • Use the zoneinfo module to handle timezone-aware datetime objects, which dynamically adjust for DST changes. This ensures your datetimes reflect accurate local times throughout the year.

Example Solution:

from datetime import datetime
from zoneinfo import ZoneInfo

# Using zoneinfo for US Central Time
timestr = datetime.now(ZoneInfo('America/Chicago')).strftime("%Y%m%d-%H%M%S-%f")

# Correctly accounts for DST transitions
print(f"Dynamic CST with zoneinfo: {timestr}")

Action Required:

  • Replace fixed offset timezone usage with zoneinfo.ZoneInfo to ensure accurate handling of time zones, including daylight saving time adjustments.

References:

Thank you for your efforts in maintaining this project! 🌟

Best,
Shrey

@XzwHan
Copy link
Owner

XzwHan commented Oct 13, 2024

Hi @ShreyTiwari, thank you very much for your detailed explanation and proposed solution! We are currently working on updates to improve the codebase’s practicality and robustness, and we’ll take your suggestions into consideration as part of this process.

@ShreyTiwari
Copy link
Author

You're welcome!

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

No branches or pull requests

2 participants