@@ -392,23 +392,42 @@ cdef _TSObject convert_datetime_to_tsobject(datetime ts, object tz,
392
392
return obj
393
393
394
394
395
- cdef _TSObject setup_tsobject_tz_using_offset(_TSObject obj,
396
- object tz, int tzoffset):
397
- obj.tzinfo = pytz.FixedOffset(tzoffset)
398
- obj.value = tz_convert_single(obj.value, obj.tzinfo, UTC)
395
+ cdef _TSObject create_tsobject_tz_using_offset(int64_t value,
396
+ object tz, int tzoffset):
397
+ """
398
+ Create tsobject from numpy datetime64 using initial timezone offset
399
+
400
+ Parameters
401
+ ----------
402
+ value: int64_t
403
+ numpy dt64
404
+ tz : tzinfo or None
405
+ timezone for the timezone-aware output.
406
+ tzoffset: int
407
+
408
+ Returns
409
+ obj : _TSObject
410
+ -------
411
+
412
+ """
413
+ cdef:
414
+ _TSObject obj
415
+ datetime dt
416
+
417
+ tzinfo = pytz.FixedOffset(tzoffset)
418
+ value = tz_convert_single(value, tzinfo, UTC)
419
+ obj = convert_to_tsobject(value, tzinfo, None , 0 , 0 )
399
420
if tz is None :
400
421
check_overflows(obj)
401
422
return obj
402
- else :
403
- # Keep the converter same as PyDateTime's
404
- obj = convert_to_tsobject(obj.value, obj.tzinfo,
405
- None , 0 , 0 )
406
- dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day,
407
- obj.dts.hour, obj.dts.min, obj.dts.sec,
408
- obj.dts.us, obj.tzinfo)
409
- obj = convert_datetime_to_tsobject(
410
- dt, tz, nanos = obj.dts.ps // 1000 )
411
- return obj
423
+
424
+ # Keep the converter same as PyDateTime's
425
+ dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day,
426
+ obj.dts.hour, obj.dts.min, obj.dts.sec,
427
+ obj.dts.us, obj.tzinfo)
428
+ obj = convert_datetime_to_tsobject(
429
+ dt, tz, nanos = obj.dts.ps // 1000 )
430
+ return obj
412
431
413
432
414
433
cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
@@ -439,16 +458,14 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
439
458
obj : _TSObject
440
459
"""
441
460
cdef:
442
- _TSObject obj
461
+ npy_datetimestruct dts
462
+ int64_t value # numpy dt64
443
463
int out_local = 0 , out_tzoffset = 0
444
- datetime dt
445
464
bint do_parse_datetime_string = False
446
465
447
466
if tz is not None :
448
467
tz = maybe_get_tz(tz)
449
468
450
- obj = _TSObject()
451
-
452
469
assert isinstance (ts, str )
453
470
454
471
if len (ts) == 0 or ts in nat_strings:
@@ -464,18 +481,18 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
464
481
# equiv: datetime.today().replace(tzinfo=tz)
465
482
else :
466
483
string_to_dts_failed = _string_to_dts(
467
- ts, & obj. dts, & out_local,
484
+ ts, & dts, & out_local,
468
485
& out_tzoffset, False
469
486
)
470
487
try :
471
488
if not string_to_dts_failed:
472
- check_dts_bounds(& obj. dts)
473
- obj. value = dtstruct_to_dt64(& obj. dts)
489
+ check_dts_bounds(& dts)
490
+ value = dtstruct_to_dt64(& dts)
474
491
if out_local == 1 :
475
- return setup_tsobject_tz_using_offset(obj , tz,
476
- out_tzoffset)
492
+ return create_tsobject_tz_using_offset(value , tz,
493
+ out_tzoffset)
477
494
else :
478
- ts = obj. value
495
+ ts = value
479
496
if tz is not None :
480
497
# shift for localize_tso
481
498
ts = tz_localize_to_utc(np.array([ts], dtype = ' i8' ), tz,
0 commit comments