|
99 | 99 | int MAX_DAYS_IN_MONTH = 31, MAX_MONTH = 12
|
100 | 100 |
|
101 | 101 |
|
102 |
| -cdef inline bint _is_not_delimiter(const char ch): |
103 |
| - return strchr(delimiters, ch) == NULL |
| 102 | +cdef inline bint _is_delimiter(const char ch): |
| 103 | + return strchr(delimiters, ch) != NULL |
| 104 | + |
| 105 | + |
| 106 | +cdef inline int _parse_1digit(const char* s): |
| 107 | + cdef int result = 0 |
| 108 | + result += getdigit_ascii(s[0], -10) * 1 |
| 109 | + return result |
104 | 110 |
|
105 | 111 |
|
106 | 112 | cdef inline int _parse_2digit(const char* s):
|
@@ -151,18 +157,37 @@ cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
|
151 | 157 | bint can_swap = 0
|
152 | 158 |
|
153 | 159 | buf = get_c_string_buf_and_size(date_string, &length)
|
154 |
| - if length == 10: |
| 160 | + if length == 10 and _is_delimiter(buf[2]) and _is_delimiter(buf[5]): |
155 | 161 | # parsing MM?DD?YYYY and DD?MM?YYYY dates
|
156 |
| - if _is_not_delimiter(buf[2]) or _is_not_delimiter(buf[5]): |
157 |
| - return None, None |
158 | 162 | month = _parse_2digit(buf)
|
159 | 163 | day = _parse_2digit(buf + 3)
|
160 | 164 | year = _parse_4digit(buf + 6)
|
161 | 165 | reso = 'day'
|
162 | 166 | can_swap = 1
|
163 |
| - elif length == 7: |
| 167 | + elif length == 9 and _is_delimiter(buf[1]) and _is_delimiter(buf[4]): |
| 168 | + # parsing M?DD?YYYY and D?MM?YYYY dates |
| 169 | + month = _parse_1digit(buf) |
| 170 | + day = _parse_2digit(buf + 2) |
| 171 | + year = _parse_4digit(buf + 5) |
| 172 | + reso = 'day' |
| 173 | + can_swap = 1 |
| 174 | + elif length == 9 and _is_delimiter(buf[2]) and _is_delimiter(buf[4]): |
| 175 | + # parsing MM?D?YYYY and DD?M?YYYY dates |
| 176 | + month = _parse_2digit(buf) |
| 177 | + day = _parse_1digit(buf + 3) |
| 178 | + year = _parse_4digit(buf + 5) |
| 179 | + reso = 'day' |
| 180 | + can_swap = 1 |
| 181 | + elif length == 8 and _is_delimiter(buf[1]) and _is_delimiter(buf[3]): |
| 182 | + # parsing M?D?YYYY and D?M?YYYY dates |
| 183 | + month = _parse_1digit(buf) |
| 184 | + day = _parse_1digit(buf + 2) |
| 185 | + year = _parse_4digit(buf + 4) |
| 186 | + reso = 'day' |
| 187 | + can_swap = 1 |
| 188 | + elif length == 7 and _is_delimiter(buf[2]): |
164 | 189 | # parsing MM?YYYY dates
|
165 |
| - if buf[2] == b'.' or _is_not_delimiter(buf[2]): |
| 190 | + if buf[2] == b'.': |
166 | 191 | # we cannot reliably tell whether e.g. 10.2010 is a float
|
167 | 192 | # or a date, thus we refuse to parse it here
|
168 | 193 | return None, None
|
|
0 commit comments