Skip to main content

Crate temps_chrono

Crate temps_chrono 

Source
Expand description

§temps-chrono

Chrono integration for the temps time expression parser.

This crate provides a ChronoProvider that implements the TimeParser trait using the chrono datetime library. It enables parsing natural language time expressions into chrono’s DateTime<Local> type.

§Features

  • Full implementation of the temps TimeParser trait
  • Support for all time expression types
  • Proper handling of month/year arithmetic
  • Timezone support (UTC and fixed offsets)
  • DST-aware local time handling

§Example

use temps_chrono::{ChronoProvider, parse_to_datetime};
use temps_core::{Language, TimeParser};

// Parse using the convenience function
let datetime = parse_to_datetime("in 5 minutes", Language::English).unwrap();
println!("In 5 minutes: {}", datetime);

// Or use the provider directly
let provider = ChronoProvider;
let expr = temps_core::parse("tomorrow at 3:30 pm", Language::English).unwrap();
let datetime = provider.parse_expression(expr).unwrap();

§Month and Year Arithmetic

This implementation uses chrono’s checked_add_months and checked_sub_months for proper month/year arithmetic. This handles edge cases correctly:

  • January 31 + 1 month = February 29 (leap year) or February 28 (non-leap year)
  • February 29, 2024 + 1 year = February 28, 2025

§Error Handling

All parsing operations return Result<DateTime<Local>, TempsError>. Common errors include:

  • ParseError: Invalid input that cannot be parsed
  • DateCalculationError: Date arithmetic that results in invalid dates
  • AmbiguousTime: Local times that are ambiguous due to DST transitions
  • InvalidDate/InvalidTime: Components that are out of valid ranges

Structs§

ChronoProvider
Chrono-based implementation of the TimeParser trait.

Functions§

parse_to_datetime
Parse a natural language time expression into a chrono DateTime<Local>.