Function datealgo::datetime_to_systemtime
source · pub fn datetime_to_systemtime(
(y, m, d, hh, mm, ss, nsec): (i32, u8, u8, u8, u8, u8, u32)
) -> Option<SystemTime>
Expand description
Convert year, month, day, hours, minutes, seconds and nanoseconds to
std::time::SystemTime
Given a (year, month, day, hours, minutes, seconds, nanoseconds)
tuple
from Unix epoch (January 1st, 1970) returns Option of
std::time::SystemTime
.
Errors
Returns None
if given datetime cannot be represented as SystemTime
.
Panics
Year must be between YEAR_MIN and YEAR_MAX. Month must be between 1
and 12
. Day must be between 1
and the number of days in the month in
question. Hours must be between 0
and 23
. Minutes must be between 0
and 59
. Seconds must be between 0
and 59
. Nanoseconds must be 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::datetime_to_systemtime;
use std::time::{Duration, UNIX_EPOCH};
assert_eq!(datetime_to_systemtime((1970, 1, 1, 0, 0, 0, 0)), Some(UNIX_EPOCH));
assert_eq!(datetime_to_systemtime((1970, 1, 1, 0, 0, 1, 0)), UNIX_EPOCH.checked_add(Duration::new(1, 0)));
assert_eq!(datetime_to_systemtime((2023, 5, 20, 9, 24, 38, 0)), UNIX_EPOCH.checked_add(Duration::from_secs(1684574678)));
Algorithm
Combination of existing functions for convenience only.