@@ -71,20 +71,13 @@ This file implements string parsing and creation for NumPy datetime.
71
71
// and decrement characters_remaining by n on success
72
72
// On failure will return -1 without incrementing
73
73
static int compare_format (const char * * format , int * characters_remaining ,
74
- const char * compare_to , int n , const int exact ) {
74
+ const char * compare_to , int n , const enum Exact exact ) {
75
+ if (exact == NO_MATCH ) {
76
+ return 0 ;
77
+ }
75
78
if (* characters_remaining < n ) {
76
- if (exact ) {
77
- // TODO(pandas-dev): in the future we should set a PyErr here
78
- // to be very clear about what went wrong
79
- return -1 ;
80
- } else if (* characters_remaining ) {
81
- // TODO(pandas-dev): same return value in this function as
82
- // above branch, but stub out a future where
83
- // we have a better error message
84
- return -1 ;
85
- } else {
86
- return 0 ;
87
- }
79
+ // TODO(pandas-dev): PyErr to differentiate what went wrong
80
+ return -1 ;
88
81
} else {
89
82
if (strncmp (* format , compare_to , n )) {
90
83
// TODO(pandas-dev): PyErr to differentiate what went wrong
@@ -102,7 +95,8 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
102
95
npy_datetimestruct * out ,
103
96
NPY_DATETIMEUNIT * out_bestunit ,
104
97
int * out_local , int * out_tzoffset ,
105
- const char * format , int format_len , int exact ) {
98
+ const char * format , int format_len ,
99
+ enum Exact exact ) {
106
100
if (len < 0 || format_len < 0 )
107
101
goto parse_error ;
108
102
int year_leap = 0 ;
@@ -139,6 +133,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
139
133
while (sublen > 0 && isspace (* substr )) {
140
134
++ substr ;
141
135
-- sublen ;
136
+ if (exact == PARTIAL_MATCH && !format_len ) {
137
+ goto finish ;
138
+ }
142
139
if (compare_format (& format , & format_len , " " , 1 , exact )) {
143
140
goto parse_error ;
144
141
}
@@ -155,6 +152,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
155
152
}
156
153
157
154
/* PARSE THE YEAR (4 digits) */
155
+ if (exact == PARTIAL_MATCH && !format_len ) {
156
+ goto finish ;
157
+ }
158
158
if (compare_format (& format , & format_len , "%Y" , 2 , exact )) {
159
159
goto parse_error ;
160
160
}
@@ -202,6 +202,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
202
202
++ substr ;
203
203
-- sublen ;
204
204
205
+ if (exact == PARTIAL_MATCH && !format_len ) {
206
+ goto finish ;
207
+ }
205
208
if (compare_format (& format , & format_len , & ymd_sep , 1 , exact )) {
206
209
goto parse_error ;
207
210
}
@@ -212,6 +215,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
212
215
}
213
216
214
217
/* PARSE THE MONTH */
218
+ if (exact == PARTIAL_MATCH && !format_len ) {
219
+ goto finish ;
220
+ }
215
221
if (compare_format (& format , & format_len , "%m" , 2 , exact )) {
216
222
goto parse_error ;
217
223
}
@@ -258,12 +264,18 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
258
264
}
259
265
++ substr ;
260
266
-- sublen ;
267
+ if (exact == PARTIAL_MATCH && !format_len ) {
268
+ goto finish ;
269
+ }
261
270
if (compare_format (& format , & format_len , & ymd_sep , 1 , exact )) {
262
271
goto parse_error ;
263
272
}
264
273
}
265
274
266
275
/* PARSE THE DAY */
276
+ if (exact == PARTIAL_MATCH && !format_len ) {
277
+ goto finish ;
278
+ }
267
279
if (compare_format (& format , & format_len , "%d" , 2 , exact )) {
268
280
goto parse_error ;
269
281
}
@@ -306,13 +318,19 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
306
318
if ((* substr != 'T' && * substr != ' ' ) || sublen == 1 ) {
307
319
goto parse_error ;
308
320
}
321
+ if (exact == PARTIAL_MATCH && !format_len ) {
322
+ goto finish ;
323
+ }
309
324
if (compare_format (& format , & format_len , substr , 1 , exact )) {
310
325
goto parse_error ;
311
326
}
312
327
++ substr ;
313
328
-- sublen ;
314
329
315
330
/* PARSE THE HOURS */
331
+ if (exact == PARTIAL_MATCH && !format_len ) {
332
+ goto finish ;
333
+ }
316
334
if (compare_format (& format , & format_len , "%H" , 2 , exact )) {
317
335
goto parse_error ;
318
336
}
@@ -359,6 +377,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
359
377
if (sublen == 0 || !isdigit (* substr )) {
360
378
goto parse_error ;
361
379
}
380
+ if (exact == PARTIAL_MATCH && !format_len ) {
381
+ goto finish ;
382
+ }
362
383
if (compare_format (& format , & format_len , ":" , 1 , exact )) {
363
384
goto parse_error ;
364
385
}
@@ -370,6 +391,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
370
391
}
371
392
372
393
/* PARSE THE MINUTES */
394
+ if (exact == PARTIAL_MATCH && !format_len ) {
395
+ goto finish ;
396
+ }
373
397
if (compare_format (& format , & format_len , "%M" , 2 , exact )) {
374
398
goto parse_error ;
375
399
}
@@ -405,6 +429,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
405
429
/* If we make it through this condition block, then the next
406
430
* character is a digit. */
407
431
if (has_hms_sep && * substr == ':' ) {
432
+ if (exact == PARTIAL_MATCH && !format_len ) {
433
+ goto finish ;
434
+ }
408
435
if (compare_format (& format , & format_len , ":" , 1 , exact )) {
409
436
goto parse_error ;
410
437
}
@@ -420,6 +447,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
420
447
}
421
448
422
449
/* PARSE THE SECONDS */
450
+ if (exact == PARTIAL_MATCH && !format_len ) {
451
+ goto finish ;
452
+ }
423
453
if (compare_format (& format , & format_len , "%S" , 2 , exact )) {
424
454
goto parse_error ;
425
455
}
@@ -448,6 +478,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
448
478
if (sublen > 0 && * substr == '.' ) {
449
479
++ substr ;
450
480
-- sublen ;
481
+ if (exact == PARTIAL_MATCH && !format_len ) {
482
+ goto finish ;
483
+ }
451
484
if (compare_format (& format , & format_len , "." , 1 , exact )) {
452
485
goto parse_error ;
453
486
}
@@ -457,6 +490,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
457
490
}
458
491
459
492
/* PARSE THE MICROSECONDS (0 to 6 digits) */
493
+ if (exact == PARTIAL_MATCH && !format_len ) {
494
+ goto finish ;
495
+ }
460
496
if (compare_format (& format , & format_len , "%f" , 2 , exact )) {
461
497
goto parse_error ;
462
498
}
@@ -524,6 +560,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
524
560
while (sublen > 0 && isspace (* substr )) {
525
561
++ substr ;
526
562
-- sublen ;
563
+ if (exact == PARTIAL_MATCH && !format_len ) {
564
+ goto finish ;
565
+ }
527
566
if (compare_format (& format , & format_len , " " , 1 , exact )) {
528
567
goto parse_error ;
529
568
}
@@ -539,6 +578,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
539
578
540
579
/* UTC specifier */
541
580
if (* substr == 'Z' ) {
581
+ if (exact == PARTIAL_MATCH && !format_len ) {
582
+ goto finish ;
583
+ }
542
584
if (compare_format (& format , & format_len , "%z" , 2 , exact )) {
543
585
goto parse_error ;
544
586
}
@@ -561,6 +603,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
561
603
-- sublen ;
562
604
}
563
605
} else if (* substr == '-' || * substr == '+' ) {
606
+ if (exact == PARTIAL_MATCH && !format_len ) {
607
+ goto finish ;
608
+ }
564
609
if (compare_format (& format , & format_len , "%z" , 2 , exact )) {
565
610
goto parse_error ;
566
611
}
@@ -647,6 +692,9 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
647
692
while (sublen > 0 && isspace (* substr )) {
648
693
++ substr ;
649
694
-- sublen ;
695
+ if (exact == PARTIAL_MATCH && !format_len ) {
696
+ goto finish ;
697
+ }
650
698
if (compare_format (& format , & format_len , " " , 1 , exact )) {
651
699
goto parse_error ;
652
700
}
0 commit comments