Reputation: 2293
In my Django App I have a view that submit data to the database for my Task Model. One of those fields includes a time. Whenever I submit that data and then read it back out into my app it looks right (ie my index page with a list of all the objects). However, when I looked at the database entries, they're all stored with times 5 hours after what I entered.. What's confusing me is how they might be entered incorrectly and why it's displaying correctly in my index. I checked around where I add it in as follows.
hour = int(request.POST['time'][:2] #request.POST['time'] is formatted HH:MM AM
minute = int(request.POST['time'][3:5]
pmOffset = 0
if 'PM' in request.POST['time']:
pmOffset = 12
print task.date
task.date = datetime.now().replace(hour=hour+pmOffset, minute=minute)
print task.date
#some other fields
print task.date
task.save()
print task.date
If I say enter 07:00 AM into my form, those print statements come out with 7am in them but the Task that's stored has noon in it. Anyone have any idea why this might be happening? I'm completely baffled. If you need any more information just ask.
Upvotes: 0
Views: 111
Reputation: 99620
It is working the way it is designed. By default, MySQL stores the datetime objects in UTC
Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.
And in your django settings, the default timezone is America/Chicago
which is 5 hours behind UTC. Hence the result
Upvotes: 2