You seem to be reinventing the wheel, Django already has timezone support
when you set USE_TZ
<https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-USE_TZ> to
True,
Note that your TIME_ZONE
<https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-TIME_ZONE>
would
be the default timezone for your project.

With everything set, you can now use .activate()
<https://docs.djangoproject.com/en/4.2/ref/utils/#django.utils.timezone.activate>
available
in `django.utils.timezone` to get the current time in your preferred
timezone.

I hope this answers your question.

Best regards
Reuben Sunday <https://reubensunday.com/>

On Wed, Aug 16, 2023 at 3:35 AM Madhusudhan Reddy <msreddy.g...@gmail.com>
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
>
> Here is the snippet and details
> [image: 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
> +91 90007 79457
> Gmail <msreddy.g...@gmail.com> | LinkedIn
> <https://www.linkedin.com/in/msreddygone/>
>
>
> --
> 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+unsubscr...@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
> <https://groups.google.com/d/msgid/django-users/CAFwQctLvw9KinV8a%3DVTysi_NSNJfu3rodBoBPCY5Cih_ih-DGQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPUD46vztpvyb8P21vnBf9Wt2d2r6NQ-hgPDD3kyCc%2BpZu3siA%40mail.gmail.com.

Reply via email to