@@ -392,6 +392,25 @@ 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)
399
+ if tz is None :
400
+ check_overflows(obj)
401
+ 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
412
+
413
+
395
414
cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
396
415
bint dayfirst = False ,
397
416
bint yearfirst = False ):
@@ -450,25 +469,11 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
450
469
)
451
470
try :
452
471
if not string_to_dts_failed:
453
- obj.value = dtstruct_to_dt64(& obj.dts)
454
472
check_dts_bounds(& obj.dts)
473
+ obj.value = dtstruct_to_dt64(& obj.dts)
455
474
if out_local == 1 :
456
- obj.tzinfo = pytz.FixedOffset(out_tzoffset)
457
- obj.value = tz_convert_single(obj.value, obj.tzinfo, UTC)
458
- if tz is None :
459
- check_overflows(obj)
460
- return obj
461
- else :
462
- # Keep the converter same as PyDateTime's
463
- obj = convert_to_tsobject(obj.value, obj.tzinfo,
464
- None , 0 , 0 )
465
- dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day,
466
- obj.dts.hour, obj.dts.min, obj.dts.sec,
467
- obj.dts.us, obj.tzinfo)
468
- obj = convert_datetime_to_tsobject(
469
- dt, tz, nanos = obj.dts.ps // 1000 )
470
- return obj
471
-
475
+ return setup_tsobject_tz_using_offset(obj, tz,
476
+ out_tzoffset)
472
477
else :
473
478
ts = obj.value
474
479
if tz is not None :
@@ -485,13 +490,12 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit,
485
490
except ValueError :
486
491
do_parse_datetime_string = True
487
492
488
- finally :
489
- if string_to_dts_failed or do_parse_datetime_string:
490
- try :
491
- ts = parse_datetime_string(ts, dayfirst = dayfirst,
492
- yearfirst = yearfirst)
493
- except Exception :
494
- raise ValueError (" could not convert string to Timestamp" )
493
+ if string_to_dts_failed or do_parse_datetime_string:
494
+ try :
495
+ ts = parse_datetime_string(ts, dayfirst = dayfirst,
496
+ yearfirst = yearfirst)
497
+ except Exception :
498
+ raise ValueError (" could not convert string to Timestamp" )
495
499
496
500
return convert_to_tsobject(ts, tz, unit, dayfirst, yearfirst)
497
501
0 commit comments