/// Convert the given Unix timestamp to a `libc::tm`. Returns `None` on any error. #[inline] fn timestamp_to_tm(timestamp: i64) -> Option<libc::tm> { #[allow(
clippy::useless_conversion,
reason = "the exact type of `timestamp` beforehand can vary, so this conversion is \
necessary"
)] let timestamp = timestamp.try_into().ok()?;
letmut tm = MaybeUninit::uninit();
// Safety: We are calling a system API, which mutates the `tm` variable. If a null // pointer is returned, an error occurred. let tm_ptr = unsafe { libc::localtime_r(×tamp, tm.as_mut_ptr()) };
if tm_ptr.is_null() {
None
} else { // Safety: The value was initialized, as we no longer have a null pointer.
Some(unsafe { tm.assume_init() })
}
}
/// Convert a `libc::tm` to a `UtcOffset`. Returns `None` on any error. // This is available to any target known to have the `tm_gmtoff` extension. #[cfg(any(
target_os = "redox",
target_os = "linux",
target_os = "l4re",
target_os = "android",
target_os = "emscripten",
target_os = "macos",
target_os = "ios",
target_os = "watchos",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd",
target_os = "haiku",
))] #[inline] fn tm_to_offset(_unix_timestamp: i64, tm: libc::tm) -> Option<UtcOffset> { let seconds = tm.tm_gmtoff.try_into().ok()?;
UtcOffset::from_whole_seconds(seconds).ok()
}
/// Convert a `libc::tm` to a `UtcOffset`. Returns `None` on any error. /// /// This method can return an incorrect value, as it only approximates the `tm_gmtoff` field. The /// reason for this is that daylight saving time does not start on the same date every year, nor are /// the rules for daylight saving time the same for every year. This implementation assumes 1970 is /// equivalent to every other year, which is not always the case. #[cfg(not(any(
target_os = "redox",
target_os = "linux",
target_os = "l4re",
target_os = "android",
target_os = "emscripten",
target_os = "macos",
target_os = "ios",
target_os = "watchos",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd",
target_os = "haiku",
)))] #[inline] fn tm_to_offset(unix_timestamp: i64, tm: libc::tm) -> Option<UtcOffset> { usecrate::Date;
letmut tm = tm; if tm.tm_sec == 60 { // Leap seconds are not currently supported.
tm.tm_sec = 59;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.