Opened 16 years ago

Closed 15 years ago

#10866 closed (fixed)

datetime_safe should zero-pad old years

Reported by: Eric Floehr <eric@…> Owned by: nobody
Component: Core (Other) Version: dev
Severity: Keywords: datetime_safe date datetime
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Datetime_safe, introduced as a result of #1443 due to strftime's limitation on years prior to 1900, does not zero-pad years with the %Y format.

I believe that the intent of %Y is to provide a 4-digit year, so years like 234 should be represented 0234, and 20 should be 0020. This is how %Y works in languages that support years less than 1900. (Rant: Why is python so behind on this, we shouldn't even have to worry about this!)

However, practically, the implementation as it exists will cause incorrect dates to be inserted into most databases.

For example begin_date=date(1,1,1), end_date=date(1948,12,31) causes the following insert to get generated:

INSERT INTO "test_table" ("begin_date", "end_date") VALUES (E'   1-01-01', E'1948-12-31')

PostgreSQL, and most other databases, will interpret a one- and two-digit years as centered around the 1900's or 2000's, and in fact PostgreSQL specifically will interpret '1-01-01' as '2001-01-01'.

It should more properly generate the following:

INSERT INTO "test_table" ("begin_date", "end_date") VALUES (E'0001-01-01', E'1948-12-31')

The change is adding a zero to the format string in datetime_safe's strftime, and adding a doctest to ensure. I've attached the diff file from changeset:10581.

I do not believe there are any negative consequences to this change.

Attachments (2)

datetime_safe_zero_padding.diff (1.5 KB ) - added by Eric Floehr <eric@…> 16 years ago.
svn diff to add zero-padding to %Y in datetime_safe, along with doctest addition
datetime_safe_zero_padding.2.diff (791 bytes ) - added by Eric Floehr <eric@…> 16 years ago.
[corrected -- ran diff twice in first one] svn diff to add zero-padding to %Y in datetime_safe, along with doctest addition

Download all attachments as: .zip

Change History (7)

by Eric Floehr <eric@…>, 16 years ago

svn diff to add zero-padding to %Y in datetime_safe, along with doctest addition

by Eric Floehr <eric@…>, 16 years ago

[corrected -- ran diff twice in first one] svn diff to add zero-padding to %Y in datetime_safe, along with doctest addition

comment:1 by Thejaswi Puthraya, 16 years ago

Component: UncategorizedCore framework

comment:2 by Karen Tracey, 16 years ago

#11132 reported this as old dates output by dumpdata cannot be loaded by loaddata, and proposed the same patch.

comment:3 by Chris Beaven, 16 years ago

Triage Stage: UnreviewedReady for checkin

comment:4 by Eric Floehr <eric@…>, 15 years ago

This ticket, and its patch, appears to have been incorporated in trunk, as part of the fix for #12524, so this ticket can be closed.

comment:5 by Karen Tracey, 15 years ago

Resolution: fixed
Status: newclosed

Fixed in r12423/r12424. (Note anyone could close the ticket -- special privs are not needed for that.)

Note: See TracTickets for help on using tickets.
Back to Top