Fixed date handling with invalid strings

svn: r1582
This commit is contained in:
Don Allingham 2003-05-23 02:45:44 +00:00
parent f5dbea2a49
commit e259320e13
3 changed files with 28 additions and 25 deletions

View File

@ -30,6 +30,7 @@ __version__ = "$Revision$"
from intl import gettext as _ from intl import gettext as _
import re import re
import Errors
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -66,7 +67,6 @@ fmt3 = re.compile(_start+r"([?\d]+)\s*[./-]\s*([?\d]+)\s*[./-]\s*([?\d]+)\s*$",
fmt7 = re.compile(_start+r"([?\d]+)\s*[./-]\s*([?\d]+)\s*$", re.IGNORECASE) fmt7 = re.compile(_start+r"([?\d]+)\s*[./-]\s*([?\d]+)\s*$", re.IGNORECASE)
fmt4 = re.compile(_start+"(\S+)\s+(\d+)\s*$", re.IGNORECASE) fmt4 = re.compile(_start+"(\S+)\s+(\d+)\s*$", re.IGNORECASE)
fmt5 = re.compile(_start+"(\d+)\s*$", re.IGNORECASE) fmt5 = re.compile(_start+"(\d+)\s*$", re.IGNORECASE)
fmt6 = re.compile(_start+"(\S+)\s*$", re.IGNORECASE)
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -440,10 +440,11 @@ class Calendar:
matches = match.groups() matches = match.groups()
mode = self.set_mode_value(matches[0]) mode = self.set_mode_value(matches[0])
month = self.set_month_string(matches[2]) month = self.set_month_string(matches[2])
day = self.set_value(matches[1]) if month != UNDEF:
if len(matches) == 4 and matches[3] != None: day = self.set_value(matches[1])
year = self.set_value(matches[3]) if len(matches) == 4 and matches[3] != None:
return (year,month,day,mode) year = self.set_value(matches[3])
return (year,month,day,mode)
match = fmt5.match(text) match = fmt5.match(text)
if match != None: if match != None:
@ -487,28 +488,24 @@ class Calendar:
matches = match.groups() matches = match.groups()
mode = self.set_mode_value(matches[0]) mode = self.set_mode_value(matches[0])
month = self.set_month_string(unicode(matches[1])) month = self.set_month_string(unicode(matches[1]))
if matches[2]: if month != UNDEF:
val = matches[2].replace(',','') if matches[2]:
day = self.set_value(val) val = matches[2].replace(',','')
year = self.set_value(matches[3]) day = self.set_value(val)
return (year,month,day,mode) year = self.set_value(matches[3])
return (year,month,day,mode)
match = fmt4.match(text) match = fmt4.match(text)
if match != None: if match != None:
matches = match.groups() matches = match.groups()
mode = self.set_mode_value(matches[0]) mode = self.set_mode_value(matches[0])
month = self.set_month_string(unicode(matches[1])) month = self.set_month_string(unicode(matches[1]))
if len(matches) == 4: if month != UNDEF:
year = self.set_value(matches[3]) if len(matches) == 4:
return (year,month,day,mode) year = self.set_value(matches[3])
return (year,month,day,mode)
match = fmt6.match(text) raise Errors.DateError
if match != None:
matches = match.groups()
mode = self.set_mode_value(matches[0])
month = self.set_value(matches[1])
return (year,month,day,mode)
_FMT_FUNC = [ _FMT_FUNC = [
@ -550,5 +547,3 @@ def calendar_names():
list.append(n) list.append(n)
list.sort() list.sort()
return list return list

View File

@ -42,6 +42,7 @@ import Gregorian
import Julian import Julian
import Hebrew import Hebrew
import FrenchRepublic import FrenchRepublic
import Errors
from intl import gettext as _ from intl import gettext as _
@ -71,8 +72,6 @@ class Date:
""" """
formatCode = 0 formatCode = 0
Error = "Illegal Date"
fstr = _("(from|between|bet|bet.)") fstr = _("(from|between|bet|bet.)")
tstr = _("(and|to|-)") tstr = _("(and|to|-)")
@ -207,7 +206,7 @@ class Date:
else: else:
self.start.set(text) self.start.set(text)
self.range = 0 self.range = 0
except Date.Error: except Errors.DateError:
if text != "": if text != "":
self.range = -1 self.range = -1
self.text = text self.text = text

View File

@ -18,6 +18,15 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# #
class DateError(Exception):
"""Error used to report Date errors"""
def __init__(self,value=""):
Exception.__init__(self)
self.value = value
def __str__(self):
return self.value
class ReportError(Exception): class ReportError(Exception):
"""Error used to report Report errors""" """Error used to report Report errors"""
def __init__(self,value,value2=""): def __init__(self,value,value2=""):