Wednesday, January 6, 2016

Re: Having trouble understanding Timezone

Ryan

I too went through USE_TZ and time zones etc but too long ago to
remember enough to answer your questions with any authority. However
this works for me ...

USE_TZ = True
TIME_ZONE = 'UTC'

At 18:23 (AESummertime) here in Melbourne Australia I just created a new
record in my Postgres 9.3 database. FWIW the Ubuntu 12.04 database
server runs on local time. I'm using a Windows 8.1 client and Django
1.8.8 dev server.

The model save() method fills in a datetime field if it is blank. It
uses my when() utility. See below.

Examining the new record in the database the date/time saved is ...

2016-01-07 18:23:53.710608+11

There is enough information there to represent a UTC time. Simply
subtract 11 hours. Then add whatever for another timezone. I don't know
if the Django framework does that automatically based on the client
system time but I'm not too concerned about that. I only care that we
are are not losing date/time data.

My when() utility was called by save() without argument. It goes like
this ...

def when(days=0):
"""Function to return a non-naive datetime."""
dday = datetime.now(tz=pytz.utc)
if days == 0:
return dday
ttime = datetime.time(dday)
return timezone.make_aware(datetime.combine(
datetime.fromordinal(dday.toordinal() + days), ttime), pytz.utc)

HTH

Mike


On 7/01/2016 2:36 PM, Ryan Causey wrote:
> Hello,
>
> I'm new to Python and Django so please bear with me on this one.
>
> I've been reading the Django docs, googling, and searching this mailing
> list on the proper configuration and usage of Django when USE_TZ = True.
> However, I'm still a bit fuzzy on things and would like clarification on
> the following:
>
> 1. The documentation says that when USE_TZ = True that "Django stores
> datetime information in UTC in the database, uses time-zone-aware
> datetime objects internally, and translates them to the end user’s
> time zone in templates and forms." Does this mean that the TIME_ZONE
> setting in settings.py should be UTC? Or should it be the timezone
> in which the database server is located?
> 2. I am using a postgresql database, and the installation defaulted the
> database's timezone to my local one. In support of Django storing
> all datetime information in UTC in the database, does the database's
> timezone needs to be set to UTC? Or is it correct for the database's
> timezone to be the local one?
> 3. The documentation also says that one should use the UTC timezone in
> the code, and only convert to local time when dealing with end
> users. I take this to mean that in any code I write I should set
> tzinfo = UTC for all datetime objects I create programmatically and
> let Django translate them in forms and views to the end user's
> timezone automatically. Is this correct?
>
> Thank you in advance for the help,
>
> -Ryan Causey
>
> --
> 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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b37291e8-3d99-49a4-8e9a-13a6639ef29b%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/b37291e8-3d99-49a4-8e9a-13a6639ef29b%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/568E1483.1010404%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment