Skip to main content

whichtime_sys/parsers/pt/
casual_time.rs

1//! Portuguese casual time parser
2//!
3//! Handles standalone Portuguese time expressions like:
4//! - "agora"
5//! - "meio-dia", "meia-noite"
6//! - "ao meio-dia", "a meia-noite"
7
8use crate::components::Component;
9use crate::context::ParsingContext;
10use crate::error::Result;
11use crate::parsers::Parser;
12use crate::results::ParsedResult;
13use crate::types::Meridiem;
14use chrono::{Datelike, Duration, Timelike};
15use fancy_regex::Regex;
16use std::sync::LazyLock;
17
18static PATTERN: LazyLock<Regex> = LazyLock::new(|| {
19    Regex::new(r"(?i)(?<![a-zA-Z])(?:(?:a|ao|à)\s+)?(meio-dia|meia-noite|agora)(?=\W|$)").unwrap()
20});
21
22const TIME_GROUP: usize = 1;
23
24/// Portuguese casual time parser
25pub struct PTCasualTimeParser;
26
27impl PTCasualTimeParser {
28    pub fn new() -> Self {
29        Self
30    }
31}
32
33impl Parser for PTCasualTimeParser {
34    fn name(&self) -> &'static str {
35        "PTCasualTimeParser"
36    }
37
38    fn should_apply(&self, _context: &ParsingContext) -> bool {
39        true
40    }
41
42    fn parse(&self, context: &ParsingContext) -> Result<Vec<ParsedResult>> {
43        let mut results = Vec::new();
44        let ref_date = context.reference.instant;
45
46        let mut start = 0;
47        while start < context.text.len() {
48            let search_text = &context.text[start..];
49            let captures = match PATTERN.captures(search_text) {
50                Ok(Some(caps)) => caps,
51                Ok(None) => break,
52                Err(_) => break,
53            };
54
55            let full_match = match captures.get(0) {
56                Some(m) => m,
57                None => break,
58            };
59
60            let match_start = start + full_match.start();
61            let match_end = start + full_match.end();
62
63            let time_keyword = captures
64                .get(TIME_GROUP)
65                .map(|m| m.as_str().to_lowercase())
66                .unwrap_or_default();
67
68            let mut components = context.create_components();
69            let mut target_date = ref_date;
70
71            match time_keyword.as_str() {
72                "agora" => {
73                    components.assign(Component::Year, ref_date.year());
74                    components.assign(Component::Month, ref_date.month() as i32);
75                    components.assign(Component::Day, ref_date.day() as i32);
76                    components.assign(Component::Hour, ref_date.hour() as i32);
77                    components.assign(Component::Minute, ref_date.minute() as i32);
78                    components.assign(Component::Second, ref_date.second() as i32);
79                }
80                "meio-dia" => {
81                    components.assign(Component::Year, target_date.year());
82                    components.assign(Component::Month, target_date.month() as i32);
83                    components.assign(Component::Day, target_date.day() as i32);
84                    components.imply(Component::Hour, 12);
85                    components.imply(Component::Minute, 0);
86                    components.assign(Component::Meridiem, Meridiem::AM as i32);
87                }
88                "meia-noite" => {
89                    // Midnight logic:
90                    // If implied, usually means coming midnight.
91                    // Always move to the next midnight (00:00) relative to now.
92                    // If it's 11:00, midnight is tomorrow 00:00.
93                    // If it's 23:00, midnight is tomorrow 00:00.
94                    // We assume standard logic: midnight is the start of the NEXT day.
95                    target_date = ref_date + Duration::days(1);
96
97                    components.assign(Component::Year, target_date.year());
98                    components.assign(Component::Month, target_date.month() as i32);
99                    components.assign(Component::Day, target_date.day() as i32);
100                    components.imply(Component::Hour, 0);
101                    components.imply(Component::Minute, 0);
102                }
103                _ => {
104                    start = match_end;
105                    continue;
106                }
107            }
108
109            results.push(context.create_result(match_start, match_end, components, None));
110
111            start = match_end;
112        }
113
114        Ok(results)
115    }
116}
117
118impl Default for PTCasualTimeParser {
119    fn default() -> Self {
120        Self::new()
121    }
122}