import pytz
# Get the current time in IST
ist = pytz.timezone('Asia/Kolkata')
current_time_in_ist = datetime.datetime.now(ist)
save_data = {
"remark": data.get('remark_data'),
"date": current_time_in_ist,
"cust_id": data.get('cust_id'),
}
print data resulted in :
data = {'remark': 'test chat 1\n', 'date': datetime.datetime(2023, 12, 21, 15, 23, 41, 230428, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD>), 'cust_id': '377'}
But current time was 15:23 - but what got inserted was 2023-12-21 09:52:21.926118
model has date = models.DateTimeField(auto_now_add=True)
Hey there!
This is probably intended. Please review the docs on the DateField/DateTimeField auto_now_add. Specially on the 2nd note.
I edited my model to use default=timezone.now
instead but data getting inserted in to the table is still not my timezone’s datetime value - and strangest thing is, this is on my laptop’s localhost.
What value should I give in my settings.py file for TIME_ZONE = ''
for it to use local time ? Server is in the same city as us (Azure VM).
Printing current_time_in_ist
is datetime.datetime(2023, 12, 21, 15, 23, 41, 230428, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD>)
- but when it comes to entering into the database, it’s 5.5 hours behind (UTC).
You probably should take a look on the implications of havings dates saved on the database with localtime.
Normally it’s recommended that you save dates on the database on UTC and only display them on the user localtime. Timezones are complicated, and you’ll save yourself some headaches by saving them on UTC.
About the timezone.now
you probably want to review the documentation on it, because it will always use UTC ignoring the TIME_ZONE setting.