Django

Code

Ticket #479 (closed: fixed)

Opened 5 years ago

Last modified 4 years ago

[patch] Implement timezone-formats in date-formatting

Reported by: sune.kirkeby@gmail.com Assigned to: adrian
Milestone: Component: Database layer (models, ORM)
Version: Keywords:
Cc: jforcier@strozllc.com Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

In db/typecast timezone-information from the database is discarded. This is bad, which this patch fixes. Also, the timezone-related formatting characters were NotImplemented? in utils/dateformat, this patch also fixes that. (I assigned this to database-wrapper, but it's just as much template-system...)

Attachments

django-tzinfo.patch (4.9 kB) - added by sune.kirkeby@gmail.com on 09/09/05 15:45:13.
django-timesince.patch (1.1 kB) - added by sune.kirkeby@gmail.com on 09/09/05 16:37:53.
Fix timesince to take timezones into account
django-timesince.2.patch (1.1 kB) - added by sune.kirkeby@gmail.com on 09/10/05 02:24:34.
timezone.patch (6.2 kB) - added by sune.kirkeby@gmail.com on 09/10/05 03:55:09.
Patch with changes from both the other patch and bugfixes
timezone.2.patch (6.6 kB) - added by sune.kirkeby@gmail.com on 09/29/05 04:06:44.
Bugfix update of previous patch
dateformat.patch (13.7 kB) - added by Sune Kirkeby <sune.kirkeby@gmail.com> on 10/13/05 04:33:45.
New, improved and working patch for timezone-formatting in dateformat
dateformat.py (1.4 kB) - added by Sune Kirkeby <sune.kirkeby@gmail.com> on 10/20/05 02:49:30.
tzinfo.py (1.5 kB) - added by Sune Kirkeby <sune.kirkeby@gmail.com> on 10/21/05 02:52:15.

Change History

09/09/05 15:45:13 changed by sune.kirkeby@gmail.com

  • attachment django-tzinfo.patch added.

09/09/05 16:37:53 changed by sune.kirkeby@gmail.com

  • attachment django-timesince.patch added.

Fix timesince to take timezones into account

09/10/05 02:24:34 changed by sune.kirkeby@gmail.com

  • attachment django-timesince.2.patch added.

09/10/05 03:55:09 changed by sune.kirkeby@gmail.com

  • attachment timezone.patch added.

Patch with changes from both the other patch and bugfixes

09/26/05 23:39:33 changed by adrian

  • status changed from new to assigned.

09/29/05 03:21:44 changed by anonymous

  • cc set to sune.kirkeby@gmail.com.

09/29/05 04:06:44 changed by sune.kirkeby@gmail.com

  • attachment timezone.2.patch added.

Bugfix update of previous patch

10/11/05 17:13:00 changed by jforcier@strozllc.com

  • cc changed from sune.kirkeby@gmail.com to sune.kirkeby@gmail.com,jforcier@strozllc.com.

10/13/05 04:33:08 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

  • cc changed from sune.kirkeby@gmail.com,jforcier@strozllc.com to jforcier@strozllc.com.
  • summary changed from [patch] Keep timezones in db/typecast, implement timezone-formats in date-formatting to [patch] Implement timezone-formats in date-formatting.

The timezone-parsing patch is bad. Very bad. It breaks the unittests for all but PostgreSQL, and datetime comparisons break spectacularly because datetime is brain-dead when it comes to timezones (Unsafe datetime comparisons).

I worked out a much better way of getting what I want (timezone-formatting in dateformat); in dateformat I assume that the timezone is local, unless explicitly given. This works beautifully.

Also, I implemented all formats except "Swatch internet time", implemented backslash-escaping of format-chars and added unittests.

10/13/05 04:33:45 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

  • attachment dateformat.patch added.

New, improved and working patch for timezone-formatting in dateformat

10/13/05 22:56:47 changed by adrian

Sune -- Just to be clear, which patches are currently valid? Just the last two (timezone.2.patch and dateformat.patch)? Apply those two, and that's it?

10/14/05 01:42:00 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

Nope, just dateformat.patch, apply that and all should be good :)

10/18/05 08:46:43 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

Except,

+        t = time.mktime(now)

in timesince should be

+        t = now.timetuple()

Sorry.

10/19/05 23:49:30 changed by adrian

I get the following unit-test errors after applying this patch (and renaming the variable from my_birthday to sunes_birthday).

'dateformat' module: API test failed
====================================
Code: "format(sunes_birthday, 'O')"
Line: 21
Expected: "'+0100'\n"
Got: "'+1800'\n"

'dateformat' module: API test failed
====================================
Code: "format(sunes_birthday, 'r')"
Line: 25
Expected: "'Sat, 7 Jul 1979 22:00:00 +0100'\n"
Got: "'Sat, 7 Jul 1979 22:00:00 +1800'\n"

'dateformat' module: API test failed
====================================
Code: "format(sunes_birthday, 'U')"
Line: 37
Expected: "'300445200'\n"
Got: "'304117200'\n"

'dateformat' module: API test failed
====================================
Code: "format(sunes_birthday, 'Z')"
Line: 49
Expected: "'3600'\n"
Got: "'64800'\n"

'dateformat' module: API test failed
====================================
Code: "format(summertime, 'O')"
Line: 54
Expected: "'+0200'\n"
Got: "'+1900'\n"

'dateformat' module: API test failed
====================================
Code: "format(wintertime, 'O')"
Line: 58
Expected: "'+0100'\n"
Got: "'+1800'\n"

10/20/05 00:31:00 changed by adrian

In [969]: Lightly refactored django.utils.dateformat to make it use less code. Also integrated some of Sune's improvements from the #479 patch, including support for backslash escaping. Also vastly improved template docs for the {% now %} tag

10/20/05 02:36:11 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

django.utils.dateformat should be reloaded after the timezone-fidgeting in the test-module, since the UTC-offsets are calculated when dateformat is first imported. D'oh!

I'll attach a new test-module now.

/me hopes this works

10/20/05 02:49:30 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

  • attachment dateformat.py added.

10/20/05 18:36:34 changed by adrian

I get the exact same unit-test errors now...Any ideas?

10/20/05 18:37:26 changed by adrian

Also, your original patch had changes to core/db/typecasts.py...Those should probably still be applied, no?

10/21/05 02:11:52 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

Also, your original patch had changes to core/db/typecasts.py...Those should probably still be applied, no?

I'd rather not; I learned that having timezones come back from the database is going to break a lot of client code; because datetime barfs if you try mixing timezone-aware and timezone-naive datetimes. And, as long as the database returns datetimes in settings.TIME_ZONE (which I know postgresql does) the defaulting to LocalTimezone in dateformat and timesince is going to do the right thing; even accounting for DST.

10/21/05 02:51:13 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

I get the exact same unit-test errors now...Any ideas?

This time I actually tried running the tests with env TZ=America/Chicago, which gave me the same errors you see. And, it seems a reload of tzinfo is not enough for some reason, so I removed the pre-calculation of timezone-offsets in tzinfo, which does the trick for me. I'll attach the new django.utils.tzinfo.

And the reload-trick in the latest dateformat-tests is not needed with this tzinfo.

10/21/05 02:52:15 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

  • attachment tzinfo.py added.

10/22/05 16:38:01 changed by adrian

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [992]) Fixed #479 -- Implemented time-zone formats in dateformat. Thanks, Sune


Add/Change #479 ([patch] Implement timezone-formats in date-formatting)




Change Properties
Action