I’ve been struggling with this, and not sure what to do at all. I’ve been avoiding using pytz
and astimezone
as I understand both of these are deprecated. I’m using Django 5.2 and Python 3.13.
I look in my test sqlite database and I’m surprised to not see any timezone offset stored in the field, yet if I attempt to use make_aware
in my model method, I get an error indicating that the field is not naive. I don’t know if it is just my settings file that is making the field “aware” rather than any actual timezone info being stored in the the database field itself.
This is the last iteration of my settings and models files that I have used, and my method to try to convert either the Unix or UTC times into New York time either just passes through the UTC time unmodified, or it does yield a time offset by 5 hours but in the wrong direction, into the next day, not earlier in the same day as you would expect New York to be, relative to UTC.
I believe I also tried some of this just directly in a Python REPL without any Django bits, and the New York time was returned as you would expect to be, offset 5 hours from UTC.
# settings.py
USE_TZ = True
TIME_ZONE = 'UTC'
# models.py
from django.db import models
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
class TestTime(models.Model):
ts_unix = models.PositiveSmallIntegerField(null=True,blank=True)
ts_utc = models.DateTimeField(null=True,blank=True)
ts_ny = models.DateTimeField(null=True,blank=True)
class Meta:
ordering = ['ts_unix']
def __str__(self):
return str(self.ts_unix)
def get_utc(self):
unix = self.ts_unix
utc = datetime.fromtimestamp(unix, tz=timezone.utc)
try:
return utc
except (TypeError, ValueError):
return None
def get_ny(self):
unix = self.ts_unix
zone = ZoneInfo("America/New_York")
ts_utc = datetime.fromtimestamp(unix, tz=timezone.utc)
# ts_ny = ts_utc.astimezone(zone)
# ts_ny = ts_utc.astimezone(ZoneInfo('America/New_York'))
# ts_ny = datetime.fromtimestamp(unix, tz=zone)
ts_ny = ts_utc.replace(tzinfo=ZoneInfo("America/New_York"))
try:
return ts_ny
except (TypeError, ValueError):
return None
def save(self, *args, **kwargs):
self.ts_utc = self.get_utc()
super(TestTime, self).save(*args, **kwargs)
If, for example, I try to get the New York datetime directly from the Unix timestamp, in the Django model method it only returns a UTC datetime.
# models.py
import datetime as dtm
from django.db import models
from zoneinfo import ZoneInfo
class TestTime(models.Model):
ts_unix = models.PositiveIntegerField(null=True, blank=True)
ts_utc = models.DateTimeField(null=True, blank=True)
ts_ny = models.DateTimeField(null=True, blank=True)
...
def get_ny_time(self):
try:
# Convert to New York time using ZoneInfo
ny_zone = ZoneInfo("America/New_York")
return dtm.datetime.fromtimestamp(self.ts_unix, tz=ny_zone)
except Exception as e:
print(f"Error converting to NY time: {e}")
return None
But if I move the same logic into a raw Python script, it returns the correct datetime, so it seems like something in Django is causing the problem:
# script.py
import datetime as dtm
from zoneinfo import ZoneInfo
ts_unix = 1741035602
def get_ny_time():
try:
# Convert to New York time using ZoneInfo
ny_zone = ZoneInfo("America/New_York")
return dtm.datetime.fromtimestamp(ts_unix, tz=ny_zone)
except Exception as e:
print(f"Error converting to NY time: {e}")
return None
print(get_ny_time())
❯ python -V
Python 3.13.1
❯ python script.py
2025-03-03 16:00:02-05:00