@@ -243,6 +243,7 @@ impl TzLUT {
243
243
pub trait DateConverter {
244
244
fn to_date ( & self , tz : Tz ) -> NaiveDate ;
245
245
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 > ;
246
247
}
247
248
248
249
impl < T > DateConverter for T
@@ -264,6 +265,31 @@ where T: AsPrimitive<i64>
264
265
}
265
266
tz. timestamp_opt ( secs, nanos as u32 ) . unwrap ( )
266
267
}
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
+ }
267
293
}
268
294
269
295
pub const MICROSECS_PER_DAY : i64 = 86_400_000_000 ;
0 commit comments