Skip to main content

whichtime_sys/parsers/ru/
casual_time.rs

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