Calendar formats added

svn: r1241
This commit is contained in:
Don Allingham 2003-01-07 03:40:22 +00:00
parent 5d176c702f
commit c440d722a6
4 changed files with 243 additions and 298 deletions

Binary file not shown.

View File

@ -70,6 +70,22 @@ fmt5 = re.compile(_start+"(\d+)\s*$", re.IGNORECASE)
fmt6 = re.compile(_start+"(\S+)\s*$", re.IGNORECASE) fmt6 = re.compile(_start+"(\S+)\s*$", re.IGNORECASE)
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def set_format_code(code):
Calendar.FORMATCODE = code
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def get_format_code():
return Calendar.FORMATCODE
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Calendar - base calendar # Calendar - base calendar
@ -77,7 +93,8 @@ fmt6 = re.compile(_start+"(\S+)\s*$", re.IGNORECASE)
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class Calendar: class Calendar:
entryCode = 0 ENTRYCODE = 0
FORMATCODE = 0
MONTHS = [ MONTHS = [
_("January"), _("February"), _("March"), _("April"), _("January"), _("February"), _("March"), _("April"),
@ -143,6 +160,9 @@ class Calendar:
def quote_display(self,year,month,day,mode): def quote_display(self,year,month,day,mode):
return "%s (%s)" % (text,Calendar.NAME) return "%s (%s)" % (text,Calendar.NAME)
def display(self,year,month,day,mode):
return _FMT_FUNC[Calendar.FORMATCODE](self,year,month,day,mode)
def format_yymmdd(self,year,month,day,mode): def format_yymmdd(self,year,month,day,mode):
if month == UNDEF and day == UNDEF and year == UNDEF : if month == UNDEF and day == UNDEF and year == UNDEF :
return "" return ""
@ -160,13 +180,62 @@ class Calendar:
retval = "????-%02d-%02d" % (month,day) retval = "????-%02d-%02d" % (month,day)
else: else:
retval = "%04d-%02d-%02d" % (year,month,day) retval = "%04d-%02d-%02d" % (year,month,day)
return self.fmt_mode(retval,mode)
if Calendar.MODE.has_key(mode): def format_mon_dd_year(self,year,month,day,mode):
return "%s %s" % (Calendar.MODE[mode],retval) """
Formats the date in the form of DD Month Year, such as
January 20, 2000
"""
if month == UNDEF and day == UNDEF and year == UNDEF:
return ""
elif day == UNDEF:
if month == UNDEF:
retval = str(year)
elif year == UNDEF:
retval = self.month(month)
else: else:
return retval retval = "%s %d" % (self.month(month),year)
elif month == UNDEF:
retval = str(year)
else:
if year == UNDEF:
retval = "%s %d, ????" % (self.month(month),day)
else:
retval = "%s %d, %d" % (self.month(month),day,year)
def display(self,year,month,day,mode): return self.fmt_mode(retval,mode)
def format_MON_dd_year(self,year,month,day,mode):
"""
Formats the date in the form of DD Month Year, such as
January 20, 2000
"""
if month == UNDEF and day == UNDEF and year == UNDEF:
return ""
elif day == UNDEF:
if month == UNDEF:
retval = str(year)
elif year == UNDEF:
retval = self.month(month).upper()[0:3]
else:
retval = "%s %d" % (self.month(month).upper()[0:3],year)
elif month == UNDEF:
retval = str(year)
else:
if year == UNDEF:
retval = "%s %d, ????" % (self.month(month).upper()[0:3],day)
else:
retval = "%s %d, %d" % (self.month(month).upper()[0:3],day,year)
return self.fmt_mode(retval,mode)
def format_dd_mon_year(self,year,month,day,mode):
"""
Formats the date in the form of DD Month Year, such as
20 January 2000
"""
if year==UNDEF: if year==UNDEF:
if month == UNDEF: if month == UNDEF:
d = "" d = ""
@ -181,10 +250,154 @@ class Calendar:
else: else:
d = "%02d %s %d" % (day,self.month(month),year) d = "%02d %s %d" % (day,self.month(month),year)
if Calendar.MODE.has_key(mode): return self.fmt_mode(d,mode)
return "%s %s" % (Calendar.MODE[mode],d)
def format_dd_MON_year(self,year,month,day,mode):
"""
Formats the date in the form of DD. Month Year, such as
20. January 2000
"""
if month == UNDEF and day == UNDEF and year == UNDEF :
return ""
elif day == UNDEF:
if month == UNDEF:
retval = str(year)
elif year == UNDEF:
retval = self.month(month).upper()[0:3]
else: else:
return d retval = "%s %d" % (self.month(month).upper()[0:3],year)
elif month == UNDEF:
retval = str(year)
else:
month_str = self.month(month).upper()[0:3]
if self.year == UNDEF:
retval = "%d %s ????" % (day,month_str)
else:
retval = "%d %s %d" % (day,month_str,year)
return self.fmt_mode(retval,mode)
def format_dd_dot_MON_year(self,year,month,day,mode):
"""
Formats the date in the form of DD. Month Year, such as
20. January 2000
"""
if month == UNDEF and day == UNDEF and year == UNDEF :
return ""
elif day == UNDEF:
if month == UNDEF:
retval = str(year)
elif year == UNDEF:
retval = self.month(month).upper()[0:3]
else:
retval = "%s %d" % (self.month(month).upper()[0:3],year)
elif month == UNDEF:
retval = str(year)
else:
month_str = self.month(month).upper()[0:3]
if self.year == UNDEF:
retval = "%d. %s ????" % (day,month_str)
else:
retval = "%d. %s %d" % (day,month_str,year)
return self.fmt_mode(retval,mode)
def format4(self,year,month,day,mode):
return self._get_mmddyyyy(year,month,day,mode,"/")
def format5(self,year,month,day,mode):
return self._get_mmddyyyy(year,month,day,mode,"-")
def format6(self,year,month,day,mode):
return self._get_ddmmyyyy(year,month,day,mode,"/")
def format7(self,year,month,day,mode):
return self._get_ddmmyyyy(year,month,day,mode,"-")
def format8(self,year,month,day,mode):
return self._get_mmddyyyy(year,month,day,mode,".")
def format9(self,year,month,day,mode):
return self._get_ddmmyyyy(year,month,day,mode,".")
def format11(self,year,month,day,mode):
return self._get_yyyymmdd(year,month,day,mode,"/")
def format12(self,year,month,day,mode):
return self._get_yyyymmdd(year,month,day,mode,"-")
def format13(self,year,month,day,mode):
return self._get_yyyymmdd(year,month,day,mode,".")
def _get_mmddyyyy(self,year,month,day,mode,sep):
if month == UNDEF and day == UNDEF and year == UNDEF :
return ""
elif day == UNDEF:
if month == UNDEF:
retval = str(year)
elif year == UNDEF:
retval = "%02d%s??%s??" % (month+1,sep,sep)
else:
retval = "%02d%s??%s%04d" % (month+1,sep,sep,year)
elif month == UNDEF:
retval = "??%s%02d%s%04d" % (sep,day,sep,year)
else:
if year == UNDEF:
retval = "%02d%s%02d%s????" % (month+1,sep,day,sep)
else:
retval = "%02d%s%02d%s%04d" % (month+1,sep,day,sep,year)
return self.fmt_mode(retval,mode)
def _get_yyyymmdd(self,year,month,day,mode,sep):
retval = ""
if month == UNDEF and day == UNDEF and year == UNDEF :
pass
elif day == UNDEF:
if month == UNDEF:
retval = str(self.year)
elif year == UNDEF:
retval = "????%s%02d%s??" % (sep,month+1,sep)
else:
retval = "%04d%s%02d" % (year,sep,month+1)
elif month == UNDEF:
retval = "%04d%s??%s%02d" % (year,sep,sep,day)
else:
if year == UNDEF:
retval = "????%s%02d%s%02d" % (sep,month+1,sep,day)
else:
retval = "%02d%s%02d%s%02d" % (year,sep,month+1,sep,day)
return self.fmt_mode(retval,mode)
def _get_ddmmyyyy(self,year,month,day,mode,sep):
retval = ""
if month == UNDEF and day == UNDEF and year == UNDEF :
pass
elif day == UNDEF:
if month == UNDEF:
retval = str(year)
elif year == UNDEF:
retval = "??%s%02d%s??" % (sep,month+1,sep)
else:
retval = "??%s%02d%s%04d" % (sep,month+1,sep,year)
elif month == UNDEF:
retval = "%02d%s??%s%04d" % (day,sep,sep,year)
else:
if year == UNDEF:
retval = "%02d%s%02d%s????" % (day,sep,month+1,sep)
else:
retval = "%02d%s%02d%s%04d" % (day,sep,month+1,sep,year)
return self.fmt_mode(retval,mode)
def fmt_mode(self,val,mode):
if Calendar.MODE.has_key(mode):
return "%s %s" % (Calendar.MODE[mode],val)
else:
return val
def get_ymd(self,val): def get_ymd(self,val):
return (0,0,0) return (0,0,0)
@ -1024,6 +1237,21 @@ class Islamic(Calendar):
day = int((sdn - self.get_sdn(year,month,1)) + 1) day = int((sdn - self.get_sdn(year,month,1)) + 1)
return (year,month,day) return (year,month,day)
_FMT_FUNC = [
Calendar.format_mon_dd_year,
Calendar.format_MON_dd_year,
Calendar.format_dd_MON_year,
Calendar.format4,
Calendar.format5,
Calendar.format6,
Calendar.format7,
Calendar.format9,
Calendar.format_dd_dot_MON_year,
Calendar.format11,
Calendar.format12,
Calendar.format13,
]
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Calendar registration # Calendar registration
@ -1055,25 +1283,3 @@ register(Persian)
register(Hebrew) register(Hebrew)
register(Islamic) register(Islamic)
if __name__ == "__main__":
e = Gregorian()
print e.get_ymd(e.get_sdn(1992,12,2)), e.display(1992,12,2,EXACT)
a = Julian()
print a.get_ymd(a.get_sdn(2002,1,28)), a.display(2002,1,28,ABOUT)
f = Hebrew()
print f.get_ymd(f.get_sdn(1992,12,2)), f.display(1992,12,2,EXACT)
e = Gregorian()
print e.get_ymd(e.get_sdn(1992,12,2)), e.display(1992,12,2,EXACT)
g = Islamic()
print g.get_ymd(g.get_sdn(1992,12,2)), g.display(1992,12,2,EXACT)
h = Persian()
print h.get_ymd(h.get_sdn(1992,12,2)), h.display(1992,12,2,EXACT)
i = FrenchRepublic()
print i.get_ymd(i.get_sdn(1992,12,2)), i.display(1992,12,2,EXACT)

View File

@ -247,24 +247,6 @@ class Date:
def isRange(self): def isRange(self):
return self.range == 1 return self.range == 1
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def set_format_code(code):
global _func
Date.formatCode = code
_func = SingleDate.fmtFunc[code]
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def get_format_code():
return Date.formatCode
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# #
@ -360,248 +342,6 @@ class SingleDate:
d = "-%02d" % self.day d = "-%02d" % self.day
return "%s%s%s" % (y,m,d) return "%s%s%s" % (y,m,d)
def _format1(self):
if self.month == UNDEF and self.day == UNDEF and self.year == UNDEF:
return ""
elif self.day == UNDEF:
if self.month == UNDEF:
retval = str(self.year)
elif self.year == UNDEF:
retval = _mname[self.month]
else:
try:
retval = "%s %d" % (_mname[self.month],self.year)
except:
retval = "**** %d %d %d ****" % (self.year,self.month,self.day)
elif self.month == UNDEF:
retval = str(self.year)
else:
try:
month = _mname[self.month]
except:
month = "<ILLEGAL MONTH>"
if self.year == UNDEF:
retval = "%s %d, ????" % (month,self.day)
else:
retval = "%s %d, %d" % (month,self.day,self.year)
if self.mode == Calendar.ABOUT:
retval = _("about") + ' ' + retval
elif self.mode == Calendar.BEFORE:
retval = _("before") + ' ' + retval
elif self.mode == Calendar.AFTER:
retval = _("after") + ' ' + retval
return retval
def _format2(self):
if self.month == UNDEF and self.day == UNDEF and self.year == UNDEF :
return ""
elif self.month != UNDEF and self.month != UNDEF:
month = _mname[self.month]
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 == UNDEF:
if self.month == UNDEF:
retval = str(self.year)
elif self.year == UNDEF:
month = _mname[self.month]
retval = string.upper(month[0:3])
else:
month = _mname[self.month]
retval = "%s %d" % (string.upper(month[0:3]),self.year)
else:
retval = str(self.year)
if self.mode == Calendar.ABOUT:
retval = "%s %s" % (_("abt"),retval)
elif self.mode == Calendar.BEFORE:
retval = "%s %s" % (_("before"),retval)
elif self.mode == Calendar.AFTER:
retval = "%s %s" % (_("after"),retval)
return retval
def _format3(self):
if self.month == UNDEF and self.day == UNDEF and self.year == UNDEF :
return ""
elif self.day == UNDEF:
if self.month == UNDEF:
retval = str(self.year)
elif self.year == UNDEF:
month = _mname[self.month]
retval = string.upper(month[0:3])
else:
month = _mname[self.month]
retval = "%s %d" % (string.upper(month[0:3]),self.year)
elif self.month == UNDEF:
retval = str(self.year)
else:
month = _mname[self.month]
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)
if self.mode == Calendar.ABOUT:
retval = "%s %s" % (_("ABOUT"),retval)
elif self.mode == Calendar.BEFORE:
retval = "%s %s" % (_("BEFORE"),retval)
elif self.mode == Calendar.AFTER:
retval = "%s %s" % (_("AFTER"),retval)
return retval
def _format10(self):
if self.month == UNDEF and self.day == UNDEF and self.year == UNDEF :
return ""
elif self.day == UNDEF:
if self.month == UNDEF:
retval = str(self.year)
elif self.year == UNDEF:
retval = _mname[self.month]
else:
month = _mname[self.month]
retval = "%s %d" % (month,self.year)
elif self.month == UNDEF:
retval = str(self.year)
else:
month = _mname[self.month]
if self.year == UNDEF:
retval = "%d. %s ????" % (self.day,month)
else:
retval = "%d. %s %d" % (self.day,month,self.year)
if self.mode == Calendar.ABOUT:
retval = "%s %s" % (_("ABOUT"),retval)
elif self.mode == Calendar.BEFORE:
retval = "%s %s" % (_("BEFORE"),retval)
elif self.mode == Calendar.AFTER:
retval = "%s %s" % (_("AFTER"),retval)
return retval
def _get_mmddyyyy(self,sep):
if self.month == UNDEF and self.day == UNDEF and self.year == UNDEF :
return ""
elif self.day == UNDEF:
if self.month == UNDEF:
retval = str(self.year)
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 == UNDEF:
retval = "??%s%02d%s%04d" % (sep,self.day,sep,self.year)
else:
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)
if self.mode == Calendar.ABOUT:
retval = "%s %s" % (_("ABOUT"),retval)
elif self.mode == Calendar.BEFORE:
retval = "%s %s" % (_("BEFORE"),retval)
elif self.mode == Calendar.AFTER:
retval = "%s %s" % (_("AFTER"),retval)
return retval
def _get_yyyymmdd(self,sep):
retval = ""
if self.month == UNDEF and self.day == UNDEF and self.year == UNDEF :
pass
elif self.day == UNDEF:
if self.month == UNDEF:
retval = str(self.year)
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 == UNDEF:
retval = "%04d%s??%s%02d" % (self.year,sep,sep,self.day)
else:
if self.year == UNDEF:
retval = "????%s%02d%s%02d" % (sep,self.month+1,sep,self.day)
else:
retval = "%02d%s%02d%s%02d" % (self.year,sep,self.month+1,sep,self.day)
if self.mode == Calendar.ABOUT:
retval = "%s %s" % (_("about"),retval)
if self.mode == Calendar.BEFORE:
retval = "%s %s" % (_("before"),retval)
elif self.mode == Calendar.AFTER:
retval = "%s %s" % (_("after"),retval)
return retval
def _format4(self):
return self._get_mmddyyyy("/")
def _format5(self):
return self._get_mmddyyyy("-")
def _format8(self):
return self._get_mmddyyyy(".")
def _get_ddmmyyyy(self,sep):
retval = ""
if self.month == UNDEF and self.day == UNDEF and self.year == UNDEF :
pass
elif self.day == UNDEF:
if self.month == UNDEF:
retval = str(self.year)
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 == UNDEF:
retval = "%02d%s??%s%04d" % (self.day,sep,sep,self.year)
else:
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)
if self.mode == Calendar.ABOUT:
retval = "%s %s" % (_("ABOUT"),retval)
if self.mode == Calendar.BEFORE:
retval = "%s %s" % (_("BEFORE"),retval)
elif self.mode == Calendar.AFTER:
retval = "%s %s" % (_("AFTER"),retval)
return retval
def _format6(self):
return self._get_ddmmyyyy("/")
def _format7(self):
return self._get_ddmmyyyy("-")
def _format9(self):
return self._get_ddmmyyyy(".")
def _format11(self):
return self._get_yyyymmdd("/")
def _format12(self):
return self._get_yyyymmdd("-")
def _format13(self):
return self._get_yyyymmdd(".")
#--------------------------------------------------------------------
#
#
#
#--------------------------------------------------------------------
fmtFunc = [ _format1, _format2, _format3, _format4, _format5, _format6,
_format7, _format8, _format9, _format10, _format11, _format12,
_format13]
def getDate(self): def getDate(self):
return self.calendar.display(self.year, self.month, self.day, self.mode) return self.calendar.display(self.year, self.month, self.day, self.mode)
@ -691,8 +431,6 @@ def compare_dates(f,s):
else: else:
return cmp(first.day,second.day) return cmp(first.day,second.day)
_func = SingleDate.fmtFunc[0]
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -29,6 +29,7 @@ import os
import PaperMenu import PaperMenu
import Plugins import Plugins
import Calendar
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -283,8 +284,8 @@ def loadConfig(call):
autocomp = not ac autocomp = not ac
set_format_code(dateFormat) Calendar.set_format_code(dateFormat)
Date.entryCode = dateEntry Calendar.ENTRYCODE = dateEntry
if _name_format == 0: if _name_format == 0:
nameof = Utils.normal_name nameof = Utils.normal_name
@ -659,7 +660,7 @@ class GrampsPreferences:
item.connect("activate", self.on_format_toggled) item.connect("activate", self.on_format_toggled)
item.show() item.show()
date_menu.append(item) date_menu.append(item)
date_menu.set_active(get_format_code()) date_menu.set_active(Calendar.get_format_code())
date_option.set_menu(date_menu) date_option.set_menu(date_menu)
date_entry = self.top.get_widget("date_entry_format") date_entry = self.top.get_widget("date_entry_format")
@ -670,7 +671,7 @@ class GrampsPreferences:
item.connect("activate", self.on_format_toggled) item.connect("activate", self.on_format_toggled)
item.show() item.show()
date_menu.append(item) date_menu.append(item)
date_menu.set_active(Date.entryCode) date_menu.set_active(Calendar.ENTRYCODE)
date_entry.set_menu(date_menu) date_entry.set_menu(date_menu)
name_option = self.top.get_widget("name_format") name_option = self.top.get_widget("name_format")
@ -905,13 +906,13 @@ class GrampsPreferences:
format_menu = self.top.get_widget("date_format").get_menu() format_menu = self.top.get_widget("date_format").get_menu()
active = format_menu.get_active().get_data(INDEX) active = format_menu.get_active().get_data(INDEX)
set_format_code(active) Calendar.set_format_code(active)
set_int("/apps/gramps/date-format",active) set_int("/apps/gramps/date-format",active)
format_menu = self.top.get_widget("date_entry_format").get_menu() format_menu = self.top.get_widget("date_entry_format").get_menu()
entry_active = format_menu.get_active().get_data(INDEX) entry_active = format_menu.get_active().get_data(INDEX)
Date.entryCode = entry_active Calendar.ENTRYCODE = entry_active
set_int("/apps/gramps/date-entry",entry_active) set_int("/apps/gramps/date-entry",entry_active)
# get the name format # get the name format