I store a datetime in database (SQLite). I save python datetime
objects with UTC timezone (so they are aware) in my models. When I check the database I literally see 2018-02-28 00:00:00
.
When I fetch data from database I see:
>>> Price.objects.last().datetime.timetuple()
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=59, tm_isdst=0)
So far so good (time is really a midnight). But once I try to divide UNIX timestamp I get wrong result:
>>> time.mktime(Price.objects.last().datetime.timetuple()) / 3600 / 24
17589.958333333332
When I adjust the timestamp with +1 hour it's finally right.
>>> (time.mktime(Price.objects.last().datetime.timetuple()) + 3600) / 3600 / 24
17590.0
What do I do wrong? Why struct_time
shows hours=0 and minutes=0 but once I call time.mktime
it gets wrong?