parse_duration

Function parse_duration 

Source
pub fn parse_duration(duration: &str) -> Result<DateTime<Utc>>
Expand description

Parse a duration string (e.g., “7d”, “24h”, “30m”) into a DateTime

Supported units:

  • d: days
  • h: hours
  • m: minutes
  • s: seconds
  • w: weeks

§Arguments

  • duration - Duration string in format like “7d”, “24h”, “30m”, “5w”

§Returns

A DateTime representing the current time minus the specified duration

§Errors

Returns InvalidInput error if:

  • Duration string is empty or too short
  • Number part is not a valid integer
  • Unit is not one of d/h/m/s/w

§Examples

use crate::time_utils::parse_duration;

let seven_days_ago = parse_duration("7d").unwrap();
let one_week_ago = parse_duration("1w").unwrap();