pub struct TimeDiff { /* private fields */ }Expand description
A wrapper type associated with locale and to info.
Implementations
sourceimpl TimeDiff
impl TimeDiff
sourcepub fn to_diff(to: String) -> Self
pub fn to_diff(to: String) -> Self
Receive a duration string from input and initiate a TimeDiff struct.
And the duration string should be a human-readable string like 3d and etc.
more human-readable duration detail -> humantime
Exmaple
use timediff::*;
assert_eq!(TimeDiff::to_diff(String::from("-10s")).parse(),Ok(String::from("a few seconds ago")))sourcepub fn to_diff_duration(to: Duration) -> Self
pub fn to_diff_duration(to: Duration) -> Self
Receive a std duration and initiate a TimeDiff struct.
Exmaple
use timediff::*;
use std::time::Duration;
assert_eq!(TimeDiff::to_diff_duration(Duration::new(30 * 60, 0)).parse(),Ok(String::from("in 30 minutes")))sourcepub fn locale(&mut self, l: String) -> Result<&mut Self, Error>
pub fn locale(&mut self, l: String) -> Result<&mut Self, Error>
Change the locale used for the diff operation on TimeDiff.
Default locale is en-US, supports zh-CN, tr-TR, ru-RU and etc.
if a custom locale is not supported currently, will return a not found error.
Exmaple
use timediff::*;
use timediff::TimeDiffError as Error;
assert_eq!(
TimeDiff::to_diff(String::from("-10s"))
.locale(String::from("zh-CN"))
.unwrap()
.parse(),
Ok(String::from("几秒前"))
);
assert_eq!(
TimeDiff::to_diff(String::from("10day")).locale(String::from("unknown")),
Err(Error::NotFoundLocale(String::from("unknown")))
);sourcepub fn parse(&mut self) -> Result<String, DurationError>
pub fn parse(&mut self) -> Result<String, DurationError>
Parses and prints a human-readable string representing the difference between two timestamps (a period of a duration)
Exmaple
use timediff::*;
assert_eq!(TimeDiff::to_diff(String::from("-10s")).parse(),Ok(String::from("a few seconds ago")))Trait Implementations
impl StructuralPartialEq for TimeDiff
Auto Trait Implementations
impl RefUnwindSafe for TimeDiff
impl Send for TimeDiff
impl Sync for TimeDiff
impl Unpin for TimeDiff
impl UnwindSafe for TimeDiff
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more