Package DateHandler :: Module _DateDisplay
[frames] | no frames]

Source Code for Module DateHandler._DateDisplay

  1  # -*- coding: utf-8 -*-  
  2  # 
  3  # Gramps - a GTK+/GNOME based genealogy program 
  4  # 
  5  # Copyright (C) 2004-2006  Donald N. Allingham 
  6  # 
  7  # This program is free software; you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation; either version 2 of the License, or 
 10  # (at your option) any later version. 
 11  # 
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  # GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License 
 18  # along with this program; if not, write to the Free Software 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 20  # 
 21  # $Id: _DateDisplay.py 6992 2006-07-04 23:38:51Z dallingham $ 
 22   
 23  """ 
 24  U.S English date display class. Should serve as the base class for all 
 25  localized tasks. 
 26  """ 
 27   
 28  __author__ = "Donald N. Allingham" 
 29  __version__ = "$Revision: 6992 $" 
 30   
 31  #------------------------------------------------------------------------- 
 32  # 
 33  # set up logging 
 34  # 
 35  #------------------------------------------------------------------------- 
 36  import logging 
 37  log = logging.getLogger(".DateDisplay") 
 38   
 39  #------------------------------------------------------------------------- 
 40  # 
 41  # GRAMPS modules 
 42  # 
 43  #------------------------------------------------------------------------- 
 44  from RelLib import Date 
 45  import GrampsLocale 
 46   
 47  #------------------------------------------------------------------------- 
 48  # 
 49  # DateDisplay 
 50  # 
 51  #------------------------------------------------------------------------- 
52 -class DateDisplay:
53 54 _months = GrampsLocale.long_months 55 _MONS = GrampsLocale.short_months 56 57 _tformat = GrampsLocale.tformat 58 59 _hebrew = ( 60 "", "Tishri", "Heshvan", "Kislev", "Tevet", "Shevat", 61 "AdarI", "AdarII", "Nisan", "Iyyar", "Sivan", "Tammuz", 62 "Av", "Elul" 63 ) 64 65 _french = ( 66 u'', 67 u"Vendémiaire", 68 u'Brumaire', 69 u'Frimaire', 70 u"Nivôse", 71 u"Pluviôse", 72 u"Ventôse", 73 u'Germinal', 74 u"Floréal", 75 u'Prairial', 76 u'Messidor', 77 u'Thermidor', 78 u'Fructidor', 79 u'Extra', 80 ) 81 82 _persian = ( 83 "", "Farvardin", "Ordibehesht", "Khordad", "Tir", 84 "Mordad", "Shahrivar", "Mehr", "Aban", "Azar", 85 "Dey", "Bahman", "Esfand" 86 ) 87 88 _islamic = ( 89 "", "Muharram", "Safar", "Rabi`al-Awwal", "Rabi`ath-Thani", 90 "Jumada l-Ula", "Jumada t-Tania", "Rajab", "Sha`ban", 91 "Ramadan", "Shawwal", "Dhu l-Qa`da", "Dhu l-Hijja" 92 ) 93 94 formats = ("YYYY-MM-DD (ISO)",) 95 96 calendar = ( 97 ""," (Julian)"," (Hebrew)"," (French Republican)", 98 " (Persian)"," (Islamic)" 99 ) 100 101 _mod_str = ("","before ","after ","about ","","","") 102 103 _qual_str = ("","estimated ","calculated ") 104 105 _bce_str = "%s B.C.E." 106
107 - def __init__(self,format=None):
108 self.display_cal = [ 109 self._display_gregorian, 110 self._display_julian, 111 self._display_hebrew, 112 self._display_french, 113 self._display_persian, 114 self._display_islamic, 115 ] 116 117 self.verify_format(format) 118 if format == None: 119 self.format = 0 120 else: 121 self.format = format
122
123 - def set_format(self,format):
124 self.format = format
125
126 - def verify_format(self,format):
127 pass
128
129 - def quote_display(self,date):
130 """ 131 Similar to the display task, except that if the value is a text only 132 value, it is enclosed in quotes. 133 """ 134 if date.get_modifier() == Date.MOD_TEXTONLY: 135 return '"%s"' % self.display(date) 136 else: 137 return self.display(date)
138
139 - def display(self,date):
140 """ 141 Returns a text string representing the date. 142 """ 143 mod = date.get_modifier() 144 cal = date.get_calendar() 145 qual = date.get_quality() 146 start = date.get_start_date() 147 148 qual_str = self._qual_str[qual] 149 150 if mod == Date.MOD_TEXTONLY: 151 return date.get_text() 152 elif start == Date.EMPTY: 153 return "" 154 elif mod == Date.MOD_SPAN or mod == Date.MOD_RANGE: 155 d1 = self.display_iso(start) 156 d2 = self.display_iso(date.get_stop_date()) 157 return "%s %s - %s%s" % (qual_str,d1,d2,self.calendar[cal]) 158 else: 159 text = self.display_iso(start) 160 return "%s%s%s%s" % (qual_str,self._mod_str[mod],text,self.calendar[cal])
161
162 - def _slash_year(self,val,slash):
163 if val < 0: 164 val = - val 165 166 if slash: 167 if val % 100 == 99: 168 year = "%d/%d" % (val,(val%1000)+1) 169 elif val % 10 == 9: 170 year = "%d/%d" % (val,(val%100)+1) 171 else: 172 year = "%d/%d" % (val,(val%10)+1) 173 else: 174 year = "%d" % (val) 175 176 return year
177
178 - def display_iso(self,date_val):
179 # YYYY-MM-DD (ISO) 180 year = self._slash_year(date_val[2],date_val[3]) 181 # FIXME: This prodices 1789-11-00 and 1789-00-00 for incomplete dates. 182 if False:#date_val[0] == 0: 183 if date_val[1] == 0: 184 value = year 185 else: 186 value = "%s-%02d" % (year,date_val[1]) 187 else: 188 value = "%s-%02d-%02d" % (year,date_val[1],date_val[0]) 189 if date_val[2] < 0: 190 return self._bce_str % value 191 else: 192 return value
193
194 - def text_display(self,date):
195 """ 196 Similar to the display task, except that if the value is a text only 197 value, it is enclosed in quotes. 198 """ 199 return date.get_text()
200 201
202 - def _display_gregorian(self,date_val):
203 year = self._slash_year(date_val[2],date_val[3]) 204 if self.format == 0: 205 return self.display_iso(date_val) 206 elif self.format == 1: 207 if date_val[3]: 208 return self.display_iso(date_val) 209 else: 210 if date_val[0] == 0 and date_val[1] == 0: 211 value = str(date_val[2]) 212 else: 213 value = self._tformat.replace('%m',str(date_val[1])) 214 value = value.replace('%d',str(date_val[0])) 215 value = value.replace('%Y',str(abs(date_val[2]))) 216 value = value.replace('-','/') 217 elif self.format == 2: 218 # Month Day, Year 219 if date_val[0] == 0: 220 if date_val[1] == 0: 221 value = year 222 else: 223 value = "%s %s" % (self._months[date_val[1]],year) 224 else: 225 value = "%s %d, %s" % (self._months[date_val[1]],date_val[0],year) 226 elif self.format == 3: 227 # MON Day, Year 228 if date_val[0] == 0: 229 if date_val[1] == 0: 230 value = year 231 else: 232 value = "%s %s" % (self._MONS[date_val[1]],year) 233 else: 234 value = "%s %d, %s" % (self._MONS[date_val[1]],date_val[0],year) 235 elif self.format == 4: 236 # Day Month Year 237 if date_val[0] == 0: 238 if date_val[1] == 0: 239 value = year 240 else: 241 value = "%s %s" % (self._months[date_val[1]],year) 242 else: 243 value = "%d %s %s" % (date_val[0],self._months[date_val[1]],year) 244 else: 245 # Day MON Year 246 if date_val[0] == 0: 247 if date_val[1] == 0: 248 value = year 249 else: 250 value = "%s %s" % (self._MONS[date_val[1]],year) 251 else: 252 value = "%d %s %s" % (date_val[0],self._MONS[date_val[1]],year) 253 if date_val[2] < 0: 254 return self._bce_str % value 255 else: 256 return value
257
258 - def _display_julian(self,date_val):
259 # Julian date display is the same as Gregorian 260 return self._display_gregorian(date_val)
261
262 - def _display_calendar(self,date_val,month_list):
263 year = abs(date_val[2]) 264 if self.format == 0 or self.format == 1: 265 return self.display_iso(date_val) 266 else: 267 if date_val[0] == 0: 268 if date_val[1] == 0: 269 value = year 270 else: 271 value = u"%s %d" % (month_list[date_val[1]],year) 272 else: 273 value = u"%s %d, %s" % (month_list[date_val[1]],date_val[0],year) 274 if date_val[2] < 0: 275 return self._bce_str % value 276 else: 277 return value
278
279 - def _display_french(self,date_val):
280 year = abs(date_val[2]) 281 if self.format == 0 or self.format == 1: 282 return self.display_iso(date_val) 283 else: 284 if date_val[0] == 0: 285 if date_val[1] == 0: 286 value = year 287 else: 288 value = u"%s %d" % (self._french[date_val[1]],year) 289 else: 290 value = u"%d %s %s" % (date_val[0],self._french[date_val[1]],year) 291 if date_val[2] < 0: 292 return self._bce_str % value 293 else: 294 return value
295
296 - def _display_hebrew(self,date_val):
297 return self._display_calendar(date_val,self._hebrew)
298
299 - def _display_persian(self,date_val):
300 return self._display_calendar(date_val,self._persian)
301
302 - def _display_islamic(self,date_val):
303 return self._display_calendar(date_val,self._islamic)
304
305 -class DateDisplayEn(DateDisplay):
306 """ 307 English language date display class. 308 """ 309 310 formats = ( 311 "YYYY-MM-DD (ISO)", "Numerical", "Month Day, Year", 312 "MON DAY, YEAR", "Day Month Year", "DAY MON YEAR" 313 ) 314
315 - def __init__(self,format=None):
316 """ 317 Creates a DateDisplay class that converts a Date object to a string 318 of the desired format. The format value must correspond to the format 319 list value (DateDisplay.format[]). 320 """ 321 322 DateDisplay.__init__(self,format)
323
324 - def display(self,date):
325 """ 326 Returns a text string representing the date. 327 """ 328 mod = date.get_modifier() 329 cal = date.get_calendar() 330 qual = date.get_quality() 331 start = date.get_start_date() 332 333 qual_str = self._qual_str[qual] 334 335 if mod == Date.MOD_TEXTONLY: 336 return date.get_text() 337 elif start == Date.EMPTY: 338 return "" 339 elif mod == Date.MOD_SPAN: 340 d1 = self.display_cal[