Returns a TIME column as a time object: >>> convert_time('15:06:17') datetime.time(15, 6, 17) Illegal values are returned as str: >>> convert_time('-25:06:17') '-25:06:17' >>> convert_time('random crap') 'random crap' Note that MySQL always returns TIM
(obj)
| 244 | |
| 245 | |
| 246 | def convert_time(obj): |
| 247 | """Returns a TIME column as a time object: |
| 248 | |
| 249 | >>> convert_time('15:06:17') |
| 250 | datetime.time(15, 6, 17) |
| 251 | |
| 252 | Illegal values are returned as str: |
| 253 | |
| 254 | >>> convert_time('-25:06:17') |
| 255 | '-25:06:17' |
| 256 | >>> convert_time('random crap') |
| 257 | 'random crap' |
| 258 | |
| 259 | Note that MySQL always returns TIME columns as (+|-)HH:MM:SS, but |
| 260 | can accept values as (+|-)DD HH:MM:SS. The latter format will not |
| 261 | be parsed correctly by this function. |
| 262 | |
| 263 | Also note that MySQL's TIME column corresponds more closely to |
| 264 | Python's timedelta and not time. However if you want TIME columns |
| 265 | to be treated as time-of-day and not a time offset, then you can |
| 266 | use set this function as the converter for FIELD_TYPE.TIME. |
| 267 | """ |
| 268 | if isinstance(obj, (bytes, bytearray)): |
| 269 | obj = obj.decode("ascii") |
| 270 | |
| 271 | m = TIME_RE.fullmatch(obj) |
| 272 | if not m: |
| 273 | return obj |
| 274 | |
| 275 | try: |
| 276 | groups = list(m.groups()) |
| 277 | groups[-1] = _convert_second_fraction(groups[-1]) |
| 278 | hours, minutes, seconds, microseconds = groups |
| 279 | return datetime.time( |
| 280 | hour=int(hours), |
| 281 | minute=int(minutes), |
| 282 | second=int(seconds), |
| 283 | microsecond=int(microseconds), |
| 284 | ) |
| 285 | except ValueError: |
| 286 | return obj |
| 287 | |
| 288 | |
| 289 | def convert_date(obj): |
nothing calls this directly
no test coverage detected