Parse a variety of ISO-8601-compatible formats like 20040105
(dateString)
| 2981 | except NameError: |
| 2982 | pass |
| 2983 | def _parse_date_iso8601(dateString): |
| 2984 | '''Parse a variety of ISO-8601-compatible formats like 20040105''' |
| 2985 | m = None |
| 2986 | for _iso8601_match in _iso8601_matches: |
| 2987 | m = _iso8601_match(dateString) |
| 2988 | if m: break |
| 2989 | if not m: return |
| 2990 | if m.span() == (0, 0): return |
| 2991 | params = m.groupdict() |
| 2992 | ordinal = params.get('ordinal', 0) |
| 2993 | if ordinal: |
| 2994 | ordinal = int(ordinal) |
| 2995 | else: |
| 2996 | ordinal = 0 |
| 2997 | year = params.get('year', '--') |
| 2998 | if not year or year == '--': |
| 2999 | year = time.gmtime()[0] |
| 3000 | elif len(year) == 2: |
| 3001 | # ISO 8601 assumes current century, i.e. 93 -> 2093, NOT 1993 |
| 3002 | year = 100 * int(time.gmtime()[0] / 100) + int(year) |
| 3003 | else: |
| 3004 | year = int(year) |
| 3005 | month = params.get('month', '-') |
| 3006 | if not month or month == '-': |
| 3007 | # ordinals are NOT normalized by mktime, we simulate them |
| 3008 | # by setting month=1, day=ordinal |
| 3009 | if ordinal: |
| 3010 | month = 1 |
| 3011 | else: |
| 3012 | month = time.gmtime()[1] |
| 3013 | month = int(month) |
| 3014 | day = params.get('day', 0) |
| 3015 | if not day: |
| 3016 | # see above |
| 3017 | if ordinal: |
| 3018 | day = ordinal |
| 3019 | elif params.get('century', 0) or \ |
| 3020 | params.get('year', 0) or params.get('month', 0): |
| 3021 | day = 1 |
| 3022 | else: |
| 3023 | day = time.gmtime()[2] |
| 3024 | else: |
| 3025 | day = int(day) |
| 3026 | # special case of the century - is the first year of the 21st century |
| 3027 | # 2000 or 2001 ? The debate goes on... |
| 3028 | if 'century' in params.keys(): |
| 3029 | year = (int(params['century']) - 1) * 100 + 1 |
| 3030 | # in ISO 8601 most fields are optional |
| 3031 | for field in ['hour', 'minute', 'second', 'tzhour', 'tzmin']: |
| 3032 | if not params.get(field, None): |
| 3033 | params[field] = 0 |
| 3034 | hour = int(params.get('hour', 0)) |
| 3035 | minute = int(params.get('minute', 0)) |
| 3036 | second = int(float(params.get('second', 0))) |
| 3037 | # weekday is normalized by mktime(), we can ignore it |
| 3038 | weekday = 0 |
| 3039 | daylight_savings_flag = -1 |
| 3040 | tm = [year, month, day, hour, minute, second, weekday, |
no test coverage detected