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 10103 2008-02-24 13:55:55Z acraphae $ 
 22   
 23  """ 
 24  U.S English date display class. Should serve as the base class for all 
 25  localized tasks. 
 26  """ 
 27   
 28  #------------------------------------------------------------------------- 
 29  # 
 30  # set up logging 
 31  # 
 32  #------------------------------------------------------------------------- 
 33  import logging 
 34  log = logging.getLogger(".DateDisplay") 
 35   
 36  #------------------------------------------------------------------------- 
 37  # 
 38  # GRAMPS modules 
 39  # 
 40  #------------------------------------------------------------------------- 
 41  from gen.lib import Date 
 42  import GrampsLocale 
 43   
 44  #------------------------------------------------------------------------- 
 45  # 
 46  # DateDisplay 
 47  # 
 48  #------------------------------------------------------------------------- 
49 -class DateDisplay:
50 51 _months = GrampsLocale.long_months 52 MONS = GrampsLocale.short_months 53 54 _tformat = GrampsLocale.tformat 55 56 hebrew = ( 57 "", "Tishri", "Heshvan", "Kislev", "Tevet", "Shevat", 58 "AdarI", "AdarII", "Nisan", "Iyyar", "Sivan", "Tammuz", 59 "Av", "Elul" 60 ) 61 62 french = ( 63 u'', 64 u"Vendémiaire", 65 u'Brumaire', 66 u'Frimaire', 67 u"Nivôse", 68 u"Pluviôse", 69 u"Ventôse", 70 u'Germinal', 71 u"Floréal", 72 u'Prairial', 73 u'Messidor', 74 u'Thermidor', 75 u'Fructidor', 76 u'Extra', 77 ) 78 79 persian = ( 80 "", "Farvardin", "Ordibehesht", "Khordad", "Tir", 81 "Mordad", "Shahrivar", "Mehr", "Aban", "Azar", 82 "Dey", "Bahman", "Esfand" 83 ) 84 85 islamic = ( 86 "", "Muharram", "Safar", "Rabi`al-Awwal", "Rabi`ath-Thani", 87 "Jumada l-Ula", "Jumada t-Tania", "Rajab", "Sha`ban", 88 "Ramadan", "Shawwal", "Dhu l-Qa`da", "Dhu l-Hijja" 89 ) 90 91 formats = ("YYYY-MM-DD (ISO)", ) 92 93 calendar = ( 94 "", " (Julian)", " (Hebrew)", " (French Republican)", 95 " (Persian)", " (Islamic)" 96 ) 97 98 _mod_str = ("", "before ", "after ", "about ", "", "", "") 99 100 _qual_str = ("", "estimated ", "calculated ") 101 102 _bce_str = "%s B.C.E." 103
104 - def __init__(self, format=None):
105 self.display_cal = [ 106 self._display_gregorian, 107 self._display_julian, 108 self._display_hebrew, 109 self._display_french, 110 self._display_persian, 111 self._display_islamic, 112 ] 113 114 self.verify_format(format) 115 if format == None: 116 self.format = 0 117 else: 118 self.format = format
119
120 - def set_format(self, format):
121 self.format = format
122
123 - def verify_format(self, format):
124 pass
125
126 - def quote_display(self, date):
127 """ 128 Similar to the display task, except that if the value is a text only 129 value, it is enclosed in quotes. 130 """ 131 if date.get_modifier() == Date.MOD_TEXTONLY: 132 return '"%s"' % self.display(date) 133 else: 134 return self.display(date)
135
136 - def display(self, date):
137 """ 138 Return a text string representing the date. 139 """ 140 mod = date.get_modifier() 141 cal = date.get_calendar() 142 qual = date.get_quality() 143 start = date.get_start_date() 144 145 qual_str = self._qual_str[qual] 146 147 if mod == Date.MOD_TEXTONLY: 148 return date.get_text() 149 elif start == Date.EMPTY: 150 return "" 151 elif mod == Date.MOD_SPAN or mod == Date.MOD_RANGE: 152 d1 = self.display_iso(start) 153 d2 = self.display_iso(date.get_stop_date()) 154 return "%s %s - %s%s" % (qual_str, d1, d2, self.calendar[cal]) 155 else: 156 text = self.display_iso(start) 157 return "%s%s%s%s" % (qual_str, self._mod_str[mod], text, 158 self.calendar[cal])
159
160 - def _slash_year(self, val, slash):
161 if val < 0: 162 val = - val 163 164 if slash: 165 if val % 100 == 99: 166 year = "%d/%d" % (val, (val%1000)+1) 167 elif val % 10 == 9: 168 year = "%d/%d" % (val, (val%100)+1) 169 else: 170 year = "%d/%d" % (val, (val%10)+1) 171 else: 172 year = "%d" % (val) 173 174 return year
175
176 - def display_iso(self, date_val):
177 # YYYY-MM-DD (ISO) 178 year = self._slash_year(date_val[2], date_val[3]) 179 # This produces 1789, 1789-00-11 and 1789-11-00 for incomplete dates. 180 if date_val[0] == 0 and date_val[1] == 0: 181 # No month and no day -> year 182 value = year 183 else: 184 value = "%s-%02d-%02d" % (year, date_val[1], date_val[0]) 185 if date_val[2] < 0: 186 return self._bce_str % value 187 else: 188 return value
189
190 - def text_display(self, date):
191 """ 192 Similar to the display task, except that if the value is a text only 193 value, it is enclosed in quotes. 194 """ 195 return date.get_text()
196 197
198 - def _display_gregorian(self, date_val):
199 year = self._slash_year(date_val[2], date_val[3]) 200 if self.format == 0: 201 return self.display_iso(date_val) 202 elif self.format == 1: 203 if date_val[3]: 204 return self.display_iso(date_val) 205 else: 206 if date_val[0] == 0 and date_val[1] == 0: 207 value = str(date_val[2]) 208 else: 209 value = self._tformat.replace('%m', str(date_val[1])) 210 value = value.replace('%d', str(date_val[0])) 211 value = value.replace('%Y', str(abs(date_val[2]))) 212 value = value.replace('-', '/') 213 elif self.format == 2: 214 # Month Day, Year 215 if date_val[0] == 0: 216 if date_val[1] == 0: 217 value = year 218 else: 219 value = "%s %s" % (self._months[date_val[1]], year) 220 else: 221 value = "%s %d, %s" % (self._months[date_val[1]], 222 date_val[0], year) 223 elif self.format == 3: 224 # MON Day, Year 225 if date_val[0] == 0: 226 if date_val[1] == 0: 227 value = year 228 else: 229 value = "%s %s" % (self.MONS[date_val[1]], year) 230 else: 231 value = "%s %d, %s" % (self.MONS[date_val[1]], 232 date_val[0], year) 233 elif self.format == 4: 234 # Day Month Year 235 if date_val[0] == 0: 236 if date_val[1] == 0: 237 value = year 238 else: 239 value = "%s %s" % (self._months[date_val[1]], year) 240 else: 241 value = "%d %s %s" % (date_val[0], self._months[date_val[1]], 242 year) 243 else: 244 # Day MON Year 245 if date_val[0] == 0: 246 if date_val[1] == 0: 247 value = year 248 else: 249 value = "%s %s" % (self.MONS[date_val[1]], year) 250 else: 251 value = "%d %s %s" % (date_val[0], self.MONS[date_val[1]], year) 252 if date_val[2] < 0: 253 return self._bce_str % value 254 else: 255 return value
256
257 - def _display_julian(self, date_val):
258 # Julian date display is the same as Gregorian 259 return self._display_gregorian(date_val)
260
261 - def _display_calendar(self, date_val, month_list):
262 year = abs(date_val[2]) 263 if self.format == 0 or self.format == 1: 264 return self.display_iso(date_val) 265 else: 266 if date_val[0] == 0: 267 if date_val[1] == 0: 268 value = year 269 else: 270 value = u"%s %d" % (month_list[date_val[1]], year) 271 else: 272 value = u"%s %d, %s" % (month_list[date_val[1]], date_val[0], year) 273 if date_val[2] < 0: 274 return self._bce_str % value 275 else: 276 return value
277
278 - def _display_french(self, date_val):
279 year = abs(date_val[2]) 280 if self.format == 0 or self.format == 1: 281 return self.display_iso(date_val) 282 else: 283 if date_val[0] == 0: 284 if date_val[1] == 0: 285 value = year 286 else: 287 value = u"%s %d" % (self.french[date_val[1]], year) 288 else: 289 value = u"%d %s %s" % (date_val[0], self.french[date_val[1]], year) 290 if date_val[2] < 0: 291 return self._bce_str % value 292 else: 293 return value
294
295 - def _display_hebrew(self, date_val):
296 return self._display_calendar(date_val, self.hebrew)
297
298 - def _display_persian(self, date_val):
299 return self._display_calendar(date_val, self.persian)
300
301 - def _display_islamic(self, date_val):
302 return self._display_calendar(date_val, self.islamic)
303
304 -class DateDisplayEn(DateDisplay):
305 """ 306 English language date display class. 307 """ 308 309 formats = ( 310 "YYYY-MM-DD (ISO)", "Numerical", "Month Day, Year", 311 "MON DAY, YEAR", "Day Month Year", "DAY MON YEAR" 312 ) 313
314 - def __init__(self, format=None):
315 """ 316 Create a DateDisplay class that converts a Date object to a string 317 of the desired format. The format value must correspond to the format 318 list value (DateDisplay.format[]). 319 """ 320 321 DateDisplay.__init__(self, format)
322
323 - def display(self, date):
324 """ 325 Return a text string representing the date. 326 """ 327 mod = date.get_modifier() 328 cal = date.get_calendar() 329 qual = date.get_quality() 330 start = date.get_start_date() 331 332 qual_str = self._qual_str[qual] 333 334 if mod == Date.MOD_TEXTONLY: 335 return date.get_text() 336 elif start == Date.EMPTY: 337 return "" 338 elif mod == Date.MOD_SPAN: 339 d1 = self.display_cal[cal](start) 340 d2 = self.display_cal[cal](date.get_stop_date()) 341 return "%sfrom %s to %s%s" % (qual_str, d1, d2, self.calendar[cal]) 342 elif mod == Date.MOD_RANGE: 343 d1 = self.display_cal[cal](start) 344 d2 = self.display_cal[cal](date.get_stop_date()) 345 return "%sbetween %s and %s%s" % (qual_str, d1, d2, 346 self.calendar[cal]) 347 else: 348 text = self.display_cal[date.get_calendar()](start) 349 return "%s%s%s%s" % (qual_str, self._mod_str[mod], 350 text, self.calendar[cal])
351