Skip to main content

whichtime_sys/refiners/
forward_date.rs

1//! Forward date refiner - adjusts dates to be in the future when ambiguous
2
3use crate::components::Component;
4use crate::context::ParsingContext;
5use crate::dictionaries::Locale;
6use crate::refiners::Refiner;
7use crate::results::ParsedResult;
8use chrono::Datelike;
9
10/// Refiner that pushes ambiguous calendar dates into the future when needed.
11pub struct ForwardDateRefiner;
12
13impl Refiner for ForwardDateRefiner {
14    fn refine(&self, context: &ParsingContext, results: Vec<ParsedResult>) -> Vec<ParsedResult> {
15        if matches!(context.locale, Locale::Ja) {
16            return results;
17        }
18
19        let ref_date = context.reference.instant;
20
21        results
22            .into_iter()
23            .map(|mut result| {
24                // Only adjust if year is not certain
25                if !result.start.is_certain(Component::Year)
26                    && let (Some(month), Some(day)) = (
27                        result.start.get(Component::Month),
28                        result.start.get(Component::Day),
29                    )
30                {
31                    let ref_month = ref_date.month() as i32;
32                    let ref_day = ref_date.day() as i32;
33
34                    // If the date is in the past this year, move to next year
35                    if month < ref_month || (month == ref_month && day < ref_day) {
36                        result.start.assign(Component::Year, ref_date.year() + 1);
37                    }
38                }
39                result
40            })
41            .collect()
42    }
43}