| | 33 | |
|---|
| | 34 | class RDateTimeConvertor(object): |
|---|
| | 35 | "Converts between HTML (string) and python (mx.DateTime.DateTime)." |
|---|
| | 36 | |
|---|
| | 37 | def fromHTML(self, dateHtml): |
|---|
| | 38 | day, month, year = None, None, None |
|---|
| | 39 | sec, min, hour = None, None, None |
|---|
| | 40 | if dateHtml in ['', None]: |
|---|
| | 41 | return None |
|---|
| | 42 | elif dateHtml.__class__ == "".__class__: |
|---|
| | 43 | rdate = sre.compile('(\d\d):(\d\d):(\d\d) (\d\d)-(\d\d)-(\d\d\d\d)') |
|---|
| | 44 | date = sre.compile('(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)') |
|---|
| | 45 | rres = rdate.match(dateHtml) |
|---|
| | 46 | res = date.match(dateHtml) |
|---|
| | 47 | if rres: |
|---|
| | 48 | hour,min,sec,day,month,year = [int(i) for i in rres.groups()] |
|---|
| | 49 | elif res: |
|---|
| | 50 | year,month,day,hour,min,sec = [int(i) for i in res.groups()] |
|---|
| | 51 | else: |
|---|
| | 52 | raise Exception, "Couldn't accept '%s' for an RDate." |
|---|
| | 53 | elif dateHtml.__class__ == datetime.date: |
|---|
| | 54 | day, month, year = dateHtml.day, dateHtml.month, dateHtml.year |
|---|
| | 55 | sec, min, hour = (0, 0, 0) |
|---|
| | 56 | elif dateHtml.__class__ == datetime.datetime: |
|---|
| | 57 | day, month, year = dateHtml.day, dateHtml.month, dateHtml.year |
|---|
| | 58 | sec, min, hour = dateHtml.second, dateHtml.minute, dateHtml.hour |
|---|
| | 59 | else: |
|---|
| | 60 | msg = "Unsupported date input type: %s" % dateHtml.__class__ |
|---|
| | 61 | raise Exception(msg) |
|---|
| | 62 | return mx.DateTime.DateTime(year, month, day, hour, min, sec) |
|---|
| | 63 | |
|---|
| | 64 | def toHTML(self, dateTimeObject): |
|---|
| | 65 | if dateTimeObject in ['', None]: |
|---|
| | 66 | return '' |
|---|
| | 67 | return "%s:%s:%s %s-%s-%s" % ( |
|---|
| | 68 | str(dateTimeObject.hour).zfill(2), |
|---|
| | 69 | str(dateTimeObject.minute).zfill(2), |
|---|
| | 70 | str(int(dateTimeObject.second)).zfill(2), |
|---|
| | 71 | str(dateTimeObject.day).zfill(2), |
|---|
| | 72 | str(dateTimeObject.month).zfill(2), |
|---|
| | 73 | str(dateTimeObject.year).zfill(4), |
|---|
| | 74 | ) |
|---|
| | 75 | |
|---|
| | 76 | class RNSDateTimeConvertor(object): |
|---|
| | 77 | "Converts between HTML (string) and python (mx.DateTime.DateTime)." |
|---|
| | 78 | |
|---|
| | 79 | def fromHTML(self, dateHtml): |
|---|
| | 80 | day, month, year = None, None, None |
|---|
| | 81 | min, hour = None, None |
|---|
| | 82 | if dateHtml in ['', None]: |
|---|
| | 83 | return None |
|---|
| | 84 | elif dateHtml.__class__ == "".__class__: |
|---|
| | 85 | rnsdate = sre.compile('(\d\d):(\d\d) (\d\d)-(\d\d)-(\d\d\d\d)') |
|---|
| | 86 | rdate = sre.compile('(\d\d):(\d\d):(\d\d) (\d\d)-(\d\d)-(\d\d\d\d)') |
|---|
| | 87 | date = sre.compile('(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)') |
|---|
| | 88 | rnsres = rnsdate.match(dateHtml) |
|---|
| | 89 | rres = rdate.match(dateHtml) |
|---|
| | 90 | res = date.match(dateHtml) |
|---|
| | 91 | if rnsres: |
|---|
| | 92 | hour,min,day,month,year = [int(i) for i in rnsres.groups()] |
|---|
| | 93 | sec = 0 |
|---|
| | 94 | elif rres: |
|---|
| | 95 | hour,min,sec,day,month,year = [int(i) for i in rres.groups()] |
|---|
| | 96 | elif res: |
|---|
| | 97 | year,month,day,hour,min,sec = [int(i) for i in res.groups()] |
|---|
| | 98 | else: |
|---|
| | 99 | raise Exception, "Couldn't accept '%s' for an RDate." |
|---|
| | 100 | elif dateHtml.__class__ == datetime.date: |
|---|
| | 101 | day, month, year = dateHtml.day, dateHtml.month, dateHtml.year |
|---|
| | 102 | sec, min, hour = (0, 0, 0) |
|---|
| | 103 | elif dateHtml.__class__ == datetime.datetime: |
|---|
| | 104 | day, month, year = dateHtml.day, dateHtml.month, dateHtml.year |
|---|
| | 105 | sec, min, hour = dateHtml.second, dateHtml.minute, dateHtml.hour |
|---|
| | 106 | else: |
|---|
| | 107 | msg = "Unsupported date input type: %s" % dateHtml.__class__ |
|---|
| | 108 | raise Exception(msg) |
|---|
| | 109 | return mx.DateTime.DateTime(year, month, day, hour, min, sec) |
|---|
| | 110 | |
|---|
| | 111 | def toHTML(self, dateTimeObject): |
|---|
| | 112 | if dateTimeObject in ['', None]: |
|---|
| | 113 | return '' |
|---|
| | 114 | return "%s:%s %s-%s-%s" % ( |
|---|
| | 115 | str(dateTimeObject.hour).zfill(2), |
|---|
| | 116 | str(dateTimeObject.minute).zfill(2), |
|---|
| | 117 | str(dateTimeObject.day).zfill(2), |
|---|
| | 118 | str(dateTimeObject.month).zfill(2), |
|---|
| | 119 | str(dateTimeObject.year).zfill(4), |
|---|
| | 120 | ) |
|---|