Function time_diff_between

Source
pub fn time_diff_between(
    start: impl Into<Timestamp>,
    end: impl Into<Timestamp>,
) -> Result<TimeDiff, TimeAgoError>
Expand description

start & end arguments 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 diffrence between two datetime
The TimeDiff struct has two main 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 datetime formats if you send start or end 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_between};
use chrono::{Duration,Local};

let current_time = Local::now();
let start = current_time
    + Duration::try_weeks(320).unwrap()
    + Duration::try_hours(7).unwrap()
    + Duration::try_minutes(13).unwrap()
    + Duration::try_seconds(37).unwrap();
let end = (current_time + Duration::try_weeks(150).unwrap() + Duration::try_hours(4).unwrap()).timestamp();
let formatted_time = start.format("%Y-%m-%d %H:%M:%S").to_string();
assert_eq!(
    time_diff_between(formatted_time, end).unwrap(),
    TimeDiff {
        years: 3,
        months: 3,
        days: 5,
        hours: 3,
        minutes: 13,
        seconds: 37,
        is_future: false,
    }
);

// Example with long_form() with persian digits