diff --git a/ChangeLog b/ChangeLog index 075a34ddf..fe41c50a1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-09-17 Don Allingham + * src/DateDisplay.py: use locale.nl_langinfo to get month + names without manual encoding + * src/DateParser.py: use locale.nl_langinfo to get month + names without manual encoding + 2004-09-16 Don Allingham * src/Calendar.py: removed * src/Gregorian.py: removed diff --git a/src/DateDisplay.py b/src/DateDisplay.py index 0f5af1d61..7695cb54b 100644 --- a/src/DateDisplay.py +++ b/src/DateDisplay.py @@ -29,6 +29,7 @@ __author__ = "Donald N. Allingham" __version__ = "$Revision$" import Date +import locale class DateDisplay: """ @@ -36,83 +37,66 @@ class DateDisplay: """ formats = ( - "YYYY-MM-DD", - "MM/DD/YYYY", - "Month Day, Year", - "MON DAY, YEAR", - "Day Month Year", - "DAY MON YEAR" + "YYYY-MM-DD", "MM/DD/YYYY", "Month Day, Year", + "MON DAY, YEAR", "Day Month Year", "DAY MON YEAR" ) - _calendar = ( - "", - " (Julian)", - " (Hebrew)", - " (French Republican)", - " (Persian)", - " (Islamic)" + calendar = ( + ""," (Julian)"," (Hebrew)"," (French Republican)", + " (Persian)"," (Islamic)" ) _mod_str = ( - "", - "before ", - "after ", - "about ", - "estimated ", - "calculated ", - "" + "","before ","after ","about ","estimated ","calculated ","" ) + + # determine the code set returned by nl_langinfo + _codeset = locale.nl_langinfo(locale.CODESET) + # get month information from nl_langinfo. Since nl_langinfo + # returns data in the code set specified by the user, and + # gnome wants unicode, we need to convert each string from + # the native code set into unicode _months = ( "", - "January", - "February", - "March", - "April", - "May", - "June", - "July", - "August", - "September", - "October", - "November", - "December" + unicode(locale.nl_langinfo(locale.MON_1),_codeset), + unicode(locale.nl_langinfo(locale.MON_2),_codeset), + unicode(locale.nl_langinfo(locale.MON_3),_codeset), + unicode(locale.nl_langinfo(locale.MON_4),_codeset), + unicode(locale.nl_langinfo(locale.MON_5),_codeset), + unicode(locale.nl_langinfo(locale.MON_6),_codeset), + unicode(locale.nl_langinfo(locale.MON_7),_codeset), + unicode(locale.nl_langinfo(locale.MON_8),_codeset), + unicode(locale.nl_langinfo(locale.MON_9),_codeset), + unicode(locale.nl_langinfo(locale.MON_10),_codeset), + unicode(locale.nl_langinfo(locale.MON_11),_codeset), + unicode(locale.nl_langinfo(locale.MON_12),_codeset), ) _MONS = ( "", - "JAN", - "FEB", - "MAR", - "APR", - "MAY", - "JUN", - "JUL", - "AUG", - "SEP", - "OCT", - "NOV", - "DEC" + unicode(locale.nl_langinfo(locale.ABMON_1),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_2),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_3),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_4),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_5),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_6),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_7),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_8),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_9),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_10),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_11),_codeset), + unicode(locale.nl_langinfo(locale.ABMON_12),_codeset), ) _hebrew = ( - "", - "Tishri", - "Heshvan", - "Kislev", - "Tevet", - "Shevat", - "AdarI", - "AdarII", - "Nisan", - "Iyyar", - "Sivan", - "Tammuz", - "Av", - "Elul" + "", "Tishri", "Heshvan", "Kislev", "Tevet", "Shevat", + "AdarI", "AdarII", "Nisan", "Iyyar", "Sivan", "Tammuz", + "Av", "Elul" ) _french = ( + "", unicode("Vendémiaire",'latin-1'), unicode("Brumaire",'latin-1'), unicode("Frimaire",'latin-1'), @@ -129,33 +113,15 @@ class DateDisplay: ) _persian = ( - "Farvardin", - "Ordibehesht", - "Khordad", - "Tir", - "Mordad", - "Shahrivar", - "Mehr", - "Aban", - "Azar", - "Dey", - "Bahman", - "Esfand" + "", "Farvardin", "Ordibehesht", "Khordad", "Tir", + "Mordad", "Shahrivar", "Mehr", "Aban", "Azar", + "Dey", "Bahman", "Esfand" ) _islamic = ( - "Muharram", - "Safar", - "Rabi`al-Awwal", - "Rabi`ath-Thani", - "Jumada l-Ula", - "Jumada t-Tania", - "Rajab", - "Sha`ban", - "Ramadan", - "Shawwal", - "Dhu l-Qa`da", - "Dhu l-Hijja" + "", "Muharram", "Safar", "Rabi`al-Awwal", "Rabi`ath-Thani", + "Jumada l-Ula", "Jumada t-Tania", "Rajab", "Sha`ban", + "Ramadan", "Shawwal", "Dhu l-Qa`da", "Dhu l-Hijja" ) def __init__(self,format=None): @@ -164,6 +130,7 @@ class DateDisplay: of the desired format. The format value must correspond to the format list value (DateDisplay.format[]). """ + self.verify_format(format) if format == None: self.format = 0 @@ -178,7 +145,7 @@ class DateDisplay: self._display_persian, self._display_islamic, ] - + def verify_format(self,format): """ Verifies that the format value is within the correct range. @@ -214,14 +181,14 @@ class DateDisplay: if mod == Date.MOD_SPAN: d1 = self.display_cal[cal](start) d2 = self.display_cal[cal](date.get_stop_date()) - return "from %s to %s%s" % (d1,d2,self._calendar[cal]) + return "from %s to %s%s" % (d1,d2,self.calendar[cal]) elif mod == Date.MOD_RANGE: d1 = self.display_cal[cal](start) d2 = self.display_cal[cal](date.get_stop_date()) - return "between %s and %s%s" % (d1,d2,self._calendar[cal]) + return "between %s and %s%s" % (d1,d2,self.calendar[cal]) else: text = self.display_cal[date.get_calendar()](start) - return "%s%s%s" % (self._mod_str[mod],text,self._calendar[cal]) + return "%s%s%s" % (self._mod_str[mod],text,self.calendar[cal]) def _slash_year(self,val,slash): if slash: diff --git a/src/DateHandler.py b/src/DateHandler.py index 40807f5ea..de1451285 100644 --- a/src/DateHandler.py +++ b/src/DateHandler.py @@ -28,7 +28,6 @@ def create_parser(): try: return _lang_to_parser[_lang]() except: - print "not found" return DateParser.DateParser() def create_display(): diff --git a/src/DateParser.py b/src/DateParser.py index d85047d24..144364054 100644 --- a/src/DateParser.py +++ b/src/DateParser.py @@ -28,39 +28,47 @@ __version__ = "$Revision$" import string import re +import time +import locale + import Date + class DateParser: """ Converts a text string into a Date object. If the date cannot be converted, the text string is assigned. """ + + # determine the code set returned by nl_langinfo + _codeset = locale.nl_langinfo(locale.CODESET) + month_to_int = { - 'jan' : 1, - 'january' : 1, - 'feb' : 2, - 'february' : 2, - 'mar' : 3, - 'march' : 3, - 'apr' : 4, - 'april' : 4, - 'may' : 5, - 'june' : 6, - 'jun' : 6, - 'july' : 7, - 'jul' : 7, - 'august' : 8, - 'aug' : 8, - 'september': 9, - 'sep' : 9, - 'sept' : 9, - 'oct' : 10, - 'october' : 10, - 'nov' : 11, - 'november' : 11, - 'dec' : 12, - 'december' : 12, - } + unicode(locale.nl_langinfo(locale.MON_1),_codeset).lower() : 1, + unicode(locale.nl_langinfo(locale.ABMON_1),_codeset).lower() : 1, + unicode(locale.nl_langinfo(locale.MON_2),_codeset).lower() : 2, + unicode(locale.nl_langinfo(locale.ABMON_2),_codeset).lower() : 2, + unicode(locale.nl_langinfo(locale.MON_3),_codeset).lower() : 3, + unicode(locale.nl_langinfo(locale.ABMON_3),_codeset).lower() : 3, + unicode(locale.nl_langinfo(locale.MON_4),_codeset).lower() : 4, + unicode(locale.nl_langinfo(locale.ABMON_4),_codeset).lower() : 4, + unicode(locale.nl_langinfo(locale.MON_5),_codeset).lower() : 5, + unicode(locale.nl_langinfo(locale.ABMON_5),_codeset).lower() : 5, + unicode(locale.nl_langinfo(locale.MON_6),_codeset).lower() : 6, + unicode(locale.nl_langinfo(locale.ABMON_6),_codeset).lower() : 6, + unicode(locale.nl_langinfo(locale.MON_7),_codeset).lower() : 7, + unicode(locale.nl_langinfo(locale.ABMON_7),_codeset).lower() : 7, + unicode(locale.nl_langinfo(locale.MON_8),_codeset).lower() : 8, + unicode(locale.nl_langinfo(locale.ABMON_8),_codeset).lower() : 8, + unicode(locale.nl_langinfo(locale.MON_9),_codeset).lower() : 9, + unicode(locale.nl_langinfo(locale.ABMON_9),_codeset).lower() : 9, + unicode(locale.nl_langinfo(locale.MON_10),_codeset).lower() : 10, + unicode(locale.nl_langinfo(locale.ABMON_10),_codeset).lower(): 10, + unicode(locale.nl_langinfo(locale.MON_11),_codeset).lower() : 11, + unicode(locale.nl_langinfo(locale.ABMON_11),_codeset).lower(): 11, + unicode(locale.nl_langinfo(locale.MON_12),_codeset).lower() : 12, + unicode(locale.nl_langinfo(locale.ABMON_12),_codeset).lower(): 12, + } modifier_to_int = { 'before' : Date.MOD_BEFORE, @@ -86,6 +94,12 @@ class DateParser: 'calculated' : Date.QUAL_CALCULATED, } + _qual_str = '(' + '|'.join( + [ key.replace('.','\.') for key in quality_to_int.keys() ] + ) + ')' + _mod_str = '(' + '|'.join( + [ key.replace('.','\.') for key in modifier_to_int.keys() ] + ) + ')' _qual_str = '(' + string.join(quality_to_int.keys(),'|') + ')' _mod_str = '(' + string.join(modifier_to_int.keys(),'|') + ')' _mon_str = '(' + string.join(month_to_int.keys(),'|') + ')' @@ -113,6 +127,12 @@ class DateParser: """ Converts only the date portion of a date. """ + try: + value = time.strptime(text) + return (value[2],value[1],value[0],False) + except ValueError: + pass + match = self._text.match(text) if match: groups = match.groups() @@ -185,7 +205,6 @@ class DateParser: grps = match.groups() start = self._parse_subdate(grps[0]) stop = self._parse_subdate(grps[1]) - date.set_modifier(Date.MOD_SPAN) date.set(qual,Date.MOD_SPAN,Date.CAL_GREGORIAN,start + stop) return