[Python-Dev] Checking input range in time.asctime and time.ctime

Alexander Belopolsky alexander.belopolsky at gmail.com
Thu Jan 6 19:33:42 CET 2011


On Thu, Jan 6, 2011 at 6:47 AM, Victor Stinner
<victor.stinner at haypocalc.com> wrote:
> Le mercredi 05 janvier 2011 à 23:48 -0500, Alexander Belopolsky a
> écrit :
>> I would be happy with just
>>
>>    if accept2dyear:
>>        if 69 <= y <= 99:
>>            y += 1900
>>        elif 0 <= y <= 68:
>>            y += 2000
>>    # call system function with tm_year = y - 1900
..
>> but I thought that would be too radical.
>
> Why ?

ISTM that time.asctime() called with a 3-digit year, particularly a
low 3-digit, one is much more likely to be a manifestation of a
lingering Y2K bug than a real intent to print an ancient date.  I do
remember that many devices were showing Jan 1, 100 back in early 2000.

The same logic does not apply to programs that run with PYTHONY2K set
because presumably users who bothered to set PYTHONY2K know that their
program does not use 2-digit years.

That's why, I think for 3.2 we should do the following:

1.  Keep PYTHONY2K logic and accept2dyear = 1 default.
2. With default accept2dyear = 1:
         - for 0 <= year < 100 issue a deprecation warning and supply
century according to POSIX rules
         - for 100 <= year < 1000 raise ValueError
         - for year >= 1000 leave year unchanged
3. With  accept2dyear = 0 leave year unchanged regardless of value.

For 3.3, remove PYTHONY2K and accept2dyear and leave year unchanged
regardless of value.

Can we agree that this is reasonable for time.asctime()?  In
time/datetime.strftime we can impose stricter limits if necessary to
work around platform bugs.


More information about the Python-Dev mailing list