@@ -25,7 +25,7 @@ use crate::format::{write_rfc2822, write_rfc3339, DelayedFormat, SecondsFormat};
25
25
use crate :: naive:: { Days , IsoWeek , NaiveDate , NaiveDateTime , NaiveTime } ;
26
26
#[ cfg( feature = "clock" ) ]
27
27
use crate :: offset:: Local ;
28
- use crate :: offset:: { FixedOffset , LocalResult , Offset , TimeZone , Utc } ;
28
+ use crate :: offset:: { FixedOffset , FromOffset , LocalResult , Offset , TimeZone , Utc } ;
29
29
#[ allow( deprecated) ]
30
30
use crate :: Date ;
31
31
use crate :: { expect, try_opt} ;
@@ -405,14 +405,30 @@ impl<Tz: TimeZone> DateTime<Tz> {
405
405
}
406
406
407
407
/// Changes the associated time zone.
408
- /// The returned `DateTime` references the same instant of time from the perspective of the
409
- /// provided time zone.
408
+ ///
409
+ /// The returned `DateTime` references the same instant of time in UTC but with the offset of
410
+ /// the target time zone.
410
411
#[ inline]
411
412
#[ must_use]
412
413
pub fn with_timezone < Tz2 : TimeZone > ( & self , tz : & Tz2 ) -> DateTime < Tz2 > {
413
414
tz. from_utc_datetime ( & self . datetime )
414
415
}
415
416
417
+ /// Changes the associated `TimeZone` type.
418
+ ///
419
+ /// The returned `DateTime` references the same instant of time in UTC but with another
420
+ /// [`TimeZone`] type. A best effort is made to convert the value of the associated [`Offset`]
421
+ /// type to the `Offset` type of the target `TimeZone`.
422
+ #[ inline]
423
+ #[ must_use]
424
+ pub fn to_timezone < Tz2 : TimeZone > ( & self ) -> DateTime < Tz2 >
425
+ where
426
+ <Tz2 as TimeZone >:: Offset : FromOffset < <Tz as TimeZone >:: Offset > ,
427
+ {
428
+ let new_offset = <Tz2 as TimeZone >:: Offset :: from_offset ( self . offset ( ) ) ;
429
+ DateTime { datetime : self . datetime , offset : new_offset }
430
+ }
431
+
416
432
/// Fix the offset from UTC to its current value, dropping the associated timezone information.
417
433
/// This it useful for converting a generic `DateTime<Tz: Timezone>` to `DateTime<FixedOffset>`.
418
434
#[ inline]
0 commit comments