# pytz has no effect in sending data to MySQL

**URL:** <https://forum.djangoproject.com/t/pytz-has-no-effect-in-sending-data-to-mysql/26322>\
**Category:** Using the ORM\
**Created:** [December 21, 2023, 10:04am UTC](https://forum.djangoproject.com/t/pytz-has-no-effect-in-sending-data-to-mysql/26322 "2023-12-21T10:04:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [December 21, 2023, 10:04am UTC](https://forum.djangoproject.com/t/pytz-has-no-effect-in-sending-data-to-mysql/26322/1 "2023-12-21T10:04:06Z")

</div>

```auto
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 :

```auto
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)`

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [December 21, 2023, 11:20am UTC](https://forum.djangoproject.com/t/pytz-has-no-effect-in-sending-data-to-mysql/26322/2 "2023-12-21T11:20:14Z")

</div>

Hey there!  
This is probably intended. Please review the docs on the DateField/DateTimeField [auto\_now\_add](https://docs.djangoproject.com/en/5.0/ref/models/fields/#django.db.models.DateField.auto_now_add). Specially on the 2nd note.

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [December 21, 2023, 11:49am UTC](https://forum.djangoproject.com/t/pytz-has-no-effect-in-sending-data-to-mysql/26322/3 "2023-12-21T11:49:48Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [December 21, 2023, 11:55am UTC](https://forum.djangoproject.com/t/pytz-has-no-effect-in-sending-data-to-mysql/26322/4 "2023-12-21T11:55:49Z")

</div>

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](https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.timezone.now), because it will always use UTC ignoring the TIME\_ZONE setting.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [December 21, 2023, 11:58am UTC](https://forum.djangoproject.com/t/pytz-has-no-effect-in-sending-data-to-mysql/26322/5 "2023-12-21T11:58:30Z")

</div>

I found that these videos are very helpful on these concept.

[Talk - Benjamin “Zags” Zagorsky: Handling Timezones in Python](https://www.youtube.com/watch?v=XZlPXLsSU2U&pp=ugMICgJwdBABGAHKBQ5weWNvbiB0aW1lem9uZQ%3D%3D)  
[Paul Ganssle - Working with Time Zones: Everything You Wish You Didn’t Need to Know - PyCon 2019](https://www.youtube.com/watch?v=rz3D8VG_2TY&pp=ugMICgJwdBABGAHKBQ5weWNvbiB0aW1lem9uZQ%3D%3D)  
[The Problem with Time & Timezones - Computerphile](https://www.youtube.com/watch?v=-5wpm-gesOY&t=23s&pp=ygUOcHljb24gdGltZXpvbmU%3D)
