@@ -318,26 +318,19 @@ int cmp_pandas_datetimestruct(const pandas_datetimestruct *a,
318
318
/*
319
319
*
320
320
* Tests for and converts a Python datetime.datetime or datetime.date
321
- * object into a NumPy pandas_datetimestruct.
321
+ * object into a NumPy pandas_datetimestruct. Uses tzinfo (if present)
322
+ * to convert to UTC time.
322
323
*
323
324
* While the C API has PyDate_* and PyDateTime_* functions, the following
324
325
* implementation just asks for attributes, and thus supports
325
326
* datetime duck typing. The tzinfo time zone conversion would require
326
327
* this style of access anyway.
327
328
*
328
- * 'out_bestunit' gives a suggested unit based on whether the object
329
- * was a datetime.date or datetime.datetime object.
330
- *
331
- * If 'apply_tzinfo' is 1, this function uses the tzinfo to convert
332
- * to UTC time, otherwise it returns the struct with the local time.
333
- *
334
329
* Returns -1 on error, 0 on success, and 1 (with no error set)
335
330
* if obj doesn't have the neeeded date or datetime attributes.
336
331
*/
337
332
int convert_pydatetime_to_datetimestruct (PyObject * obj ,
338
- pandas_datetimestruct * out ,
339
- PANDAS_DATETIMEUNIT * out_bestunit ,
340
- int apply_tzinfo ) {
333
+ pandas_datetimestruct * out ) {
341
334
PyObject * tmp ;
342
335
int isleap ;
343
336
@@ -404,10 +397,6 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
404
397
!PyObject_HasAttrString (obj , "minute" ) ||
405
398
!PyObject_HasAttrString (obj , "second" ) ||
406
399
!PyObject_HasAttrString (obj , "microsecond" )) {
407
- /* The best unit for date is 'D' */
408
- if (out_bestunit != NULL ) {
409
- * out_bestunit = PANDAS_FR_D ;
410
- }
411
400
return 0 ;
412
401
}
413
402
@@ -465,7 +454,7 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
465
454
}
466
455
467
456
/* Apply the time zone offset if it exists */
468
- if (apply_tzinfo && PyObject_HasAttrString (obj , "tzinfo" )) {
457
+ if (PyObject_HasAttrString (obj , "tzinfo" )) {
469
458
tmp = PyObject_GetAttrString (obj , "tzinfo" );
470
459
if (tmp == NULL ) {
471
460
return -1 ;
@@ -506,11 +495,6 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
506
495
}
507
496
}
508
497
509
- /* The resolution of Python's datetime is 'us' */
510
- if (out_bestunit != NULL ) {
511
- * out_bestunit = PANDAS_FR_us ;
512
- }
513
-
514
498
return 0 ;
515
499
516
500
invalid_date :
@@ -529,51 +513,34 @@ int convert_pydatetime_to_datetimestruct(PyObject *obj,
529
513
530
514
npy_datetime pandas_datetimestruct_to_datetime (PANDAS_DATETIMEUNIT fr ,
531
515
pandas_datetimestruct * d ) {
532
- pandas_datetime_metadata meta ;
533
516
npy_datetime result = PANDAS_DATETIME_NAT ;
534
517
535
- meta .base = fr ;
536
- meta .num = 1 ;
537
-
538
- convert_datetimestruct_to_datetime (& meta , d , & result );
518
+ convert_datetimestruct_to_datetime (fr , d , & result );
539
519
return result ;
540
520
}
541
521
542
522
void pandas_datetime_to_datetimestruct (npy_datetime val , PANDAS_DATETIMEUNIT fr ,
543
523
pandas_datetimestruct * result ) {
544
- pandas_datetime_metadata meta ;
545
-
546
- meta .base = fr ;
547
- meta .num = 1 ;
548
-
549
- convert_datetime_to_datetimestruct (& meta , val , result );
524
+ convert_datetime_to_datetimestruct (fr , val , result );
550
525
}
551
526
552
527
void pandas_timedelta_to_timedeltastruct (npy_timedelta val ,
553
528
PANDAS_DATETIMEUNIT fr ,
554
529
pandas_timedeltastruct * result ) {
555
- pandas_datetime_metadata meta ;
556
-
557
- meta .base = fr ;
558
- meta .num = 1 ;
559
-
560
- convert_timedelta_to_timedeltastruct (& meta , val , result );
530
+ convert_timedelta_to_timedeltastruct (fr , val , result );
561
531
}
562
532
563
533
564
534
/*
565
535
* Converts a datetime from a datetimestruct to a datetime based
566
- * on some metadata. The date is assumed to be valid.
567
- *
568
- * TODO: If meta->num is really big, there could be overflow
536
+ * on a metadata unit. The date is assumed to be valid.
569
537
*
570
538
* Returns 0 on success, -1 on failure.
571
539
*/
572
- int convert_datetimestruct_to_datetime (pandas_datetime_metadata * meta ,
540
+ int convert_datetimestruct_to_datetime (PANDAS_DATETIMEUNIT base ,
573
541
const pandas_datetimestruct * dts ,
574
542
npy_datetime * out ) {
575
543
npy_datetime ret ;
576
- PANDAS_DATETIMEUNIT base = meta -> base ;
577
544
578
545
if (base == PANDAS_FR_Y ) {
579
546
/* Truncate to the year */
@@ -665,15 +632,6 @@ int convert_datetimestruct_to_datetime(pandas_datetime_metadata *meta,
665
632
}
666
633
}
667
634
668
- /* Divide by the multiplier */
669
- if (meta -> num > 1 ) {
670
- if (ret >= 0 ) {
671
- ret /= meta -> num ;
672
- } else {
673
- ret = (ret - meta -> num + 1 ) / meta -> num ;
674
- }
675
- }
676
-
677
635
* out = ret ;
678
636
679
637
return 0 ;
@@ -682,7 +640,7 @@ int convert_datetimestruct_to_datetime(pandas_datetime_metadata *meta,
682
640
/*
683
641
* Converts a datetime based on the given metadata into a datetimestruct
684
642
*/
685
- int convert_datetime_to_datetimestruct (pandas_datetime_metadata * meta ,
643
+ int convert_datetime_to_datetimestruct (PANDAS_DATETIMEUNIT base ,
686
644
npy_datetime dt ,
687
645
pandas_datetimestruct * out ) {
688
646
npy_int64 perday ;
@@ -693,14 +651,11 @@ int convert_datetime_to_datetimestruct(pandas_datetime_metadata *meta,
693
651
out -> month = 1 ;
694
652
out -> day = 1 ;
695
653
696
- /* TODO: Change to a mechanism that avoids the potential overflow */
697
- dt *= meta -> num ;
698
-
699
654
/*
700
655
* Note that care must be taken with the / and % operators
701
656
* for negative values.
702
657
*/
703
- switch (meta -> base ) {
658
+ switch (base ) {
704
659
case PANDAS_FR_Y :
705
660
out -> year = 1970 + dt ;
706
661
break ;
@@ -902,11 +857,11 @@ int convert_datetime_to_datetimestruct(pandas_datetime_metadata *meta,
902
857
903
858
/*
904
859
* Converts a timedelta from a timedeltastruct to a timedelta based
905
- * on some metadata. The timedelta is assumed to be valid.
860
+ * on a metadata unit . The timedelta is assumed to be valid.
906
861
*
907
862
* Returns 0 on success, -1 on failure.
908
863
*/
909
- int convert_timedelta_to_timedeltastruct (pandas_timedelta_metadata * meta ,
864
+ int convert_timedelta_to_timedeltastruct (PANDAS_DATETIMEUNIT base ,
910
865
npy_timedelta td ,
911
866
pandas_timedeltastruct * out ) {
912
867
npy_int64 frac ;
@@ -918,7 +873,7 @@ int convert_timedelta_to_timedeltastruct(pandas_timedelta_metadata *meta,
918
873
/* Initialize the output to all zeros */
919
874
memset (out , 0 , sizeof (pandas_timedeltastruct ));
920
875
921
- switch (meta -> base ) {
876
+ switch (base ) {
922
877
case PANDAS_FR_ns :
923
878
924
879
// put frac in seconds
0 commit comments