gramps/src/Date.py

768 lines
23 KiB
Python
Raw Normal View History

2001-05-13 07:26:57 +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 the dates"
import re
import string
import intl
_ = intl.gettext
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
class Date:
formatCode = 0
BadFormat = _("Unknown Format")
Error = _("Illegal Date")
range = 1
normal = 0
invalid = -1
2001-05-13 07:26:57 +05:30
from_str = _("(from|between|bet)")
to_str = _("(and|to)")
fmt = re.compile("\s*" + from_str + "(.+)" + to_str + "(.+)\s*$",
2001-05-13 07:26:57 +05:30
re.IGNORECASE)
def __init__(self):
self.start = SingleDate()
self.stop = SingleDate()
self.range = 0
self.text = ""
2001-05-13 07:26:57 +05:30
def get_start_date(self):
return self.start
def get_stop_date(self):
return self.stop
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def set(self,text):
match = Date.fmt.match(text)
try:
if match:
matches = match.groups()
self.start.set(matches[1])
self.stop.set(matches[3])
self.range = 1
else:
self.start.set(text)
self.range = 0
except:
self.range = -1
self.text = text
2001-05-13 07:26:57 +05:30
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getDate(self):
function = SingleDate.fmtFunc[Date.formatCode]
if self.range == 1:
return _("from") + " " + function(self.start) + " " + \
_("to") + " " + function(self.stop)
elif self.range == -1:
return self.text
else:
return function(self.start)
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getQuoteDate(self):
function = SingleDate.fmtFunc[Date.formatCode]
if self.range == 1:
return _("from") + " " + function(self.start) + " " + \
_("to") + " " + function(self.stop)
elif self.range == -1 and self.text:
return '"' + self.text + '"'
2001-05-13 07:26:57 +05:30
else:
return function(self.start)
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getSaveDate(self):
if self.range == 1:
2001-05-13 07:26:57 +05:30
return "FROM " + self.start.getFmt3() + " TO " + self.stop.getFmt3()
elif self.range == -1:
return self.text
2001-05-13 07:26:57 +05:30
else:
return self.start.getFmt3()
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def quick_set(self,text):
try:
if text[0:2] == "FR":
match = Date.fmt.match(text)
if match:
matches = match.groups()
self.start.set(matches[1])
self.stop.set(matches[3])
self.range = 1
else:
self.range = -1
self.text = text
else:
try:
self.start.quick_set(text)
self.range = 0
except:
self.start.set(text)
self.range = 0
except:
self.range = -1
self.text = text
2001-05-13 07:26:57 +05:30
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
class SingleDate:
"Date handling"
exact = 0
about = 1
before = 2
after = 3
entryCode = 0
mname = [ "January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December" ]
m2num = { "jan" : 0, "feb" : 1, "mar" : 2, "apr" : 3,
"may" : 4, "jun" : 5, "jul" : 6, "aug" : 7,
"sep" : 8, "oct" : 9, "nov" : 10,"dec" : 11 }
m2v = { _("abt") : about ,
_("about") : about,
_("est") : about ,
_("circa") : about,
_("around") : about,
_("before") : before,
_("bef") : before,
_("after") : after,
_("aft") : after }
modifiers = "(" + \
_("abt") + '|' + \
_("about") + '|' + \
_("est") + '|' + \
_("circa") + '|' + \
_("around") + '|' + \
_("before") + '|' + \
_("after") + '|' + \
_("aft") + '|' + \
_("bef") + ")"
2001-05-13 07:26:57 +05:30
start = "^\s*" + modifiers + "?\s*"
fmt1 = re.compile(start + "(\S+)(\s+\d+\s*,)?\s*(\d+)?\s*$",
re.IGNORECASE)
fmt2 = re.compile(start + "(\d+)\s+(\S+)(\s+\d+)?\s*$",
re.IGNORECASE)
quick= re.compile(start + "(\d+)?\s(\S\S\S)?\s(\d+)?",
2001-05-13 07:26:57 +05:30
re.IGNORECASE)
fmt3 = re.compile(start + "(\d+)\s*[./-]\s*(\d+)\s*[./-]\s*(\d+)\s*$",
re.IGNORECASE)
fmt4 = re.compile(start + "(\S+)\s+(\d+)\s*$",
re.IGNORECASE)
fmt5 = re.compile(start + "(\d+)\s*$",
re.IGNORECASE)
fmt6 = re.compile(start + "(\S+)\s*$",
re.IGNORECASE)
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def __init__(self):
self.month = -1
self.day = -1
self.year = -1
self.mode = SingleDate.exact
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def __cmp__(self,other):
if self.year != other.year:
return cmp(self.year,other.year)
elif self.month != other.month:
return cmp(self.month,other.month)
elif self.day != other.day:
return cmp(self.day,other.day)
elif self.mode != other.mode:
if self.mode == SingleDate.exact:
return -1
else:
return 1
else:
return 0
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def setMode(self,val):
if val == None:
self.mode = SingleDate.exact
else:
val = string.lower(val)
self.mode = SingleDate.m2v[val]
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def setMonth(self,val):
if val > 0 and val < 13:
self.month = val - 1
else:
self.month = -1
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getMonth(self):
return self.month + 1
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def setDay(self,val):
if val > 0 or val < 32:
self.day = val
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getDay(self):
return self.day
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def setYear(self,val):
self.year = val
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getYear(self):
return self.year
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def setMonthStr(self,text):
if SingleDate.m2num.has_key(string.lower(text[0:3])):
self.month = SingleDate.m2num[string.lower(text[0:3])]
else:
self.month = -1
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def setMonthStrEng(self,text):
if SingleDate.m2num.has_key(string.lower(text[0:3])):
self.month = SingleDate.m2num[string.lower(text[0:3])]
else:
self.month = -1
2001-05-13 07:26:57 +05:30
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getMonthStr(self):
return SingleDate.mname[self.month]
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getSaveDate(self):
return self.getFmt3()
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getFmt1(self):
retval = ""
if self.month == -1 and self.day == -1 and self.year == -1 :
pass
elif self.day == -1:
if self.month == -1:
retval = "%d" % self.year
elif self.year == -1:
retval = SingleDate.mname[self.month]
else:
retval = "%s %d" % (SingleDate.mname[self.month],self.year)
elif self.month == -1:
retval = "%d" % self.year
else:
2001-05-13 07:26:57 +05:30
month = SingleDate.mname[self.month]
if self.year == -1:
retval = "%s %d, ????" % (month,self.day)
else:
retval = "%s %d, %d" % (month,self.day,self.year)
if self.mode == SingleDate.about:
retval = _("about") + ' ' + retval
2001-05-13 07:26:57 +05:30
if self.mode == SingleDate.before:
retval = _("before") + ' ' + retval
2001-05-13 07:26:57 +05:30
elif self.mode == SingleDate.after:
retval = _("after") + ' ' + retval
2001-05-13 07:26:57 +05:30
return retval
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getFmt2(self):
retval = ""
if self.month == -1 and self.day == -1 and self.year == -1 :
pass
elif self.day == -1:
if self.month == -1:
retval = "%d" % self.year
elif self.year == -1:
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:
retval = "%d" % self.year
else:
2001-05-13 07:26:57 +05:30
month = SingleDate.mname[self.month]
if self.year == -1:
retval = "%s %d, ????" % (string.upper(month[0:3]),self.day)
else:
retval = "%s %d, %d" % (string.upper(month[0:3]),self.day,self.year)
if self.mode == SingleDate.about:
retval = "ABT" + ' ' + retval
2001-05-13 07:26:57 +05:30
if self.mode == SingleDate.before:
retval = _("BEFORE") + " " + retval
2001-05-13 07:26:57 +05:30
elif self.mode == SingleDate.after:
retval = _("AFTER") + " " + retval
2001-05-13 07:26:57 +05:30
return retval
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getFmt3(self):
retval = ""
if self.month == -1 and self.day == -1 and self.year == -1 :
pass
elif self.day == -1:
if self.month == -1:
retval = "%d" % self.year
elif self.year == -1:
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:
retval = "%d" % self.year
else:
month = SingleDate.mname[self.month]
if self.year == -1:
retval = "%d %s ????" % (self.day,string.upper(month[0:3]))
else:
retval = "%d %s %d" % (self.day,string.upper(month[0:3]),self.year)
if self.mode == SingleDate.about:
retval = "ABT " + retval
if self.mode == SingleDate.before:
retval = _("BEFORE") + " " + retval
2001-05-13 07:26:57 +05:30
elif self.mode == SingleDate.after:
retval = _("AFTER") + " " + retval
2001-05-13 07:26:57 +05:30
return retval
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getFmt4(self):
retval = ""
if self.month == -1 and self.day == -1 and self.year == -1 :
pass
elif self.day == -1:
if self.month == -1:
retval = "%d" % self.year
elif self.month == -1:
retval = "%d" % self.year
else:
2001-05-13 07:26:57 +05:30
if year == -1:
retval = "%d/%d/????" % (self.month+1,self.day)
else:
retval = "%d/%d/%d" % (self.month+1,self.day,self.year)
if self.mode == SingleDate.about:
retval = "ABT" + ' ' + retval
2001-05-13 07:26:57 +05:30
if self.mode == SingleDate.before:
retval = "BEFORE " + retval
elif self.mode == SingleDate.after:
retval = "AFTER " + retval
return retval
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getFmt5(self):
retval = ""
if self.month == -1 and self.day == -1 and self.year == -1 :
pass
elif self.day == -1:
if self.month == -1:
retval = "%d" % self.year
elif self.month == -1:
retval = "%d" % self.year
else:
2001-05-13 07:26:57 +05:30
if self.year == -1:
retval = "%d-%d-????" % (self.month+1,self.day)
else:
retval = "%d-%d-%d" % (self.month+1,self.day,self.year)
if self.mode == SingleDate.about:
retval = "ABT" + ' ' + retval
2001-05-13 07:26:57 +05:30
if self.mode == SingleDate.before:
retval = _("BEFORE") + " " + retval
2001-05-13 07:26:57 +05:30
elif self.mode == SingleDate.after:
retval = _("AFTER") + " " + retval
2001-05-13 07:26:57 +05:30
return retval
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getFmt6(self):
retval = ""
if self.month == -1 and self.day == -1 and self.year == -1 :
pass
elif self.day == -1:
if self.month == -1:
retval = "%d" % self.year
elif self.month == -1:
retval = "%d" % self.year
else:
2001-05-13 07:26:57 +05:30
if self.year == -1:
retval = "%d/%d/????" % (self.day,self.month+1)
else:
retval = "%d/%d/%d" % (self.day,self.month+1,self.year)
if self.mode == SingleDate.about:
retval = "ABT" + ' ' + retval
2001-05-13 07:26:57 +05:30
if self.mode == SingleDate.before:
retval = _("BEFORE") + " " + retval
2001-05-13 07:26:57 +05:30
elif self.mode == SingleDate.after:
retval = _("AFTER") + " " + retval
2001-05-13 07:26:57 +05:30
return retval
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getFmt7(self):
retval = ""
if self.month == -1 and self.day == -1 and self.year == -1 :
pass
elif self.day == -1:
if self.month == -1:
retval = "%d" % self.year
elif self.month == -1:
retval = "%d" % self.year
else:
2001-05-13 07:26:57 +05:30
if self.year == -1:
retval = "%d-%d-????" % (self.day,self.month+1)
else:
retval = "%d-%d-%d" % (self.day,self.month+1,self.year)
if self.mode == SingleDate.about:
retval = "ABT" + ' ' + retval
2001-05-13 07:26:57 +05:30
if self.mode == SingleDate.before:
retval = _("BEFORE") + " " + retval
2001-05-13 07:26:57 +05:30
elif self.mode == SingleDate.after:
retval = _("AFTER") + " " + retval
2001-05-13 07:26:57 +05:30
return retval
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
fmtFunc = [ getFmt1, getFmt2, getFmt3, getFmt4, getFmt5, getFmt6, getFmt7 ]
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getDate(self):
function = SingleDate.fmtFunc[Date.formatCode]
return function(self)
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def getMode(self,val):
if val == None:
self.mode = SingleDate.exact
elif string.lower(val)[0:3] == "bef":
self.mode = SingleDate.before
elif string.lower(val)[0:3] == "aft":
self.mode = SingleDate.after
else:
self.mode = SingleDate.about
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def set(self,text):
match = SingleDate.fmt2.match(text)
if match != None:
matches = match.groups()
self.getMode(matches[0])
self.setMonthStr(matches[2])
self.setDay(string.atoi(matches[1]))
if len(matches) == 4:
val = matches[3]
if val == None or val[0] == '?':
self.setYear(-1)
else:
self.setYear(string.atoi(val))
else:
self.setYear(-1)
return 1
match = SingleDate.fmt5.match(text)
if match != None:
matches = match.groups()
self.getMode(matches[0])
self.setMonth(-1)
self.setDay(-1)
self.setYear(string.atoi(matches[1]))
return 1
2001-05-13 07:26:57 +05:30
match = SingleDate.fmt3.match(text)
if match != None:
matches = match.groups()
self.getMode(matches[0])
if SingleDate.entryCode == 0:
self.setMonth(string.atoi(matches[1]))
self.setDay(string.atoi(matches[2]))
else:
self.setMonth(string.atoi(matches[2]))
self.setDay(string.atoi(matches[1]))
val = matches[3]
if val == None or val[0] == '?':
self.setYear(-1)
else:
self.setYear(string.atoi(val))
return 1
match = SingleDate.fmt1.match(text)
if match != None:
matches = match.groups()
self.getMode(matches[0])
self.setMonthStr(matches[1])
val = matches[2]
if val:
self.setDay(string.atoi(string.replace(val,',','')))
2001-05-13 07:26:57 +05:30
else:
self.setDay(-1)
val = matches[3]
if val == None or val[0] == '?':
2001-05-13 07:26:57 +05:30
self.setYear(-1)
else:
self.setYear(string.atoi(val))
2001-05-13 07:26:57 +05:30
return 1
match = SingleDate.fmt4.match(text)
if match != None:
matches = match.groups()
self.getMode(matches[0])
self.setMonthStr(matches[1])
self.setDay(-1)
if len(matches) == 4:
val = matches[3]
if val == None or val[0] == '?' :
self.setYear(-1)
else:
self.setYear(string.atoi(val))
return 1
match = SingleDate.fmt6.match(text)
if match != None:
matches = match.groups()
self.getMode(matches[0])
self.setMonth(matches[1])
self.setDay(-1)
self.setYear(-1)
return 1
raise Date.Error,text
2001-05-13 07:26:57 +05:30
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
def quick_set(self,text):
match = SingleDate.quick.match(text)
if match != None:
matches = match.groups()
self.setMode(matches[0])
self.setMonthStrEng(matches[2])
2001-05-13 07:26:57 +05:30
self.setDay(string.atoi(matches[1]))
if len(matches) == 4:
val = matches[3]
if val == None or val[0] == '?':
self.setYear(-1)
else:
self.setYear(string.atoi(val))
else:
self.setYear(-1)
else:
self.setYear(-1)
self.setMonth(-1)
self.setDay(-1)
raise Date.Error,text
2001-05-13 07:26:57 +05:30
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def compare_dates(f,s):
first = f.get_start_date()
second = s.get_start_date()
if first.year != second.year:
return cmp(first.year,second.year)
elif first.month != second.month:
return cmp(first.month,second.month)
else:
return cmp(first.day,second.day)
if __name__ == "__main__":
def checkit(s):
d = Date()
d.set(s)
print s, ':', d.getDate(), ':', d.getQuoteDate(),'\n'
checkit("June 11")
checkit("1923")
checkit("11/12/1293")
checkit("11 JAN 1923")
checkit("11-1-1929")
checkit("4/3/1203")
checkit("January 4, 1923")
checkit("before 3/3/1239")
checkit("est 2-3-1023")
checkit("between January 4, 1234 and NOV 4, 1245")
checkit("from 3-2-1234 to 5-4-2345")