time_c/
lib.rs

1//!Wrapper for time functions of C standard library
2//!
3//!This provides minimal and uniform API to use in Rust without need for cumbersome crates like time
4#![no_std]
5#![warn(missing_docs)]
6#![allow(clippy::style)]
7
8pub mod format;
9pub mod sys;
10
11#[derive(Copy, Clone, Debug, PartialEq, Eq)]
12///Normalized [tm](sys/struct.tm.html)
13pub struct Time {
14    ///Seconds after the minute. Range 0-60
15    pub sec: u8,
16    ///Minutes after the hour. Range 0-59
17    pub min: u8,
18    ///Hours since midnight. Range 0-23
19    pub hour: u8,
20    ///Day of the month. Range 1-31
21    pub month_day: u8,
22    ///Months since January. Range 1-12
23    pub month: u8,
24    ///Year
25    pub year: u16,
26    ///days since Sunday. Range 0-6
27    pub week_day: u8,
28    ///Days since January 1. Range 0-365
29    pub day: u16,
30    ///Indicates Daylight Saving Time presence.
31    pub is_dst: bool,
32}
33
34impl Time {
35    #[inline(always)]
36    ///Attempts to parse provided unix time and returns new instance of self
37    pub fn parse_unix(secs: &sys::time_t) -> Option<Self> {
38        sys::parse_unix(secs).map(|time| time.normalize())
39    }
40
41    #[inline(always)]
42    ///Gets current UTC time
43    pub fn now_utc() -> Option<Self> {
44        sys::get_time().and_then(|time| Self::parse_unix(&time))
45    }
46
47    #[inline(always)]
48    ///Gets RFC-3339 formatter for this instance.
49    pub fn rfc3339(&self) -> format::Rfc3339<'_, Self> {
50        format::Rfc3339(self)
51    }
52}