pub struct TimeDiff { /* private fields */ }
Expand description
A wrapper type associated with locale
and to
info.
Implementationsยง
Sourceยงimpl 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 Freeze for TimeDiff
impl RefUnwindSafe for TimeDiff
impl Send for TimeDiff
impl Sync for TimeDiff
impl Unpin for TimeDiff
impl UnwindSafe for TimeDiff
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
Mutably borrows from an owned value. Read more