Omega359 commented on code in PR #18990:
URL: https://github.com/apache/datafusion/pull/18990#discussion_r2750083691


##########
datafusion/functions/src/datetime/common.rs:
##########
@@ -23,22 +23,89 @@ use arrow::array::{
     StringArrayType, StringViewArray,
 };
 use arrow::compute::DecimalCast;
-use arrow::compute::kernels::cast_utils::string_to_datetime;
-use arrow::datatypes::{DataType, TimeUnit};
+use arrow::compute::kernels::cast_utils::{
+    string_to_datetime, string_to_timestamp_nanos,
+};
+use arrow::datatypes::{ArrowTimestampType, DataType, TimeUnit};
 use arrow_buffer::ArrowNativeType;
 use chrono::LocalResult::Single;
 use chrono::format::{Parsed, StrftimeItems, parse};
-use chrono::{DateTime, TimeZone, Utc};
+use chrono::{DateTime, MappedLocalTime, TimeDelta, TimeZone, Utc};
 use datafusion_common::cast::as_generic_string_array;
 use datafusion_common::{
     DataFusionError, Result, ScalarValue, exec_datafusion_err, exec_err,
     internal_datafusion_err, unwrap_or_internal_err,
 };
 use datafusion_expr::ColumnarValue;
+use std::ops::Add;
 
 /// Error message if nanosecond conversion request beyond supported interval
 const ERR_NANOSECONDS_NOT_SUPPORTED: &str = "The dates that can be represented 
as nanoseconds have to be between 1677-09-21T00:12:44.0 and 
2262-04-11T23:47:16.854775804";
 
+pub fn adjust_to_local_time<T: ArrowTimestampType>(ts: i64, tz: Tz) -> 
Result<i64> {
+    fn convert_timestamp<F>(ts: i64, converter: F) -> Result<DateTime<Utc>>
+    where
+        F: Fn(i64) -> MappedLocalTime<DateTime<Utc>>,
+    {
+        match converter(ts) {
+            MappedLocalTime::Ambiguous(earliest, latest) => exec_err!(
+                "Ambiguous timestamp. Do you mean {:?} or {:?}",
+                earliest,
+                latest
+            ),
+            MappedLocalTime::None => exec_err!(
+                "The local time does not exist because there is a gap in the 
local time."
+            ),
+            Single(date_time) => Ok(date_time),
+        }
+    }
+
+    let date_time = match T::UNIT {
+        TimeUnit::Nanosecond => Utc.timestamp_nanos(ts),
+        TimeUnit::Microsecond => convert_timestamp(ts, |ts| 
Utc.timestamp_micros(ts))?,
+        TimeUnit::Millisecond => convert_timestamp(ts, |ts| 
Utc.timestamp_millis_opt(ts))?,
+        TimeUnit::Second => convert_timestamp(ts, |ts| Utc.timestamp_opt(ts, 
0))?,
+    };
+
+    // Get the timezone offset for this datetime
+    let tz_offset = tz.offset_from_utc_datetime(&date_time.naive_utc());
+    // Convert offset to seconds - offset is formatted like "+01:00" or 
"-05:00"
+    let offset_str = format!("{tz_offset}");

Review Comment:
   Submitted a PR to your branch @codetyri0n 



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to