How do I get dates to display using my settings TIME_ZONE - django

I'm using Django and Python 3.7. I have the following set in my settings file ...
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Chicago'
USE_I18N = True
USE_L10N = True
USE_TZ = True
I would like for all dates to display using the default time zone I have above. However, when I print out fields like
self.created_on.ctime()
in which "created_on" maps to a PostGres DATETIME field, the date is printed out using a UTC timezone. How can I get this to display using the time zone I have?

TIME_ZONE is a session parameter. Find out more here
and here
from datetime import datetime
from pytz import timezone
self.created_on.astimezone(timezone('America/Chicago'))

looks like you can use the django.utils.timezone.localtime
from django.utils.timezone import localtime
localtime(self.created_on).ctime()

Related

Store Datetime field in localtime in Django

I use Django 3 with postgresql. USE_TZ is False and TIME_ZONE is 'Asia/Baku'.
django.utils.timezone.now() correctly return Baku localtime, but all DateTimeFields with auto_now_add save datetime in UTC in database. I want to use localtime everywhere in my app.
edit: I use ubuntu server and timezone is set on Baku

Timezone is UTC infact of changing it in settings.py file

My app is showing the timezone.now() as UTC time when I see it in Heroku scheduler. Also I built a custom manage.py command to test it but it still shows UTC time.
Here is my settings.py
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True
Here is my personalied manage.py command
managemet/commaands/abhiwaqt.py
from django.core.management.base import BaseCommand, CommandError
from django.apps import apps
from Directory.utils import *
class Command(BaseCommand):
help="Refresh models and its fields."
def handle(self,*args, **options):
try:
print(timezone.now())
except:
raise CommandError("Something went wrong")
Infact of setting TIME_ZONE='Asia/Kolkata' I am getting this issue.
It is probably good practice to keep your back end using UTC so everything is uniform and handles daylight savings and you simply handle the changes when displaying to the user in templates or views. If you want timezone support (since it is disabled by default) use the setting USE_TZ = True. Take a look at the docs for more in depth explanation on this topic.enter link description here

Get Timezone to take effect

I am going through the Django tutorial.
I thought the TIME_ZONE in settings.py was of form 'UTC-5', but it isn't.
I replaced it with 'America/Chicago'
However, when I do:
python manage.py shell
from django.utils import timezone
timezone.now()
I get 'UTC'
How do I get the timezone to take effect?
It can be confusing. If you run the code below, you will see that it is set, but it won't output as you expect:
from django.utils import timezone
timezone.get_current_timezone() # Should be 'America/Chicago'
timezone.now() # should show UTC
If you want it to output in the shell with your set timezone, use timezone.localtime()
from django.utils import timezone
timezone.localtime()

Django TIME_ZONE definition ignored by time.tzname

I need use Timezone in a Django app.
I defined in settings.py UTC timezone :
TIME_ZONE = 'UTC'
I put the following code in a view :
import time
print(time.tzname)
But it displays me :
('Paris, Madrid')
Do you know how to define timezone in all the Django app ?
Your settings are fine. time.tzname just tells you what Python thinks the local system time zone is, it has nothing to do with your Django settings.
If you use django.utils.timezone.get_default_timezone_name() you should see UTC as you expect.

Date time and Date format localization with Django Rest Framework

I am creating API with DRF and Django 1.8 using python 2.7.x and Postgres 9.4. API is consumed by front end using AngularJS. I need to do time conversion to different time zones as per user set timezone. I tried creating a middleware as shown below -
import pytz
from django.utils import timezone
from django.conf import settings
class TimezoneMiddleware(object):
def process_request(self, request):
request.session["user_pref_timezone"] = "Europe/Paris"
tzname = request.session.get('user_pref_timezone')
settings.DATE_FORMAT = request.session.get('date')
settings.TIME_FORMAT = request.session.get('time')
if tzname:
timezone.activate(pytz.timezone(tzname))
else:
timezone.deactivate()
And added it to settings.py under MIDDLEWARE_CLASSES as
'core.middleware.timezone.TimezoneMiddleware',
in settings.py I have added ,
USE_TZ = True
import pytz
TIMEZONES = ( (tz, tz) for tz in pytz.common_timezones )
still I can not see my time converted from UTC to specific country timezone. Am I missing something?