Parse a variety of ISO-8601-compatible formats like 20040105
(dateString)
| 3132 | except NameError: |
| 3133 | pass |
| 3134 | def _parse_date_iso8601(dateString): |
| 3135 | '''Parse a variety of ISO-8601-compatible formats like 20040105''' |
| 3136 | m = None |
| 3137 | for _iso8601_match in _iso8601_matches: |
| 3138 | m = _iso8601_match(dateString) |
| 3139 | if m: |
| 3140 | break |
| 3141 | if not m: |
| 3142 | return |
| 3143 | if m.span() == (0, 0): |
| 3144 | return |
| 3145 | params = m.groupdict() |
| 3146 | ordinal = params.get('ordinal', 0) |
| 3147 | if ordinal: |
| 3148 | ordinal = int(ordinal) |
| 3149 | else: |
| 3150 | ordinal = 0 |
| 3151 | year = params.get('year', '--') |
| 3152 | if not year or year == '--': |
| 3153 | year = time.gmtime()[0] |
| 3154 | elif len(year) == 2: |
| 3155 | # ISO 8601 assumes current century, i.e. 93 -> 2093, NOT 1993 |
| 3156 | year = 100 * int(time.gmtime()[0] / 100) + int(year) |
| 3157 | else: |
| 3158 | year = int(year) |
| 3159 | month = params.get('month', '-') |
| 3160 | if not month or month == '-': |
| 3161 | # ordinals are NOT normalized by mktime, we simulate them |
| 3162 | # by setting month=1, day=ordinal |
| 3163 | if ordinal: |
| 3164 | month = 1 |
| 3165 | else: |
| 3166 | month = time.gmtime()[1] |
| 3167 | month = int(month) |
| 3168 | day = params.get('day', 0) |
| 3169 | if not day: |
| 3170 | # see above |
| 3171 | if ordinal: |
| 3172 | day = ordinal |
| 3173 | elif params.get('century', 0) or \ |
| 3174 | params.get('year', 0) or params.get('month', 0): |
| 3175 | day = 1 |
| 3176 | else: |
| 3177 | day = time.gmtime()[2] |
| 3178 | else: |
| 3179 | day = int(day) |
| 3180 | # special case of the century - is the first year of the 21st century |
| 3181 | # 2000 or 2001 ? The debate goes on... |
| 3182 | if 'century' in params: |
| 3183 | year = (int(params['century']) - 1) * 100 + 1 |
| 3184 | # in ISO 8601 most fields are optional |
| 3185 | for field in ['hour', 'minute', 'second', 'tzhour', 'tzmin']: |
| 3186 | if not params.get(field, None): |
| 3187 | params[field] = 0 |
| 3188 | hour = int(params.get('hour', 0)) |
| 3189 | minute = int(params.get('minute', 0)) |
| 3190 | second = int(float(params.get('second', 0))) |
| 3191 | # weekday is normalized by mktime(), we can ignore it |
no test coverage detected
searching dependent graphs…