Function datealgo::secs_to_datetime

source ·
pub const fn secs_to_datetime(secs: i64) -> (i32, u32, u32, u8, u8, u8)
Expand description

Convert total seconds to year, month, day, hours, minutes and seconds

Given seconds counting from Unix epoch (January 1st, 1970) returns a (year, month, day, hours, minutes, seconds) tuple. Argument must be between RD_SECONDS_MIN and RD_SECONDS_MAX inclusive. 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_datetime;

assert_eq!(secs_to_datetime(0), (1970, 1, 1, 0, 0, 0));
assert_eq!(secs_to_datetime(86400), (1970, 1, 2, 0, 0, 0));
assert_eq!(secs_to_datetime(86399), (1970, 1, 1, 23, 59, 59));
assert_eq!(secs_to_datetime(-1), (1969, 12, 31, 23, 59, 59));
assert_eq!(secs_to_datetime(1684574678), (2023, 5, 20, 9, 24, 38));

Algorithm

Combination of existing functions for convenience only.