We store our time values in database as a 13 digit unix epoch at utc. So when I fetch those values to process in python, I need them to be sensitive to timezone.
For example, when I convert a epoch value to datetime, it gives me a naive(timezone insensitive) datetime object. and after that once try to convert it into some other native time format, it messes up the calculations. (Cause my local pc is not in utc timezone)
>>>from datetime import datetime
>>>dt = datetime.utcfromtimestamp(1691868688457/1000)
>>>dt.astimezone(pytz.timezone('America/Chicago'))
the output comes out as:
datetime.datetime(2023, 8, 12, 9, 1, 28, 457000, tzinfo=<DstTzInfo 'America/Chicago' CDT-1 day, 19:00:00 DST>)
where as it should have been:
datetime.datetime(2023, 8, 12, 14, 31, 28, 457000, tzinfo=<DstTzInfo 'America/Chicago' CDT-1 day, 19:00:00 DST>)
is there any way to read epoch value in timezone sensitive way? for now I am just converting it by a crude way like:
dt = datetime(year=dt.year, month=dt.month, day=dt.day,
hour=dt.hour, minute=dt.minute, second=dt.second,
microsecond=dt.microsecond, tzinfo=pytz.utc)