Skip to main content

deep_time/alloc_parse/
parse_date.rs

1use crate::{
2    ClassifiedDate, DateClassification, Dt, DtErr, DtErrKind, MAX_DATE_STRING_LEN, Mode, Order,
3    OrderFirst, ParseCfg, Scale, an_err, classify_date, default_date_parse_options,
4    generate_ambiguous_day_first_candidates, generate_ambiguous_month_first_candidates,
5    generate_ambiguous_year_first_candidates, generate_unambiguous_candidates,
6    is_week_date_missing_weekday, parse_pure_numeric_unix_timestamp, parse_syslog_no_year,
7    parse_week_date_no_weekday, parse_yyyy_mm, smart_detect_date_order, try_pure_numeric,
8};
9use alloc::borrow::Cow;
10use alloc::string::String;
11
12impl Dt {
13    /// Automatically parses datetime [`str`] into a [`Dt`] by guessing and generating the format. Supports the vast
14    /// majority of date formats.
15    ///
16    /// Requires the `"alloc"` feature.
17    ///
18    /// ## Parameters
19    ///
20    /// - `s`: The string to parse. Must be non-empty and no longer than 255 bytes. Empty strings or overly
21    ///   long inputs return an error.
22    /// - `opts`: Optional [`ParseCfg`]. Pass `None` to use the defaults.
23    ///
24    /// ## Configuration Options (via [`ParseCfg`])
25    ///
26    /// | Field          | Default     | Effect |
27    /// |----------------|-------------|--------|
28    /// | `lang`         | `En`        | Language, scroll down to see currently supported languages |
29    /// | `order`        | `Smart`     | How to resolve ambiguous numeric dates like `01/02/03` |
30    /// | `mode`         | `Auto`      | Special handling for purely numeric inputs |
31    /// | `parse`        | `None`      | If provided, these exact `strftime`-style formats are tried **first** (and exclusively if `mode` is `Explicit`) |
32    /// | `relative`     | `true`      | Enable phrases like "tomorrow", "next Friday", "in 3 days" |
33    /// | `ref_time`     | `None`      | Reference time for relative dates and syslog-style "no-year" dates (uses system time if `std` feature is enabled) |
34    /// | `to_lower`     | `true`      | Automatically lowercase the input, set to `false` only if it's already lowercase |
35    ///
36    /// ## Purely Numeric Inputs
37    ///
38    /// When the input consists **only** of digits (and optionally a decimal point),
39    /// the parser uses a fast, mode-aware path before trying any other strategies.
40    /// The exact interpretation depends on the number of digits and the selected `mode`.
41    ///
42    /// | Digits | Example(s)               | `Mode`          | Interpreted as                          | Notes |
43    /// |--------|--------------------------|-----------------|-----------------------------------------|-------|
44    /// | 1–4    | `2024`, `24`, `5`        | `Auto`/`Legacy` | Year (2-digit uses 2000/1900 pivot)    | 1- and 3-digit years only work in `Scientific` |
45    /// | 5      | `24123`, `60400`         | `Legacy`        | Ordinal date (YYDDD)                    | — |
46    /// | 5      | `60400`, `60400.75`      | `Scientific`    | Modified Julian Date (MJD)              | Fractional days supported |
47    /// | 5      | `24123`, `60400.75`      | `Auto`          | Ordinal (non-decimal) or MJD (decimal) | Smart default |
48    /// | 6      | `240315`, `202403`       | `Auto`          | YYYYMM if plausible year, else YYMMDD   | Most common compact form |
49    /// | 6      | `240315`                 | `Legacy`        | YYMMDD preferred                        | — |
50    /// | 6      | `202403`                 | `Scientific`    | YYYYMM preferred                        | — |
51    /// | 7      | `2024123`                | `Legacy`        | Ordinal date (YYYYDDD)                  | — |
52    /// | 7      | `2460123`, `2460123.5`   | `Scientific`    | Julian Day (JD)                         | Fractional days supported |
53    /// | 7      | `2024123`                | `Auto`          | Ordinal (integer) or JD (decimal)       | Smart default |
54    /// | 10–11  | `1735689600`             | any             | Unix seconds                            | — |
55    /// | 12–15  | `1735689600123`          | any             | Unix milliseconds                       | Most common high-precision case |
56    /// | 16–18  | `1735689600123456`       | any             | Unix microseconds                       | — |
57    /// | 19+    | `1735689600123456789`    | any             | Unix nanoseconds                        | Full precision |
58    ///
59    /// **Tip**: Use `Mode::UnixTimestamp` when you know the input is always a Unix timestamp.
60    ///
61    /// ## Ambiguous Numeric Dates
62    ///
63    /// Dates where the components could map to different orders (e.g. `01/02/03`,
64    /// `3-4-5`, `15.03.24`, `2024.03.15`) are resolved via the `order` field:
65    ///
66    /// - **`Order::Smart`** (default) — Applies the fast heuristic described in [`Order::Smart`].
67    ///   It strongly prefers modern/tech conventions (Year-first for compact/ISO-like data)
68    ///   while handling the majority of international and US-style dates.
69    ///
70    /// - **`Order::Year`**, **`Order::Day`**, or **`Order::Month`** force a
71    ///   specific interpretation and bypass the heuristic entirely.
72    ///
73    /// This combination of `Smart` + `Auto` mode gives the best real-world parsing
74    /// success rate for mixed data sources.
75    ///
76    /// ## Other Supported Formats
77    ///
78    /// - **ISO 8601** and variants: `2024-03-15`, `2024-03-15T14:30:00Z`, `2024-03-15T14:30:00+01:00[Europe/Paris]`
79    /// - **Named dates** (in supported languages): `15 March 2024`, `15 mars 2024`, `15. März 2024`, `15 de marzo de 2024`
80    /// - **Week dates**: `2024-W15`, `2024-W15-3`, `2024W153` (missing weekday defaults to Monday)
81    /// - **Syslog-style** (no year): `Mar  5 10:23:45` (year inferred from `ref_time`)
82    /// - **Relative expressions**: `tomorrow`, `next Friday at 09:00`, `in 3 days`, `2 weeks ago`
83    /// - **12-hour time**: `2:30 PM`, `14:30:45.123`
84    /// - **Offsets and timezones**: `+0100`, `-05:30`, `Z`, IANA names in brackets
85    ///
86    /// ## Examples
87    ///
88    /// ```rust
89    /// use deep_time::{Dt, ParseCfg, Order, Mode, Lang};
90    ///
91    /// // Default smart parsing
92    /// let dt = Dt::from_str_parse("2024-03-15 14:30:00", &None).unwrap();
93    ///
94    /// // German named date
95    /// let cfg = ParseCfg { lang: Lang::De, ..Default::default() };
96    /// let dt = Dt::from_str_parse("15. März 2024 um 14:30", &Some(cfg)).unwrap();
97    ///
98    /// // Force month-first
99    /// let cfg = ParseCfg { order: Order::Month, ..Default::default() };
100    /// let dt = Dt::from_str_parse("03/15/2024", &Some(cfg)).unwrap();
101    ///
102    /// // Pure numeric compact form
103    /// let dt = Dt::from_str_parse("20240315", &None).unwrap(); // March 15, 2024
104    ///
105    /// // Unix timestamp (milliseconds)
106    /// let cfg = ParseCfg { mode: Mode::UnixTimestamp, ..Default::default() };
107    /// let dt = Dt::from_str_parse("1735689600123", &Some(cfg)).unwrap();
108    ///
109    /// // Explicit formats only (no fallback)
110    /// let cfg = ParseCfg {
111    ///     parse: Some(vec!["%d/%m/%Y".into(), "%Y-%m-%d".into()]),
112    ///     mode: Mode::Explicit,
113    ///     ..Default::default()
114    /// };
115    /// let dt = Dt::from_str_parse("15/03/2024", &Some(cfg)).unwrap();
116    ///
117    /// // Relative date
118    /// let dt = Dt::from_str_parse("2 days from now", &None).unwrap();
119    /// ```
120    ///
121    /// ## Notes
122    ///
123    /// - The `Smart` + `Auto` combination gives the best real-world success rate for mixed data.
124    /// - All successfully parsed [`Dt`] values are stored with attosecond precision on the internal TAI timescale.
125    /// - For maximum reproducibility in production code, prefer `ParseCfg` with `parse: Some(...)` and `mode: Explicit`.
126    /// - Timezone handling (IANA names and fixed offsets) is fully supported.
127    ///
128    /// See also: [`ParseCfg`], [`Order`], [`Mode`], [`Lang`], [`Dt`],
129    /// [`Dt::str_to_attos`], [`Dt::str_to_ms`], [`Dt::str_to_unix_ms`].
130    ///
131    /// ## Supported Languages:
132    ///
133    /// - En
134    /// - De
135    /// - Es
136    /// - Fr
137    ///
138    pub fn from_str_parse(s: &str, opts: &Option<ParseCfg>) -> Result<Dt, DtErr> {
139        let opts: &ParseCfg = opts
140            .as_ref()
141            .unwrap_or_else(|| default_date_parse_options());
142
143        if s.is_empty() {
144            return Err(an_err!(DtErrKind::Incomplete, "empty"));
145        } else if s.len() > MAX_DATE_STRING_LEN {
146            return Err(an_err!(DtErrKind::InvalidInput, "too long: {}", s));
147        }
148
149        let lang = opts.lang;
150        let ref_time = &opts.ref_time;
151
152        let lowered: Cow<str> = if opts.to_lower {
153            Cow::Owned(s.to_lowercase())
154        } else {
155            Cow::Borrowed(s)
156        };
157
158        let classification = match classify_date(&lowered, lang, ref_time) {
159            Ok(ClassifiedDate::Parsed(time_point)) => return Ok(time_point),
160            Ok(ClassifiedDate::Cls(c)) => c,
161            Err(e) => {
162                // std::eprintln!("{}", e);
163                return Err(an_err!(
164                    DtErrKind::InvalidInput,
165                    "{}",
166                    s => e
167                ));
168            }
169        };
170
171        // let xx = &classification.date;
172        // if xx != trimmed {
173        //     eprintln!("NOT EQUAL: {:?}, {:?}", trimmed, xx);
174        // }
175        // eprintln!("BEFORE & AFTER: {:?}, {:?}", lowered, &classification.date);
176
177        let normalized = &classification.date;
178
179        let (mode, date_order) = if let Some(formats) = &opts.parse {
180            if !formats.is_empty() {
181                for fmt in formats {
182                    if let Ok(value) = Self::from_str(normalized, fmt, true, true, false) {
183                        return Ok(value);
184                    }
185                }
186                // None of the provided formats worked and mode is Explicit
187                if opts.mode == Mode::Explicit {
188                    return Err(an_err!(DtErrKind::InvalidInput, "{}", s));
189                }
190            }
191            (opts.mode, opts.order)
192        } else {
193            (opts.mode, opts.order)
194        };
195
196        // if s == "2006-04-02 02:30-05 America/Indiana/Vevay" {
197        //     std::eprintln!("{:?}", classification);
198        // }
199        // std::eprintln!("{:?}", classification);
200
201        if classification.is_pure_numeric {
202            match mode {
203                Mode::UnixTimestamp => {
204                    if let Some(dt) = parse_pure_numeric_unix_timestamp(
205                        normalized,
206                        classification.num_non_decimal_digits as usize,
207                    ) {
208                        return Ok(dt);
209                    }
210                }
211                _ => {
212                    if let Some(dt) = try_pure_numeric(
213                        normalized,
214                        classification.num_digits,
215                        classification.num_non_decimal_digits,
216                        classification.is_decimal,
217                        mode,
218                    ) {
219                        // std::eprintln!("NUMERIC INPUT SUCCESS: {:?}", s);
220                        return Ok(dt);
221                    }
222                }
223            }
224        }
225        if !classification.has_year
226            && let Some(dt) = parse_syslog_no_year(normalized, lang, ref_time)
227        {
228            return Ok(dt);
229        }
230
231        if is_week_date_missing_weekday(&classification) {
232            // std::eprintln!("IS WEEK DATE MISSING WEEKDAY: {:?}", s);
233            if let Some(dt) = parse_week_date_no_weekday(&classification, lang, ref_time) {
234                return Ok(dt);
235            }
236        }
237        if let Some(dt) = try_unambiguous(normalized, &classification) {
238            return Ok(dt);
239        }
240        // std::eprintln!("done trying unambiguous");
241        if let Some(dt) = match date_order {
242            Order::Smart => {
243                let order = smart_detect_date_order(normalized, &classification);
244                let mut result: Option<Dt>;
245
246                match order {
247                    OrderFirst::Day => {
248                        result = try_compatible_formats(
249                            normalized,
250                            generate_ambiguous_day_first_candidates(&classification),
251                        );
252                        // std::eprintln!("done trying day first: {:?}", result);
253
254                        if result.is_none() {
255                            result = try_compatible_formats(
256                                normalized,
257                                generate_ambiguous_month_first_candidates(&classification),
258                            );
259                            // std::eprintln!("done trying month first: {:?}", result);
260                        }
261
262                        if result.is_none() {
263                            result = try_compatible_formats(
264                                normalized,
265                                generate_ambiguous_year_first_candidates(&classification),
266                            );
267                            // std::eprintln!("done trying year first: {:?}", result);
268                        }
269                    }
270                    OrderFirst::Month => {
271                        result = try_compatible_formats(
272                            normalized,
273                            generate_ambiguous_month_first_candidates(&classification),
274                        );
275                        // std::eprintln!("done trying month first: {:?}", result);
276
277                        if result.is_none() {
278                            result = try_compatible_formats(
279                                normalized,
280                                generate_ambiguous_day_first_candidates(&classification),
281                            );
282                            // std::eprintln!("done trying day first: {:?}", result);
283                        }
284
285                        if result.is_none() {
286                            result = try_compatible_formats(
287                                normalized,
288                                generate_ambiguous_year_first_candidates(&classification),
289                            );
290                            // std::eprintln!("done trying year first: {:?}", result);
291                        }
292                    }
293                    OrderFirst::Year => {
294                        result = try_compatible_formats(
295                            normalized,
296                            generate_ambiguous_year_first_candidates(&classification),
297                        );
298                        // std::eprintln!("done trying year first: {:?}", result);
299
300                        if result.is_none() {
301                            result = try_compatible_formats(
302                                normalized,
303                                generate_ambiguous_day_first_candidates(&classification),
304                            );
305                            // std::eprintln!("done trying day first: {:?}", result);
306                        }
307
308                        if result.is_none() {
309                            result = try_compatible_formats(
310                                normalized,
311                                generate_ambiguous_month_first_candidates(&classification),
312                            );
313                            // std::eprintln!("done trying month first: {:?}", result);
314                        }
315                    }
316                }
317
318                result
319            }
320            Order::Year => try_compatible_formats(
321                normalized,
322                generate_ambiguous_year_first_candidates(&classification),
323            ),
324            Order::Day => try_compatible_formats(
325                normalized,
326                generate_ambiguous_day_first_candidates(&classification),
327            ),
328            Order::Month => try_compatible_formats(
329                normalized,
330                generate_ambiguous_month_first_candidates(&classification),
331            ),
332        } {
333            return Ok(dt);
334        }
335        // std::eprintln!("NOW trying numeric timestamp");
336        if classification.is_pure_numeric
337            && mode != Mode::UnixTimestamp
338            && let Some(dt) = parse_pure_numeric_unix_timestamp(
339                normalized,
340                classification.num_non_decimal_digits as usize,
341            )
342        {
343            return Ok(dt);
344        }
345        Err(an_err!(DtErrKind::InvalidInput, "{}", s))
346    }
347
348    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
349    /// but returns attoseconds since the library epoch: 2000-01-01 12:00:00 UTC
350    /// (on the UTC scale).
351    ///
352    /// Returns `Some(attos)` on success (negative for pre-2000 dates) or `None`
353    /// on any parse error.
354    #[inline]
355    pub fn str_to_attos(s: &str, opts: &Option<ParseCfg>) -> Option<i128> {
356        Dt::from_str_parse(s, opts).ok().map(|tp| tp.to_attos())
357    }
358
359    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
360    /// but returns milliseconds since the library epoch: 2000-01-01 12:00:00 UTC
361    /// (on the UTC scale).
362    ///
363    /// Returns `Some(millis)` on success (negative for pre-2000 dates) or `None`
364    /// on any parse error.
365    #[inline]
366    pub fn str_to_ms(s: &str, opts: &Option<ParseCfg>) -> Option<i128> {
367        Dt::from_str_parse(s, opts).ok().map(|tp| tp.to_ms())
368    }
369
370    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
371    /// but returns nanoseconds since the library epoch: 2000-01-01 12:00:00 UTC
372    /// (on the UTC scale).
373    ///
374    /// Returns `Some(nanos)` on success (negative for pre-2000 dates) or `None`
375    /// on any parse error.
376    #[inline]
377    pub fn str_to_ns(s: &str, opts: &Option<ParseCfg>) -> Option<i128> {
378        Dt::from_str_parse(s, opts).ok().map(|tp| tp.to_ns())
379    }
380
381    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
382    /// but returns milliseconds since the UNIX epoch: (1970-01-01 00:00:00 UTC).
383    ///
384    /// Returns `Some(millis)` on success (negative for pre-2000 dates) or `None`
385    /// on any parse error.
386    #[inline]
387    pub fn str_to_unix_ms(s: &str, opts: &Option<ParseCfg>) -> Option<i128> {
388        Dt::from_str_parse(s, opts).ok().map(|tp| {
389            tp.to_scale_and_then_diff(Scale::UTC, Dt::UNIX_EPOCH)
390                .to_ms()
391        })
392    }
393
394    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
395    /// but returns nanoseconds since the UNIX epoch: (1970-01-01 00:00:00 UTC).
396    ///
397    /// Returns `Some(nanos)` on success (negative for pre-2000 dates) or `None`
398    /// on any parse error.
399    #[inline]
400    pub fn str_to_unix_ns(s: &str, opts: &Option<ParseCfg>) -> Option<i128> {
401        Dt::from_str_parse(s, opts).ok().map(|tp| {
402            tp.to_scale_and_then_diff(Scale::UTC, Dt::UNIX_EPOCH)
403                .to_ns()
404        })
405    }
406}
407
408/// Core zero-allocation helper (updated to match the new `&str` signature).
409///
410/// The `fmt` we get from the iterator is still `'static`, but it coerces automatically
411/// to `&str`, so everything continues to work.
412#[inline]
413pub(crate) fn try_compatible_formats<I>(s: &str, formats: I) -> Option<Dt>
414where
415    I: IntoIterator<Item = String>,
416{
417    let mut dt = None;
418
419    for fmt in formats.into_iter() {
420        // eprintln!("TRYING FMT: {}", fmt);
421        if let Ok(parsed) = Dt::from_str(s, &fmt, true, true, false) {
422            dt = Some(parsed);
423            break;
424        }
425        // === DEBUG ===
426        // eprintln!("Tried format: {:?}", fmt);
427    }
428
429    dt
430    // formats
431    //     .into_iter()
432    //     .find_map(|fmt| Dt::from_str(s, &fmt, true, true, false).ok())
433}
434
435#[inline]
436pub(crate) fn try_unambiguous(s: &str, classification: &DateClassification) -> Option<Dt> {
437    if matches!(classification.bytes_len, 6..=8)
438        && let Some(dt) = parse_yyyy_mm(s.as_bytes())
439    {
440        return Some(dt);
441    }
442    try_compatible_formats(s, generate_unambiguous_candidates(classification))
443}