An XML datetime object supporting the xsd:dateTime datatype. @ivar value: The object value. @type value: B{datetime}.I{datetime}
| 159 | |
| 160 | |
| 161 | class DateTime(object): |
| 162 | """An XML datetime object supporting the xsd:dateTime datatype. |
| 163 | |
| 164 | @ivar value: The object value. |
| 165 | @type value: B{datetime}.I{datetime} |
| 166 | |
| 167 | """ |
| 168 | __slots__ = ('value', ) |
| 169 | |
| 170 | def __init__(self, value): |
| 171 | """Constructor. |
| 172 | |
| 173 | @param value: The datetime value of the object. |
| 174 | @type value: (datetime.datetime|str) |
| 175 | @raise ValueError: When I{value} is invalid. |
| 176 | |
| 177 | """ |
| 178 | if isinstance(value, datetime.datetime): |
| 179 | self.value = value |
| 180 | elif isinstance(value, str): |
| 181 | self.value = self.parse(value) |
| 182 | else: |
| 183 | raise ValueError('invalid type for DateTime(): %s' % type(value)) |
| 184 | |
| 185 | @staticmethod |
| 186 | def parse(value): |
| 187 | """Parse the string datetime. |
| 188 | |
| 189 | This supports the subset of ISO8601 used by xsd:dateTime, but is |
| 190 | lenient with what is accepted, handling most reasonable syntax. |
| 191 | |
| 192 | @param value: A datetime string. |
| 193 | @type value: str |
| 194 | @return: A datetime object. |
| 195 | @rtype: B{datetime}.I{datetime} |
| 196 | |
| 197 | """ |
| 198 | match_result = RE_DATETIME.match(value) |
| 199 | if match_result is None: |
| 200 | raise ValueError('date data has invalid format "%s"' % (value, )) |
| 201 | |
| 202 | date = date_from_match(match_result) |
| 203 | time = time_from_match(match_result) |
| 204 | tzinfo = tzinfo_from_match(match_result) |
| 205 | |
| 206 | value = datetime.datetime.combine(date, time) |
| 207 | value = value.replace(tzinfo=tzinfo) |
| 208 | |
| 209 | return value |
| 210 | |
| 211 | def __str__(self): |
| 212 | return self.value.isoformat() |
| 213 | |
| 214 | def __unicode__(self): |
| 215 | return self.value.isoformat() |
| 216 | |
| 217 | |
| 218 | class FixedOffsetTimezone(datetime.tzinfo): |