[Zope3-checkins] CVS: Zope3/src/zope/i18n - _strptime.py:1.1 locales.py:1.6

Tim Peters tim.one@comcast.net
Fri, 10 Jan 2003 13:55:35 -0500


Update of /cvs-repository/Zope3/src/zope/i18n
In directory cvs.zope.org:/tmp/cvs-serv7890/src/zope/i18n

Modified Files:
	locales.py 
Added Files:
	_strptime.py 
Log Message:
locales.py needs time.strptime(), but that function doesn't exist on all
platforms before Python 2.3.  As a workaround, I'm checking in a (slightly
modified) version of Python 2.3's _strptime.py here, and altered locales.py
to use it when the platform time.strptime() doesn't exist.  This workaround
can go away whenever Python 2.3 (or later) becomes mandatory.  This fixes
the many recent test failures on Win2K and Win98SE, under Python 2.2.2.


=== Added File Zope3/src/zope/i18n/_strptime.py === (457/557 lines abridged)
# This file is a slightly modified copy of Python 2.3's Lib/_strptime.py.
# This file is under the Python Software Foundation (PSF) license.
#
# Modifications:
#     In order to allow running under Python 2.2, Tim modified the
#     _insensitiveindex() function to remove use of 2.3's enumerate() builtin.
#
# Purpose:
#     Zope's locales.py needs strptime(), but time.strptime() doesn't exist
#     on all platforms before Python 2.3.  The file can be removed when
#     Python 2.3 (or later) is required, at which time locales.py should
#     also be changed to stop importing this file.

"""Strptime-related classes and functions.

CLASSES:
    LocaleTime -- Discovers and/or stores locale-specific time information
    TimeRE -- Creates regexes for pattern matching a string of text containing
                time information as is returned by time.strftime()

FUNCTIONS:
    firstjulian -- Calculates the Julian date up to the first of the specified
                    year
    gregorian -- Calculates the Gregorian date based on the Julian day and
                    year
    julianday -- Calculates the Julian day since the first of the year based
                    on the Gregorian date
    dayofweek -- Calculates the day of the week from the Gregorian date.
    strptime -- Calculates the time struct represented by the passed-in string

Requires Python 2.2.1 or higher.
Can be used in Python 2.2 if the following line is added:
    >>> True = 1; False = 0
"""
import time
import locale
import calendar
from re import compile as re_compile
from re import IGNORECASE
from string import whitespace as whitespace_string

__author__ = "Brett Cannon"
__email__ = "drifty@bigfoot.com"

__all__ = ['strptime']

RegexpType = type(re_compile(''))


class LocaleTime(object):

[-=- -=- -=- 457 lines omitted -=- -=- -=-]

    # Perform a case-insensitive index search.

    #XXX <bc>: If LocaleTime is not exposed, then consider removing this and
    #          just lowercase when LocaleTime sets its vars and lowercasing
    #          search values.
    findme = findme.lower()
    key = 0
    for item in lst:
        if item.lower() == findme:
            return key
        key += 1
    else:
        raise ValueError("value not in list")

def firstjulian(year):
    """Calculate the Julian date up until the first of the year."""
    return ((146097 * (year + 4799)) // 400) - 31738

def julianday(year, month, day):
    """Calculate the Julian day since the beginning of the year.
    Calculated from the Gregorian date.
    """
    a = (14 - month) // 12
    return (day - 32045
            + (((153 * (month + (12 * a) - 3)) + 2) // 5)
            + ((146097 * (year + 4800 - a)) // 400)) - firstjulian(year) + 1

def gregorian(julian, year):
    """Return 3-item list containing Gregorian date based on the Julian day."""
    a = 32043 + julian + firstjulian(year)
    b = ((4 * a) + 3) // 146097
    c = a - ((146097 * b) // 4)
    d = ((4 * c) + 3) // 1461
    e = c - ((1461 * d) // 4)
    m = ((5 * e) + 2) // 153
    day = 1 + e - (((153 * m) + 2) // 5)
    month = m + 3 - (12 * (m // 10))
    year = (100 * b) + d - 4800 + (m // 10)
    return [year, month, day]

def dayofweek(year, month, day):
    """Calculate the day of the week (Monday is 0)."""
    a = (14 - month) // 12
    y = year - a
    weekday = (day + y + ((97 * y) // 400)
               + ((31 * (month + (12 * a) -2 )) // 12)) % 7
    if weekday == 0:
        return 6
    else:
        return weekday-1


=== Zope3/src/zope/i18n/locales.py 1.5 => 1.6 ===
--- Zope3/src/zope/i18n/locales.py:1.5	Thu Jan  9 14:19:38 2003
+++ Zope3/src/zope/i18n/locales.py	Fri Jan 10 13:55:32 2003
@@ -15,10 +15,19 @@
 
 $Id$
 """
-import time, os
+import os
 import datetime
 from xml.dom.minidom import parse as parseXML
 
+# time.strptime() isn't available on all platforms before Python 2.3.  When
+# it isn't available, use the implementation from 2.3's _strptime.py, checked
+# into this source tree for convenience.  This workaround can be removed
+# when Python 2.3 (or later) becomes required.
+try:
+    from time import strptime
+except ImportError:
+    from _strptime import strptime
+
 from zope.i18n.interfaces import ILocaleProvider, ILocale
 from zope.i18n.interfaces import ILocaleVersion, ILocaleIdentity
 from zope.i18n.interfaces import ILocaleTimeZone, ILocaleCalendar
@@ -757,7 +766,7 @@
         for version in versioning.getElementsByTagName('version'):
             id = version.getAttribute('number')
             date = version.getAttribute('date')
-            date = time.strptime(date, '%a %b %d %H:%M:%S %Y')
+            date = strptime(date, '%a %b %d %H:%M:%S %Y')
             date = datetime.datetime(*date[:6])
             comment = self._getText(version.childNodes)
             versions.append(ICULocaleVersion(id, date, comment))