* 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


svn: r3550
This commit is contained in:
Don Allingham 2004-09-17 23:52:09 +00:00
parent a32207e5c5
commit 9c8e337e96
4 changed files with 104 additions and 113 deletions

View File

@ -1,3 +1,9 @@
2004-09-17 Don Allingham <dallingham@users.sourceforge.net>
* 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 <dallingham@users.sourceforge.net> 2004-09-16 Don Allingham <dallingham@users.sourceforge.net>
* src/Calendar.py: removed * src/Calendar.py: removed
* src/Gregorian.py: removed * src/Gregorian.py: removed

View File

@ -29,6 +29,7 @@ __author__ = "Donald N. Allingham"
__version__ = "$Revision$" __version__ = "$Revision$"
import Date import Date
import locale
class DateDisplay: class DateDisplay:
""" """
@ -36,83 +37,66 @@ class DateDisplay:
""" """
formats = ( formats = (
"YYYY-MM-DD", "YYYY-MM-DD", "MM/DD/YYYY", "Month Day, Year",
"MM/DD/YYYY", "MON DAY, YEAR", "Day Month Year", "DAY MON YEAR"
"Month Day, Year",
"MON DAY, YEAR",
"Day Month Year",
"DAY MON YEAR"
) )
_calendar = ( calendar = (
"", ""," (Julian)"," (Hebrew)"," (French Republican)",
" (Julian)", " (Persian)"," (Islamic)"
" (Hebrew)",
" (French Republican)",
" (Persian)",
" (Islamic)"
) )
_mod_str = ( _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 = ( _months = (
"", "",
"January", unicode(locale.nl_langinfo(locale.MON_1),_codeset),
"February", unicode(locale.nl_langinfo(locale.MON_2),_codeset),
"March", unicode(locale.nl_langinfo(locale.MON_3),_codeset),
"April", unicode(locale.nl_langinfo(locale.MON_4),_codeset),
"May", unicode(locale.nl_langinfo(locale.MON_5),_codeset),
"June", unicode(locale.nl_langinfo(locale.MON_6),_codeset),
"July", unicode(locale.nl_langinfo(locale.MON_7),_codeset),
"August", unicode(locale.nl_langinfo(locale.MON_8),_codeset),
"September", unicode(locale.nl_langinfo(locale.MON_9),_codeset),
"October", unicode(locale.nl_langinfo(locale.MON_10),_codeset),
"November", unicode(locale.nl_langinfo(locale.MON_11),_codeset),
"December" unicode(locale.nl_langinfo(locale.MON_12),_codeset),
) )
_MONS = ( _MONS = (
"", "",
"JAN", unicode(locale.nl_langinfo(locale.ABMON_1),_codeset),
"FEB", unicode(locale.nl_langinfo(locale.ABMON_2),_codeset),
"MAR", unicode(locale.nl_langinfo(locale.ABMON_3),_codeset),
"APR", unicode(locale.nl_langinfo(locale.ABMON_4),_codeset),
"MAY", unicode(locale.nl_langinfo(locale.ABMON_5),_codeset),
"JUN", unicode(locale.nl_langinfo(locale.ABMON_6),_codeset),
"JUL", unicode(locale.nl_langinfo(locale.ABMON_7),_codeset),
"AUG", unicode(locale.nl_langinfo(locale.ABMON_8),_codeset),
"SEP", unicode(locale.nl_langinfo(locale.ABMON_9),_codeset),
"OCT", unicode(locale.nl_langinfo(locale.ABMON_10),_codeset),
"NOV", unicode(locale.nl_langinfo(locale.ABMON_11),_codeset),
"DEC" unicode(locale.nl_langinfo(locale.ABMON_12),_codeset),
) )
_hebrew = ( _hebrew = (
"", "", "Tishri", "Heshvan", "Kislev", "Tevet", "Shevat",
"Tishri", "AdarI", "AdarII", "Nisan", "Iyyar", "Sivan", "Tammuz",
"Heshvan", "Av", "Elul"
"Kislev",
"Tevet",
"Shevat",
"AdarI",
"AdarII",
"Nisan",
"Iyyar",
"Sivan",
"Tammuz",
"Av",
"Elul"
) )
_french = ( _french = (
"",
unicode("Vendémiaire",'latin-1'), unicode("Vendémiaire",'latin-1'),
unicode("Brumaire",'latin-1'), unicode("Brumaire",'latin-1'),
unicode("Frimaire",'latin-1'), unicode("Frimaire",'latin-1'),
@ -129,33 +113,15 @@ class DateDisplay:
) )
_persian = ( _persian = (
"Farvardin", "", "Farvardin", "Ordibehesht", "Khordad", "Tir",
"Ordibehesht", "Mordad", "Shahrivar", "Mehr", "Aban", "Azar",
"Khordad", "Dey", "Bahman", "Esfand"
"Tir",
"Mordad",
"Shahrivar",
"Mehr",
"Aban",
"Azar",
"Dey",
"Bahman",
"Esfand"
) )
_islamic = ( _islamic = (
"Muharram", "", "Muharram", "Safar", "Rabi`al-Awwal", "Rabi`ath-Thani",
"Safar", "Jumada l-Ula", "Jumada t-Tania", "Rajab", "Sha`ban",
"Rabi`al-Awwal", "Ramadan", "Shawwal", "Dhu l-Qa`da", "Dhu l-Hijja"
"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): def __init__(self,format=None):
@ -164,6 +130,7 @@ class DateDisplay:
of the desired format. The format value must correspond to the format of the desired format. The format value must correspond to the format
list value (DateDisplay.format[]). list value (DateDisplay.format[]).
""" """
self.verify_format(format) self.verify_format(format)
if format == None: if format == None:
self.format = 0 self.format = 0
@ -178,7 +145,7 @@ class DateDisplay:
self._display_persian, self._display_persian,
self._display_islamic, self._display_islamic,
] ]
def verify_format(self,format): def verify_format(self,format):
""" """
Verifies that the format value is within the correct range. Verifies that the format value is within the correct range.
@ -214,14 +181,14 @@ class DateDisplay:
if mod == Date.MOD_SPAN: if mod == Date.MOD_SPAN:
d1 = self.display_cal[cal](start) d1 = self.display_cal[cal](start)
d2 = self.display_cal[cal](date.get_stop_date()) 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: elif mod == Date.MOD_RANGE:
d1 = self.display_cal[cal](start) d1 = self.display_cal[cal](start)
d2 = self.display_cal[cal](date.get_stop_date()) 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: else:
text = self.display_cal[date.get_calendar()](start) 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): def _slash_year(self,val,slash):
if slash: if slash:

View File

@ -28,7 +28,6 @@ def create_parser():
try: try:
return _lang_to_parser[_lang]() return _lang_to_parser[_lang]()
except: except:
print "not found"
return DateParser.DateParser() return DateParser.DateParser()
def create_display(): def create_display():

View File

@ -28,39 +28,47 @@ __version__ = "$Revision$"
import string import string
import re import re
import time
import locale
import Date import Date
class DateParser: class DateParser:
""" """
Converts a text string into a Date object. If the date cannot be Converts a text string into a Date object. If the date cannot be
converted, the text string is assigned. converted, the text string is assigned.
""" """
# determine the code set returned by nl_langinfo
_codeset = locale.nl_langinfo(locale.CODESET)
month_to_int = { month_to_int = {
'jan' : 1, unicode(locale.nl_langinfo(locale.MON_1),_codeset).lower() : 1,
'january' : 1, unicode(locale.nl_langinfo(locale.ABMON_1),_codeset).lower() : 1,
'feb' : 2, unicode(locale.nl_langinfo(locale.MON_2),_codeset).lower() : 2,
'february' : 2, unicode(locale.nl_langinfo(locale.ABMON_2),_codeset).lower() : 2,
'mar' : 3, unicode(locale.nl_langinfo(locale.MON_3),_codeset).lower() : 3,
'march' : 3, unicode(locale.nl_langinfo(locale.ABMON_3),_codeset).lower() : 3,
'apr' : 4, unicode(locale.nl_langinfo(locale.MON_4),_codeset).lower() : 4,
'april' : 4, unicode(locale.nl_langinfo(locale.ABMON_4),_codeset).lower() : 4,
'may' : 5, unicode(locale.nl_langinfo(locale.MON_5),_codeset).lower() : 5,
'june' : 6, unicode(locale.nl_langinfo(locale.ABMON_5),_codeset).lower() : 5,
'jun' : 6, unicode(locale.nl_langinfo(locale.MON_6),_codeset).lower() : 6,
'july' : 7, unicode(locale.nl_langinfo(locale.ABMON_6),_codeset).lower() : 6,
'jul' : 7, unicode(locale.nl_langinfo(locale.MON_7),_codeset).lower() : 7,
'august' : 8, unicode(locale.nl_langinfo(locale.ABMON_7),_codeset).lower() : 7,
'aug' : 8, unicode(locale.nl_langinfo(locale.MON_8),_codeset).lower() : 8,
'september': 9, unicode(locale.nl_langinfo(locale.ABMON_8),_codeset).lower() : 8,
'sep' : 9, unicode(locale.nl_langinfo(locale.MON_9),_codeset).lower() : 9,
'sept' : 9, unicode(locale.nl_langinfo(locale.ABMON_9),_codeset).lower() : 9,
'oct' : 10, unicode(locale.nl_langinfo(locale.MON_10),_codeset).lower() : 10,
'october' : 10, unicode(locale.nl_langinfo(locale.ABMON_10),_codeset).lower(): 10,
'nov' : 11, unicode(locale.nl_langinfo(locale.MON_11),_codeset).lower() : 11,
'november' : 11, unicode(locale.nl_langinfo(locale.ABMON_11),_codeset).lower(): 11,
'dec' : 12, unicode(locale.nl_langinfo(locale.MON_12),_codeset).lower() : 12,
'december' : 12, unicode(locale.nl_langinfo(locale.ABMON_12),_codeset).lower(): 12,
} }
modifier_to_int = { modifier_to_int = {
'before' : Date.MOD_BEFORE, 'before' : Date.MOD_BEFORE,
@ -86,6 +94,12 @@ class DateParser:
'calculated' : Date.QUAL_CALCULATED, '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(),'|') + ')' _qual_str = '(' + string.join(quality_to_int.keys(),'|') + ')'
_mod_str = '(' + string.join(modifier_to_int.keys(),'|') + ')' _mod_str = '(' + string.join(modifier_to_int.keys(),'|') + ')'
_mon_str = '(' + string.join(month_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. 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) match = self._text.match(text)
if match: if match:
groups = match.groups() groups = match.groups()
@ -185,7 +205,6 @@ class DateParser:
grps = match.groups() grps = match.groups()
start = self._parse_subdate(grps[0]) start = self._parse_subdate(grps[0])
stop = self._parse_subdate(grps[1]) stop = self._parse_subdate(grps[1])
date.set_modifier(Date.MOD_SPAN)
date.set(qual,Date.MOD_SPAN,Date.CAL_GREGORIAN,start + stop) date.set(qual,Date.MOD_SPAN,Date.CAL_GREGORIAN,start + stop)
return return