pub struct Time(_, _);Expand description
Human-readable std::time::Duration.
The input is always assumed to be in seconds.
Time::from input can be:
The lowest unit is second, the highest is year, and week is skipped in favor of 7 days.
Inlining
If the feature flag inline_time is enabled, inputs ranging from 0..3660 into
Time::from will return inlined static bytes, the max string representation of this is 1 hour, 1 minute.
Warning: This feature is disabled by default. While it increases speed, it also heavily increases build time and binary size.
Cloning
Clone may be expensive:
// Cheap (stack allocated string).
let a = Time::from(60_u8);
assert!(a == "1 minute");
let b = a.clone();
// Expensive (heap allocated string).
let a = Time::from(3234815_u64);
assert!(a == "1 month, 6 days, 23 hours, 59 minutes, 59 seconds");
let b = a.clone();The actual string used internally is not a String,
but a CompactString so that any string 24 bytes (12 bytes on 32-bit) or less are stack allocated instead of heap allocated.
The documentation will still refer to the inner string as a String. Anything returned will also be a String.
Math
These operators are overloaded. They will always output a new Self:
Add +Sub -Div /Mul *Rem %
They can either be:
- Combined with another
Self:Time::from(1) + Time::from(1) - Or with the inner number itself:
Time::from(1) + 1
They also have the same panic!() behavior on overflow as the normal ones, because internally,
it is just calling .inner() $OPERATOR $NUMBER.
assert!(Time::from(10_u64) + 10 == Time::from(20_u64));
assert!(Time::from(10_u64) - 10 == Time::from(0_u64));
assert!(Time::from(10_u64) / 10 == Time::from(1_u64));
assert!(Time::from(10_u64) * 10 == Time::from(100_u64));
assert!(Time::from(10_u64) % 10 == Time::from(0_u64));Overflow example:
let n = Time::from(u64::MAX) + u64::MAX;Examples
assert!(Time::from(0_u64) == "0 seconds");
assert!(Time::from(1_u64) == "1 second");
assert!(Time::from(2_u64) == "2 seconds");
assert!(Time::from(59_u64) == "59 seconds");
assert!(Time::from(60_u64) == "1 minute");
assert!(Time::from(61_u64) == "1 minute, 1 second");
assert!(Time::from(62_u64) == "1 minute, 2 seconds");
assert!(Time::from(120_u64) == "2 minutes");
assert!(Time::from(121_u64) == "2 minutes, 1 second");
assert!(Time::from(122_u64) == "2 minutes, 2 seconds");
assert!(Time::from(179_u64) == "2 minutes, 59 seconds");
assert!(Time::from(3599_u64) == "59 minutes, 59 seconds");
assert!(Time::from(3600_u64) == "1 hour");
assert!(Time::from(3601_u64) == "1 hour, 1 second");
assert!(Time::from(3602_u64) == "1 hour, 2 seconds");
assert!(Time::from(3660_u64) == "1 hour, 1 minute");
assert!(Time::from(3720_u64) == "1 hour, 2 minutes");
assert!(Time::from(86399_u64) == "23 hours, 59 minutes, 59 seconds");
assert!(Time::from(86400_u64) == "1 day");
assert!(Time::from(86401_u64) == "1 day, 1 second");
assert!(Time::from(86402_u64) == "1 day, 2 seconds");
assert!(Time::from(86460_u64) == "1 day, 1 minute");
assert!(Time::from(86520_u64) == "1 day, 2 minutes");
assert!(Time::from(90000_u64) == "1 day, 1 hour");
assert!(Time::from(93600_u64) == "1 day, 2 hours");
assert!(Time::from(604799_u64) == "6 days, 23 hours, 59 minutes, 59 seconds");
assert!(Time::from(604800_u64) == "7 days");
assert!(Time::from(2630016_u64) == "1 month");
assert!(Time::from(3234815_u64) == "1 month, 6 days, 23 hours, 59 minutes, 59 seconds");
assert!(Time::from(5260032_u64) == "2 months");
assert!(Time::from(31557600_u64) == "1 year");
assert!(Time::from(63115200_u64) == "2 years");
assert_eq!(
Time::from(u64::MAX),
"584542046090 years, 7 months, 15 days, 17 hours, 5 minutes, 3 seconds",
);Credit
This code is forked from https://docs.rs/humantime, edited to remove sub-second time, change spacing and some words.
Implementations§
source§impl Time
impl Time
sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns the valid byte slice of the inner String
These bytes can always safely be used for std::str::from_utf8_unchecked.
sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
sourcepub fn head_dot(&self, len: usize) -> String
pub fn head_dot(&self, len: usize) -> String
Same as Self::head but returns a String ending with ...
This will return the full string without ... if
the len is longer than the actual inner str.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.head_dot(4) == "2021...");sourcepub fn tail_dot(&self, len: usize) -> String
pub fn tail_dot(&self, len: usize) -> String
Same as Self::tail but returns a String starting with ...
This will return the full string without ... if
the len is longer than the actual inner str.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.tail_dot(5) == "...12-11");sourcepub fn head_tail(&self, head: usize, tail: usize) -> String
pub fn head_tail(&self, head: usize, tail: usize) -> String
Return the first head bytes and last tail
bytes of this string separated with ....
Since all readable types happen to only contain
ASCII characters, all char’s are equal to 1 byte.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.head_tail(3, 2) == "202...11");
assert!(date.head_tail(3, 3) == "202...-11");
assert!(date.head_tail(3, 5) == "202...12-11");sourcepub const fn zero() -> Self
pub const fn zero() -> Self
assert!(Time::zero() == 0_u64);
assert!(Time::zero() == "0 seconds");sourcepub const fn second() -> Self
pub const fn second() -> Self
assert!(Time::second() == 1_u64);
assert!(Time::second() == "1 second");sourcepub const fn minute() -> Self
pub const fn minute() -> Self
assert!(Time::minute() == 60_u64);
assert!(Time::minute() == "1 minute");sourcepub const fn hour() -> Self
pub const fn hour() -> Self
assert!(Time::hour() == 3600_u64);
assert!(Time::hour() == "1 hour");sourcepub const fn day() -> Self
pub const fn day() -> Self
assert!(Time::day() == 86400_u64);
assert!(Time::day() == "1 day");Trait Implementations§
source§impl<'__de> BorrowDecode<'__de> for Time
impl<'__de> BorrowDecode<'__de> for Time
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 Time
impl<'de> Deserialize<'de> for Time
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 Ord for Time
impl Ord for Time
source§impl PartialEq<&Time> for str
impl PartialEq<&Time> for str
source§impl PartialEq<&Time> for u64
impl PartialEq<&Time> for u64
source§impl PartialEq<&str> for Time
impl PartialEq<&str> for Time
source§impl PartialEq<Time> for Time
impl PartialEq<Time> for Time
source§impl PartialEq<Time> for str
impl PartialEq<Time> for str
source§impl PartialEq<Time> for u64
impl PartialEq<Time> for u64
source§impl PartialOrd<&Time> for str
impl PartialOrd<&Time> for str
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 PartialOrd<&Time> for u64
impl PartialOrd<&Time> for u64
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 PartialOrd<&str> for Time
impl PartialOrd<&str> for Time
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 PartialOrd<Time> for Time
impl PartialOrd<Time> for Time
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 PartialOrd<Time> for str
impl PartialOrd<Time> for str
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 PartialOrd<Time> for u64
impl PartialOrd<Time> for u64
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 PartialOrd<str> for Time
impl PartialOrd<str> for Time
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 PartialOrd<u64> for &Time
impl PartialOrd<u64> for &Time
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 PartialOrd<u64> for Time
impl PartialOrd<u64> for Time
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 moreimpl Eq for Time
impl StructuralEq for Time
impl StructuralPartialEq for Time
Auto Trait Implementations§
impl RefUnwindSafe for Time
impl Send for Time
impl Sync for Time
impl Unpin for Time
impl UnwindSafe for Time
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere T: Display,
source§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more