pub fn time_diff_now(
datetime: impl Into<Timestamp>,
) -> Result<TimeDiff, TimeAgoError>
Expand description
datetime argument can be a integer as timestamp or a string as datetime
Returns a TimeDiff struct based on how much time is remaining or passed based on the givin datetime
The TimeDiff struct has two methods , short_form()
& long_form()
\
the short_form()
returns a short description about time difference\
- 5 دقیقه قبل
- حدود 2 هفته بعد
the long_form()
returns a long and exact description about time difference\
- 6 سال و 6 ماه و 10 روز و 12 دقیقه و 37 ثانیه بعد
also there are some other methods like short_form_fa_digits()
or short_form_ar_digits()
that is the same as short_form()
but with farsi or arabic digits
§Warning
This function is designed to only works for these date time formats if you send datetime argument as datetime string :
%Y-%m-%d %H:%M:%S
: Sortable format%Y/%m/%d %H:%M:%S
: Sortable format%Y-%m-%dT%H:%M:%S%:z
: ISO 8601 with timezone offset%Y-%m-%dT%H:%M:%S%.3f%:z
: ISO 8601 with milliseconds and timezone offset%a, %d %b %Y %H:%M:%S %z
: RFC 2822 Format
timezone is set with the current timezone of the OS.
§Examples
use rust_persian_tools::time_diff::{TimeDiff , time_diff_now};
use chrono::{Duration,Local};
let current_time = Local::now();
let due_date = current_time
+ Duration::try_weeks(320).unwrap()
+ Duration::try_hours(7).unwrap()
+ Duration::try_minutes(13).unwrap()
+ Duration::try_seconds(37).unwrap();
let formatted_time = due_date.format("%Y-%m-%d %H:%M:%S").to_string();
assert_eq!(
time_diff_now(formatted_time).unwrap(),
TimeDiff {
years: 6,
months: 1,
days: 20,
hours: 7,
minutes: 13,
seconds: 37,
is_future: true,
}
);
// Example with short_form()
let current_time = Local::now();
let ten_minutes_ago = current_time - Duration::try_minutes(10).unwrap();
let formatted_time = ten_minutes_ago.format("%Y-%m-%d %H:%M:%S").to_string(); // create datetime string from 10 minutes ago
assert!(time_diff_now(formatted_time).is_ok_and(|datetime| datetime.short_form() == "10 دقیقه قبل"));