|
1 | 1 | import json
|
| 2 | +import re |
| 3 | + |
| 4 | +dateFormat = '([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1])' |
| 5 | +dateRe = '%s)?)?' % dateFormat |
| 6 | + |
| 7 | +timeRE = '([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?' |
| 8 | + |
| 9 | +dateTimeRE = '%s(T%s(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?' % (dateFormat, timeRE) |
2 | 10 |
|
3 | 11 |
|
4 | 12 | class FP_Type:
|
@@ -89,37 +97,79 @@ def toString(self):
|
89 | 97 |
|
90 | 98 |
|
91 | 99 | # TODO
|
92 |
| -class FP_TimeBase: |
93 |
| - pass |
| 100 | +class FP_TimeBase(FP_Type): |
| 101 | + |
| 102 | + def __init__(self, timeStr): |
| 103 | + self.asStr = timeStr |
| 104 | + self.timeMatchData = None |
| 105 | + |
| 106 | + def _getMatchData(self, regEx): |
| 107 | + if not self.timeMatchData: |
| 108 | + self.timeMatchData = re.match(regEx, self.asStr).group(0) |
| 109 | + return self.timeMatchData |
94 | 110 |
|
95 | 111 |
|
96 | 112 | # TODO
|
97 |
| -class FP_Time: |
| 113 | +class FP_DateTime(FP_TimeBase): |
| 114 | + |
| 115 | + def __init__(self, timeStr): |
| 116 | + super(FP_DateTime, self).__init__(timeStr) |
| 117 | + self.timeMatchData = self._getMatchData() |
| 118 | + |
| 119 | + def _getMatchData(self, regEx=dateTimeRE): |
| 120 | + return super(FP_DateTime, self)._getMatchData(regEx) |
| 121 | + |
98 | 122 | @staticmethod
|
99 | 123 | def check_string(value):
|
100 | 124 | """
|
101 | 125 | Tests str to see if it is convertible to a DateTime.
|
102 | 126 | * @return If str is convertible to a DateTime, returns an FP_DateTime otherwise returns None
|
103 | 127 | """
|
104 |
| - d = FP_Time(value) |
| 128 | + d = FP_DateTime(value) |
105 | 129 | if not d._getMatchData():
|
106 | 130 | return None
|
107 | 131 | return d
|
108 | 132 |
|
109 | 133 |
|
110 | 134 | # TODO
|
111 |
| -class FP_DateTime: |
112 |
| - # TODO |
113 |
| - def _getMatchData(self, data): |
114 |
| - pass |
| 135 | +class FP_Date(FP_TimeBase): |
| 136 | + |
| 137 | + def __init__(self, timeStr): |
| 138 | + super(FP_Date, self).__init__(timeStr) |
| 139 | + self.timeMatchData = self._getMatchData() |
| 140 | + |
| 141 | + def _getMatchData(self, regEx=dateRe): |
| 142 | + return super(FP_Date, self)._getMatchData(regEx) |
115 | 143 |
|
116 | 144 | @staticmethod
|
117 | 145 | def check_string(value):
|
118 | 146 | """
|
119 | 147 | Tests str to see if it is convertible to a DateTime.
|
120 |
| - * @return If str is convertible to a DateTime, returns an FP_DateTime otherwise returns None |
| 148 | + * @return If str is convertible to a DateTime, returns an FP_Date otherwise returns None |
121 | 149 | """
|
122 |
| - d = FP_DateTime(value) |
| 150 | + d = FP_Date(value) |
| 151 | + if not d._getMatchData(): |
| 152 | + return None |
| 153 | + return d |
| 154 | + |
| 155 | + |
| 156 | +# TODO |
| 157 | +class FP_Time(FP_TimeBase): |
| 158 | + |
| 159 | + def __init__(self, timeStr): |
| 160 | + super(FP_Time, self).__init__(timeStr) |
| 161 | + self.timeMatchData = self._getMatchData() |
| 162 | + |
| 163 | + def _getMatchData(self, regEx=timeRE): |
| 164 | + return super(FP_Time, self)._getMatchData(regEx) |
| 165 | + |
| 166 | + @staticmethod |
| 167 | + def check_string(value): |
| 168 | + """ |
| 169 | + Tests str to see if it is convertible to a DateTime. |
| 170 | + * @return If str is convertible to a DateTime, returns an FP_Time otherwise returns None |
| 171 | + """ |
| 172 | + d = FP_Time(value) |
123 | 173 | if not d._getMatchData():
|
124 | 174 | return None
|
125 | 175 | return d
|
|
0 commit comments