Function datealgo::secs_to_systemtime
source · pub fn secs_to_systemtime((secs, nsecs): (i64, u32)) -> Option<SystemTime>
Expand description
Convert seconds and nanoseconds to std::time::SystemTime
Given a tuple of seconds and nanoseconds counting from Unix epoch (January
1st, 1970) returns Option of std::time::SystemTime
.
Errors
Returns None
if given datetime cannot be represented as SystemTime
.
Panics
Seconds must be between RD_SECONDS_MIN and RD_SECONDS_MAX inclusive.
Nanoseconds must between 0
and 999_999_999
. Bounds are checked using
debug_assert
only, so that the checks are not present in release builds,
similar to integer overflow checks.
Examples
use datealgo::secs_to_systemtime;
use std::time::{Duration, UNIX_EPOCH};
assert_eq!(secs_to_systemtime((0, 0)), Some(UNIX_EPOCH));
assert_eq!(secs_to_systemtime((0, 1)), UNIX_EPOCH.checked_add(Duration::new(0, 1)));
assert_eq!(secs_to_systemtime((1, 0)), UNIX_EPOCH.checked_add(Duration::new(1, 0)));
assert_eq!(secs_to_systemtime((-1, 999_999_999)), UNIX_EPOCH.checked_sub(Duration::new(0, 1)));
assert_eq!(secs_to_systemtime((-1, 0)), UNIX_EPOCH.checked_sub(Duration::new(1, 0)));
assert_eq!(secs_to_systemtime((-2, 999_999_999)), UNIX_EPOCH.checked_sub(Duration::new(1, 1)));
Algorithm
Combination of existing functions for convenience only.