@@ -80,6 +80,9 @@ This file implements string parsing and creation for NumPy datetime.
80
80
// On failure will return -1 without incrementing
81
81
static int compare_format (const char * * format , int * characters_remaining ,
82
82
const char * compare_to , int n , const int exact ) {
83
+ if (exact == 2 ) {
84
+ return 0 ;
85
+ }
83
86
if (* characters_remaining < n ) {
84
87
// TODO(pandas-dev): PyErr to differentiate what went wrong
85
88
return -1 ;
@@ -137,13 +140,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
137
140
while (sublen > 0 && isspace (* substr )) {
138
141
++ substr ;
139
142
-- sublen ;
140
- if (exact != 2 ) {
141
- if (exact == 0 && !format_len ) {
142
- goto finish ;
143
- }
144
- if (compare_format (& format , & format_len , " " , 1 , exact )) {
145
- goto parse_error ;
146
- }
143
+ if (exact == 0 && !format_len ) {
144
+ goto finish ;
145
+ }
146
+ if (compare_format (& format , & format_len , " " , 1 , exact )) {
147
+ goto parse_error ;
147
148
}
148
149
}
149
150
@@ -158,13 +159,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
158
159
}
159
160
160
161
/* PARSE THE YEAR (4 digits) */
161
- if (exact != 2 ) {
162
- if (exact == 0 && !format_len ) {
163
- goto finish ;
164
- }
165
- if (compare_format (& format , & format_len , "%Y" , 2 , exact )) {
166
- goto parse_error ;
167
- }
162
+ if (exact == 0 && !format_len ) {
163
+ goto finish ;
164
+ }
165
+ if (compare_format (& format , & format_len , "%Y" , 2 , exact )) {
166
+ goto parse_error ;
168
167
}
169
168
170
169
out -> year = 0 ;
@@ -210,13 +209,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
210
209
++ substr ;
211
210
-- sublen ;
212
211
213
- if (exact != 2 ) {
214
- if (exact == 0 && !format_len ) {
215
- goto finish ;
216
- }
217
- if (compare_format (& format , & format_len , & ymd_sep , 1 , exact )) {
218
- goto parse_error ;
219
- }
212
+ if (exact == 0 && !format_len ) {
213
+ goto finish ;
214
+ }
215
+ if (compare_format (& format , & format_len , & ymd_sep , 1 , exact )) {
216
+ goto parse_error ;
220
217
}
221
218
/* Cannot have trailing separator */
222
219
if (sublen == 0 || !isdigit (* substr )) {
@@ -225,13 +222,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
225
222
}
226
223
227
224
/* PARSE THE MONTH */
228
- if (exact != 2 ) {
229
- if (exact == 0 && !format_len ) {
230
- goto finish ;
231
- }
232
- if (compare_format (& format , & format_len , "%m" , 2 , exact )) {
233
- goto parse_error ;
234
- }
225
+ if (exact == 0 && !format_len ) {
226
+ goto finish ;
227
+ }
228
+ if (compare_format (& format , & format_len , "%m" , 2 , exact )) {
229
+ goto parse_error ;
235
230
}
236
231
/* First digit required */
237
232
out -> month = (* substr - '0' );
@@ -276,25 +271,21 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
276
271
}
277
272
++ substr ;
278
273
-- sublen ;
279
- if (exact != 2 ) {
280
- if (exact == 0 && !format_len ) {
281
- goto finish ;
282
- }
283
- if (compare_format (& format , & format_len , & ymd_sep , 1 , exact )) {
284
- goto parse_error ;
285
- }
286
- }
287
- }
288
-
289
- /* PARSE THE DAY */
290
- if (exact != 2 ) {
291
274
if (exact == 0 && !format_len ) {
292
275
goto finish ;
293
276
}
294
- if (compare_format (& format , & format_len , "%d" , 2 , exact )) {
277
+ if (compare_format (& format , & format_len , & ymd_sep , 1 , exact )) {
295
278
goto parse_error ;
296
279
}
297
280
}
281
+
282
+ /* PARSE THE DAY */
283
+ if (exact == 0 && !format_len ) {
284
+ goto finish ;
285
+ }
286
+ if (compare_format (& format , & format_len , "%d" , 2 , exact )) {
287
+ goto parse_error ;
288
+ }
298
289
/* First digit required */
299
290
if (!isdigit (* substr )) {
300
291
goto parse_error ;
@@ -334,25 +325,21 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
334
325
if ((* substr != 'T' && * substr != ' ' ) || sublen == 1 ) {
335
326
goto parse_error ;
336
327
}
337
- if (exact != 2 ) {
338
- if (exact == 0 && !format_len ) {
339
- goto finish ;
340
- }
341
- if (compare_format (& format , & format_len , substr , 1 , exact )) {
342
- goto parse_error ;
343
- }
344
- }
345
- ++ substr ;
346
- -- sublen ;
347
-
348
- /* PARSE THE HOURS */
349
- if (exact != 2 ) {
350
328
if (exact == 0 && !format_len ) {
351
329
goto finish ;
352
330
}
353
- if (compare_format (& format , & format_len , "%H" , 2 , exact )) {
331
+ if (compare_format (& format , & format_len , substr , 1 , exact )) {
354
332
goto parse_error ;
355
333
}
334
+ ++ substr ;
335
+ -- sublen ;
336
+
337
+ /* PARSE THE HOURS */
338
+ if (exact == 0 && !format_len ) {
339
+ goto finish ;
340
+ }
341
+ if (compare_format (& format , & format_len , "%H" , 2 , exact )) {
342
+ goto parse_error ;
356
343
}
357
344
/* First digit required */
358
345
if (!isdigit (* substr )) {
@@ -397,13 +384,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
397
384
if (sublen == 0 || !isdigit (* substr )) {
398
385
goto parse_error ;
399
386
}
400
- if (exact != 2 ) {
401
- if (exact == 0 && !format_len ) {
402
- goto finish ;
403
- }
404
- if (compare_format (& format , & format_len , ":" , 1 , exact )) {
405
- goto parse_error ;
406
- }
387
+ if (exact == 0 && !format_len ) {
388
+ goto finish ;
389
+ }
390
+ if (compare_format (& format , & format_len , ":" , 1 , exact )) {
391
+ goto parse_error ;
407
392
}
408
393
} else if (!isdigit (* substr )) {
409
394
if (!hour_was_2_digits ) {
@@ -413,13 +398,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
413
398
}
414
399
415
400
/* PARSE THE MINUTES */
416
- if (exact != 2 ) {
417
- if (exact == 0 && !format_len ) {
418
- goto finish ;
419
- }
420
- if (compare_format (& format , & format_len , "%M" , 2 , exact )) {
421
- goto parse_error ;
422
- }
401
+ if (exact == 0 && !format_len ) {
402
+ goto finish ;
403
+ }
404
+ if (compare_format (& format , & format_len , "%M" , 2 , exact )) {
405
+ goto parse_error ;
423
406
}
424
407
/* First digit required */
425
408
out -> min = (* substr - '0' );
@@ -453,13 +436,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
453
436
/* If we make it through this condition block, then the next
454
437
* character is a digit. */
455
438
if (has_hms_sep && * substr == ':' ) {
456
- if (exact != 2 ) {
457
- if (exact == 0 && !format_len ) {
458
- goto finish ;
459
- }
460
- if (compare_format (& format , & format_len , ":" , 1 , exact )) {
461
- goto parse_error ;
462
- }
439
+ if (exact == 0 && !format_len ) {
440
+ goto finish ;
441
+ }
442
+ if (compare_format (& format , & format_len , ":" , 1 , exact )) {
443
+ goto parse_error ;
463
444
}
464
445
++ substr ;
465
446
-- sublen ;
@@ -473,13 +454,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
473
454
}
474
455
475
456
/* PARSE THE SECONDS */
476
- if (exact != 2 ) {
477
- if (exact == 0 && !format_len ) {
478
- goto finish ;
479
- }
480
- if (compare_format (& format , & format_len , "%S" , 2 , exact )) {
481
- goto parse_error ;
457
+ if (exact == 0 && !format_len ) {
458
+ goto finish ;
482
459
}
460
+ if (compare_format (& format , & format_len , "%S" , 2 , exact )) {
461
+ goto parse_error ;
483
462
}
484
463
/* First digit required */
485
464
out -> sec = (* substr - '0' );
@@ -506,27 +485,23 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
506
485
if (sublen > 0 && * substr == '.' ) {
507
486
++ substr ;
508
487
-- sublen ;
509
- if (exact != 2 ) {
510
- if (exact == 0 && !format_len ) {
511
- goto finish ;
512
- }
513
- if (compare_format (& format , & format_len , "." , 1 , exact )) {
514
- goto parse_error ;
515
- }
488
+ if (exact == 0 && !format_len ) {
489
+ goto finish ;
490
+ }
491
+ if (compare_format (& format , & format_len , "." , 1 , exact )) {
492
+ goto parse_error ;
516
493
}
517
494
} else {
518
495
bestunit = NPY_FR_s ;
519
496
goto parse_timezone ;
520
497
}
521
498
522
499
/* PARSE THE MICROSECONDS (0 to 6 digits) */
523
- if (exact != 2 ) {
524
- if (exact == 0 && !format_len ) {
525
- goto finish ;
526
- }
527
- if (compare_format (& format , & format_len , "%f" , 2 , exact )) {
528
- goto parse_error ;
500
+ if (exact == 0 && !format_len ) {
501
+ goto finish ;
529
502
}
503
+ if (compare_format (& format , & format_len , "%f" , 2 , exact )) {
504
+ goto parse_error ;
530
505
}
531
506
numdigits = 0 ;
532
507
for (i = 0 ; i < 6 ; ++ i ) {
@@ -592,13 +567,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
592
567
while (sublen > 0 && isspace (* substr )) {
593
568
++ substr ;
594
569
-- sublen ;
595
- if (exact != 2 ) {
596
- if (exact == 0 && !format_len ) {
597
- goto finish ;
598
- }
599
- if (compare_format (& format , & format_len , " " , 1 , exact )) {
600
- goto parse_error ;
601
- }
570
+ if (exact == 0 && !format_len ) {
571
+ goto finish ;
572
+ }
573
+ if (compare_format (& format , & format_len , " " , 1 , exact )) {
574
+ goto parse_error ;
602
575
}
603
576
}
604
577
@@ -612,13 +585,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
612
585
613
586
/* UTC specifier */
614
587
if (* substr == 'Z' ) {
615
- if (exact != 2 ) {
616
- if (exact == 0 && !format_len ) {
617
- goto finish ;
618
- }
619
- if (compare_format (& format , & format_len , "%z" , 2 , exact )) {
620
- goto parse_error ;
621
- }
588
+ if (exact == 0 && !format_len ) {
589
+ goto finish ;
590
+ }
591
+ if (compare_format (& format , & format_len , "%z" , 2 , exact )) {
592
+ goto parse_error ;
622
593
}
623
594
/* "Z" should be equivalent to tz offset "+00:00" */
624
595
if (out_local != NULL ) {
@@ -639,13 +610,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
639
610
-- sublen ;
640
611
}
641
612
} else if (* substr == '-' || * substr == '+' ) {
642
- if (exact != 2 ) {
643
- if (exact == 0 && !format_len ) {
644
- goto finish ;
645
- }
646
- if (compare_format (& format , & format_len , "%z" , 2 , exact )) {
647
- goto parse_error ;
648
- }
613
+ if (exact == 0 && !format_len ) {
614
+ goto finish ;
615
+ }
616
+ if (compare_format (& format , & format_len , "%z" , 2 , exact )) {
617
+ goto parse_error ;
649
618
}
650
619
/* Time zone offset */
651
620
int offset_neg = 0 , offset_hour = 0 , offset_minute = 0 ;
@@ -730,13 +699,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
730
699
while (sublen > 0 && isspace (* substr )) {
731
700
++ substr ;
732
701
-- sublen ;
733
- if (exact != 2 ) {
734
- if (exact == 0 && !format_len ) {
735
- goto finish ;
736
- }
737
- if (compare_format (& format , & format_len , " " , 1 , exact )) {
738
- goto parse_error ;
739
- }
702
+ if (exact == 0 && !format_len ) {
703
+ goto finish ;
704
+ }
705
+ if (compare_format (& format , & format_len , " " , 1 , exact )) {
706
+ goto parse_error ;
740
707
}
741
708
}
742
709
0 commit comments