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