pub struct UtcOffset { /* private fields */ }
Expand description
An offset from UTC.
Guaranteed to store values up to ±23:59:59. Any values outside this range may have incidental support that can change at any time without notice. If you need support outside this range, please file an issue with your use case.
Implementations
sourceimpl UtcOffset
impl UtcOffset
sourcepub const fn east_hours(hours: u8) -> Self
pub const fn east_hours(hours: u8) -> Self
Create a UtcOffset
representing an easterly offset by the number of
hours provided.
assert_eq!(UtcOffset::east_hours(1).as_hours(), 1);
assert_eq!(UtcOffset::east_hours(2).as_minutes(), 120);
sourcepub const fn west_hours(hours: u8) -> Self
pub const fn west_hours(hours: u8) -> Self
Create a UtcOffset
representing a westerly offset by the number of
hours provided.
assert_eq!(UtcOffset::west_hours(1).as_hours(), -1);
assert_eq!(UtcOffset::west_hours(2).as_minutes(), -120);
sourcepub const fn hours(hours: i8) -> Self
pub const fn hours(hours: i8) -> Self
Create a UtcOffset
representing an offset by the number of hours
provided.
assert_eq!(UtcOffset::hours(2).as_minutes(), 120);
assert_eq!(UtcOffset::hours(-2).as_minutes(), -120);
sourcepub const fn east_minutes(minutes: u16) -> Self
pub const fn east_minutes(minutes: u16) -> Self
Create a UtcOffset
representing an easterly offset by the number of
minutes provided.
assert_eq!(UtcOffset::east_minutes(60).as_hours(), 1);
sourcepub const fn west_minutes(minutes: u16) -> Self
pub const fn west_minutes(minutes: u16) -> Self
Create a UtcOffset
representing a westerly offset by the number of
minutes provided.
assert_eq!(UtcOffset::west_minutes(60).as_hours(), -1);
sourcepub const fn minutes(minutes: i16) -> Self
pub const fn minutes(minutes: i16) -> Self
Create a UtcOffset
representing a offset by the number of minutes
provided.
assert_eq!(UtcOffset::minutes(60).as_hours(), 1);
assert_eq!(UtcOffset::minutes(-60).as_hours(), -1);
sourcepub const fn east_seconds(seconds: u32) -> Self
pub const fn east_seconds(seconds: u32) -> Self
Create a UtcOffset
representing an easterly offset by the number of
seconds provided.
assert_eq!(UtcOffset::east_seconds(3_600).as_hours(), 1);
assert_eq!(UtcOffset::east_seconds(1_800).as_minutes(), 30);
sourcepub const fn west_seconds(seconds: u32) -> Self
pub const fn west_seconds(seconds: u32) -> Self
Create a UtcOffset
representing a westerly offset by the number of
seconds provided.
assert_eq!(UtcOffset::west_seconds(3_600).as_hours(), -1);
assert_eq!(UtcOffset::west_seconds(1_800).as_minutes(), -30);
sourcepub const fn seconds(seconds: i32) -> Self
pub const fn seconds(seconds: i32) -> Self
Create a UtcOffset
representing an offset by the number of seconds
provided.
assert_eq!(UtcOffset::seconds(3_600).as_hours(), 1);
assert_eq!(UtcOffset::seconds(-3_600).as_hours(), -1);
sourcepub const fn as_seconds(self) -> i32
pub const fn as_seconds(self) -> i32
Get the number of seconds from UTC the value is. Positive is east, negative is west.
assert_eq!(UtcOffset::UTC.as_seconds(), 0);
assert_eq!(UtcOffset::hours(12).as_seconds(), 43_200);
assert_eq!(UtcOffset::hours(-12).as_seconds(), -43_200);
sourcepub const fn as_minutes(self) -> i16
pub const fn as_minutes(self) -> i16
Get the number of minutes from UTC the value is. Positive is east, negative is west.
assert_eq!(UtcOffset::UTC.as_minutes(), 0);
assert_eq!(UtcOffset::hours(12).as_minutes(), 720);
assert_eq!(UtcOffset::hours(-12).as_minutes(), -720);
sourcepub const fn as_hours(self) -> i8
pub const fn as_hours(self) -> i8
Get the number of hours from UTC the value is. Positive is east, negative is west.
assert_eq!(UtcOffset::UTC.as_hours(), 0);
assert_eq!(UtcOffset::hours(12).as_hours(), 12);
assert_eq!(UtcOffset::hours(-12).as_hours(), -12);
sourcepub fn local_offset_at(datetime: OffsetDateTime) -> Self
👎 Deprecated since 0.2.23: UTC is returned if the local offset cannot be determined
pub fn local_offset_at(datetime: OffsetDateTime) -> Self
UTC is returned if the local offset cannot be determined
Obtain the system’s UTC offset at a known moment in time. If the offset cannot be determined, UTC is returned.
let unix_epoch = OffsetDateTime::unix_epoch();
let local_offset = UtcOffset::local_offset_at(unix_epoch);
println!("{}", local_offset.format("%z"));
sourcepub fn try_local_offset_at(
datetime: OffsetDateTime
) -> Result<Self, IndeterminateOffset>
pub fn try_local_offset_at(
datetime: OffsetDateTime
) -> Result<Self, IndeterminateOffset>
Attempt to obtain the system’s UTC offset at a known moment in time. If the offset cannot be determined, an error is returned.
let unix_epoch = OffsetDateTime::unix_epoch();
let local_offset = UtcOffset::try_local_offset_at(unix_epoch);
assert!(local_offset.is_ok());
sourcepub fn current_local_offset() -> Self
👎 Deprecated since 0.2.23: UTC is returned if the local offset cannot be determined
pub fn current_local_offset() -> Self
UTC is returned if the local offset cannot be determined
Obtain the system’s current UTC offset. If the offset cannot be determined, UTC is returned.
let local_offset = UtcOffset::current_local_offset();
println!("{}", local_offset.format("%z"));
sourcepub fn try_current_local_offset() -> Result<Self, IndeterminateOffset>
pub fn try_current_local_offset() -> Result<Self, IndeterminateOffset>
Attempt to obtain the system’s current UTC offset. If the offset cannot be determined, an error is returned.
let local_offset = UtcOffset::try_current_local_offset();
assert!(local_offset.is_ok());
sourceimpl UtcOffset
impl UtcOffset
Methods that allow parsing and formatting the UtcOffset
.
sourcepub fn format(self, format: impl AsRef<str>) -> String
pub fn format(self, format: impl AsRef<str>) -> String
Format the UtcOffset
using the provided string.
assert_eq!(UtcOffset::hours(2).format("%z"), "+0200");
assert_eq!(UtcOffset::hours(-2).format("%z"), "-0200");
sourcepub fn lazy_format(self, format: impl AsRef<str>) -> impl Display
pub fn lazy_format(self, format: impl AsRef<str>) -> impl Display
Format the UtcOffset
using the provided string.
assert_eq!(UtcOffset::hours(2).lazy_format("%z").to_string(), "+0200");
assert_eq!(UtcOffset::hours(-2).lazy_format("%z").to_string(), "-0200");
Trait Implementations
sourceimpl Ord for UtcOffset
impl Ord for UtcOffset
sourceimpl PartialOrd<UtcOffset> for UtcOffset
impl PartialOrd<UtcOffset> for UtcOffset
sourcefn partial_cmp(&self, other: &UtcOffset) -> Option<Ordering>
fn partial_cmp(&self, other: &UtcOffset) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Copy for UtcOffset
impl Eq for UtcOffset
impl StructuralEq for UtcOffset
impl StructuralPartialEq for UtcOffset
Auto Trait Implementations
impl RefUnwindSafe for UtcOffset
impl Send for UtcOffset
impl Sync for UtcOffset
impl Unpin for UtcOffset
impl UnwindSafe for UtcOffset
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more