Reputation: 617
After reading many doc, I am confused by UTC, UTC timezone and how it's used in Django.
Is UTC format and UTC timezone same thing? Can UTC format store Pacific timezone date+time?
I have a django project with database and UI.
class FunModel(models.Model):
fun_time = models.DateTimeField()
What I am expecting:
1) Page 1. click a button in a browser in Pacific timezone. Server to create/store (on server side, not client to create time) 'fun_time' as Pacific time zone date + time (not UTC zone, but format is UTC format)
2) Page 2. When page 2 is loaded. it retrieves time as Pacific date + time (Not UTC zone)
3) In database, I expect to see time stored is Pacific date+time.
4) As far as my understanding, FunModel class will store time as Pacific time with UTC format. The timezone is configured in settings.
----Settings.py
# Tried 'US/Pacific' as well. No difference. Date+time stored in db is the same. Really confused!
TIME_ZONE = 'UTC'
# Only for retrieving date from db to be Pacific by calling active(USER_TIME_ZONE)?
USER_TIME_ZONE = 'US/Pacific'
# With above 2 settings, date stored/retrieved will be Pacific zone automatically. Not really!
USE_I18N = True
USE_L10N = True
USE_TZ = True
My expectation is database will store time as US/Pacific timezone, and the time format is UTC (always UTC format right?)
----Page1_Save.py
from django.utils import timezone
timezone.activate(settings.USER_TIME_ZONE)
time_param = timezone.localtime(timezone.now())
fun_time_obj = FunModel(fun_time=time_param)
fun_time_obj.save()
# time_param = 2019-04-16 01:00:00
# database: 2019-04-16 01:00:00 + 7 hours = 2019-04-16 08:00:00 (Not correct!)
I am expecting database: 2019-04-16 01:00:00.
I also tried,
time_param = timezone.now()
Same result:
# time_param = 2019-04-16 01:00:00
# database: 2019-04-16 01:00:00 + 7 hours = 2019-04-16 08:00:00 (Not correct!)
Where I did wrong?
---Page2_Load.py (Use Q to compare date stored in db)
# Browser in Pacific timezone without timezone passed to server. Just time picked from Javascript.
ui_date = '2019-04-16 01:00:00'
I want to use Q function to compare:
Q(fun_time__exact=ui_date)
A) If database time is Pacific time, comparison is valid.
B) If database time is UTC time, the comparison is not valid. Now, I can't compare the date stored in db!!! How to fix it?
C) I don't want date+time in database to be UTC timezone!!!
Upvotes: 0
Views: 411
Reputation: 617
I got it to work, but I am more confused by Django time zone settings.
Server computer system clock is UTC timezone.
---settings.py
# Totally useless
#TIME_ZONE = 'UTC' # comment out, and server runs well.
# I suspect this is useless as well.
USER_TIME_ZONE = 'US/Pacific'
USE_I18N = True
USE_L10N = True
USE_TZ = False # Make server timezone not aware.
---page1_save.py
from datetime import datetime
import pytz
utc = pytz.utc.localize(datetime.utcnow()) # Generate time from server, which is UTC timezone
instance_time_zone = pytz.timezone('US/Pacific')
time_param = utc.astimezone(instance_time_zone)
time_param = time_param.replace(tzinfo=None) # USE_TZ = False. Make it without timezone.
fun_time_obj = FunModel(fun_time=time_param)
fun_time_obj.save()
Now, time in db is Pacific time, which is what I expect.
TIME_ZONE, USER_TIME_ZONE are useless in this case, when USE_TZ = False. Also, with USE_TZ = True, TIME_ZONE = 'What ever' seems also useless, as I tried TIME_ZONE = 'US/Pacific', USER_TIME_ZONE = 'US/Pacific', and can't get db to store pacific time.
Upvotes: 0