pub struct TimeUnit { /* private fields */ }Expand description
Unit of time
Unlike most readable types, this struct is not a string;
it is a utility type that takes seconds as input and calculates
the different units of time from it.
For example:
assert_eq!(TimeUnit::from(1).seconds(), 1);
assert_eq!(TimeUnit::from(60).minutes(), 1);
assert_eq!(TimeUnit::from(3600).hours(), 1);
assert_eq!(TimeUnit::from(86400).days(), 1);
assert_eq!(TimeUnit::from(86400 * 7).weeks(), 1);
assert_eq!(TimeUnit::from(86400 * 31).months(), 1);
assert_eq!(TimeUnit::from(86400 * 365).years(), 1);If a unit overflows it will carry over to the lower unit, for example:
let unit = TimeUnit::from(
31536000 + // 1 year
5356800 + // 2 months
1814400 + // 3 weeks
345600 + // 4 days
18000 + // 5 hours
360 + // 6 minutes
7 // 7 seconds
);
assert_eq!(unit.years(), 1);
assert_eq!(unit.months(), 2);
assert_eq!(unit.weeks(), 3);
assert_eq!(unit.days(), 4);
assert_eq!(unit.hours(), 5);
assert_eq!(unit.minutes(), 6);
assert_eq!(unit.seconds(), 7);
// Total amount of seconds.
assert_eq!(unit.inner(), 39071167);Size
assert_eq!(std::mem::size_of::<TimeUnit>(), 12);Uptime & Conversion
Like the other readable::time types, TimeUnit implements Uptime
and can be losslessly convert from/into other readable::time types, even
maintaining unknown variants:
// Uptime
let time = Uptime::from(86461);
assert_eq!(time, "1d, 1m, 1s");
// TimeUnit
let unit = TimeUnit::from(time);
assert_eq!(unit.inner(), 86461);
assert_eq!(unit.years(), 0);
assert_eq!(unit.months(), 0);
assert_eq!(unit.weeks(), 0);
assert_eq!(unit.days(), 1);
assert_eq!(unit.hours(), 0);
assert_eq!(unit.minutes(), 1);
assert_eq!(unit.seconds(), 1);
// Maintain the `unknown` variant.
let time: Uptime = Uptime::unknown();
let unit: TimeUnit = TimeUnit::from(time);
assert!(unit.is_unknown());
let time: Uptime = Uptime::from(unit);
assert!(time.is_unknown());Naive time
Like the othear readable::time types, TimeUnit naively assumes that:
- Each day is
86400seconds - Each month is
31days - Each year is
365days
This is incorrect as not all months are 31 days long and leap years exist.
Implementations§
source§impl TimeUnit
impl TimeUnit
sourcepub const UNKNOWN: Self = _
pub const UNKNOWN: Self = _
assert_eq!(TimeUnit::UNKNOWN.inner(), 0);
assert_eq!(TimeUnit::UNKNOWN.years(), 0);
assert_eq!(TimeUnit::UNKNOWN.months(), 0);
assert_eq!(TimeUnit::UNKNOWN.weeks(), 0);
assert_eq!(TimeUnit::UNKNOWN.days(), 0);
assert_eq!(TimeUnit::UNKNOWN.hours(), 0);
assert_eq!(TimeUnit::UNKNOWN.minutes(), 0);
assert_eq!(TimeUnit::UNKNOWN.seconds(), 0);
assert_eq!(TimeUnit::UNKNOWN, TimeUnit::from(-1));
assert_eq!(TimeUnit::UNKNOWN, TimeUnit::from(u64::MAX));
assert_eq!(TimeUnit::UNKNOWN, TimeUnit::from(f32::NAN));
assert!(TimeUnit::UNKNOWN.is_unknown());sourcepub const ZERO: Self = _
pub const ZERO: Self = _
assert_eq!(TimeUnit::ZERO.inner(), 0);
assert_eq!(TimeUnit::ZERO, TimeUnit::from(0));sourcepub const SECOND: Self = _
pub const SECOND: Self = _
assert_eq!(TimeUnit::SECOND.inner(), 1);
assert_eq!(TimeUnit::SECOND.seconds(), 1);
assert_eq!(TimeUnit::SECOND, TimeUnit::from(1));sourcepub const MINUTE: Self = _
pub const MINUTE: Self = _
assert_eq!(TimeUnit::MINUTE.inner(), 60);
assert_eq!(TimeUnit::MINUTE.minutes(), 1);
assert_eq!(TimeUnit::MINUTE, TimeUnit::from(60));sourcepub const HOUR: Self = _
pub const HOUR: Self = _
assert_eq!(TimeUnit::HOUR.inner(), 3600);
assert_eq!(TimeUnit::HOUR.hours(), 1);
assert_eq!(TimeUnit::HOUR, TimeUnit::from(3600));sourcepub const DAY: Self = _
pub const DAY: Self = _
assert_eq!(TimeUnit::DAY.inner(), 86400);
assert_eq!(TimeUnit::DAY.days(), 1);
assert_eq!(TimeUnit::DAY, TimeUnit::from(86400));sourcepub const WEEK: Self = _
pub const WEEK: Self = _
assert_eq!(TimeUnit::WEEK.inner(), 604800);
assert_eq!(TimeUnit::WEEK.weeks(), 1);
assert_eq!(TimeUnit::WEEK, TimeUnit::from(604800));sourcepub const MONTH: Self = _
pub const MONTH: Self = _
assert_eq!(TimeUnit::MONTH.inner(), 2678400);
assert_eq!(TimeUnit::MONTH.months(), 1);
assert_eq!(TimeUnit::MONTH, TimeUnit::from(2678400));sourcepub const YEAR: Self = _
pub const YEAR: Self = _
assert_eq!(TimeUnit::YEAR.inner(), 31536000);
assert_eq!(TimeUnit::YEAR.years(), 1);
assert_eq!(TimeUnit::YEAR, TimeUnit::from(31536000));sourcepub const MAX: Self = _
pub const MAX: Self = _
assert_eq!(TimeUnit::MAX.inner(), u32::MAX);
assert_eq!(TimeUnit::MAX.years(), 136);
assert_eq!(TimeUnit::MAX.months(), 2);
assert_eq!(TimeUnit::MAX.weeks(), 1);
assert_eq!(TimeUnit::MAX.days(), 1);
assert_eq!(TimeUnit::MAX.hours(), 6);
assert_eq!(TimeUnit::MAX.minutes(), 28);
assert_eq!(TimeUnit::MAX.seconds(), 15);
assert_eq!(TimeUnit::MAX, TimeUnit::from(u32::MAX));source§impl TimeUnit
impl TimeUnit
sourcepub const fn new(secs: u32) -> Self
pub const fn new(secs: u32) -> Self
Create a new TimeUnit from seconds as input.
This will divide and use the remainder to calculate each unit,
for example 62 as input would lead to 1 minute and 2 seconds.
let unit = TimeUnit::from(62);
assert_eq!(unit.minutes(), 1);
assert_eq!(unit.seconds(), 2);sourcepub const fn from_minutes(minutes: u32) -> Self
pub const fn from_minutes(minutes: u32) -> Self
Create Self with minutes as input
let unit = TimeUnit::from_minutes(1);
assert_eq!(unit.inner(), 60);Maximum Input
The maximum input is 71_582_788 minutes before this function saturates.
sourcepub const fn from_hours(hours: u32) -> Self
pub const fn from_hours(hours: u32) -> Self
Create Self with hours as input
let unit = TimeUnit::from_hours(1);
assert_eq!(unit.inner(), 3_600);Maximum Input
The maximum input is 1_193_046 hours before this function saturates.
sourcepub const fn from_days(days: u16) -> Self
pub const fn from_days(days: u16) -> Self
Create Self with days as input
let unit = TimeUnit::from_days(1);
assert_eq!(unit.inner(), 86_400);Maximum Input
The maximum input is 49_710 days before this function saturates.
sourcepub const fn from_weeks(weeks: u16) -> Self
pub const fn from_weeks(weeks: u16) -> Self
Create Self with weeks as input
let unit = TimeUnit::from_weeks(1);
assert_eq!(unit.inner(), 604_800);Maximum Input
The maximum input is 7_101 weeks before this function saturates.
sourcepub const fn from_months(months: u16) -> Self
pub const fn from_months(months: u16) -> Self
Create Self with months as input
let unit = TimeUnit::from_months(1);
assert_eq!(unit.inner(), 2_678_400);Maximum Input
The maximum input is 1_603 months before this function saturates.
sourcepub const fn from_years(years: u8) -> Self
pub const fn from_years(years: u8) -> Self
Create Self with years as input
let unit = TimeUnit::from_years(1);
assert_eq!(unit.inner(), 31_536_000);Maximum Input
The maximum input is 136 years before this function saturates.
sourcepub const fn new_variety(
years: u8,
months: u16,
weeks: u16,
days: u16,
hours: u32,
minutes: u32,
seconds: u32
) -> Self
pub const fn new_variety( years: u8, months: u16, weeks: u16, days: u16, hours: u32, minutes: u32, seconds: u32 ) -> Self
Create a new TimeUnit from a variety of input
This multiplies and combines all the input.
Maximum Input
If the total second count of all the inputs combined exceeds u32::MAX
then this function will saturate and return TimeUnit::MAX.
Examples
let unit = TimeUnit::new_variety(
0, // years (0s)
1, // months (2678400s)
2, // weeks (1209600s)
3, // days (259200s)
4, // hours (14400s)
5, // minutes (300s)
6, // seconds (6s)
);
// Total second count: 4,161,906 seconds
assert_eq!(unit.inner(), 4_161_906);
assert_eq!(unit.years(), 0);
assert_eq!(unit.months(), 1);
assert_eq!(unit.weeks(), 2);
assert_eq!(unit.days(), 3);
assert_eq!(unit.hours(), 4);
assert_eq!(unit.minutes(), 5);
assert_eq!(unit.seconds(), 6);Example of saturating inputs.
let unit = TimeUnit::new_variety(
172, // years
134, // months
22, // weeks
32575, // days
46, // hours
5123, // minutes
54, // seconds
);
assert_eq!(unit, TimeUnit::MAX);sourcepub const fn into_raw(self) -> (bool, u32, u8, u8, u8, u8, u8, u8, u8)
pub const fn into_raw(self) -> (bool, u32, u8, u8, u8, u8, u8, u8, u8)
Returns the internal structure.
A tuple is returned mirroring the internal structure of TimeUnit, going from left-to-right:
bool- If thisTimeUnitis unknown or not (TimeUnit::is_unknown())u32- The total amount of seconds (TimeUnit::inner())u8-TimeUnit::years()u8-TimeUnit::months()u8-TimeUnit::weeks()u8-TimeUnit::days()u8-TimeUnit::hours()u8-TimeUnit::minutes()u8-TimeUnit::seconds()
Example
let (
unknown,
inner,
years,
months,
weeks,
days,
hours,
minutes,
seconds,
) = TimeUnit::from(39071167).into_raw();
assert_eq!(unknown, false);
assert_eq!(inner, 39071167);
assert_eq!(years, 1);
assert_eq!(months, 2);
assert_eq!(weeks, 3);
assert_eq!(days, 4);
assert_eq!(hours, 5);
assert_eq!(minutes, 6);
assert_eq!(seconds, 7);sourcepub const fn to_raw(&self) -> (bool, u32, u8, u8, u8, u8, u8, u8, u8)
pub const fn to_raw(&self) -> (bool, u32, u8, u8, u8, u8, u8, u8, u8)
Same as TimeUnit::into_raw() but does not destruct self
sourcepub const fn is_unknown(&self) -> bool
pub const fn is_unknown(&self) -> bool
An unknown TimeUnit can be created on irregular input (negative integer, NaN float, etc)
or if it was converted from a different readable::time type that was unknown.
This function checks if self is unknown.
Although all inner numbers are all set to 0,
a flag is set internally such that:
assert!(TimeUnit::zero() != TimeUnit::unknown());Examples
assert!(TimeUnit::UNKNOWN.is_unknown());
assert!(TimeUnit::from(Uptime::UNKNOWN).is_unknown());
assert!(TimeUnit::from(f32::NAN).is_unknown());
assert!(TimeUnit::from(-1).is_unknown());sourcepub const fn inner(&self) -> u32
pub const fn inner(&self) -> u32
Returns the total amount of seconds this TimeUnit represents.
let unit = TimeUnit::from(123);
assert_eq!(unit.minutes(), 2);
assert_eq!(unit.seconds(), 3);
assert_eq!(unit.inner(), 123);sourcepub const fn years(&self) -> u8
pub const fn years(&self) -> u8
Returns the remaining amount of years this TimeUnit represents.
assert_eq!(TimeUnit::from(86400 * 364).years(), 0);
assert_eq!(TimeUnit::from(86400 * 365).years(), 1);sourcepub const fn months(&self) -> u8
pub const fn months(&self) -> u8
Returns the remaining amount of months this TimeUnit represents.
assert_eq!(TimeUnit::from(86400 * 30).months(), 0);
assert_eq!(TimeUnit::from(86400 * 31).months(), 1);sourcepub const fn weeks(&self) -> u8
pub const fn weeks(&self) -> u8
Returns the remaining amount of weeks this TimeUnit represents.
assert_eq!(TimeUnit::from(86400 * 6).weeks(), 0);
assert_eq!(TimeUnit::from(86400 * 7).weeks(), 1);sourcepub const fn days(&self) -> u8
pub const fn days(&self) -> u8
Returns the remaining amount of days this TimeUnit represents.
assert_eq!(TimeUnit::from(86399).days(), 0);
assert_eq!(TimeUnit::from(86400).days(), 1);sourcepub const fn hours(&self) -> u8
pub const fn hours(&self) -> u8
Returns the remaining amount of hours this TimeUnit represents.
assert_eq!(TimeUnit::from(3599).hours(), 0);
assert_eq!(TimeUnit::from(3600).hours(), 1);sourcepub const fn minutes(&self) -> u8
pub const fn minutes(&self) -> u8
Returns the remaining amount of minutes this TimeUnit represents.
assert_eq!(TimeUnit::from(59).minutes(), 0);
assert_eq!(TimeUnit::from(60).minutes(), 1);sourcepub const fn seconds(&self) -> u8
pub const fn seconds(&self) -> u8
Returns the remaining amount of seconds this TimeUnit represents.
This is the remaining amount of seconds, not the total amount of seconds.
// `0` is returned since `60` is == `1 minute`.
assert_eq!(TimeUnit::from(60).seconds(), 0);
assert_eq!(TimeUnit::from(60).minutes(), 1);
assert_eq!(TimeUnit::from(60).inner(), 60);
// `1` is returned since there's 1 remaining second.
assert_eq!(TimeUnit::from(61).seconds(), 1);
assert_eq!(TimeUnit::from(61).minutes(), 1);
assert_eq!(TimeUnit::from(61).inner(), 61);Trait Implementations§
source§impl<'__de> BorrowDecode<'__de> for TimeUnit
impl<'__de> BorrowDecode<'__de> for TimeUnit
source§fn borrow_decode<__D: BorrowDecoder<'__de>>(
decoder: &mut __D
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de>>( decoder: &mut __D ) -> Result<Self, DecodeError>
source§impl<'de> Deserialize<'de> for TimeUnit
impl<'de> Deserialize<'de> for TimeUnit
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl From<&TimeUnit> for UptimeFull
impl From<&TimeUnit> for UptimeFull
source§impl From<&UptimeFull> for TimeUnit
impl From<&UptimeFull> for TimeUnit
source§fn from(from: &UptimeFull) -> Self
fn from(from: &UptimeFull) -> Self
source§impl From<TimeUnit> for UptimeFull
impl From<TimeUnit> for UptimeFull
source§impl From<UptimeFull> for TimeUnit
impl From<UptimeFull> for TimeUnit
source§fn from(from: UptimeFull) -> Self
fn from(from: UptimeFull) -> Self
source§impl Ord for TimeUnit
impl Ord for TimeUnit
source§impl PartialEq for TimeUnit
impl PartialEq for TimeUnit
source§impl PartialOrd for TimeUnit
impl PartialOrd for TimeUnit
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl SysUptime for TimeUnit
impl SysUptime for TimeUnit
source§fn sys_uptime() -> Self
fn sys_uptime() -> Self
Self from the live system uptime and can be used on: Read more