Hello,
This is by design. Timezones don't make sense for date or times, only for datetimes.
See also https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#naive-and-aware-datetime-objects
Best regards,
--
Aymeric.
On 20 févr. 2012, at 21:29, Anssi Kääriäinen wrote:
> Another question I have meant to ask is if 1.4 is too early to have
> USE_TZ = True as default setting? I am afraid there are still some
> bugs to iron out, some documentation to improve, helper functions to
> add and most of all the timezone handling might confuse new users.
I was thinking about that too.
The main reason for enabling time zone support by default in new projects (through the settings.py template) was to store UTC in the database (on SQLite, MySQL and Oracle; PostgreSQL always does that anyway).
This decision was certainly skewed by my background in enterprise software, where reliable handling of datetimes is a no brainer. But this isn't the most common use case for Django.
Python doesn't make it very easy to deal with time zones, so forcing that concept on developers isn't friendly. The "right way" to do things is impractical, and there isn't that much space for improvement. Besides, the average website doesn't need time zone support, and IRC discussions show that developers don't care.
What do others think?
--
Aymeric.
On 02/20/2012 01:59 PM, Aymeric Augustin wrote:
> The main reason for enabling time zone support by default in new
> projects (through the settings.py template) was to store UTC in the
> database (on SQLite, MySQL and Oracle; PostgreSQL always does that
> anyway).
>
> This decision was certainly skewed by my background in enterprise
> software, where reliable handling of datetimes is a no brainer. But
> this isn't the most common use case for Django.
>
> Python doesn't make it very easy to deal with time zones, so forcing
> that concept on developers isn't friendly. The "right way" to do
> things is impractical, and there isn't that much space for
> improvement.
Can you give more specifics here? Exactly how much harder is it to work
with USE_TZ = True than USE_TZ = False for a new Django developer,
presuming they don't really care about timezones at this point and just
want to do things more or less right so as not to box themselves in a
corner if they want to add real timezone support in the future?
I guess I think there's some value in setting people on the right path
earlier rather than later, but it really depends on exactly how much
harder that path is.
Carl
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk9CugQACgkQ8W4rlRKtE2fr6wCg2/cXMn/bHL/p6Cg5YDzZmPNe
AasAoKWeKEnDnWvcuYZR3EsqRsMlRfnB
=Sjc9
-----END PGP SIGNATURE-----
--Aymeric.--You received this message because you are subscribed to the Google Groups "Django developers" group.To post to this group, send email to django-d...@googlegroups.com.To unsubscribe from this group, send email to django-develop...@googlegroups.com.For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
You weren't handling date objects, just asking datetimes for their date.
As you said, nothing surprising here, but I don't understand how this
is "dangerous":
> DateField(auto_now=True) together with USE_TZ, some user will see
> last_edited either in the future, or in the past.
Timezone-aware that's what I would expect.
Using your above example, if I'm in Sydney saving a model instance,
the date will be 21 Feb. No timezone in it.
What else would you want to express with "auto_now" - if you have two
users in different timezones, whose date is the "authoritative" one?
Cheers,
Danny
-----BEGIN PGP SIGNED MESSAGE-----Hash: SHA1On 02/20/2012 01:59 PM, Aymeric Augustin wrote:The main reason for enabling time zone support by default in newprojects (through the settings.py template) was to store UTC in thedatabase (on SQLite, MySQL and Oracle; PostgreSQL always does thatanyway).This decision was certainly skewed by my background in enterprisesoftware, where reliable handling of datetimes is a no brainer. Butthis isn't the most common use case for Django.Python doesn't make it very easy to deal with time zones, so forcingthat concept on developers isn't friendly. The "right way" to dothings is impractical, and there isn't that much space forimprovement.Can you give more specifics here? Exactly how much harder is it to workwith USE_TZ = True than USE_TZ = False for a new Django developer,presuming they don't really care about timezones at this point and justwant to do things more or less right so as not to box themselves in acorner if they want to add real timezone support in the future?
I guess I think there's some value in setting people on the right pathearlier rather than later, but it really depends on exactly how muchharder that path is.Carl-----BEGIN PGP SIGNATURE-----Version: GnuPG v1.4.10 (GNU/Linux)Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/iEYEARECAAYFAk9CugQACgkQ8W4rlRKtE2fr6wCg2/cXMn/bHL/p6Cg5YDzZmPNeAasAoKWeKEnDnWvcuYZR3EsqRsMlRfnB=Sjc9-----END PGP SIGNATURE-----
I very much agree.
I've been bitten by a few "can't use naive datetimes" errors as I've
been upgrading, but these are easy bugs: they show up when running
tests, and are fixed trivially.
On the other hand, the kind of bugs that *not* having good timezone
support causes are *much* more complicated: email notifications going
out at the wrong time, people's credit cards getting billed early or
late or not at all, etc. And don't get me started on calendaring.
Essentially, without timezones any sort of scheduling functionality is a
rats nest of subtle bugs. Adding timezone support exposes these bugs
early, and that's a *good thing*.
I think of it along the lines of Python 3's new Unicode support: it's a
bit of a pain as you learn the new rules, but once you learn them all
those run-time "cannot decode/encode" errors go away. Ditto timezones.
So yeah, USE_TZ=True in the default project template.
But I do think we should have some help for people transitioning --
perhaps a timezone FAQ would be in order?
Jacob
I agree that a "Timezones FAQ" should mention
DateField(auto_now=True), with a reference from there also back to the
FAQ.
Really the complication are the timezones themselves, not the
mechanism handling them. If USE_TZ is True by default, users without
timezone experience should be advised that "today" can also be a
matter of location.
Cheers,
Danny
There's a logic to either way, but I agree that auto_now for a date
should use the user's timezone to determine "today", not the server's.
It would be very weird for me to add a model instance and be told that
I did that "tomorrow".
Cheers,
Danny
>
> --
> You received this message because you are subscribed to the Google Groups "Django developers" group.
> To post to this group, send email to django-d...@googlegroups.com.
> To unsubscribe from this group, send email to django-develop...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
>
--
Kind regards,
Danny W. Adair
Director
Unfold Limited
New Zealand
Talk: +64 - 9 - 9555 101
Fax: +64 - 9 - 9555 111
Write: danny...@unfold.co.nz
Browse: www.unfold.co.nz
Visit/Post: 253 Paihia Road, RD 2, Kawakawa 0282, New Zealand
"We are what we repeatedly do. Excellence, then, is not an act but a habit."
==============================
Caution
The contents of this email and any attachments contain information
which is CONFIDENTIAL to the recipient. If you are not the intended
recipient, you must not read, use, distribute, copy or retain this
email or its attachments. If you have received this email in error,
please notify us immediately by return email or collect telephone call
and delete this email. Thank you. We do not accept any
responsibility for any changes made to this email or any attachment
after transmission from us.
==============================
Not sure what you mean by "using UTC", I assume you mean "USE_TZ = True".
As per first sentence of
https://docs.djangoproject.com/en/dev/topics/i18n/timezones/
"When support for time zones is enabled, Django stores date and time
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."
> A) If I have a view that simply adds a record of a model with a
> datetime field, and 3 users in different timezones load the view at
> the same time, all 3 records will have the same datetime value
> (assuming, of course, the database was able to write all 3 records
> within the same microsecond)
They will have the same value in the database, yes.
> B) UTC is *the* way to go for almost any application which will have a
> timezone = models.CharField(... setting for each user profile
See above - database will store everything in UTC.
You could use a timezone field like that to store user timezone and
activate it, e.g. through a middleware -
https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#selecting-the-current-time-zone
NB: PostgrSQL already stores UTC internally -
https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#postgresql
"The PostgreSQL backend stores datetimes as timestamp with time zone.
In practice, this means it converts datetimes from the connection's
time zone to UTC on storage, and from UTC to the connection's time
zone on retrieval."
For other backends that store naive datetimes "in UTC" means "assumed
as being UTC".
> C) When each user has a timezone setting, it doesn't affect the
> datetime that's entered into the database, it just gives me the
> ability to display the time to them in their timezone
It doesn't affect the database.
But it's up to you (middleware) to "activate" their timezone in order
to make it the "current" timezone.
If you don't do that, the "default" timezone (TIME_ZONE setting) will
be the current timezone.
Whatever a user enters in their "current" timezone is converted to UTC
for storage - UTC is the only sane "common denominator".
Cheers,
Danny
> But I do think we should have some help for people transitioning --
> perhaps a timezone FAQ would be in order?
Jacob,
That's a good idea. I've created a ticket: https://code.djangoproject.com/ticket/17738
Everyone,
If you think timezones are hard, please post your questions or problems in the comments of that ticket, so I can address them in the FAQ.
Thanks!
--
Aymeric.
I agree, "last edited" as a date with auto_now=True is useless.
Can you think of an example that's not? I haven't been able to yet.
> I really, really think the date should be UTC date. That is what you
> are going to do comparisons against in the DB. In other words, once
> you put the date value into the DB, it will be in time zone UTC in
> there when you compare it against any datetime value!
That goes together with your comment below about the default timezone
always being UTC.
If I'm in Sydney and the server's TIME_ZONE is Helsinki, either choice
will be converted.
The question was if TIME_ZONE is not UTC then whose "today" should it
be? And in that case I just find the user's "today" more logical than
the server's.
But again, this is only a problem for an "auto_now" date which I can't
think of a sensible use case for.
> In addition, I will once more say that if settings.USE_TZ = True, then
> the Python process default time zone should be UTC. When using time
Sorry I more or less just joined the discussion - I assume you mean by
that TIME_ZONE should not be configurable if USE_TZ is True, but
rather be fixed to UTC.
> zone aware datetimes, they are handled as UTC datetimes in the Python
> process. I can't see any point why the time zone for the process
> should be something else? What is gained by that? Isn't using Europe/
> Helsinki for the process timezone downright dangerous due to daylight
> saving times? Am I missing something obvious here?
What is gained by that is that there's a default "activation" in case
you don't provide one of your own.
At least that's what I understood the interna to do (always work with
UTC, convert to localtime for output, default to TIME_ZONE)
If that's not the case, I agree, "working" with a local time is very dangerous.
Nice example at http://pytz.sourceforge.net/#problems-with-localtime
I would personally prefer if timezone was part of auth.User,
activation built-in and always done, and TIME_ZONE always UTC.
Aymeric's response was:
"As long as Django doesn't have a schema alteration API, it's
difficult to change the User model.
There's also the question of how to store timezones in the database.
If we implemented this feature, we'd have to bless a timezone
implementation. pytz is pretty much the standard, and it's highly
recommended, but it's possible to use another implementation of tzinfo
classes if you desire."
which makes sense to me.
Cheers,
Danny
> Some information about how hard this is can be gotten by updating the
> tutorial part I to correctly use timezone aware datetimes.
This is a good point and I will ensure that the tutorial works properly (there's a ticket about that).
However, the tutorial isn't entirely representative. It often uses hardcoded values, which hardly ever happen in real code, for instance:
> >>> p.pub_date = datetime.datetime(2007, 4, 1, 0, 0)
> >>> p.save()
Best regards,
--
Aymeric.
>>> I can't see any point why the time zone for the process
>>> should be something else? What is gained by that? Isn't using Europe/
>>> Helsinki for the process timezone downright dangerous due to daylight
>>> saving times? Am I missing something obvious here?
>>
>> What is gained by that is that there's a default "activation" in case
>> you don't provide one of your own.
>> At least that's what I understood the interna to do (always work with
>> UTC, convert to localtime for output, default to TIME_ZONE)
>> If that's not the case, I agree, "working" with a local time is very dangerous.
>> Nice example athttp://pytz.sourceforge.net/#problems-with-localtime
>
> I am not talking about the default time zone for users. I am talking
> about the default time zone of the Python process. What is stored in
> os.environ['TZ']. As you can have only a single value there (changing
> it in-flight is a no-no due to threading issues) I think the sanest
> choice is to have it as UTC. Sorry for the confusion about this.
os.environ['TZ'] controls the ouptut of functions such as datetime.fromtimestamp().
If you're running with USE_TZ = True, you should use the UTC versions, e.g. datetime.utcfromtimestamp() in your own code. However, you may be relying on third party code that hasn't been adapted for time zone support yet.
For such code to work properly, it's better to keep settings.TIME_ZONE == os.environ['TZ'] == your "main" time zone == what the user sees by default.
Best regards,
--
Aymeric.