Date arithmetic

svn: r9658
This commit is contained in:
Doug Blank 2007-12-31 16:43:58 +00:00
parent fc419c3134
commit 70a02003ba
2 changed files with 77 additions and 13 deletions

View File

@ -144,11 +144,34 @@ class Date:
_("Persian"),
_("Islamic")]
def __init__(self, source=None):
def __init__(self, *source):
"""
Creates a new Date instance.
"""
if source:
#### setup None, Date, or numbers
if len(source) == 0:
source = None
elif len(source) == 1:
if type(source[0]) == int:
source = (source[0], 0, 0)
else:
source = source[0]
elif len(source) == 2:
source = (source[0], source[1], 0)
elif len(source) == 3:
pass # source is ok
else:
raise AttributeError, "invalid args to Date: %s" % source
#### ok, process either date or tuple
if type(source) == tuple:
self.calendar = Date.CAL_GREGORIAN
self.modifier = Date.MOD_NONE
self.quality = Date.QUAL_NONE
self.dateval = Date.EMPTY
self.text = u""
self.sortval = 0
self.set_yr_mon_day(*source)
elif source:
self.calendar = source.calendar
self.modifier = source.modifier
self.quality = source.quality
@ -205,6 +228,40 @@ class Date:
else:
return -1
def __add__(self, other):
"""
Date artithmetic: Date() + years, or Date() + (years, [months, [days]])
"""
if type(other) == int:
return self.copy_offset_ymd(other)
elif type(other) in [tuple, list]:
return self.copy_offset_ymd(*other)
else:
raise AttributeError, "unknown date add type: %s " % type(other)
def __radd__(self, other):
"""
Add a number + Date() or (years, months, days) + Date()
"""
return self + other
def __sub__(self, other):
"""
Date artithmetic: Date() - years, Date - (y,m,d), or Date() - Date()
"""
if type(other) == int:
return self.copy_offset_ymd(-other)
elif type(other) == type(self): # date
diff = self.sortval - other.sortval
years = int(diff / 365)
months = int((diff - (years * 365)) / 30)
days = (diff - (years * 365)) - (months * 30)
return (years, months, days)
elif type(other) in [tuple, list]:
return self.copy_offset_ymd(*map(lambda x: -x, other))
else:
raise AttributeError, "unknown date sub type: %s " % type(other)
def is_equal(self, other):
"""
Return 1 if the given Date instance is the same as the present
@ -794,3 +851,9 @@ class Date:
"""
return Date._calendar_change[Date.CAL_GREGORIAN](self.sortval + value)
def offset_date(self, value):
"""
Returns (year, month, day) of this date +- value.
"""
return Date(Date._calendar_change[Date.CAL_GREGORIAN](self.sortval + value))

View File

@ -90,7 +90,6 @@ class CalendarGadget(Gadget):
self.update()
def run_update(self, signal, *args):
print "signal:", signal
self.update()
def refresh(self, *obj):
@ -163,20 +162,21 @@ class LogGadget(Gadget):
def active_changed(self, handle):
self.log_active_changed(handle)
# FIXME: added support for family display and clicks
def log_person_add(self, handles):
self.get_person(handles, "person-add")
self.get_person(handles, _("Added"))
def log_person_delete(self, handles):
self.get_person(handles, "person-delete")
self.get_person(handles, _("Deleted"))
def log_person_update(self, handles):
self.get_person(handles, "person-update")
self.get_person(handles, _("Updated"))
def log_family_add(self, handles):
self.append_text("family-add: %s" % handles)
self.append_text(_("Added") + ": family" )
def log_family_delete(self, handles):
self.append_text("family-delete: %s" % handles)
self.append_text(_("Deleted") + ": family" )
def log_family_update(self, handles):
self.append_text("family-update: %s" % handles)
self.append_text(_("Updated") + ": family" )
def log_active_changed(self, handles):
self.get_person([handles], "active-changed")
self.get_person([handles], "Selected")
def get_person(self, handles, ltype):
for person_handle in handles:
@ -359,6 +359,7 @@ class PythonGadget(Gadget):
self.env = {"dbstate": self.gui.dbstate,
"uistate": self.gui.uistate,
"self": self,
"Date": gen.lib.Date,
}
# GUI setup:
self.gui.textview.set_editable(True)
@ -368,7 +369,7 @@ class PythonGadget(Gadget):
def format_exception(self, max_tb_level=10):
retval = ''
cla, exc, trbk = sys.exc_info()
retval += "ERROR: %s %s" %(cla, exc)
retval += _("Error") + (" : %s %s" %(cla, exc))
return retval
def on_enter(self, widget, event):
@ -424,7 +425,7 @@ class TODOGadget(Gadget):
self.save_text_to_data()
def make_welcome_content(gui):
text = """
text = _("""
Welcome to GRAMPS!
GRAMPS is a software package designed for genealogical research. Although similar to other genealogical programs, GRAMPS offers some unique and powerful features.
@ -439,7 +440,7 @@ You are currently reading from the "My Gramps" page, where you can add your own
You can right-click on the background of this page to add additional gadgets and change the number of columns. You can also drag the Properties button to reposition the gadget on this page, and detach the gadget to float above GRAMPS. If you close GRAMPS with a gadget detached, it will re-opened detached the next time you start GRAMPS.
"""
""")
gui.set_text(text)