Code

Opened 5 years ago

Closed 4 years ago

#10866 closed (fixed)

datetime_safe should zero-pad old years

Reported by: Eric Floehr <eric@…> Owned by: nobody
Component: Core (Other) Version: master
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: UI/UX:

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@…> 5 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@…> 5 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)

Changed 5 years ago by Eric Floehr <eric@…>

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

Changed 5 years ago by Eric Floehr <eric@…>

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

comment:1 Changed 5 years ago by thejaswi_puthraya

  • Component changed from Uncategorized to Core framework
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by kmtracey

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

comment:3 Changed 5 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Ready for checkin

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

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 Changed 4 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.