2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000 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
|
|
|
|
#
|
|
|
|
|
|
|
|
"Support for dates"
|
|
|
|
|
|
|
|
__author__ = "Donald N. Allingham"
|
|
|
|
__version__ = "$Revision$"
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
from re import IGNORECASE, compile
|
|
|
|
import string
|
|
|
|
import time
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import Calendar
|
2003-01-09 10:11:08 +05:30
|
|
|
import Gregorian
|
|
|
|
import Julian
|
|
|
|
import Hebrew
|
|
|
|
import FrenchRepublic
|
2003-05-23 08:15:44 +05:30
|
|
|
import Errors
|
2003-01-09 10:11:08 +05:30
|
|
|
|
2003-08-17 07:44:33 +05:30
|
|
|
from gettext import gettext as _
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2003-01-02 10:01:52 +05:30
|
|
|
# Constants
|
2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2003-01-02 10:01:52 +05:30
|
|
|
UNDEF = -999999
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2003-01-02 10:01:52 +05:30
|
|
|
_calendar_val = [
|
2003-01-09 10:11:08 +05:30
|
|
|
Gregorian.Gregorian,
|
|
|
|
Julian.Julian,
|
|
|
|
Hebrew.Hebrew,
|
|
|
|
FrenchRepublic.FrenchRepublic,
|
2002-10-20 19:55:16 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Date class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class Date:
|
|
|
|
"""
|
|
|
|
The core date handling class for GRAMPs. Supports partial dates,
|
|
|
|
date ranges, and alternate calendars.
|
|
|
|
"""
|
|
|
|
formatCode = 0
|
|
|
|
|
|
|
|
fstr = _("(from|between|bet|bet.)")
|
|
|
|
tstr = _("(and|to|-)")
|
|
|
|
|
|
|
|
fmt = compile("\s*%s\s+(.+)\s+%s\s+(.+)\s*$" % (fstr,tstr),IGNORECASE)
|
2003-06-11 07:05:04 +05:30
|
|
|
fmt1 = compile("\s*([^-]+)\s*-\s*([^-]+)\s*$",IGNORECASE)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def __init__(self,source=None):
|
|
|
|
if source:
|
|
|
|
self.start = SingleDate(source.start)
|
|
|
|
if source.stop:
|
|
|
|
self.stop = SingleDate(source.stop)
|
|
|
|
else:
|
|
|
|
self.stop = None
|
|
|
|
self.range = source.range
|
|
|
|
self.text = source.text
|
|
|
|
self.calendar = source.calendar
|
|
|
|
else:
|
|
|
|
self.start = SingleDate()
|
|
|
|
self.stop = None
|
|
|
|
self.range = 0
|
|
|
|
self.text = ""
|
2003-01-09 10:11:08 +05:30
|
|
|
self.calendar = Gregorian.Gregorian()
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def get_calendar(self):
|
|
|
|
return self.calendar
|
|
|
|
|
|
|
|
def set_calendar(self,val):
|
2003-01-02 10:01:52 +05:30
|
|
|
self.calendar = val()
|
|
|
|
self.start.convert_to(val)
|
|
|
|
if self.stop:
|
|
|
|
self.stop.convert_to(val)
|
|
|
|
|
|
|
|
def set_calendar_obj(self,val):
|
2002-10-20 19:55:16 +05:30
|
|
|
self.calendar = val
|
2003-01-02 10:01:52 +05:30
|
|
|
self.start.convert_to_obj(val)
|
|
|
|
if self.stop:
|
|
|
|
self.stop.convert_to_obj(val)
|
|
|
|
|
|
|
|
def set_calendar_val(self,integer):
|
|
|
|
val = _calendar_val[integer]
|
|
|
|
self.calendar = val()
|
2002-10-20 19:55:16 +05:30
|
|
|
self.start.convert_to(val)
|
|
|
|
if self.stop:
|
|
|
|
self.stop.convert_to(val)
|
|
|
|
|
|
|
|
def get_start_date(self):
|
|
|
|
return self.start
|
|
|
|
|
|
|
|
def get_stop_date(self):
|
|
|
|
if self.stop == None:
|
|
|
|
self.stop = SingleDate()
|
|
|
|
self.stop.calendar = self.calendar
|
|
|
|
return self.stop
|
|
|
|
|
|
|
|
def getLowYear(self):
|
|
|
|
return self.start.getYear()
|
|
|
|
|
|
|
|
def getHighYear(self):
|
|
|
|
if self.stop == None:
|
|
|
|
return self.start.getYear()
|
|
|
|
else:
|
|
|
|
return self.stop.getYear()
|
|
|
|
|
|
|
|
def getYear(self):
|
|
|
|
return self.start.year
|
|
|
|
|
|
|
|
def getYearValid(self):
|
|
|
|
return self.start.year != UNDEF
|
|
|
|
|
|
|
|
def getMonth(self):
|
|
|
|
if self.start.month == UNDEF:
|
|
|
|
return UNDEF
|
2003-01-20 18:58:33 +05:30
|
|
|
return self.start.month
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def getMonthValid(self):
|
|
|
|
return self.start.month != UNDEF
|
|
|
|
|
|
|
|
def getDay(self):
|
|
|
|
return self.start.day
|
|
|
|
|
|
|
|
def getDayValid(self):
|
|
|
|
return self.start.day != UNDEF
|
|
|
|
|
|
|
|
def getValid(self):
|
|
|
|
""" Returns true if any part of the date is valid"""
|
|
|
|
return self.start.year != UNDEF or self.start.month != UNDEF or self.start.day != UNDEF
|
|
|
|
|
|
|
|
def getIncomplete(self):
|
|
|
|
return self.range == 0 and self.start.year == UNDEF or \
|
|
|
|
self.start.month == UNDEF or self.start.day == UNDEF
|
|
|
|
|
|
|
|
def getStopYear(self):
|
|
|
|
if self.stop == None:
|
|
|
|
self.stop = SingleDate()
|
|
|
|
self.stop.calendar = self.calendar
|
|
|
|
return self.stop.year
|
|
|
|
|
|
|
|
def getStopMonth(self):
|
|
|
|
if self.stop == None:
|
|
|
|
self.stop = SingleDate()
|
|
|
|
self.stop.calendar = self.calendar
|
|
|
|
return self.stop.month+1
|
|
|
|
|
|
|
|
def getStopDay(self):
|
|
|
|
if self.stop == None:
|
|
|
|
self.stop = SingleDate()
|
|
|
|
self.stop.calendar = self.calendar
|
|
|
|
return self.stop.day
|
|
|
|
|
|
|
|
def getText(self):
|
|
|
|
return self.text
|
|
|
|
|
|
|
|
def greater_than(self,other):
|
|
|
|
return compare_dates(self,other) > 0
|
|
|
|
|
|
|
|
def less_than(self,other):
|
|
|
|
return compare_dates(self,other) < 0
|
|
|
|
|
|
|
|
def equal_to(self,other):
|
|
|
|
return compare_dates(self,other) == 0
|
|
|
|
|
|
|
|
def set(self,text):
|
2003-09-21 01:25:45 +05:30
|
|
|
if text.strip() == "":
|
|
|
|
self.start = SingleDate()
|
|
|
|
self.stop = None
|
|
|
|
self.range = 0
|
|
|
|
self.text = ""
|
|
|
|
self.calendar = Gregorian.Gregorian()
|
|
|
|
return
|
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
try:
|
2003-06-11 07:05:04 +05:30
|
|
|
match = Date.fmt.match(text)
|
2002-10-20 19:55:16 +05:30
|
|
|
if match:
|
|
|
|
matches = match.groups()
|
|
|
|
self.start.set(matches[1])
|
2003-06-25 16:25:15 +05:30
|
|
|
self.range = 0
|
2002-10-20 19:55:16 +05:30
|
|
|
if self.stop == None:
|
|
|
|
self.stop = SingleDate()
|
|
|
|
self.stop.calendar = self.calendar
|
|
|
|
self.stop.set(matches[3])
|
|
|
|
self.range = 1
|
2003-06-11 07:05:04 +05:30
|
|
|
return
|
|
|
|
|
|
|
|
match = Date.fmt1.match(text)
|
|
|
|
if match:
|
|
|
|
matches = match.groups()
|
|
|
|
self.start.set(matches[0])
|
2003-06-25 16:25:15 +05:30
|
|
|
self.range = 0
|
2003-06-11 07:05:04 +05:30
|
|
|
if self.stop == None:
|
|
|
|
self.stop = SingleDate()
|
|
|
|
self.stop.calendar = self.calendar
|
|
|
|
self.stop.set(matches[1])
|
|
|
|
self.range = 1
|
|
|
|
return
|
|
|
|
|
|
|
|
self.start.set(text)
|
|
|
|
self.range = 0
|
2003-05-23 08:15:44 +05:30
|
|
|
except Errors.DateError:
|
2002-10-20 19:55:16 +05:30
|
|
|
if text != "":
|
|
|
|
self.range = -1
|
|
|
|
self.text = text
|
|
|
|
|
|
|
|
def set_text(self,text):
|
|
|
|
self.range = -1
|
|
|
|
self.text = text
|
|
|
|
|
|
|
|
def set_range(self,val):
|
|
|
|
self.range = val
|
|
|
|
|
|
|
|
def getDate(self):
|
|
|
|
if self.range == 0:
|
2003-01-02 10:01:52 +05:30
|
|
|
return self.start.getDate()
|
2002-10-20 19:55:16 +05:30
|
|
|
elif self.range == -1:
|
2003-01-19 11:55:20 +05:30
|
|
|
return self.text
|
2002-10-20 19:55:16 +05:30
|
|
|
else:
|
2002-12-29 11:14:35 +05:30
|
|
|
return _("from %(start_date)s to %(stop_date)s") % {
|
2003-01-02 10:01:52 +05:30
|
|
|
'start_date' : self.start.getDate(),
|
|
|
|
'stop_date' : self.stop.getDate() }
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2003-01-02 10:01:52 +05:30
|
|
|
def getQuoteDate(self):
|
2002-10-20 19:55:16 +05:30
|
|
|
if self.range == 0:
|
2003-01-02 10:01:52 +05:30
|
|
|
return self.start.getQuoteDate()
|
2002-10-20 19:55:16 +05:30
|
|
|
elif self.range == -1:
|
|
|
|
if self.text:
|
2003-01-02 10:01:52 +05:30
|
|
|
return '"%s"' % self.text
|
2002-10-20 19:55:16 +05:30
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
else:
|
2003-01-02 10:01:52 +05:30
|
|
|
return _("from %(start_date)s to %(stop_date)s") % {
|
|
|
|
'start_date' : self.start.getQuoteDate(),
|
|
|
|
'stop_date' : self.stop.getQuoteDate() }
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def isEmpty(self):
|
|
|
|
s = self.start
|
2002-12-30 06:12:47 +05:30
|
|
|
return s.year==UNDEF and s.month==UNDEF and s.day==UNDEF and not self.text
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def isValid(self):
|
2003-01-09 10:11:08 +05:30
|
|
|
if self.range == -1:
|
|
|
|
return 0
|
|
|
|
elif self.range:
|
|
|
|
return self.start.getValid() and self.stop.getValid()
|
|
|
|
return self.start.getValid()
|
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
def isRange(self):
|
|
|
|
return self.range == 1
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class SingleDate:
|
|
|
|
"Date handling"
|
|
|
|
|
|
|
|
def __init__(self,source=None):
|
|
|
|
if source:
|
|
|
|
self.month = source.month
|
|
|
|
self.day = source.day
|
|
|
|
self.year = source.year
|
|
|
|
self.mode = source.mode
|
|
|
|
self.calendar = source.calendar
|
|
|
|
else:
|
|
|
|
self.month = UNDEF
|
|
|
|
self.day = UNDEF
|
|
|
|
self.year = UNDEF
|
2003-01-02 10:01:52 +05:30
|
|
|
self.mode = Calendar.EXACT
|
2003-01-09 10:11:08 +05:30
|
|
|
self.calendar = Gregorian.Gregorian()
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def setMode(self,val):
|
2003-01-02 10:01:52 +05:30
|
|
|
self.mode = self.calendar.set_mode_value(val)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def setMonth(self,val):
|
|
|
|
if val > 14 or val < 0:
|
|
|
|
self.month = UNDEF
|
|
|
|
else:
|
2003-01-02 10:01:52 +05:30
|
|
|
self.month = val
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def setMonthVal(self,s):
|
2003-01-02 10:01:52 +05:30
|
|
|
self.month = self.calendar.set_value(s)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def setDayVal(self,s):
|
2003-01-02 10:01:52 +05:30
|
|
|
self.day = self.calendar.set_value(s)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def setYearVal(self,s):
|
2003-01-02 10:01:52 +05:30
|
|
|
if s:
|
|
|
|
self.year = self.calendar.set_value(s)
|
|
|
|
else:
|
2002-10-20 19:55:16 +05:30
|
|
|
self.year = UNDEF
|
|
|
|
|
|
|
|
def getMonth(self):
|
2003-01-02 10:01:52 +05:30
|
|
|
return self.month
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def getMonthValid(self):
|
|
|
|
return self.month != UNDEF
|
|
|
|
|
|
|
|
def setDay(self,val):
|
|
|
|
self.day = val
|
|
|
|
|
|
|
|
def getDay(self):
|
|
|
|
return self.day
|
|
|
|
|
|
|
|
def getDayValid(self):
|
|
|
|
return self.day != UNDEF
|
|
|
|
|
|
|
|
def setYear(self,val):
|
|
|
|
self.year = val
|
|
|
|
|
|
|
|
def getYear(self):
|
|
|
|
return self.year
|
|
|
|
|
|
|
|
def getYearValid(self):
|
|
|
|
return self.year != UNDEF
|
|
|
|
|
|
|
|
def getValid(self):
|
|
|
|
""" Returns true if any part of the date is valid"""
|
2003-01-09 10:11:08 +05:30
|
|
|
if self.year == UNDEF and self.month == UNDEF and self.day == UNDEF:
|
|
|
|
return 1
|
|
|
|
return self.calendar.check(self.year,self.month,self.day)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def setMonthStr(self,text):
|
2003-01-02 10:01:52 +05:30
|
|
|
self.calendar.set_month_string(text)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def getMonthStr(self):
|
2003-01-10 10:51:32 +05:30
|
|
|
return self.calendar.month(self.month)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def getIsoDate(self):
|
|
|
|
if self.year == UNDEF:
|
|
|
|
y = "????"
|
|
|
|
else:
|
|
|
|
y = "%04d" % self.year
|
|
|
|
if self.month == UNDEF:
|
|
|
|
if self.day == UNDEF:
|
|
|
|
m = ""
|
|
|
|
else:
|
|
|
|
m = "-??"
|
|
|
|
else:
|
2003-01-20 18:58:33 +05:30
|
|
|
m = "-%02d" % (self.month)
|
2002-10-20 19:55:16 +05:30
|
|
|
if self.day == UNDEF:
|
|
|
|
d = ''
|
|
|
|
else:
|
|
|
|
d = "-%02d" % self.day
|
|
|
|
return "%s%s%s" % (y,m,d)
|
|
|
|
|
|
|
|
|
|
|
|
def getDate(self):
|
2003-01-02 10:01:52 +05:30
|
|
|
return self.calendar.display(self.year, self.month, self.day, self.mode)
|
|
|
|
|
|
|
|
def getQuoteDate(self):
|
|
|
|
if self.year == UNDEF and self.month == UNDEF and self.day == UNDEF:
|
|
|
|
return ""
|
2002-10-20 19:55:16 +05:30
|
|
|
else:
|
2003-01-02 10:01:52 +05:30
|
|
|
return self.calendar.quote_display(self.year, self.month, self.day, self.mode)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def setIsoDate(self,v):
|
|
|
|
data = string.split(v)
|
|
|
|
if len(data) > 1:
|
|
|
|
self.setMode(data[0])
|
|
|
|
v = data[1]
|
|
|
|
|
|
|
|
vals = string.split(v,'-')
|
|
|
|
self.setYearVal(vals[0])
|
|
|
|
if len(vals) > 1:
|
2003-03-20 08:24:28 +05:30
|
|
|
try:
|
|
|
|
self.setMonthVal(int(vals[1]))
|
|
|
|
except:
|
|
|
|
self.month = UNDEF
|
2003-01-02 10:01:52 +05:30
|
|
|
else:
|
|
|
|
self.month = UNDEF
|
2002-10-20 19:55:16 +05:30
|
|
|
if len(vals) > 2:
|
|
|
|
self.setDayVal(vals[2])
|
2003-01-02 10:01:52 +05:30
|
|
|
else:
|
|
|
|
self.day = UNDEF
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def getModeVal(self):
|
|
|
|
return self.mode
|
|
|
|
|
|
|
|
def setModeVal(self,val):
|
|
|
|
self.mode = val
|
|
|
|
|
|
|
|
def set(self,text):
|
2003-01-02 10:01:52 +05:30
|
|
|
self.year, self.month, self.day, self.mode = self.calendar.set(text)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
def convert_to(self,val):
|
2003-01-02 10:01:52 +05:30
|
|
|
sdn = self.calendar.get_sdn(self.year, self.month, self.day)
|
|
|
|
self.calendar = val()
|
|
|
|
(self.year, self.month, self.day) = self.calendar.get_ymd(sdn)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2003-01-02 10:01:52 +05:30
|
|
|
def convert_to_obj(self,val):
|
|
|
|
sdn = self.calendar.get_sdn(self.year, self.month, self.day)
|
|
|
|
self.calendar = val
|
|
|
|
(self.year, self.month, self.day) = self.calendar.get_ymd(sdn)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def not_too_old(date):
|
|
|
|
time_struct = time.localtime(time.time())
|
|
|
|
current_year = time_struct[0]
|
|
|
|
if date.year != UNDEF and current_year - date.year > 110:
|
|
|
|
return 0
|
|
|
|
return 1
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def compare_dates(f,s):
|
2003-01-02 10:01:52 +05:30
|
|
|
if f.calendar.NAME != s.calendar.NAME:
|
2002-10-20 19:55:16 +05:30
|
|
|
return 1
|
|
|
|
if f.range == -1 and s.range == -1:
|
|
|
|
return cmp(f.text,s.text)
|
|
|
|
if f.range == -1 or s.range == -1:
|
|
|
|
return -1
|
2003-06-25 16:25:15 +05:30
|
|
|
if f.range != s.range:
|
|
|
|
return 1
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
first = f.get_start_date()
|
|
|
|
second = s.get_start_date()
|
2003-07-08 06:12:26 +05:30
|
|
|
if first.mode != second.mode:
|
|
|
|
return 1
|
|
|
|
elif first.year != second.year:
|
2002-10-20 19:55:16 +05:30
|
|
|
return cmp(first.year,second.year)
|
|
|
|
elif first.month != second.month:
|
|
|
|
return cmp(first.month,second.month)
|
|
|
|
elif f.range != 1:
|
|
|
|
return cmp(first.day,second.day)
|
|
|
|
else:
|
|
|
|
first = f.get_stop_date()
|
|
|
|
second = s.get_stop_date()
|
2003-07-08 06:12:26 +05:30
|
|
|
if first.mode != second.mode:
|
|
|
|
return 1
|
|
|
|
elif first.year != second.year:
|
2002-10-20 19:55:16 +05:30
|
|
|
return cmp(first.year,second.year)
|
|
|
|
elif first.month != second.month:
|
|
|
|
return cmp(first.month,second.month)
|
|
|
|
else:
|
|
|
|
return cmp(first.day,second.day)
|