How to handle daylight saving time transition in a Python application using pytz?
I've hit a wall trying to I'm working on a personal project and I'm trying to figure out I'm trying to figure out I'm working on a personal project and I'm working on a Python application that logs events with timestamps, and I'm using the `pytz` library to handle timezone conversions. However, I've run into an scenario where events that occur during the transition into or out of daylight saving time (DST) seem to have incorrect timestamps when I retrieve them from the database. For instance, when I store a timestamp for March 12, 2023, at 2:30 AM in New York, it should actually be recorded as EDT after DST kicks in. However, when I retrieve it later, it appears as if the time is still in EST. I've tried using `datetime.astimezone()` to convert the timestamps, but I keep getting results that are off by an hour for those specific dates. Here's a simplified version of the code I'm using: ```python from datetime import datetime import pytz new_york_tz = pytz.timezone('America/New_York') # Storing the event timestamp local_time = new_york_tz.localize(datetime(2023, 3, 12, 2, 30)) # This should be EDT print('Stored Time:', local_time) # Simulate retrieving from the database retrieved_time = local_time.astimezone(new_york_tz) print('Retrieved Time:', retrieved_time) ``` When I run this code, the `Retrieved Time` prints the same as `Stored Time`, which is what I expect initially, but when I check it against UTC or another timezone, it seems off by an hour. I'm not sure how to correctly handle the DST transition for event logging. Any insights or best practices for avoiding this scenario would be greatly appreciated! My development environment is Ubuntu. Thanks in advance! Cheers for any assistance! The stack includes Python and several other technologies. I'd really appreciate any guidance on this. This is happening in both development and production on Windows 10. Any ideas how to fix this? I'm working in a CentOS environment. Could this be a known issue?