Skip to content

Commit a1ed5c5

Browse files
florannTCeason
authored andcommitted
Feature: function about convert_timezone databendlabs#16177
1 parent ff49cc6 commit a1ed5c5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/query/expression/src/utils/date_helper.rs

+26
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ impl TzLUT {
243243
pub trait DateConverter {
244244
fn to_date(&self, tz: Tz) -> NaiveDate;
245245
fn to_timestamp(&self, tz: Tz) -> DateTime<Tz>;
246+
fn convert_timezone(&self, target_tz: Tz, src_timestamp: DateTime<Tz>, src_tz: Option<Tz>, src_ntz_timestamp: Option<DateTime<Tz>>) -> DateTime<Tz>;
246247
}
247248

248249
impl<T> DateConverter for T
@@ -264,6 +265,31 @@ where T: AsPrimitive<i64>
264265
}
265266
tz.timestamp_opt(secs, nanos as u32).unwrap()
266267
}
268+
/// Convert a timestamp to the specify timezone.
269+
///
270+
/// # Parameters
271+
/// - `target_tz`: Timezone in which the timestamp will be converted
272+
/// - `src_timestamp`: Source timestamp to be converted
273+
/// - `src_tz`: Timezone of the timestamp to be converted - Optional
274+
/// - `src_ntz_timestamp`: Source timestamp with unspecified timezone to be converted - Optional
275+
fn convert_timezone(&self, target_tz: Tz, src_timestamp: DateTime<Tz>, src_tz: Option<Tz>, src_ntz_timestamp: Option<NaiveDateTime>) -> DateTime<Tz> {
276+
277+
let timestamp_to_convert: DateTime<Tz>;
278+
if let (Some(ntz_timestamp), Some(tz)) = (src_ntz_timestamp, src_tz) {
279+
timestamp_to_convert = tz.from_local_datetime(&ntz_timestamp).unwrap();
280+
}
281+
else {
282+
timestamp_to_convert = src_timestamp;
283+
}
284+
285+
if timestamp_to_convert.timezone() != target_tz {
286+
timestamp_to_convert.with_timezone(&target_tz)
287+
}
288+
else {
289+
timestamp_to_convert
290+
}
291+
292+
}
267293
}
268294

269295
pub const MICROSECS_PER_DAY: i64 = 86_400_000_000;

0 commit comments

Comments
 (0)