aprimadi commented on code in PR #4201:
URL: https://github.com/apache/arrow-rs/pull/4201#discussion_r1192426649
##########
arrow-cast/src/cast.rs:
##########
@@ -3005,6 +3049,34 @@ fn cast_string_to_month_day_nano_interval<Offset:
OffsetSizeTrait>(
Ok(Arc::new(interval_array) as ArrayRef)
}
+fn adjust_timestamp_to_timezone<
+ T: ArrowTimestampType,
+>(
+ array: PrimitiveArray<Int64Type>,
+ to_tz: &Tz,
+ cast_options: &CastOptions,
+) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
+ let adjusted = if cast_options.safe {
+ array.unary_opt::<_, Int64Type>(|o| {
+ let local = as_datetime::<T>(o)?;
+ let offset = to_tz.offset_from_local_datetime(&local).single()?;
+ T::make_value(local - offset.fix())
+ })
+ } else {
+ let error_fn = || {
+ ArrowError::CastError(
+ "Cannot cast timezone to different timezone".to_string(),
+ )
+ };
+ array.try_unary::<_, Int64Type, _>(|o| {
+ let local = as_datetime::<T>(o).ok_or_else(error_fn)?;
+ let offset =
to_tz.offset_from_local_datetime(&local).single().ok_or_else(error_fn)?;
+ T::make_value(local - offset.fix()).ok_or_else(error_fn)
+ })?
+ };
Review Comment:
Yes, I agree this looks a lot nicer 👍
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]