Struct icu_timezone::GmtOffset
source · pub struct GmtOffset(i32);
Expand description
The GMT offset in seconds for a timezone
Tuple Fields§
§0: i32
Implementations§
source§impl GmtOffset
impl GmtOffset
sourcepub fn try_from_offset_seconds(seconds: i32) -> Result<Self, TimeZoneError>
pub fn try_from_offset_seconds(seconds: i32) -> Result<Self, TimeZoneError>
Attempt to create a GmtOffset
from a seconds input. It returns
TimeZoneError::OffsetOutOfBounds
when the seconds are out of bounds.
sourcepub fn try_from_bytes(chars: &[u8]) -> Result<Self, TimeZoneError>
pub fn try_from_bytes(chars: &[u8]) -> Result<Self, TimeZoneError>
Parse a GmtOffset
from bytes.
The offset must range from GMT-12 to GMT+14. The string must be an ISO-8601 time zone designator: e.g. Z e.g. +05 e.g. +0500 e.g. +05:00
§Examples
use icu::timezone::GmtOffset;
use icu::timezone::TimeZoneError;
let offset0: GmtOffset =
GmtOffset::try_from_bytes(b"Z").expect("Failed to parse a time zone");
let offset1: GmtOffset =
GmtOffset::try_from_bytes(b"+05").expect("Failed to parse a time zone");
let offset2: GmtOffset = GmtOffset::try_from_bytes(b"+0500")
.expect("Failed to parse a time zone");
let offset3: GmtOffset = GmtOffset::try_from_bytes(b"-05:00")
.expect("Failed to parse a time zone");
let offset_err0: TimeZoneError =
GmtOffset::try_from_bytes(b"0500").expect_err("Invalid input");
let offset_err1: TimeZoneError =
GmtOffset::try_from_bytes(b"+05000").expect_err("Invalid input");
assert_eq!(offset0.offset_seconds(), 0);
assert_eq!(offset1.offset_seconds(), 18000);
assert_eq!(offset2.offset_seconds(), 18000);
assert_eq!(offset3.offset_seconds(), -18000);
assert_eq!(offset_err0, TimeZoneError::InvalidOffset);
assert_eq!(offset_err1, TimeZoneError::InvalidOffset);
sourcepub unsafe fn from_offset_seconds_unchecked(seconds: i32) -> Self
pub unsafe fn from_offset_seconds_unchecked(seconds: i32) -> Self
Create a GmtOffset
from a seconds input without checking bounds.
§Safety
The seconds must be a valid value as returned by Self::offset_seconds
.
sourcepub fn offset_seconds(self) -> i32
pub fn offset_seconds(self) -> i32
Returns the raw offset value in seconds.
sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Returns true
if the GmtOffset
is positive, otherwise false
.
sourcepub fn has_minutes(self) -> bool
pub fn has_minutes(self) -> bool
Returns true
if the GmtOffset
has non-zero minutes, otherwise false
.
sourcepub fn has_seconds(self) -> bool
pub fn has_seconds(self) -> bool
Returns true
if the GmtOffset
has non-zero seconds, otherwise false
.
Trait Implementations§
source§impl FromStr for GmtOffset
impl FromStr for GmtOffset
source§fn from_str(input: &str) -> Result<Self, Self::Err>
fn from_str(input: &str) -> Result<Self, Self::Err>
Parse a GmtOffset
from a string.
The offset must range from GMT-12 to GMT+14. The string must be an ISO 8601 time zone designator: e.g. Z e.g. +05 e.g. +0500 e.g. +05:00
§Examples
use icu::timezone::GmtOffset;
let offset0: GmtOffset = "Z".parse().expect("Failed to parse a GMT offset");
let offset1: GmtOffset =
"-09".parse().expect("Failed to parse a GMT offset");
let offset2: GmtOffset =
"-0930".parse().expect("Failed to parse a GMT offset");
let offset3: GmtOffset =
"-09:30".parse().expect("Failed to parse a GMT offset");