I'm using Django and when I retrieve instance (with objects.all()) the models.DateTime are changing so that 7 hours are added.
I'm in the US Pacific time zone and I'm guessing that my DB is defined as UTC I'm using PostgreSQL and I can't figure out where is the setting for time zone.
One other thing I'm also getting this warning:
RuntimeWarning: DateTimeField received a naive datetime while time zone support is active.
Maybe the two are connected?
settings.py:
TIME_ZONE = 'America/Los_Angeles'
# database not use UTC
USE_TZ = False
see this
Related
I want to set Django timezone so that clock should remain consistent with local time in Florida, which is Eastern+Daylight Savings Time (DST). Please suggest the changes to the Django settings timezone such that it is consistent with this time zone; currently the django is set for EST (Eastern Standard Time) rather than EDT (Eastern Daylight Time). It should automatically change when we enter/leave DST.
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Chicago'
I am running my application in amazon ubuntu. What is the best time_zone to use for Florida local time. I am using Django 1.5.
Correct timezone name (from tz database) for Florida is "America/New_York", simply put that into your TIME_ZONE setting instead of "America/Chicago"
Here is a list of time zones along with their string values:
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Setting it to America/New_York will cover most eastern states. Refer to the wikipedia for a list of all possible options.
Running a Django web app and settings.py has the following:
TIME_ZONE = 'UTC'
A model's time field is set to datetime.datetime.utcnow().
When I look at the field in the admin screen, it is correct with the UTC timestamp at +8 hours from my local time.
Yet when I display the time on the client, the time is yet another +8 (for a total +16 hours) from my local time.
When I step through the code, there is no change to the date. When the timestamp is queried from the database, it is +8 offset from the actual UTC.
What am I doing wrong? We are running the development environment on Windows and there has been some warning about results being inaccurate. But I've also pushed this to our Linux box and the results are identical.
In settings.py, if
USE_TZ = True
...then results might not be as expected. I believe there is documentation and I simply missed it. When I removed the setting, the times were being reported as expected.
Provided by #Two-BitAlchemist:
Here is the relevant documentation. In particular, if you do not have USE_TZ=True, it assumes every date you use is in your local time and converts it to UTC
I'm now going through the process of making a django application time zone aware. Initially, the TIME_ZONE setting was set to "America/Los_Angeles", but I've decided that it's probably more standard to make it "UTC".
I discovered some unintended behavior when I render a datetime to a web page in Javascript like so (start_date is a naive datetime that I define earlier in the view):
django view: cal_start_date = time.mktime(start_date.timetuple())
js: startDate = new Date(response.cal_start_date * 1000)
Depending on the TIME_ZONE I set in settings.py, I get a different timestamp for cal_start_date which is understandable: the time.mktime method returns a floating point number representing the number of seconds since the epoch (01/01/1970). However, the number of seconds since the epoch depends on the time zone we are referring to. I believe that time.mktime is automatically taking the TIME_ZONE setting as the one to use for this reference, right?
In general, is it bad practice to change the django TIME_ZONE setting?
Take a look this answer:
time.mktime() assumes that the passed tuple is in local time,
calendar.timegm() assumes it's in GMT/UTC. Depending on the
interpretation the tuple represents a different time, so the functions
return different values (seconds since the epoch are UTC based).
The difference between the values should be equal to the time zone
offset of your local time zone.
I also recomend you ensure that USE_TZ = True in your settings.py
I am developing a Django application that is based around event information. My client is sending all date/time information in UTC and making the conversion to local time. These are then sent to the server as a string. Furthermore, I'm using Postgres as the DB which also stores information in UTC.
Timezone support is active, but I am placing naive datetimes into Django and am receiving warnings. Do I even need time zone support for my application?
If so, how can I resolve the warnings?
USE_TZ = True # to activate time zone setting.
TIME_ZONE = 'America/Chicago' # set your timezone
When you activate time zone setting, DJango shows warning for each time you set Naive date time in your cod. Nothing to panic. You need to change your code to use Aware date time step by step.
https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#concepts
from django.utils.timezone import utc, now
class SomeModel(Model):
schedule = django_models.DateTimeField(default=now)
I would highly encourage that you use pytz module. It makes working with timezones less painful.
I'm trying to deal with timezone information in Django. I tried doing something like:
results = Competitor.objects.raw("SELECT official_start AT TIME ZONE 'UTC', official_finish AT TIME ZONE 'UTC' FROM competitor WHERE race_id=1")
Thinking that this way I would know that the timezone was UTC but say I store a time in the database that is '2010-07-30 15:11:23' in UTC, in Django it will show up as '2010-07-30 10:11:23'. Any idea what is going on?
I realized that in the settings.py file there is an option: TIME_ZONE. setting this to UTC solved the problem.