Repository navigation
Expand file tree
/
Copy path_strptime.c
More file actions
250 lines (241 loc) · 7.8 KB
/
Copy path_strptime.c
File metadata and controls
250 lines (241 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "Python.h"
#include "pycore_time.h"
/* Implement common parts of strptime in C to improve performance. Callers
* fall back to Lib/_strptime.py for a full parse when this doesn't match.
*
* Supported inputs are exact str objects containing only ASCII, with:
* - numeric %Y, %y, %m, %d, %H, %M, %S, and %f fields;
* - a terminal %z: empty, Z, or +/-HHMM or +/-HH:MM with hours below 24;
* - literal characters, %%, and ASCII whitespace.
*
* Duplicate directives, mixed %Y/%y, and day-of-month without a year use the
* fallback. So do other directives (including locale-dependent names and
* week/day-of-year calculations), and offsets with seconds or fractions.
*/
static int
strptime_digits(const unsigned char *data, Py_ssize_t length, Py_ssize_t pos,
int minimum, int maximum, int *value)
{
int count = 0;
*value = 0;
while (count < maximum && pos + count < length) {
unsigned char c = data[pos + count];
if (c < '0' || c > '9') {
break;
}
*value = *value * 10 + c - '0';
count++;
}
return count >= minimum ? count : 0;
}
static int
strptime_offset(const unsigned char *data, Py_ssize_t length, Py_ssize_t pos,
_PyTime_StrptimeFields *fields)
{
Py_ssize_t remaining = length - pos;
if (remaining == 0) {
return 1;
}
if (remaining == 1 && data[pos] == 'Z') {
fields->gmtoff = 0;
return 1;
}
/* Offsets containing seconds or fractions fall back to _strptime.py. */
if ((remaining != 5 && remaining != 6) ||
(data[pos] != '+' && data[pos] != '-')) {
return 0;
}
int hours, minutes;
int colon = remaining == 6;
if ((colon && data[pos + 3] != ':') ||
strptime_digits(data, length, pos + 1, 2, 2, &hours) != 2 ||
strptime_digits(data, length, pos + 3 + colon, 2, 2, &minutes) != 2 ||
hours > 23 || minutes > 59) {
return 0;
}
fields->gmtoff = (hours * 3600 + minutes * 60) *
(data[pos] == '-' ? -1 : 1);
return 1;
}
/* Return 1 for a complete numeric match, or 0 if fallback is required.
*
* Parsing doesn't allocate or set exceptions. Some mismatches require regex
* backtracking, but even inputs known to be invalid use the Python parser:
* it owns the exception messages and error precedence. For example, month 13
* and empty input can't be repaired by backtracking, but diagnosing them here
* would duplicate the Python parser's error handling. A return value of 0
* therefore doesn't distinguish invalid input from an unsupported format.
*/
int
_PyTime_Strptime(PyObject *string, PyObject *format, _PyTime_StrptimeFields *fields)
{
enum {
YEAR = 1U << 0,
SHORT_YEAR = 1U << 1,
MONTH = 1U << 2,
DAY = 1U << 3,
HOUR = 1U << 4,
MINUTE = 1U << 5,
SECOND = 1U << 6,
FRACTION = 1U << 7,
OFFSET = 1U << 8,
ANY_YEAR = YEAR | SHORT_YEAR,
};
static const unsigned short directive_bits[128] = {
['Y'] = YEAR,
['y'] = SHORT_YEAR,
['m'] = MONTH,
['d'] = DAY,
['H'] = HOUR,
['M'] = MINUTE,
['S'] = SECOND,
['f'] = FRACTION,
['z'] = OFFSET,
};
/* String subclasses and non-ASCII input retain Python's behavior. */
if (!PyUnicode_CheckExact(string) || !PyUnicode_CheckExact(format) ||
!PyUnicode_IS_ASCII(string) || !PyUnicode_IS_ASCII(format)) {
return 0;
}
const unsigned char *data = PyUnicode_1BYTE_DATA(string);
const unsigned char *fmt = PyUnicode_1BYTE_DATA(format);
Py_ssize_t length = PyUnicode_GET_LENGTH(string);
Py_ssize_t fmt_length = PyUnicode_GET_LENGTH(format);
*fields = (_PyTime_StrptimeFields){.year = 1900, .month = 1, .day = 1, .gmtoff = INT_MIN};
unsigned int seen = 0;
Py_ssize_t pos = 0;
for (Py_ssize_t i = 0; i < fmt_length; i++) {
unsigned char c = fmt[i];
/* Match regex \s, including ASCII separators U+001C through U+001F. */
if (Py_UNICODE_ISSPACE(c)) {
if (pos == length || !Py_UNICODE_ISSPACE(data[pos])) {
return 0;
}
while (i + 1 < fmt_length && Py_UNICODE_ISSPACE(fmt[i + 1])) {
i++;
}
do {
pos++;
} while (pos < length && Py_UNICODE_ISSPACE(data[pos]));
continue;
}
if (c != '%') {
if (pos == length || data[pos++] != c) {
return 0;
}
continue;
}
if (++i == fmt_length) {
return 0;
}
c = fmt[i];
if (c == '%') {
if (pos == length || data[pos++] != '%') {
return 0;
}
continue;
}
/* Reject duplicate groups, aliases, and locale-dependent directives. */
unsigned int bit = directive_bits[c];
if (bit == 0) {
return 0;
}
if (seen & bit) {
return 0;
}
seen |= bit;
if (c == 'z') {
if (i != fmt_length - 1 ||
!strptime_offset(data, length, pos, fields)) {
return 0;
}
pos = length;
continue;
}
int minimum = 1;
int maximum = 2;
if (c == 'Y') {
minimum = maximum = 4;
}
else if (c == 'y') {
minimum = maximum = 2;
}
else if (c == 'f') {
maximum = 6;
}
else if ((c == 'd' || c == 'H') && pos < length && data[pos] == ' ') {
pos++;
maximum = 1;
}
int value;
int count = strptime_digits(data, length, pos, minimum, maximum, &value);
if (count == 0) {
return 0;
}
pos += count;
switch (c) {
case 'Y':
fields->year = value;
break;
case 'y':
fields->year = value + (value <= 68 ? 2000 : 1900);
break;
case 'm':
if (value < 1 || value > 12) {
return 0;
}
fields->month = value;
break;
case 'd':
if (value < 1 || value > 31) {
return 0;
}
fields->day = value;
break;
case 'H':
if (value > 23) {
return 0;
}
fields->hour = value;
break;
case 'M':
if (value > 59) {
return 0;
}
fields->minute = value;
break;
case 'S':
if (value > 61) {
return 0;
}
fields->second = value;
break;
case 'f':
for (; count < 6; count++) {
value *= 10;
}
fields->fraction = value;
break;
}
}
static const int days_before_month[] = {
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
};
int leap = fields->year % 4 == 0 &&
(fields->year % 100 != 0 || fields->year % 400 == 0);
int month_days = days_before_month[fields->month + 1] -
days_before_month[fields->month];
if (fields->month == 2) {
month_days += leap;
}
/* Mixed %Y/%y and day-without-year diagnostics belong to Python. */
if (pos != length || (seen & ANY_YEAR) == ANY_YEAR ||
((seen & DAY) && !(seen & ANY_YEAR)) ||
fields->year < 1 ||
fields->day > month_days) {
return 0;
}
fields->yday = days_before_month[fields->month] + fields->day +
(fields->month > 2 && leap);
return 1;
}