From 6202d3eb765d6cfd729d679559e32c6bf10aa271 Mon Sep 17 00:00:00 2001 From: Don Allingham Date: Fri, 23 Nov 2001 06:15:22 +0000 Subject: [PATCH] More calendar work svn: r578 --- gramps/src/Calendar.py | 486 +++++++++++++++++++++++++++++++++++++++++ gramps/src/Date.py | 401 +++++++++++++++------------------- 2 files changed, 663 insertions(+), 224 deletions(-) create mode 100644 gramps/src/Calendar.py diff --git a/gramps/src/Calendar.py b/gramps/src/Calendar.py new file mode 100644 index 000000000..fa5a71ff7 --- /dev/null +++ b/gramps/src/Calendar.py @@ -0,0 +1,486 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2001 Donald N. Allingham +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +_FR_SDN_OFFSET = 2375474 +_FR_DAYS_PER_4_YEARS = 1461 +_FR_DAYS_PER_MONTH = 30 +_FR_FIRST_VALID = 2375840 +_FR_LAST_VALID = 2380952 +_GR_SDN_OFFSET = 32045 +_GR_DAYS_PER_5_MONTHS = 153 +_GR_DAYS_PER_4_YEARS = 1461 +_GR_DAYS_PER_400_YEARS = 146097 +_J_SDN_OFFSET = 32083 +_J_DAYS_PER_5_MONTHS = 153 +_J_DAYS_PER_4_YEARS = 1461 + +_HALAKIM_PER_HOUR = 1080 +_HALAKIM_PER_DAY = 25920 +_HALAKIM_PER_LUNAR_CYCLE = ((29 * _HALAKIM_PER_DAY) + 13753) +_HALAKIM_PER_METONIC_CYCLE = (_HALAKIM_PER_LUNAR_CYCLE * (12 * 19 + 7)) + +_H_SDN_OFFSET = 347997 +_NEW_MOON_OF_CREATION = 31524 + +_SUNDAY = 0 +_MONDAY = 1 +_TUESDAY = 2 +_WEDNESDAY= 3 +_THURSDAY = 4 +_FRIDAY = 5 +_SATURDAY = 6 + +_NOON = (18 * _HALAKIM_PER_HOUR) +_AM3_11_20 = ((9 * _HALAKIM_PER_HOUR) + 204) +_AM9_32_43 = ((15 * _HALAKIM_PER_HOUR) + 589) + +monthsPerYear = [ + 12, 12, 13, 12, 12, 13, 12, 13, 12, 12, 13, 12, 12, 13, 12, 12, 13, 12, 13 +] + +yearOffset = [ + 0, 12, 24, 37, 49, 61, 74, 86, 99, 111, 123, + 136, 148, 160, 173, 185, 197, 210, 222 +] + +def french_to_sdn(year,month,day): + if (year < 1 or year > 14 or month < 1 or month > 13 or day < 1 or day > 30): + return 0 + + return (year*_FR_DAYS_PER_4_YEARS)/4 + (month-1)*_FR_DAYS_PER_MONTH + day +_FR_SDN_OFFSET + +def sdn_to_french(sdn): + if (sdn < _FR_FIRST_VALID or sdn > _FR_LAST_VALID) : + return (0,0,0) + + temp = (sdn - _FR_SDN_OFFSET) * 4 - 1 + year = temp / _FR_DAYS_PER_4_YEARS + dayOfYear = (temp % _FR_DAYS_PER_4_YEARS) / 4 + month = dayOfYear / _FR_DAYS_PER_MONTH + 1 + day = dayOfYear % _FR_DAYS_PER_MONTH + 1 + return (year,month,day) + + +def sdn_to_gregorian(sdn): + if sdn <= 0: + return (0,0,0) + + temp = (sdn + _GR_SDN_OFFSET) * 4 - 1 + + # Calculate the century (year/100) + century = temp / _GR_DAYS_PER_400_YEARS + + # Calculate the year and day of year (1 <= dayOfYear <= 366) + + temp = ((temp % _GR_DAYS_PER_400_YEARS) / 4) * 4 + 3 + year = (century * 100) + (temp / _GR_DAYS_PER_4_YEARS) + dayOfYear = (temp % _GR_DAYS_PER_4_YEARS) / 4 + 1 + + # Calculate the month and day of month + temp = dayOfYear * 5 - 3 + month = temp / _GR_DAYS_PER_5_MONTHS + day = (temp % _GR_DAYS_PER_5_MONTHS) / 5 + 1 + + # Convert to the normal beginning of the year + if month < 10 : + month = month + 3 + else: + year = year + 1 + month = month + 9 + + # Adjust to the B.C./A.D. type numbering + year = year - 4800 + if year <= 0: + year = year - 1 + + return (year,month,day) + +def gregorian_to_sdn(iyear,imonth,iday): + + # check for invalid dates + if iyear==0 or iyear<-4714 or imonth<=0 or imonth>12 or iday<=0 or iday>31: + return 0 + + # check for dates before SDN 1 (Nov 25, 4714 B.C.) + if iyear == -4714: + if imonth < 11 or imonth == 11 and iday < 25: + return 0 + + if iyear < 0: + year = iyear + 4801 + else: + year = iyear + 4800 + + # Adjust the start of the year + + if imonth > 2: + month = imonth - 3 + else: + month = imonth + 9 + year = year - 1 + + return( ((year / 100) * _GR_DAYS_PER_400_YEARS) / 4 + + ((year % 100) * _GR_DAYS_PER_4_YEARS) / 4 + + (month * _GR_DAYS_PER_5_MONTHS + 2) / 5 + + iday + - _GR_SDN_OFFSET ); + + +def sdn_to_julian(sdn): + if sdn <= 0 : + return (0,0,0) + + temp = (sdn + _J_SDN_OFFSET) * 4 - 1 + + # Calculate the year and day of year (1 <= dayOfYear <= 366) + year = temp / _J_DAYS_PER_4_YEARS + dayOfYear = (temp % _J_DAYS_PER_4_YEARS) / 4 + 1 + + # Calculate the month and day of month + temp = dayOfYear * 5 - 3; + month = temp / _J_DAYS_PER_5_MONTHS; + day = (temp % _J_DAYS_PER_5_MONTHS) / 5 + 1; + + # Convert to the normal beginning of the year + if month < 10: + month = month + 3 + else: + year = year + 1 + month = month - 9 + + # Adjust to the B.C./A.D. type numbering + year = year - 4800 + if year <= 0: + year = year - 1 + + return (year,month,day) + +def julian_to_sdn(iyear,imonth,iday): + + # check for invalid dates + if iyear==0 or iyear<-4713 or imonth<=0 or imonth>12 or iday<=0 or iday>31: + return 0 + + # check for dates before SDN 1 (Jan 2, 4713 B.C.) + if iyear == -4713: + if imonth == 1 and iday == 1: + return 0 + + # Make year always a positive number + if iyear < 0: + year = iyear + 4801 + else: + year = iyear + 4800 + + # Adjust the start of the year + if imonth > 2: + month = imonth - 3 + else: + month = imonth + 9 + year = year - 1 + + return (year*_J_DAYS_PER_4_YEARS)/4 + (month*_J_DAYS_PER_5_MONTHS+2)/5 + iday - _J_SDN_OFFSET + +def Tishri1(metonicYear, moladDay, moladHalakim): + + tishri1 = moladDay + dow = tishri1 % 7 + leapYear = metonicYear == 2 or metonicYear == 5 or metonicYear == 7 or \ + metonicYear == 10 or metonicYear == 13 or metonicYear == 16 or \ + metonicYear == 18 + lastWasLeapYear = metonicYear == 3 or metonicYear == 6 or metonicYear == 8 or \ + metonicYear == 11 or metonicYear == 14 or metonicYear == 17 or \ + metonicYear == 0 + + # Apply rules 2, 3 and 4. + if ((moladHalakim >= _NOON) or + ((not leapYear) and dow == _TUESDAY and moladHalakim >= _AM3_11_20) or + (lastWasLeapYear and dow == _MONDAY and moladHalakim >= _AM9_32_43)) : + tishri1 = tishri1 + 1 + dow = dow + 1 + if dow == 7: + dow = 0 + + # Apply rule 1 after the others because it can cause an additional + # delay of one day + + if dow == _WEDNESDAY or dow == _FRIDAY or dow == _SUNDAY: + tishri1 = tishri1 + 1 + + return tishri1 + + +def MoladOfMetonicCycle(metonicCycle): + + # Start with the time of the first molad after creation. + + r1 = _NEW_MOON_OF_CREATION; + + # Calculate metonicCycle * HALAKIM_PER_METONIC_CYCLE. The upper 32 + # bits of the result will be in r2 and the lower 16 bits will be + # in r1. + + r1 = r1 + (metonicCycle * (_HALAKIM_PER_METONIC_CYCLE & 0xFFFF)) + r2 = r1 >> 16 + r2 = r2 + (metonicCycle * ((_HALAKIM_PER_METONIC_CYCLE >> 16) & 0xFFFF)) + + # Calculate r2r1 / HALAKIM_PER_DAY. The remainder will be in r1, the + # upper 16 bits of the quotient will be in d2 and the lower 16 bits + # will be in d1. + + d2 = r2 / _HALAKIM_PER_DAY + r2 = r2 - (d2 * _HALAKIM_PER_DAY) + r1 = (r2 << 16) | (r1 & 0xFFFF) + d1 = r1 / _HALAKIM_PER_DAY + r1 = r1 - ( d1 * _HALAKIM_PER_DAY) + + MoladDay = (d2 << 16) | d1 + MoladHalakim = r1 + + return (MoladDay,MoladHalakim) + +def FindTishriMolad(inputDay): + + # Estimate the metonic cycle number. Note that this may be an under + # estimate because there are 6939.6896 days in a metonic cycle not + # 6940, but it will never be an over estimate. The loop below will + # correct for any error in this estimate. */ + + metonicCycle = (inputDay + 310) / 6940 + + # Calculate the time of the starting molad for this metonic cycle. */ + + (moladDay, moladHalakim) = MoladOfMetonicCycle(metonicCycle) + + # If the above was an under estimate, increment the cycle number until + # the correct one is found. For modern dates this loop is about 98.6% + # likely to not execute, even once, because the above estimate is + # really quite close. + + while moladDay < (inputDay - 6940 + 310): + metonicCycle = metonicCycle + 1 + moladHalakim = moladHalakim + _HALAKIM_PER_METONIC_CYCLE + moladDay = moladDay + ( moladHalakim / _HALAKIM_PER_DAY) + moladHalakim = moladHalakim % _HALAKIM_PER_DAY + + # Find the molad of Tishri closest to this date. + + for metonicYear in range(0,18): + if moladDay > inputDay - 74: + break + + moladHalakim = moladHalakim + \ + (_HALAKIM_PER_LUNAR_CYCLE * monthsPerYear[metonicYear]) + moladDay = moladDay + (moladHalakim / _HALAKIM_PER_DAY) + moladHalakim = moladHalakim % _HALAKIM_PER_DAY + else: + metonicYear = metonicYear + 1 + return (metonicCycle, metonicYear, moladDay, moladHalakim) + +def FindStartOfYear(year): + + pMetonicCycle = (year - 1) / 19; + pMetonicYear = (year - 1) % 19; + (pMoladDay, pMoladHalakim) = MoladOfMetonicCycle(pMetonicCycle) + + pMoladHalakim = pMoladHalakim + (_HALAKIM_PER_LUNAR_CYCLE * yearOffset[pMetonicYear]) + pMoladDay = pMoladDay + (pMoladHalakim / _HALAKIM_PER_DAY) + pMoladHalakim = pMoladHalakim % _HALAKIM_PER_DAY + + pTishri1 = Tishri1(pMetonicYear, pMoladDay, pMoladHalakim); + + return (pMetonicCycle, pMetonicYear, pMoladDay, pMoladHalakim, pTishri1) + +def sdn_to_jewish(sdn): + + if sdn <= _H_SDN_OFFSET : + return (0,0,0) + + inputDay = sdn - _H_SDN_OFFSET + + (metonicCycle, metonicYear, day, halakim) = FindTishriMolad(inputDay) + tishri1 = Tishri1(metonicYear, day, halakim); + + if inputDay >= tishri1: + # It found Tishri 1 at the start of the year + + pYear = (metonicCycle * 19) + metonicYear + 1 + if inputDay < tishri1 + 59: + if inputDay < tishri1 + 30: + pMonth = 1 + pDay = inputDay - tishri1 + 1 + else: + pMonth = 2 + pDay = inputDay - tishri1 - 29 + return (pYear, pMonth, pDay) + + # We need the length of the year to figure this out, so find + # Tishri 1 of the next year. */ + + halakim = halakim + (_HALAKIM_PER_LUNAR_CYCLE * monthsPerYear[metonicYear]) + day = day + (halakim / _HALAKIM_PER_DAY) + halakim = halakim % _HALAKIM_PER_DAY; + tishri1After = Tishri1((metonicYear + 1) % 19, day, halakim); + else: + # It found Tishri 1 at the end of the year. + + pYear = metonicCycle * 19 + metonicYear + if inputDay >= tishri1 - 177: + # It is one of the last 6 months of the year. + if inputDay > tishri1 - 30: + pMonth = 13 + pDay = inputDay - tishri1 + 30 + elif inputDay > tishri1 - 60: + pMonth = 12 + pDay = inputDay - tishri1 + 60 + elif inputDay > tishri1 - 89: + pMonth = 11 + pDay = inputDay - tishri1 + 89 + elif inputDay > tishri1 - 119: + pMonth = 10 + pDay = inputDay - tishri1 + 119 + elif inputDay > tishri1 - 148: + pMonth = 9 + pDay = inputDay - tishri1 + 148 + else: + pMonth = 8 + pDay = inputDay - tishri1 + 178 + return (pYear,pMonth,pDay) + else: + if monthsPerYear[(pYear - 1) % 19] == 13: + pMonth = 7 + pDay = inputDay - tishri1 + 207 + if pDay > 0: + return (pYear,pMonth,pDay) + pMonth = pMonth - 1 + pDay = pDay + 30 + if pDay > 0: + return (pYear,pMonth,pDay) + pMonth = pMonth - 1 + pDay = pDay + 30 + else: + pMonth = 6 + pDay = inputDay - tishri1 + 207 + if pDay > 0: + return (pYear,pMonth,pDay) + pMonth = pMonth - 1 + pDay = pDay + 30 + + if pDay > 0: + return (pYear,pMonth,pDay) + pMonth = pMonth - 1 + pDay = pDay + 29 + if pDay > 0: + return (pYear,pMonth,pDay) + + # We need the length of the year to figure this out, so find + # Tishri 1 of this year. */ + tishri1After = tishri1; + (metonicCycle,metonicYear,day,halakim) = FindTishriMolad(day-365) + tishri1 = Tishri1(metonicYear, day, halakim) + + yearLength = tishri1After - tishri1; + day = inputDay - tishri1 - 29; + if yearLength == 355 or yearLength == 385 : + # Heshvan has 30 days + if day <= 30: + pMonth = 2 + pDay = day + return (pYear,pMonth,pDay) + day = day - 30 + else: + # Heshvan has 29 days + if day <= 29: + pMonth = 2 + pDay = day + return (pYear,pMonth,pDay) + + day = day - 29 + + # It has to be Kislev + return (pYear,3,day) + + +def jewish_to_sdn(year, month, day): + if year <= 0 or day <= 0 or day > 30 : + return 0 + + if month == 1 or month == 2: + # It is Tishri or Heshvan - don't need the year length. + (metonicCycle,metonicYear,moladDay,moladHalakim,tishri1) = FindStartOfYear(year) + if month == 1: + sdn = tishri1 + day - 1 + else: + sdn = tishri1 + day + 29 + elif month == 3: + # It is Kislev - must find the year length. + + # Find the start of the year. + (metonicCycle,metonicYear,moladDay,moladHalakim,tishri1) = FindStartOfYear(year) + + # Find the end of the year. + moladHalakim = moladHalakim + (_HALAKIM_PER_LUNAR_CYCLE*monthsPerYear[metonicYear]) + moladDay = moladDay + (moladHalakim / _HALAKIM_PER_DAY) + moladHalakim = moladHalakim % _HALAKIM_PER_DAY + tishri1After = Tishri1((metonicYear + 1) % 19, moladDay, moladHalakim) + + yearLength = tishri1After - tishri1 + + if yearLength == 355 or yearLength == 385: + sdn = tishri1 + day + 59 + else: + sdn = tishri1 + day + 58 + elif month == 4 or month == 5 or month == 6: + # It is Tevet, Shevat or Adar I - don't need the year length + + (metonicCycle,metonicYear,moladDay,moladHalakim,tishri1After) = FindStartOfYear(year+1) + + if monthsPerYear[(year - 1) % 19] == 12: + lengthOfAdarIAndII = 29 + else: + lengthOfAdarIAndII = 59 + + if month == 4: + sdn = tishri1After + day - lengthOfAdarIAndII - 237 + elif month == 5: + sdn = tishri1After + day - lengthOfAdarIAndII - 208 + else: + sdn = tishri1After + day - lengthOfAdarIAndII - 178 + else: + # It is Adar II or later - don't need the year length. + (metonicCycle,metonicYear,moladDay,moladHalakim,tishri1After) = FindStartOfYear(year+1) + + if month == 7: + sdn = tishri1After + day - 207 + elif month == 8: + sdn = tishri1After + day - 178 + elif month == 9: + sdn = tishri1After + day - 148 + elif month == 10: + sdn = tishri1After + day - 119 + elif month == 11: + sdn = tishri1After + day - 89 + elif month == 12: + sdn = tishri1After + day - 60 + elif month == 13: + sdn = tishri1After + day - 30 + else: + return 0 + return sdn + _H_SDN_OFFSET diff --git a/gramps/src/Date.py b/gramps/src/Date.py index 2592eebf6..a61dd6235 100644 --- a/gramps/src/Date.py +++ b/gramps/src/Date.py @@ -37,12 +37,11 @@ JULIAN = 1 HEBREW = 2 FRENCH = 3 -_index2cal = [ _("Gregorian"), _("Julian"), _("Hebrew"), _("French Republican") ] -_cal2index = { _("Gregorian") : 0, - _("Julian") : 1, - _("Hebrew") : 2, - _("French Republican"): 3 } - +#------------------------------------------------------------------------- +# +# Month mappings +# +#------------------------------------------------------------------------- _fmonth = [ "Vendemiaire", "Brumaire", "Frimaire", "Nivose", "Pluviose", "Ventose", "Germinal", "Floreal", "Prairial", "Messidor", "Thermidor", @@ -58,6 +57,15 @@ _hmonth = [ "AdarII", "Nisan", "Iyyar", "Sivan", "Tammuz", "Av", "Elul" ] +_hmonth2num = { + "Tishri" : 1, "Heshvan" : 2, "Kislev" : 3, "Tevet" : 4, + "Shevat" : 5, "AdarI" : 6, "AdarII" : 7, "Nisan" : 8, + "Iyyar" : 9, "Sivan" :10, "Tammuz" :11, "Av" : 12, + "Elul" : 13 + } + +_UNDEF = -999999 + #------------------------------------------------------------------------- # # Date class @@ -75,9 +83,6 @@ class Date: from_str = _("(from|between|bet|bet.)") to_str = _("(and|to|-)") - efmt = compile(r"\s*(from|between|bet)\s+(.+)\s+(and|to)\s+(.+)\s*$", - IGNORECASE) - fmt = compile(r"\s*" + from_str + r"\s+(.+)\s+" + to_str + r"\s+(.+)\s*$", IGNORECASE) @@ -117,40 +122,40 @@ class Date: return self.stop def getYear(self): - return self.get_start_date().getYear() + return self.start.year def getHighYear(self): if self.stop == None: - return self.start.getYear() + return self.start.year else: - return self.stop.getYear() + return self.stop.year def getLowYear(self): return self.start.getYear() def getMonth(self): - return self.get_start_date().getMonth() + return self.start.month+1 def getDay(self): - return self.get_start_date().getDay() + return self.start.day def getStopYear(self): if self.stop == None: self.stop = SingleDate() self.stop.calendar = self.calendar - return self.get_stop_date().getYear() + return self.stop.year def getStopMonth(self): if self.stop == None: self.stop = SingleDate() self.stop.calendar = self.calendar - return self.get_stop_date().getMonth() + return self.stop.month+1 def getStopDay(self): if self.stop == None: self.stop = SingleDate() self.stop.calendar = self.calendar - return self.get_stop_date().getDay() + return self.stop.day def getText(self): return self.text @@ -185,45 +190,27 @@ class Date: def set_range(self,val): self.range = val - def getDate(self): + def get_fmt(self,func): if self.range == 0: - return self.start.getDate() + return func(self.start) elif self.range == -1: return self.text else: - d1 = self.start.getDate() - d2 = self.stop.getDate() + d1 = func(self.start) + d2 = func(self.stop) return "%s %s %s %s" % ( _("from"),d1,_("to"),d2 ) + + def getDate(self): + return self.get_fmt(SingleDate.getDate) def getFrench(self): - if self.range == 0: - return self.start.getFrench() - elif self.range == -1: - return self.text - else: - d1 = self.start.displayFrench(self.start) - d2 = self.start.displayFrench(self.stop) - return "%s %s %s %s" % ( _("from"),d1,_("to"),d2 ) + return self.get_fmt(SingleDate.displayFrench) def getHebrew(self): - if self.range == 0: - return self.start.displayHebrew() - elif self.range == -1: - return self.text - else: - d1 = self.start.displayHebrew(self.start) - d2 = self.start.displayHebrew(self.stop) - return "%s %s %s %s" % ( _("from"),d1,_("to"),d2 ) + return self.get_fmt(SingleDate.displayHebrew) def getJulian(self): - if self.range == 0: - return self.start.displayJulian() - elif self.range == -1: - return self.text - else: - d1 = self.start.displayJulian(self.start) - d2 = self.start.displayJulian(self.stop) - return "%s %s %s %s" % ( _("from"),d1,_("to"),d2 ) + return self.get_fmt(SingleDate.displayJulian) def getQuoteDate(self): if self.calendar == GREGORIAN: @@ -248,44 +235,27 @@ class Date: d2 = _func(self.stop) return "%s %s %s %s" % ( _("from"),d1,_("to"), d2) - def getFrenchQuoteDate(self): + def get_quote_date(self,func,cal_str): if self.range == 0: - return "%s (%s)" % (self.start.displayFrench(),_("French")) + return "%s (%s)" % (func(self.start),cal_str) elif self.range == -1: if self.text: - return '"%s (%s)"' % (self.text,_("French")) + return '"%s (%s)"' % (self.text,cal_str) else: - return '%s' % _("French") + return '' else: - d1 = self.start.getFrench() - d2 = self.stop.getFrench() - return "%s %s %s %s (%s)" % ( _("from"),d1,_("to"), d2,_("French")) + d1 = func(self.start) + d2 = func(self.stop) + return "%s %s %s %s (%s)" % ( _("from"),d1,_("to"), d2,cal_str) + + def getFrenchQuoteDate(self): + return self.get_quote_date(SingleDate.displayFrench,_("French")) def getJulianQuoteDate(self): - if self.range == 0: - return "%s (%s)" % (self.start.displayJulian(),_("Julian")) - elif self.range == -1: - if self.text: - return '"%s (%s)"' % (self.text,_("Julian")) - else: - return '%s' % _("Julian") - else: - d1 = self.start.getJulian() - d2 = self.stop.getJulian() - return "%s %s %s %s (%s)" % ( _("from"),d1,_("to"), d2,_("Julian")) + return self.get_quote_date(SingleDate.displayJulian,_("Julian")) def getHebrewQuoteDate(self): - if self.range == 0: - return "%s (%s)" % (self.start.displayHebrew(),_("Hebrew")) - elif self.range == -1: - if self.text: - return '"%s (%s)"' % (self.text,_("Hebrew")) - else: - return '%s' % _("Hebrew") - else: - d1 = self.start.getHebrew() - d2 = self.stop.getHebrew() - return "%s %s %s %s (%s)" % ( _("from"),d1,_("to"), d2,_("Hebrew")) + return self.get_quote_date(SingleDate.displayHebrew,_("Hebrew")) def getSaveDate(self): if self.range == 1: @@ -298,22 +268,14 @@ class Date: return self.start.getSaveDate() def isEmpty(self): - if self.start.year == -1 and self.start.month == -1 and self.start.day == -1: - return 1 - else: - return 0 + s = self.start + return s.year==_UNDEF and s.month==_UNDEF and s.day==_UNDEF def isValid(self): - if self.range == -1: - return 0 - else: - return 1 + return self.range != -1 def isRange(self): - if self.range == 1: - return 1 - else: - return 0 + return self.range == 1 #------------------------------------------------------------------------- # @@ -346,18 +308,9 @@ class SingleDate: before = 2 after = 3 - mname = [ _("January"), - _("February"), - _("March"), - _("April"), - _("May"), - _("June"), - _("July"), - _("August"), - _("September"), - _("October"), - _("November"), - _("December") ] + mname = [ _("January"), _("February"), _("March"), _("April"), + _("May"), _("June"), _("July"), _("August"), + _("September"), _("October"), _("November"), _("December") ] emname =[ 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC' ] @@ -431,9 +384,9 @@ class SingleDate: self.mode = source.mode self.calendar = source.calendar else: - self.month = -1 - self.day = -1 - self.year = -1 + self.month = _UNDEF + self.day = _UNDEF + self.year = _UNDEF self.mode = SingleDate.exact self.calendar = GREGORIAN @@ -446,7 +399,7 @@ class SingleDate: def setMonth(self,val): if val > 12: - self.month = -1 + self.month = _UNDEF else: self.month = val - 1 @@ -475,24 +428,24 @@ class SingleDate: try: self.month = SingleDate.em2num[string.lower(text[0:3])] except KeyError: - self.month = -1 + self.month = _UNDEF def getMonthStr(self): return SingleDate.mname[self.month] def getIsoDate(self): - if self.year == -1: + if self.year == _UNDEF: y = "?" else: y = "%04d" % self.year - if self.month == -1: - if self.day == -1: + if self.month == _UNDEF: + if self.day == _UNDEF: m = "" else: m = "-?" else: m = "-%02d" % (self.month+1) - if self.day == -1: + if self.day == _UNDEF: d = '' else: d = "-%02d" % self.day @@ -501,20 +454,20 @@ class SingleDate: def getSaveDate(self): retval = "" - if self.month == -1 and self.day == -1 and self.year == -1 : + if self.month == _UNDEF and self.day == _UNDEF and self.year == _UNDEF : pass - elif self.day == -1: - if self.month == -1: + elif self.day == _UNDEF: + if self.month == _UNDEF: retval = str(self.year) - elif self.year == -1: + elif self.year == _UNDEF: retval = SingleDate.emname[self.month] else: retval = "%s %d" % (SingleDate.emname[self.month],self.year) - elif self.month == -1: + elif self.month == _UNDEF: retval = str(self.year) else: month = SingleDate.emname[self.month] - if self.year == -1: + if self.year == _UNDEF: retval = "%d %s ????" % (self.day,month) else: retval = "%d %s %d" % (self.day,month,self.year) @@ -531,20 +484,20 @@ class SingleDate: def getFmt1(self): - if self.month == -1 and self.day == -1 and self.year == -1 : + if self.month == _UNDEF and self.day == _UNDEF and self.year == _UNDEF : return "" - elif self.day == -1: - if self.month == -1: + elif self.day == _UNDEF: + if self.month == _UNDEF: retval = str(self.year) - elif self.year == -1: + elif self.year == _UNDEF: retval = SingleDate.mname[self.month] else: retval = "%s %d" % (SingleDate.mname[self.month],self.year) - elif self.month == -1: + elif self.month == _UNDEF: retval = str(self.year) else: month = SingleDate.mname[self.month] - if self.year == -1: + if self.year == _UNDEF: retval = "%s %d, ????" % (month,self.day) else: retval = "%s %d, %d" % (month,self.day,self.year) @@ -560,18 +513,18 @@ class SingleDate: return retval def getFmt2(self): - if self.month == -1 and self.day == -1 and self.year == -1 : + if self.month == _UNDEF and self.day == _UNDEF and self.year == _UNDEF : return "" - elif self.month != -1 and self.month != -1: + elif self.month != _UNDEF and self.month != _UNDEF: month = SingleDate.mname[self.month] - if self.year == -1: + if self.year == _UNDEF: retval = "%s %d, ????" % (string.upper(month[0:3]),self.day) else: retval = "%s %d, %d" % (string.upper(month[0:3]),self.day,self.year) - elif self.day == -1: - if self.month == -1: + elif self.day == _UNDEF: + if self.month == _UNDEF: retval = str(self.year) - elif self.year == -1: + elif self.year == _UNDEF: month = SingleDate.mname[self.month] retval = string.upper(month[0:3]) else: @@ -592,22 +545,22 @@ class SingleDate: def getFmt3(self): retval = "" - if self.month == -1 and self.day == -1 and self.year == -1 : + if self.month == _UNDEF and self.day == _UNDEF and self.year == _UNDEF : pass - elif self.day == -1: - if self.month == -1: + elif self.day == _UNDEF: + if self.month == _UNDEF: retval = str(self.year) - elif self.year == -1: + elif self.year == _UNDEF: month = SingleDate.mname[self.month] retval = string.upper(month[0:3]) else: month = SingleDate.mname[self.month] retval = "%s %d" % (string.upper(month[0:3]),self.year) - elif self.month == -1: + elif self.month == _UNDEF: retval = str(self.year) else: month = SingleDate.mname[self.month] - if self.year == -1: + if self.year == _UNDEF: retval = "%d %s ????" % (self.day,string.upper(month[0:3])) else: retval = "%d %s %d" % (self.day,string.upper(month[0:3]),self.year) @@ -624,21 +577,21 @@ class SingleDate: def getFmt10(self): retval = "" - if self.month == -1 and self.day == -1 and self.year == -1 : + if self.month == _UNDEF and self.day == _UNDEF and self.year == _UNDEF : pass - elif self.day == -1: - if self.month == -1: + elif self.day == _UNDEF: + if self.month == _UNDEF: retval = str(self.year) - elif self.year == -1: + elif self.year == _UNDEF: retval = SingleDate.mname[self.month] else: month = SingleDate.mname[self.month] retval = "%s %d" % (month,self.year) - elif self.month == -1: + elif self.month == _UNDEF: retval = str(self.year) else: month = SingleDate.mname[self.month] - if self.year == -1: + if self.year == _UNDEF: retval = "%d. %s ????" % (self.day,month) else: retval = "%d. %s %d" % (self.day,month,self.year) @@ -655,19 +608,19 @@ class SingleDate: def get_mmddyyyy(self,sep): retval = "" - if self.month == -1 and self.day == -1 and self.year == -1 : + if self.month == _UNDEF and self.day == _UNDEF and self.year == _UNDEF : pass - elif self.day == -1: - if self.month == -1: + elif self.day == _UNDEF: + if self.month == _UNDEF: retval = str(self.year) - elif self.year == -1: + elif self.year == _UNDEF: retval = "%02d%s??%s??" % (self.month+1,sep,sep) else: retval = "%02d%s??%s%04d" % (self.month+1,sep,sep,self.year) - elif self.month == -1: + elif self.month == _UNDEF: retval = "??%s%02d%s%04d" % (sep,self.day,sep,self.year) else: - if self.year == -1: + if self.year == _UNDEF: retval = "%02d%s%02d%s????" % (self.month+1,sep,self.day,sep) else: retval = "%02d%s%02d%s%04d" % (self.month+1,sep,self.day,sep,self.year) @@ -685,19 +638,19 @@ class SingleDate: def get_yyyymmdd(self,sep): retval = "" - if self.month == -1 and self.day == -1 and self.year == -1 : + if self.month == _UNDEF and self.day == _UNDEF and self.year == _UNDEF : pass - elif self.day == -1: - if self.month == -1: + elif self.day == _UNDEF: + if self.month == _UNDEF: retval = str(self.year) - elif self.year == -1: + elif self.year == _UNDEF: retval = "????%s%02d%s??" % (sep,self.month+1,sep) else: retval = "%04d%s%02d" % (self.year,sep,self.month+1) - elif self.month == -1: + elif self.month == _UNDEF: retval = "%04d%s??%s%02d" % (self.year,sep,sep,self.day) else: - if self.year == -1: + if self.year == _UNDEF: retval = "????%02d%s%02d%s" % (self.month+1,sep,self.day,sep) else: retval = "%02d%s%02d%s%02d" % (self.year,sep,self.month+1,sep,self.day) @@ -724,19 +677,19 @@ class SingleDate: def get_ddmmyyyy(self,sep): retval = "" - if self.month == -1 and self.day == -1 and self.year == -1 : + if self.month == _UNDEF and self.day == _UNDEF and self.year == _UNDEF : pass - elif self.day == -1: - if self.month == -1: + elif self.day == _UNDEF: + if self.month == _UNDEF: retval = str(self.year) - elif self.year == -1: + elif self.year == _UNDEF: retval = "??%s%02d%s??" % (sep,self.month+1,sep) else: retval = "??%s%02d%s%04d" % (sep,self.month+1,sep,self.year) - elif self.month == -1: + elif self.month == _UNDEF: retval = "%02d%s??%s%04d" % (self.day,sep,sep,self.year) else: - if self.year == -1: + if self.year == _UNDEF: retval = "%02d%s%02d%s????" % (self.day,sep,self.month+1,sep) else: retval = "%02d%s%02d%s%04d" % (self.day,sep,self.month+1,sep,self.year) @@ -778,46 +731,46 @@ class SingleDate: getFmt13] def displayFrench(self): - if self.year==-1: - if self.month == -1: + if self.year==_UNDEF: + if self.month == _UNDEF: return "" - elif self.day == -1: + elif self.day == _UNDEF: return _fmonth[self.month] else: return "%02 %s" % (self.day,_fmonth[self.month]) - elif self.month == -1: + elif self.month == _UNDEF: return "%d" % self.year - elif self.day == -1: + elif self.day == _UNDEF: return "%s %d" % (_fmonth[self.month],self.year) else: return "%02d %s %d" % (self.day,_fmonth[self.month],self.year) def displayHebrew(self): - if self.year==-1: - if self.month == -1: + if self.year==_UNDEF: + if self.month == _UNDEF: return "" - elif self.day == -1: + elif self.day == _UNDEF: return _hmonth[self.month] else: return "%02 %s" % (self.day,_hmonth[self.month]) - elif self.month == -1: + elif self.month == _UNDEF: return "%d" % self.year - elif self.day == -1: + elif self.day == _UNDEF: return "%s %d" % (_hmonth[self.month],self.year) else: return "%02d %s %d" % (self.day,_hmonth[self.month],self.year) def displayJulian(self): - if self.year==-1: - if self.month == -1: + if self.year==_UNDEF: + if self.month == _UNDEF: return "" - elif self.day == -1: + elif self.day == _UNDEF: return self.mname[self.month] else: return "%02 %s" % (self.day,self.mname[self.month]) - elif self.month == -1: + elif self.month == _UNDEF: return "%d" % self.year - elif self.day == -1: + elif self.day == _UNDEF: return "%s %d" % (self.mname[self.month],self.year) else: return "%02d %s %d" % (self.day,self.mname[self.month],self.year) @@ -840,17 +793,17 @@ class SingleDate: vals = string.split(v,'-') if vals[0] == '?': - self.year = -1 + self.year = _UNDEF else: self.year = int(vals[0]) if len(vals) > 1 and vals[1] != '?': self.month = int(vals[1])-1 else: - self.month = -1 + self.month = _UNDEF if len(vals) > 2: self.day = int(vals[2]) else: - self.day = -1 + self.day = _UNDEF def getModeVal(self): return self.mode @@ -892,8 +845,8 @@ class SingleDate: return else: self.setYear(int(matches[3])) - self.setMonth(-1) - self.setDay(-1) + self.setMonth(_UNDEF) + self.setDay(_UNDEF) return match = SingleDate.fmt3.match(text) if match: @@ -902,9 +855,9 @@ class SingleDate: self.setMonth(int(matches[2])) self.setDay(int(matches[1])) else: - self.setYear(-1) - self.setMonth(-1) - self.setDay(-1) + self.setYear(_UNDEF) + self.setMonth(_UNDEF) + self.setDay(_UNDEF) def set_hebrew(self,text): pass @@ -921,8 +874,8 @@ class SingleDate: return else: self.setYear(int(matches[3])) - self.setMonth(-1) - self.setDay(-1) + self.setMonth(_UNDEF) + self.setDay(_UNDEF) return match = SingleDate.fmt3.match(text) if match: @@ -931,9 +884,9 @@ class SingleDate: self.setMonth(int(matches[2])) self.setDay(int(matches[1])) else: - self.setYear(-1) - self.setMonth(-1) - self.setDay(-1) + self.setYear(_UNDEF) + self.setMonth(_UNDEF) + self.setDay(_UNDEF) def set_gregorian(self,text): match = SingleDate.fmt2.match(text) @@ -941,25 +894,25 @@ class SingleDate: matches = match.groups() self.getMode(matches[0]) self.setMonthStr(matches[2]) - if self.month == -1: + if self.month == _UNDEF: raise Date.Error,text self.day = int(matches[1]) if len(matches) == 4: val = matches[3] if val == None or val[0] == '?': - self.year = -1 + self.year = _UNDEF else: self.year = int(val) else: - self.year = -1 + self.year = _UNDEF return 1 match = SingleDate.fmt5.match(text) if match != None: matches = match.groups() self.getMode(matches[0]) - self.month = -1 - self.day = -1 + self.month = _UNDEF + self.day = _UNDEF self.year = int(matches[1]) return 1 @@ -973,11 +926,11 @@ class SingleDate: if self.month > 11: raise Date.Error,text except ValueError: - self.month = -1 + self.month = _UNDEF try: self.year = int(matches[1]) except ValueError: - self.year = -1 + self.year = _UNDEF return 1 else: try: @@ -985,11 +938,11 @@ class SingleDate: if self.month > 11: raise Date.Error,text except ValueError: - self.month = -1 + self.month = _UNDEF try: self.year = int(matches[2]) except ValueError: - self.year = -1 + self.year = _UNDEF return 1 match = SingleDate.fmt3.match(text) @@ -1002,14 +955,14 @@ class SingleDate: if self.month > 11: raise Date.Error,text except ValueError: - self.month = -1 + self.month = _UNDEF try: self.day = int(matches[2]) except ValueError: - self.day = -1 + self.day = _UNDEF val = matches[3] if val == None or val[0] == '?': - self.year = -1 + self.year = _UNDEF else: self.year = int(val) elif Date.entryCode == 1: @@ -1018,14 +971,14 @@ class SingleDate: if self.month > 11: raise Date.Error,text except ValueError: - self.month = -1 + self.month = _UNDEF try: self.day = int(matches[1]) except ValueError: - self.day = -1 + self.day = _UNDEF val = matches[3] if val == None or val[0] == '?': - self.year = -1 + self.year = _UNDEF else: self.year = int(val) else: @@ -1034,14 +987,14 @@ class SingleDate: if self.month > 11: raise Date.Error,text except ValueError: - self.month = -1 + self.month = _UNDEF try: self.day = int(matches[3]) except ValueError: - self.day = -1 + self.day = _UNDEF val = matches[1] if val == None or val[0] == '?': - self.year = -1 + self.year = _UNDEF else: self.year = int(val) return 1 @@ -1051,16 +1004,16 @@ class SingleDate: matches = match.groups() self.getMode(matches[0]) self.setMonthStr(matches[1]) - if self.month == -1: + if self.month == _UNDEF: raise Date.Error,text val = matches[2] if val: self.day = int(string.replace(val,',','')) else: - self.day = -1 + self.day = _UNDEF val = matches[3] if val == None or val[0] == '?': - self.year = -1 + self.year = _UNDEF else: self.year = int(val) return 1 @@ -1070,13 +1023,13 @@ class SingleDate: matches = match.groups() self.getMode(matches[0]) self.setMonthStr(matches[1]) - if self.month == -1: + if self.month == _UNDEF: raise Date.Error,text - self.day = -1 + self.day = _UNDEF if len(matches) == 4: val = matches[3] if val == None or val[0] == '?' : - self.year = -1 + self.year = _UNDEF else: self.year = int(val) return 1 @@ -1088,20 +1041,20 @@ class SingleDate: self.month = int(matches[1])-1 if self.month > 11: raise Date.Error,text - self.day = -1 - self.year = -1 + self.day = _UNDEF + self.year = _UNDEF return 1 raise Date.Error,text def get_sdn(self): - if self.year == -1: + if self.year == _UNDEF: return 0 - if self.month == -1: + if self.month == _UNDEF: month = 1 else: month = self.month + 1 - if self.day == -1: + if self.day == _UNDEF: day = 1 else: day = self.day @@ -1131,9 +1084,9 @@ class SingleDate: (y,m,d) = sdn_to_french(sdn) self.calendar = FRENCH if y == 0 and m == 0 and d == 0: - self.year = -1 - self.month = -1 - self.day = -1 + self.year = _UNDEF + self.month = _UNDEF + self.day = _UNDEF else: self.year = y self.month = m-1 @@ -1144,9 +1097,9 @@ class SingleDate: (y,m,d) = sdn_to_jewish(sdn) self.calendar = HEBREW if y == 0 and m == 0 and d == 0: - self.year = -1 - self.month = -1 - self.day = -1 + self.year = _UNDEF + self.month = _UNDEF + self.day = _UNDEF else: self.year = y self.month = m-1 @@ -1157,9 +1110,9 @@ class SingleDate: self.calendar = JULIAN (y,m,d) = sdn_to_julian(sdn) if y == 0 and m == 0 and d == 0: - self.year = -1 - self.month = -1 - self.day = -1 + self.year = _UNDEF + self.month = _UNDEF + self.day = _UNDEF else: self.year = y self.month = m-1 @@ -1170,9 +1123,9 @@ class SingleDate: self.calendar = GREGORIAN (y,m,d) = sdn_to_gregorian(sdn) if y == 0 and m == 0 and d == 0: - self.year = -1 - self.month = -1 - self.day = -1 + self.year = _UNDEF + self.month = _UNDEF + self.day = _UNDEF else: self.year = y self.month = m-1