Struct time::utc_offset::UtcOffset
source · pub struct UtcOffset {
hours: i8,
minutes: i8,
seconds: i8,
}
Expand description
An offset from UTC.
This struct can store values up to ±23:59:59. If you need support outside this range, please file an issue with your use case.
Fields§
§hours: i8
§minutes: i8
§seconds: i8
Implementations§
source§impl UtcOffset
impl UtcOffset
sourcepub const fn __from_hms_unchecked(hours: i8, minutes: i8, seconds: i8) -> Self
pub const fn __from_hms_unchecked(hours: i8, minutes: i8, seconds: i8) -> Self
Create a UtcOffset
representing an offset of the hours, minutes, and seconds provided, the
validity of which must be guaranteed by the caller. All three parameters must have the same
sign.
sourcepub const fn from_hms(
hours: i8,
minutes: i8,
seconds: i8
) -> Result<Self, ComponentRange>
pub const fn from_hms( hours: i8, minutes: i8, seconds: i8 ) -> Result<Self, ComponentRange>
Create a UtcOffset
representing an offset by the number of hours, minutes, and seconds
provided.
The sign of all three components should match. If they do not, all smaller components will have their signs flipped.
assert_eq!(UtcOffset::from_hms(1, 2, 3)?.as_hms(), (1, 2, 3));
assert_eq!(UtcOffset::from_hms(1, -2, -3)?.as_hms(), (1, 2, 3));
Runsourcepub const fn from_whole_seconds(seconds: i32) -> Result<Self, ComponentRange>
pub const fn from_whole_seconds(seconds: i32) -> Result<Self, ComponentRange>
Create a UtcOffset
representing an offset by the number of seconds provided.
assert_eq!(UtcOffset::from_whole_seconds(3_723)?.as_hms(), (1, 2, 3));
Runsourcepub const fn as_hms(self) -> (i8, i8, i8)
pub const fn as_hms(self) -> (i8, i8, i8)
Obtain the UTC offset as its hours, minutes, and seconds. The sign of all three components will always match. A positive value indicates an offset to the east; a negative to the west.
assert_eq!(offset!(+1:02:03).as_hms(), (1, 2, 3));
assert_eq!(offset!(-1:02:03).as_hms(), (-1, -2, -3));
Runsourcepub const fn whole_hours(self) -> i8
pub const fn whole_hours(self) -> i8
Obtain the number of whole hours the offset is from UTC. A positive value indicates an offset to the east; a negative to the west.
assert_eq!(offset!(+1:02:03).whole_hours(), 1);
assert_eq!(offset!(-1:02:03).whole_hours(), -1);
Runsourcepub const fn whole_minutes(self) -> i16
pub const fn whole_minutes(self) -> i16
Obtain the number of whole minutes the offset is from UTC. A positive value indicates an offset to the east; a negative to the west.
assert_eq!(offset!(+1:02:03).whole_minutes(), 62);
assert_eq!(offset!(-1:02:03).whole_minutes(), -62);
Runsourcepub const fn minutes_past_hour(self) -> i8
pub const fn minutes_past_hour(self) -> i8
Obtain the number of minutes past the hour the offset is from UTC. A positive value indicates an offset to the east; a negative to the west.
assert_eq!(offset!(+1:02:03).minutes_past_hour(), 2);
assert_eq!(offset!(-1:02:03).minutes_past_hour(), -2);
Runsourcepub const fn whole_seconds(self) -> i32
pub const fn whole_seconds(self) -> i32
Obtain the number of whole seconds the offset is from UTC. A positive value indicates an offset to the east; a negative to the west.
assert_eq!(offset!(+1:02:03).whole_seconds(), 3723);
assert_eq!(offset!(-1:02:03).whole_seconds(), -3723);
Runsourcepub const fn seconds_past_minute(self) -> i8
pub const fn seconds_past_minute(self) -> i8
Obtain the number of seconds past the minute the offset is from UTC. A positive value indicates an offset to the east; a negative to the west.
assert_eq!(offset!(+1:02:03).seconds_past_minute(), 3);
assert_eq!(offset!(-1:02:03).seconds_past_minute(), -3);
Runsourcepub const fn is_utc(self) -> bool
pub const fn is_utc(self) -> bool
Check if the offset is exactly UTC.
assert!(!offset!(+1:02:03).is_utc());
assert!(!offset!(-1:02:03).is_utc());
assert!(offset!(UTC).is_utc());
Runsourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Check if the offset is positive, or east of UTC.
assert!(offset!(+1:02:03).is_positive());
assert!(!offset!(-1:02:03).is_positive());
assert!(!offset!(UTC).is_positive());
Runsourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Check if the offset is negative, or west of UTC.
assert!(!offset!(+1:02:03).is_negative());
assert!(offset!(-1:02:03).is_negative());
assert!(!offset!(UTC).is_negative());
Runsourcepub fn local_offset_at(
datetime: OffsetDateTime
) -> Result<Self, IndeterminateOffset>
Available on crate feature local-offset
only.
pub fn local_offset_at( datetime: OffsetDateTime ) -> Result<Self, IndeterminateOffset>
local-offset
only.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 local_offset = UtcOffset::local_offset_at(OffsetDateTime::UNIX_EPOCH);
assert!(local_offset.is_ok());
Runsourcepub fn current_local_offset() -> Result<Self, IndeterminateOffset>
Available on crate feature local-offset
only.
pub fn current_local_offset() -> Result<Self, IndeterminateOffset>
local-offset
only.Attempt to obtain the system’s current UTC offset. If the offset cannot be determined, an error is returned.
let local_offset = UtcOffset::current_local_offset();
assert!(local_offset.is_ok());
Runsource§impl UtcOffset
impl UtcOffset
sourcepub fn format_into(
self,
output: &mut impl Write,
format: &impl Formattable + ?Sized
) -> Result<usize, Format>
Available on crate feature formatting
only.
pub fn format_into( self, output: &mut impl Write, format: &impl Formattable + ?Sized ) -> Result<usize, Format>
formatting
only.Format the UtcOffset
using the provided format description.
sourcepub fn format(
self,
format: &impl Formattable + ?Sized
) -> Result<String, Format>
Available on crate feature formatting
only.
pub fn format( self, format: &impl Formattable + ?Sized ) -> Result<String, Format>
formatting
only.Format the UtcOffset
using the provided format description.
let format = format_description::parse("[offset_hour sign:mandatory]:[offset_minute]")?;
assert_eq!(offset!(+1).format(&format)?, "+01:00");
Runsource§impl UtcOffset
impl UtcOffset
sourcepub fn parse(
input: &str,
description: &impl Parsable + ?Sized
) -> Result<Self, Parse>
Available on crate feature parsing
only.
pub fn parse( input: &str, description: &impl Parsable + ?Sized ) -> Result<Self, Parse>
parsing
only.Parse a UtcOffset
from the input using the provided format
description.
let format = format_description!("[offset_hour]:[offset_minute]");
assert_eq!(UtcOffset::parse("-03:42", &format)?, offset!(-3:42));
RunTrait Implementations§
source§impl<'a> Deserialize<'a> for UtcOffset
Available on crate feature serde
only.
impl<'a> Deserialize<'a> for UtcOffset
serde
only.source§fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error>
source§fn deserialize_in_place<D>(
deserializer: D,
place: &mut Self
) -> Result<(), <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize_in_place<D>( deserializer: D, place: &mut Self ) -> Result<(), <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,
self
from the given Deserializer. Read moresource§impl Distribution<UtcOffset> for Standard
Available on crate feature rand
only.
impl Distribution<UtcOffset> for Standard
rand
only.source§fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> UtcOffset
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> UtcOffset
T
, using rng
as the source of randomness.source§impl Ord for UtcOffset
impl Ord for UtcOffset
source§impl PartialEq<UtcOffset> for UtcOffset
impl PartialEq<UtcOffset> for UtcOffset
source§impl PartialOrd<UtcOffset> for UtcOffset
impl PartialOrd<UtcOffset> for UtcOffset
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more