Timezone issue in when storing in DB

48 views
Skip to first unread message

Madhusudhan Reddy

unread,
Aug 15, 2023, 10:36:04 PM8/15/23
to django...@googlegroups.com
Hi All,

I am trying to store the date with the timezone. 
for example, my servers are in Mumbai, when my servers are trying to get the time of America/Detroit. it is not able to get proper time

Here is the snippet and details
Screenshot from 2023-08-16 08-00-21.png
def get_created_at(timezone):
local_tz = pytz.timezone(timezone)
print(datetime.utcnow())
created_at = local_tz.localize(datetime.utcnow()).astimezone(tz=pytz.timezone(timezone))
return created_at


--
Best regards,
Madhusudhan


Mike Dewhirst

unread,
Aug 16, 2023, 1:20:08 AM8/16/23
to django...@googlegroups.com
On 16/08/2023 12:35 pm, Madhusudhan Reddy wrote:
Hi All,

I am trying to store the date with the timezone. 
for example, my servers are in Mumbai, when my servers are trying to get the time of America/Detroit. it is not able to get proper time

pytz is deprecated. You should consider using zoneinfo (install tzdata first) instead.

import zoneinfo
from datetime import datetime

tz = "America/Detroit"
rightnow = datetime.now(zoneinfo.ZoneInfo(tz))
datetime_in_tz = "%s-%s-%s %s:%02d:%02d" % (
        rightnow.year,
        rightnow.month,
        rightnow.day,
        rightnow.hour,
        rightnow.minute,
        rightnow.second,
)
print(f'{tz}:  {datetime_in_tz}')






Here is the snippet and details
Screenshot from 2023-08-16 08-00-21.png
def get_created_at(timezone):
local_tz = pytz.timezone(timezone)
print(datetime.utcnow())
created_at = local_tz.localize(datetime.utcnow()).astimezone(tz=pytz.timezone(timezone))
return created_at


--
Best regards,
Madhusudhan


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFwQctLvw9KinV8a%3DVTysi_NSNJfu3rodBoBPCY5Cih_ih-DGQ%40mail.gmail.com.


-- 
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Your
email software can handle signing.
OpenPGP_signature

Ruby

unread,
Aug 16, 2023, 6:15:39 AM8/16/23
to django...@googlegroups.com
You seem to be reinventing the wheel, Django already has timezone support when you set USE_TZ to True, 
Note that your TIME_ZONE would be the default timezone for your project.

With everything set, you can now use .activate() available in `django.utils.timezone` to get the current time in your preferred timezone.

I hope this answers your question.

Best regards

Madhusudhan Reddy

unread,
Aug 16, 2023, 1:46:53 PM8/16/23
to dev.ru...@gmail.com, django...@googlegroups.com
But I am using only fast api for APIs

ivan harold

unread,
Aug 16, 2023, 2:11:05 PM8/16/23
to Django users

Fred Legace

unread,
Aug 17, 2023, 11:15:04 AM8/17/23
to Django users
Would it not make sense to store times in UCT then in a user profile allow the user to choose their local timezone for display?

ivan harold

unread,
Aug 17, 2023, 2:16:48 PM8/17/23
to Django users
it is more logical if the page for the user automatically adapts to the timezone where they are currently in to avoid confusion. 
Reply all
Reply to author
Forward
0 new messages