[egenix-users] At what year does (yy, mm, dd) become (19yy, mm,
dd)?
M.-A. Lemburg
mal at egenix.com
Wed May 7 17:04:22 CEST 2008
On 2008-05-07 15:56, Dick Moores wrote:
> On Wed, May 7, 2008 at 4:36 AM, M.-A. Lemburg <mal at egenix.com> wrote:
>> On 2008-05-07 12:09, Dick Moores wrote:
>>> For mxDateTime.
>>>
>>> I know I saw this in the manual, but I can't locate it again.
>> Note about the Y2K problems:
>>
>> The parser can only handle years with at least 2 digits. 2
>> digit year values get expanded by adding the century using
>> DateTime.add_century(), while 3 digit year get converted
>> literally. To have 2 digit years also be interpreted literally,
>> add leading zeros, e.g. year 99 must be written as 099 or 0099.
>>
>> def add_century(year):
>>
>> """ Sliding window approach to the Y2K problem: adds a suitable
>> century to the given year and returns it as integer.
>>
>> The window used depends on the current year (at import time).
>> If adding the current century to the given year gives a year
>> within the range current_year-70...current_year+30 [both
>> inclusive], then the current century is added. Otherwise the
>> century (current + 1 or - 1) producing the smallest difference is
>> chosen.
>
> Sorry, but I can't follow that. Please give some examples. From my own
> testing, I can see that for current year 2008, if 2-digit years are
> represented by yy, then if yy <= 38, the century is the 21st (20yy).
> Whereas if yy > 39, the century is the 20th (19yy). But I don't
> understand what happens when the current year changes. Please
> illustrate your rule with some well-chosen examples.
>
> Examples for my narrow rule: (38, 1, 1) -> (2038, 01, 01); (39, 1,
> 1) -> (1939, 01, 01); (87, 12, 31) -> (1987, 12, 31); (01, 9, 11) ->
> (2001, 09, 11)
Those two snippets were taken from Parser.py and DateTime.py.
Please have a look at add_century() in DateTime.py for more information
on the algorithm. This function is always called when the parser
finds that a two-digit year was used, regardless of the month and
day:
_current_year = now().year
_current_century, _current_year_in_century = divmod(_current_year, 100)
_current_century = _current_century * 100
def add_century(year,
current_year=_current_year,
current_century=_current_century):
""" Sliding window approach to the Y2K problem: adds a suitable
century to the given year and returns it as integer.
The window used depends on the current year (at import time).
If adding the current century to the given year gives a year
within the range current_year-70...current_year+30 [both
inclusive], then the current century is added. Otherwise the
century (current + 1 or - 1) producing the smallest difference is
chosen.
"""
if year > 99:
# Take it as-is
return year
year = year + current_century
diff = year - current_year
if diff >= -70 and diff <= 30:
return year
elif diff < -70:
return year + 100
else:
return year - 100
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, May 07 2008)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
More information about the egenix-users
mailing list