Skip to content

Commit

Permalink
Feature: function about convert_timezone databendlabs#16177
Browse files Browse the repository at this point in the history
  • Loading branch information
florann committed Aug 4, 2024
1 parent a65013d commit 499a4ac
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/query/expression/src/utils/date_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ impl TzLUT {
pub trait DateConverter {
fn to_date(&self, tz: Tz) -> NaiveDate;
fn to_timestamp(&self, tz: Tz) -> DateTime<Tz>;
fn convert_timezone(&self, target_tz: Tz, src_timestamp: DateTime<Tz>, src_tz: Option<Tz>, src_ntz_timestamp: Option<DateTime<Tz>>) -> DateTime<Tz>;
}

impl<T> DateConverter for T
Expand All @@ -262,6 +263,31 @@ where T: AsPrimitive<i64>
}
tz.timestamp_opt(secs, nanos as u32).unwrap()
}
/// Convert a timestamp to the specify timezone.
///
/// # Parameters
/// - `target_tz`: Timezone in which the timestamp will be converted
/// - `src_timestamp`: Source timestamp to be converted
/// - `src_tz`: Timezone of the timestamp to be converted - Optional
/// - `src_ntz_timestamp`: Source timestamp with unspecified timezone to be converted - Optional
fn convert_timezone(&self, target_tz: Tz, src_timestamp: DateTime<Tz>, src_tz: Option<Tz>, src_ntz_timestamp: Option<NaiveDateTime>) -> DateTime<Tz> {

let timestamp_to_convert: DateTime<Tz>;
if let (Some(ntz_timestamp), Some(tz)) = (src_ntz_timestamp, src_tz) {
timestamp_to_convert = tz.from_local_datetime(&ntz_timestamp).unwrap();
}
else {
timestamp_to_convert = src_timestamp;
}

if timestamp_to_convert.timezone() != target_tz {
timestamp_to_convert.with_timezone(&target_tz)
}
else {
timestamp_to_convert
}

}
}

// Timestamp arithmetic factors.
Expand Down

0 comments on commit 499a4ac

Please sign in to comment.