mbutrovich commented on code in PR #304:
URL: https://github.com/apache/datafusion-spark/pull/304#discussion_r4605593835


##########
crates/spark/src/function/datetime/timezone.rs:
##########
@@ -105,6 +174,68 @@ impl SparkTimeZone {
             }
         }
     }
+
+    /// Converts `local`, a local date-time in this time zone encoded as if it
+    /// were UTC, to UTC, the way Java's `LocalDateTime.atZone` does. The
+    /// inverse of [`Self::adjust_to_local_time`].
+    ///
+    /// A local time that happens twice, when clocks go back, resolves to the
+    /// earlier offset. A local time in the gap when clocks go forward moves
+    /// forward by the length of the gap.
+    pub(crate) fn local_to_utc<T: ArrowTimestampType>(self, local: i64) -> 
Result<i64> {
+        let units = units_per_second::<T>();
+        let shift = |offset_seconds: i32| {
+            local
+                .checked_sub(i64::from(offset_seconds) * units)
+                .ok_or_else(|| exec_datafusion_err!("Timestamp {local} 
overflows"))
+        };
+        match self {
+            Self::FixedOffset(seconds) => shift(seconds),
+            Self::Region(tz) => {
+                let local_datetime =
+                    DateTime::<Utc>::from_timestamp(local.div_euclid(units), 0)
+                        .ok_or_else(|| {
+                            exec_datafusion_err!(
+                                "Timestamp is outside Chrono's calendar range"
+                            )
+                        })?
+                        .naive_utc();
+                let offset = region_offset_at_local(&tz, local_datetime);
+                shift(offset)
+            }
+            Self::SystemV(standard_offset) => {
+                // Use daylight time if the result is in daylight time. That 
picks
+                // the earlier offset in the overlap, and standard time, the
+                // offset before the transition, in the gap.
+                let daylight_offset = standard_offset + 3600;
+                let utc = shift(daylight_offset)?;
+                if system_v_offset::<T>(utc, standard_offset)? == 
daylight_offset {
+                    Ok(utc)
+                } else {
+                    shift(standard_offset)
+                }
+            }
+        }
+    }
+}
+
+/// The offset of `tz`, in seconds, at the local date-time `local`, resolved as
+/// Java's `LocalDateTime.atZone` does. The fraction of a second does not 
affect
+/// the offset, so `local` is in whole seconds.
+fn region_offset_at_local(tz: &Tz, local: NaiveDateTime) -> i32 {
+    let offset_of = |result: LocalResult<DateTime<Tz>>| match result {
+        LocalResult::Single(dt) | LocalResult::Ambiguous(dt, _) => {
+            Some(dt.offset().fix().local_minus_utc())
+        }
+        LocalResult::None => None,
+    };
+    offset_of(tz.from_local_datetime(&local))
+        // In a gap, apply the offset in effect before it, which moves the time
+        // forward by the length of the gap.
+        .or_else(|| {
+            offset_of(tz.from_local_datetime(&(local - 
chrono::Duration::hours(3))))
+        })
+        .unwrap_or(0)

Review Comment:
   > Taking the offset from the UTC side 14 hours back can't fail, so there's 
no fallback to 0 any more.
   
   I compared this with Java's `LocalDateTime.atZone`, which Spark's 
`convertTz` uses, at five local times around each of the 22,269 gaps in 539 
zones, and every local time that chrono-tz places in a gap gets Java's result, 
including the 17 zones with gaps longer than three hours.



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