pub struct TimeUnit { /* private fields */ }time only.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
Sourcepub const fn from_hours(hours: u32) -> Self
pub const fn from_hours(hours: u32) -> Self
Sourcepub const fn from_weeks(weeks: u16) -> Self
pub const fn from_weeks(weeks: u16) -> Self
Sourcepub const fn from_months(months: u16) -> Self
pub const fn from_months(months: u16) -> Self
Sourcepub const fn from_years(years: u8) -> Self
pub const fn from_years(years: u8) -> Self
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 BorshDeserialize for TimeUnit
impl BorshDeserialize for TimeUnit
fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>
Source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
Source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
Source§impl BorshSerialize for TimeUnit
impl BorshSerialize for TimeUnit
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
Available on crate feature up only.
impl From<&TimeUnit> for UptimeFull
up only.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
Available on crate feature up only.
impl From<TimeUnit> for UptimeFull
up only.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 PartialOrd for TimeUnit
impl PartialOrd for TimeUnit
Source§impl SysUptime for TimeUnit
Available on crate feature up only.
impl SysUptime for TimeUnit
up only.Source§fn sys_uptime() -> Self
fn sys_uptime() -> Self
Self from the live system uptime and can be used on: Read more