Changeset 358

Show
Ignore:
Timestamp:
05/19/08 02:09:38 (3 months ago)
Author:
johnbywater
Message:

Refactored date-time convertors.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/dm/datetimeconvertor.py

    r354 r358  
    22import datetime 
    33import sre 
     4import dm.times 
    45 
    56class DateTimeConvertor(object):   
    67    "Converts between HTML (string) and Python (mx.DateTime.DateTime)." 
    78         
    8     def fromHTML(self, dateTimeString): 
    9         if dateTimeString in ['', None]: 
     9    acceptableFormats = [ 
     10        "%H:%M:%S %d-%m-%Y", 
     11        "%Y-%m-%d %H:%M:%S", 
     12        "%H:%M %d-%m-%Y", 
     13        "%Y-%m-%d %H:%M", 
     14        "%Y-%m-%d", 
     15        "%d-%m-%Y", 
     16        "%Y-%m", 
     17        "%m-%Y", 
     18        "%Y", 
     19    ] 
     20    normalFormat = "%Y-%m-%d %H:%M:%S" 
     21    labelFormat = "%H:%M:%S, %a %e %b, %Y" 
     22     
     23    def fromHTML(self, dateHtml): 
     24        if dateHtml in ['', None]: 
    1025            return None 
    11         if dateTimeString.__class__ == datetime.datetime: 
    12             dateTimeString = str(dateTimeString) 
    13         (date, time) = dateTimeString.split() 
    14         (year, month, day) = date.split('-') 
    15         (hour, min, sec) = time.split(':') 
    16         return mx.DateTime.DateTime( 
    17             int(year), int(month), int(day), 
    18             int(hour), int(min), int(sec) 
    19         ) 
     26        if dateHtml.__class__ == "".__class__: 
     27            if 'now' == dateHtml.strip(): 
     28                return dm.times.getLocalNow() 
     29            dateTime = self.generouslyParse(dateHtml) 
     30            if dateTime == None: 
     31                msg = "Couldn't accept '%s' for a DateTime." % dateHtml 
     32                raise Exception, msg 
     33            else: 
     34                return dateTime 
     35        day, month, year = None, None, None 
     36        min, hour = None, None 
     37        if dateHtml.__class__ == datetime.date: 
     38            day, month, year = dateHtml.day, dateHtml.month, dateHtml.year 
     39            sec, min, hour = (0, 0, 0) 
     40        elif dateHtml.__class__ == datetime.datetime: 
     41            day = int(dateHtml.day) 
     42            month = int(dateHtml.month) 
     43            year = int(dateHtml.year) 
     44            sec = int(dateHtml.second) 
     45            min = int(dateHtml.minute) 
     46            hour = int(dateHtml.hour) 
     47        else: 
     48            msg = "Unsupported date input type: %s" % dateHtml.__class__ 
     49            raise Exception(msg) 
     50        return mx.DateTime.DateTime(year, month, day, hour, min, sec) 
     51 
    2052 
    2153    def toHTML(self, dateTimeObject): 
    2254        if dateTimeObject in ['', None]: 
    2355            return '' 
    24         return "%s-%s-%s %s:%s:%s" % ( 
    25             str(dateTimeObject.year).zfill(4), 
    26             str(dateTimeObject.month).zfill(2), 
    27             str(dateTimeObject.day).zfill(2), 
    28             str(dateTimeObject.hour).zfill(2), 
    29             str(dateTimeObject.minute).zfill(2), 
    30             str(int(dateTimeObject.second)).zfill(2), 
    31         ) 
     56        return dateTimeObject.strftime(self.normalFormat) 
    3257 
    3358    def toLabel(self, dateTimeObject): 
    3459        if dateTimeObject in ['', None]: 
    3560            return '' 
    36         return dateTimeObject.strftime("%H:%M:%S, %a %e %b, %Y") 
     61        return dateTimeObject.strftime(self.labelFormat) 
     62 
     63    def generouslyParse(self, string): 
     64        dateTime = None 
     65        for format in self.acceptableFormats: 
     66            try: 
     67                dateTime = mx.DateTime.strptime(string, format) 
     68            except: 
     69                pass 
     70        return dateTime 
     71 
    3772 
    3873 
     
    4075    "Converts between HTML (string) and python (mx.DateTime.DateTime)." 
    4176         
    42     def fromHTML(self, dateHtml): 
    43         day, month, year = None, None, None 
    44         sec, min, hour = None, None, None 
    45         if dateHtml in ['', None]: 
    46             return None 
    47         elif dateHtml.__class__ == "".__class__: 
    48             rdate = sre.compile('(\d\d):(\d\d):(\d\d) (\d\d)-(\d\d)-(\d\d\d\d)') 
    49             date = sre.compile('(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)') 
    50             rres = rdate.match(dateHtml) 
    51             res = date.match(dateHtml) 
    52             if rres: 
    53                 hour,min,sec,day,month,year = [int(i) for i in rres.groups()] 
    54             elif res: 
    55                 year,month,day,hour,min,sec = [int(i) for i in res.groups()] 
    56             else: 
    57                 raise Exception, "Couldn't accept '%s' for an RDate."  
    58         elif dateHtml.__class__ == datetime.date: 
    59             day, month, year = dateHtml.day, dateHtml.month, dateHtml.year 
    60             sec, min, hour = (0, 0, 0) 
    61         elif dateHtml.__class__ == datetime.datetime: 
    62             day, month, year = dateHtml.day, dateHtml.month, dateHtml.year 
    63             sec, min, hour = dateHtml.second, dateHtml.minute, dateHtml.hour 
    64         else: 
    65             msg = "Unsupported date input type: %s" % dateHtml.__class__ 
    66             raise Exception(msg) 
    67         return mx.DateTime.DateTime(year, month, day, hour, min, sec) 
     77    normalFormat = "%H:%M:%S %d-%m-%Y" 
    6878 
    69     def toHTML(self, dateTimeObject): 
    70         if dateTimeObject in ['', None]: 
    71             return '' 
    72         return "%s:%s:%s %s-%s-%s" % ( 
    73             str(dateTimeObject.hour).zfill(2), 
    74             str(dateTimeObject.minute).zfill(2), 
    75             str(int(dateTimeObject.second)).zfill(2), 
    76             str(dateTimeObject.day).zfill(2), 
    77             str(dateTimeObject.month).zfill(2), 
    78             str(dateTimeObject.year).zfill(4), 
    79         ) 
    80  
    81     def toLabel(self, dateTimeObject): 
    82         if dateTimeObject in ['', None]: 
    83             return '' 
    84         return dateTimeObject.strftime("%H:%M:%S, %a %e %b, %Y") 
    8579 
    8680 
     
    8882    "Converts between HTML (string) and python (mx.DateTime.DateTime)." 
    8983         
    90     def fromHTML(self, dateHtml): 
    91         day, month, year = None, None, None 
    92         min, hour = None, None 
    93         if dateHtml in ['', None]: 
    94             return None 
    95         elif dateHtml.__class__ == "".__class__: 
    96             rnsdate = sre.compile('(\d\d):(\d\d) (\d\d)-(\d\d)-(\d\d\d\d)') 
    97             rdate = sre.compile('(\d\d):(\d\d):(\d\d) (\d\d)-(\d\d)-(\d\d\d\d)') 
    98             date = sre.compile('(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)') 
    99             rnsres = rnsdate.match(dateHtml) 
    100             rres = rdate.match(dateHtml) 
    101             res = date.match(dateHtml) 
    102             if rnsres: 
    103                 hour,min,day,month,year = [int(i) for i in rnsres.groups()] 
    104                 sec = 0 
    105             elif rres: 
    106                 hour,min,sec,day,month,year = [int(i) for i in rres.groups()] 
    107             elif res: 
    108                 year,month,day,hour,min,sec = [int(i) for i in res.groups()] 
    109             else: 
    110                 raise Exception, "Couldn't accept '%s' for an RDate." % dateHtml  
    111         elif dateHtml.__class__ == datetime.date: 
    112             day, month, year = dateHtml.day, dateHtml.month, dateHtml.year 
    113             sec, min, hour = (0, 0, 0) 
    114         elif dateHtml.__class__ == datetime.datetime: 
    115             day, month, year = dateHtml.day, dateHtml.month, dateHtml.year 
    116             sec, min, hour = dateHtml.second, dateHtml.minute, dateHtml.hour 
    117         else: 
    118             msg = "Unsupported date input type: %s" % dateHtml.__class__ 
    119             raise Exception(msg) 
    120         return mx.DateTime.DateTime(year, month, day, hour, min, sec) 
    121  
    122     def toHTML(self, dateTimeObject): 
    123         if dateTimeObject in ['', None]: 
    124             return '' 
    125         return "%s:%s %s-%s-%s" % ( 
    126             str(dateTimeObject.hour).zfill(2), 
    127             str(dateTimeObject.minute).zfill(2), 
    128             str(dateTimeObject.day).zfill(2), 
    129             str(dateTimeObject.month).zfill(2), 
    130             str(dateTimeObject.year).zfill(4), 
    131         ) 
    132  
    133     def toLabel(self, dateTimeObject): 
    134         if dateTimeObject in ['', None]: 
    135             return '' 
    136         return dateTimeObject.strftime("%H:%M, %a %e %b, %Y") 
     84    normalFormat = "%H:%M %d-%m-%Y" 
     85    labelFormat = "%H:%M, %a %e %b, %Y" 
    13786 
    13887 
     
    14089    "Converts between HTML (string) and python (mx.DateTime.Date)." 
    14190         
    142     def fromHTML(self, dateHtml): 
    143         year, month, day = None, None, None 
    144         if dateHtml in ['', None]: 
    145             return None 
    146         elif dateHtml.__class__ == "".__class__: 
    147             year, month, day = [int(i) for i in dateHtml.split('-')] 
    148         elif dateHtml.__class__ == datetime.date: 
    149             year, month, day = dateHtml.year, dateHtml.month, dateHtml.day 
    150         elif dateHtml.__class__ == datetime.datetime: 
    151             year, month, day = dateHtml.year, dateHtml.month, dateHtml.day 
    152         else: 
    153             msg = "Unsupported date input type: %s" % dateHtml.__class__ 
    154             raise Exception(msg) 
    155         return mx.DateTime.Date(year, month, day) 
    156  
    157     def toHTML(self, dateObject): 
    158         if dateObject in ['', None]: 
    159             return '' 
    160         return "%s-%s-%s" % ( 
    161             str(dateObject.year).zfill(4), 
    162             str(dateObject.month).zfill(2), 
    163             str(dateObject.day).zfill(2), 
    164         ) 
    165  
    166     def toLabel(self, dateTimeObject): 
    167         if dateTimeObject in ['', None]: 
    168             return '' 
    169         return dateTimeObject.strftime("%a, %e %b, %Y") 
     91    normalFormat = "%Y-%m-%d" 
     92    labelFormat = "%a, %e %b, %Y" 
    17093 
    17194 
     
    17396    "Converts between HTML (string) and python (mx.DateTime.Date)." 
    17497         
    175     def fromHTML(self, dateHtml): 
    176         day, month, year = None, None, None 
    177         if dateHtml in ['', None]: 
    178             return None 
    179         elif dateHtml.__class__ == "".__class__: 
    180             rdate = sre.compile('(\d\d)-(\d\d)-(\d\d\d\d)') 
    181             date = sre.compile('(\d\d\d\d)-(\d\d)-(\d\d)') 
    182             rres = rdate.match(dateHtml) 
    183             res = date.match(dateHtml) 
    184             if rres: 
    185                 day, month, year = [int(i) for i in rres.groups()] 
    186             elif res: 
    187                 year, month, day = [int(i) for i in res.groups()] 
    188             else: 
    189                 raise Exception, "Couldn't accept '%s' for an RDate."  
    190         elif dateHtml.__class__ == datetime.date: 
    191             day, month, year = dateHtml.day, dateHtml.month, dateHtml.year 
    192         elif dateHtml.__class__ == datetime.datetime: 
    193             day, month, year = dateHtml.day, dateHtml.month, dateHtml.year 
    194         else: 
    195             msg = "Unsupported date input type: %s" % dateHtml.__class__ 
    196             raise Exception(msg) 
    197         return mx.DateTime.Date(year, month, day) 
     98    normalFormat = "%d-%m-%Y" 
     99    labelFormat = "%a, %e %b, %Y" 
    198100 
    199     def toHTML(self, dateObject): 
    200         if dateObject in ['', None]: 
    201             return '' 
    202         return "%s-%s-%s" % ( 
    203             str(dateObject.day).zfill(2), 
    204             str(dateObject.month).zfill(2), 
    205             str(dateObject.year).zfill(4), 
    206         ) 
    207