Skip to main content

deep_time/alloc_parse/
parse_date.rs

1use crate::{
2    ClassifiedDate, DateClassification, Dt, DtErr, DtErrKind, Lang, Mode, Order, OrderFirst,
3    ParseCfg, STRTIME_SIZE, an_err, classify_date, generate_ambiguous_day_first_candidates,
4    generate_ambiguous_month_first_candidates, generate_ambiguous_year_first_candidates,
5    generate_unambiguous_candidates, is_week_date_missing_weekday,
6    parse_pure_numeric_unix_timestamp, parse_syslog_no_year, parse_week_date_no_weekday,
7    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 `"parse"` feature (which enables `alloc`).
17    /// - The returned [`Dt`] is internally on the TAI time scale. The `attos` field is an [`i128`] attosecond
18    ///   count since TAI 2000-01-01 noon. See [`Scale`] for more information.
19    /// - The returned [`Dt`] is **not** in local time, if a timezone is parsed then it's used to find the offset
20    ///   to return non-local instant.
21    ///
22    /// ## Parameters
23    ///
24    /// - `s`: The string to parse. Must be non-empty and no longer than 255 bytes. Empty strings or overly
25    ///   long inputs return an error.
26    /// - `opts`: The [`ParseCfg`] to use. Pass `&ParseCfg::DEFAULT` (or `&ParseCfg::default()`)
27    ///   to use the standard smart defaults. You can create a `ParseCfg` once and pass `&cfg`
28    ///   on every call for consistent behavior and to avoid repeated construction.
29    ///
30    /// ## Configuration Options
31    ///
32    /// These are the fields of the configuration options struct [`ParseCfg`], their types and defaults.
33    ///
34    /// See [`ParseCfg`] for more information.
35    ///
36    /// | Field          | Type and Default     | Effect |
37    /// |----------------|----------------------------------|--------|
38    /// | `lang`         | [`Lang::En`]                     | Language, scroll down to see currently supported languages                                        |
39    /// | `order`        | [`Order::Smart`]                 | How to resolve ambiguous numeric dates like `01/02/03`                                            |
40    /// | `mode`         | [`Mode::Auto`]                   | Special handling for purely numeric inputs                                                        |
41    /// | `parse`        | [`Option<Vec<String>>`] - `None` | An explicit list of formats to try, if the [`Mode`] is Explicit then only these formats are tried |
42    /// | `relative`     | [`bool`] - `true`                | Enable phrases like "tomorrow", "in 3 days"                   |
43    /// | `ref_time`     | [`Option<Dt>`] - `None`          | Reference time for relative dates and syslog-style "no-year" dates                                |
44    /// | `to_lower`     | [`bool`] - `true`                | Automatically lowercase the input, **only** set to false if it's already lowercase                |
45    ///
46    /// ## Purely Numeric Inputs
47    ///
48    /// When the input consists **only** of digits (and optionally a decimal point),
49    /// the parser uses a fast, mode-aware path before trying any other strategies.
50    /// The exact interpretation depends on the number of digits and the selected `mode`.
51    ///
52    /// | Digits | Example(s)               | `Mode`          | Interpreted as                          | Notes |
53    /// |--------|--------------------------|-----------------|-----------------------------------------|-------|
54    /// | 1–4    | `2024`, `24`, `5`        | `Auto`/`Legacy` | Year (2-digit uses 2000/1900 pivot)    | 1- and 3-digit years only work in `Scientific` |
55    /// | 5      | `24123`, `60400`         | `Legacy`        | Ordinal date (YYDDD)                    | — |
56    /// | 5      | `60400`, `60400.75`      | `Scientific`    | Modified Julian Date (MJD)              | Fractional days supported |
57    /// | 5      | `24123`, `60400.75`      | `Auto`          | Ordinal (non-decimal) or MJD (decimal) | Smart default |
58    /// | 6      | `240315`, `202403`       | `Auto`          | YYYYMM if plausible year, else YYMMDD   | Most common compact form |
59    /// | 6      | `240315`                 | `Legacy`        | YYMMDD preferred                        | — |
60    /// | 6      | `202403`                 | `Scientific`    | YYYYMM preferred                        | — |
61    /// | 7      | `2024123`                | `Legacy`        | Ordinal date (YYYYDDD)                  | — |
62    /// | 7      | `2460123`, `2460123.5`   | `Scientific`    | Julian Day (JD)                         | Fractional days supported |
63    /// | 7      | `2024123`                | `Auto`          | Ordinal (integer) or JD (decimal)       | Smart default |
64    /// | 10–11  | `1735689600`             | any             | Unix seconds                            | — |
65    /// | 12–15  | `1735689600123`          | any             | Unix milliseconds                       | Most common high-precision case |
66    /// | 16–18  | `1735689600123456`       | any             | Unix microseconds                       | — |
67    /// | 19+    | `1735689600123456789`    | any             | Unix nanoseconds                        | Full precision |
68    ///
69    /// Use `Mode::UnixTimestamp` when you know the input is always a Unix timestamp.
70    ///
71    /// ## Ambiguous Numeric Dates
72    ///
73    /// Dates where the components could map to different orders (e.g. `01/02/03`,
74    /// `3-4-5`, `15.03.24`, `2024.03.15`) are resolved via the `order` field:
75    ///
76    /// - **`Order::Smart`** (default) — Applies the fast heuristic described in [`Order::Smart`].
77    ///   It strongly prefers modern/tech conventions (Year-first for compact/ISO-like data)
78    ///   while handling the majority of international and US-style dates.
79    ///
80    /// - **`Order::Year`**, **`Order::Day`**, or **`Order::Month`** force a
81    ///   specific interpretation and bypass the heuristic entirely.
82    ///
83    /// ## Supported Formats
84    ///
85    /// The parser tokenizes known words (month/day names, relative phrases, timezones, etc.), generates candidate
86    /// formats from the token pattern, and tries them until one matches. Thousands of layouts are supported.
87    ///
88    /// Separators generally don't matter, they could be spaces, slashes, or hyphens, but **not colons** - colons are
89    /// reserved for the time connector, times, and offsets.
90    ///
91    /// Generally speaking the date part must come first, and stuff like time components, offsets and iana timezone names
92    /// must come afterwards.
93    ///
94    /// - **ISO 8601** and variants: `2024-03-15`, `2024-03-15T14:30:00Z`, `2024-03-15T14:30:00+01:00[Europe/Paris]`
95    /// - **Named dates** (in supported languages): `15 March 2024`, `15 mars 2024`, `15. März 2024`, `15 de marzo de 2024`
96    /// - **Week dates**: `2024-W15`, `2024-W15-3`, `2024W153` (missing weekday defaults to Monday)
97    /// - **Syslog-style** (no year): `Mar  5 10:23:45` (year inferred from `ref_time`)
98    /// - **Relative expressions**: `tomorrow`, `in 3 days`, `2 weeks ago`
99    /// - **12-hour time**: `2:30 PM`, `14:30:45.123`
100    /// - **Offsets and timezones**: `+0100`, `-05:30`, `Z`, IANA timezone names (with the `jiff-tz` feature enabled)
101    /// - **Library time scales**: `TAI`, `TT`, etc. are detected and parsed, must come after the date part of the input
102    ///
103    /// Relative dates are also automatically supported, except for bare numbers with no colons like `0900`, as these
104    /// are differently interpreted.
105    ///
106    /// ## Examples
107    ///
108    /// ```rust
109    /// use deep_time::{Dt, ParseCfg, Order, Mode, Scale};
110    ///
111    /// // Default smart parsing
112    /// let dt = Dt::from_str_parse("2024-03-15 14:30:00", &ParseCfg::DEFAULT).unwrap();
113    ///
114    /// // German named date (requires the `de` feature)
115    /// # #[cfg(feature = "de")]
116    /// # {
117    /// # use deep_time::Lang;
118    /// let cfg = ParseCfg { lang: Lang::De, ..Default::default() };
119    /// let dt = Dt::from_str_parse("15. März 2024 um 14:30", &cfg).unwrap();
120    /// # }
121    ///
122    /// // Pure numeric compact form
123    /// let dt = Dt::from_str_parse("20240315", &ParseCfg::DEFAULT).unwrap(); // March 15, 2024
124    ///
125    /// // Unix timestamp (milliseconds)
126    /// let cfg = ParseCfg { mode: Mode::UnixTimestamp, ..Default::default() };
127    /// let dt = Dt::from_str_parse("1735689600123", &cfg).unwrap();
128    ///
129    /// // Explicit formats only (no fallback)
130    /// let cfg = ParseCfg {
131    ///     parse: Some(vec!["%d/%m/%Y".into(), "%Y-%m-%d".into()]),
132    ///     mode: Mode::Explicit,
133    ///     ..Default::default()
134    /// };
135    /// let dt = Dt::from_str_parse("15/03/2024", &cfg).unwrap();
136    ///
137    /// // Relative dates — build config once, borrow repeatedly
138    /// let ref_time = Dt::from_ymd(2026, 6, 16, Scale::UTC, 12, 0, 0, 0);
139    /// let cfg = ParseCfg {
140    ///     ref_time: Some(ref_time),
141    ///     ..Default::default()
142    /// };
143    /// let dt = Dt::from_str_parse("next Monday at 14:00", &cfg).unwrap();
144    ///
145    /// assert_eq!(dt, Dt::from_ymd(2026, 6, 22, Scale::UTC, 14, 0, 0, 0));
146    /// ```
147    ///
148    /// ## Notes
149    ///
150    /// - The `Smart` + `Auto` combination gives the best real-world success rate for mixed data.
151    /// - Relative expressions and syslog-style no-year dates need a reference time. If `ref_time` is `None`
152    ///   and the `std` feature is enabled, system time is used; without `std`, set `ref_time` explicitly or
153    ///   parsing will fail.
154    /// - All successfully parsed [`Dt`] values are stored with attosecond precision on the internal
155    ///   TAI timescale.
156    /// - Timezone handling (IANA names and fixed offsets) is fully supported when the `jiff-tz` feature
157    ///   is enabled.
158    ///
159    /// ## Supported Languages:
160    ///
161    /// Language support here basically means supporting abbreviated and full day and month names.
162    /// Non-Ascii types of numeric characters are also supported such as full width digits.
163    ///
164    /// Some day/month names in non-English languages are not supported due to clashes, any such missing
165    /// support is noted below.
166    ///
167    /// - En
168    /// - De
169    ///     - Won't parse "t" as short form for day.
170    /// - Es
171    ///     - English word "ago" won't be detected as relative date word.
172    ///     - Won't parse "mar" as tuesday, will instead parse as march.
173    /// - Fr
174    ///     - Won't parse "mar" as tuesday, will instead parse as march.
175    ///
176    /// ## See also
177    ///
178    /// - [`ParseCfg`]
179    /// - [`Order`]
180    /// - [`Mode`]
181    /// - [`Lang`]
182    /// - [`Dt`]
183    /// - [`Dt::from_str_iso`](../struct.Dt.html#method.from_str_iso)
184    pub fn from_str_parse(s: &str, opts: &ParseCfg) -> Result<Dt, DtErr> {
185        if s.is_empty() {
186            return Err(an_err!(DtErrKind::Empty));
187        } else if s.len() > STRTIME_SIZE {
188            return Err(an_err!(DtErrKind::InvalidLen));
189        }
190
191        let lang: Lang = opts.lang;
192        let ref_time = &opts.ref_time;
193
194        let lowered: Cow<str> = if opts.to_lower {
195            Cow::Owned(s.to_lowercase())
196        } else {
197            Cow::Borrowed(s)
198        };
199
200        let classification = match classify_date(&lowered, lang, ref_time) {
201            Ok(ClassifiedDate::Parsed(time_point)) => return Ok(time_point),
202            Ok(ClassifiedDate::Cls(c)) => c,
203            Err(e) => {
204                // std::eprintln!("{}", e);
205                return Err(an_err!(" {}", s => e));
206            }
207        };
208
209        // let xx = &classification.date;
210        // if xx != trimmed {
211        //     eprintln!("NOT EQUAL: {:?}, {:?}", trimmed, xx);
212        // }
213        // eprintln!("BEFORE & AFTER: {:?}, {:?}", lowered, &classification.date);
214
215        let normalized = &classification.date;
216
217        let (mode, date_order) = if let Some(formats) = &opts.parse {
218            if !formats.is_empty() {
219                for fmt in formats {
220                    if let Ok(value) = Self::from_str(normalized, fmt, true, true, false) {
221                        return Ok(value);
222                    }
223                }
224                // None of the provided formats worked and mode is Explicit
225                if opts.mode == Mode::Explicit {
226                    return Err(an_err!(DtErrKind::InvalidInput, "{}", s));
227                }
228            }
229            (opts.mode, opts.order)
230        } else {
231            (opts.mode, opts.order)
232        };
233
234        // if s == "on the 5th of april 2024 at 00:00am" {
235        //     std::eprintln!("{:?}", classification);
236        // }
237        // std::eprintln!("{:?}", classification);
238
239        if classification.is_pure_numeric {
240            match mode {
241                Mode::UnixTimestamp => {
242                    if let Some(dt) = parse_pure_numeric_unix_timestamp(
243                        normalized,
244                        classification.num_non_decimal_digits as usize,
245                    ) {
246                        return Ok(dt);
247                    }
248                }
249                _ => {
250                    if let Some(dt) = try_pure_numeric(
251                        normalized,
252                        classification.num_digits,
253                        classification.num_non_decimal_digits,
254                        classification.is_decimal,
255                        mode,
256                    ) {
257                        // std::eprintln!("NUMERIC INPUT SUCCESS: {:?}", s);
258                        return Ok(dt);
259                    }
260                }
261            }
262        }
263        if !classification.has_year
264            && let Some(dt) = parse_syslog_no_year(normalized, lang, ref_time)
265        {
266            return Ok(dt);
267        }
268
269        if is_week_date_missing_weekday(&classification) {
270            // std::eprintln!("IS WEEK DATE MISSING WEEKDAY: {:?}", s);
271            if let Some(dt) = parse_week_date_no_weekday(&classification, lang, ref_time) {
272                return Ok(dt);
273            }
274        }
275        if let Some(dt) = try_unambiguous(normalized, &classification) {
276            return Ok(dt);
277        }
278        // std::eprintln!("done trying unambiguous");
279        if let Some(dt) = match date_order {
280            Order::Smart => {
281                let order = smart_detect_date_order(normalized, &classification);
282                let mut result: Option<Dt>;
283
284                match order {
285                    OrderFirst::Day => {
286                        result = try_compatible_formats(
287                            normalized,
288                            generate_ambiguous_day_first_candidates(&classification),
289                        );
290                        // std::eprintln!("done trying day first: {:?}", result);
291
292                        if result.is_none() {
293                            result = try_compatible_formats(
294                                normalized,
295                                generate_ambiguous_month_first_candidates(&classification),
296                            );
297                            // std::eprintln!("done trying month first: {:?}", result);
298                        }
299
300                        if result.is_none() {
301                            result = try_compatible_formats(
302                                normalized,
303                                generate_ambiguous_year_first_candidates(&classification),
304                            );
305                            // std::eprintln!("done trying year first: {:?}", result);
306                        }
307                    }
308                    OrderFirst::Month => {
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                        if result.is_none() {
316                            result = try_compatible_formats(
317                                normalized,
318                                generate_ambiguous_day_first_candidates(&classification),
319                            );
320                            // std::eprintln!("done trying day first: {:?}", result);
321                        }
322
323                        if result.is_none() {
324                            result = try_compatible_formats(
325                                normalized,
326                                generate_ambiguous_year_first_candidates(&classification),
327                            );
328                            // std::eprintln!("done trying year first: {:?}", result);
329                        }
330                    }
331                    OrderFirst::Year => {
332                        result = try_compatible_formats(
333                            normalized,
334                            generate_ambiguous_year_first_candidates(&classification),
335                        );
336                        // std::eprintln!("done trying year first: {:?}", result);
337
338                        if result.is_none() {
339                            result = try_compatible_formats(
340                                normalized,
341                                generate_ambiguous_day_first_candidates(&classification),
342                            );
343                            // std::eprintln!("done trying day first: {:?}", result);
344                        }
345
346                        if result.is_none() {
347                            result = try_compatible_formats(
348                                normalized,
349                                generate_ambiguous_month_first_candidates(&classification),
350                            );
351                            // std::eprintln!("done trying month first: {:?}", result);
352                        }
353                    }
354                }
355
356                result
357            }
358            Order::Year => try_compatible_formats(
359                normalized,
360                generate_ambiguous_year_first_candidates(&classification),
361            ),
362            Order::Day => try_compatible_formats(
363                normalized,
364                generate_ambiguous_day_first_candidates(&classification),
365            ),
366            Order::Month => try_compatible_formats(
367                normalized,
368                generate_ambiguous_month_first_candidates(&classification),
369            ),
370        } {
371            return Ok(dt);
372        }
373        // std::eprintln!("NOW trying numeric timestamp");
374        if classification.is_pure_numeric
375            && mode != Mode::UnixTimestamp
376            && let Some(dt) = parse_pure_numeric_unix_timestamp(
377                normalized,
378                classification.num_non_decimal_digits as usize,
379            )
380        {
381            return Ok(dt);
382        }
383        Err(an_err!(DtErrKind::InvalidInput, "{}", s))
384    }
385
386    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
387    /// but returns attoseconds since the library epoch: 2000-01-01 12:00:00 UTC
388    /// (on the UTC scale).
389    ///
390    /// Returns `Some(attos)` on success (negative for pre-2000 dates) or `None`
391    /// on any parse error.
392    #[inline]
393    pub fn str_to_attos(s: &str, opts: &ParseCfg) -> Option<i128> {
394        Dt::from_str_parse(s, opts).ok().map(|tp| tp.to_attos())
395    }
396
397    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
398    /// but returns milliseconds since the library epoch: 2000-01-01 12:00:00 UTC
399    /// (on the UTC scale).
400    ///
401    /// Returns `Some(millis)` on success (negative for pre-2000 dates) or `None`
402    /// on any parse error.
403    #[inline]
404    pub fn str_to_ms(s: &str, opts: &ParseCfg) -> Option<i128> {
405        Dt::from_str_parse(s, opts).ok().map(|tp| tp.to_ms())
406    }
407
408    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
409    /// but returns nanoseconds since the library epoch: 2000-01-01 12:00:00 UTC
410    /// (on the UTC scale).
411    ///
412    /// Returns `Some(nanos)` on success (negative for pre-2000 dates) or `None`
413    /// on any parse error.
414    #[inline]
415    pub fn str_to_ns(s: &str, opts: &ParseCfg) -> Option<i128> {
416        Dt::from_str_parse(s, opts).ok().map(|tp| tp.to_ns())
417    }
418
419    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
420    /// but returns milliseconds since the UNIX epoch: (1970-01-01 00:00:00 UTC).
421    ///
422    /// Returns `Some(millis)` on success (negative for pre-2000 dates) or `None`
423    /// on any parse error.
424    #[inline]
425    pub fn str_to_unix_ms(s: &str, opts: &ParseCfg) -> Option<i128> {
426        Dt::from_str_parse(s, opts)
427            .ok()
428            .map(|tp| tp.to_scale_and_diff(Dt::UNIX_EPOCH, false).to_ms())
429    }
430
431    /// Same parsing logic as [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse),
432    /// but returns nanoseconds since the UNIX epoch: (1970-01-01 00:00:00 UTC).
433    ///
434    /// Returns `Some(nanos)` on success (negative for pre-2000 dates) or `None`
435    /// on any parse error.
436    #[inline]
437    pub fn str_to_unix_ns(s: &str, opts: &ParseCfg) -> Option<i128> {
438        Dt::from_str_parse(s, opts)
439            .ok()
440            .map(|tp| tp.to_scale_and_diff(Dt::UNIX_EPOCH, false).to_ns())
441    }
442}
443
444/// Core zero-allocation helper (updated to match the new `&str` signature).
445///
446/// The `fmt` we get from the iterator is still `'static`, but it coerces automatically
447/// to `&str`, so everything continues to work.
448#[inline]
449pub(crate) fn try_compatible_formats<I>(s: &str, formats: I) -> Option<Dt>
450where
451    I: IntoIterator<Item = String>,
452{
453    // let mut dt = None;
454
455    // for fmt in formats.into_iter() {
456    //     eprintln!("TRYING FMT: {}", fmt);
457    //     dt = match Dt::from_str(s, &fmt, true, true, false) {
458    //         Ok(parsed) => Some(parsed),
459    //         Err(e) => {
460    //             eprintln!("  FAILED with: {:?}", e);
461    //             continue;
462    //         }
463    //     };
464    //     if dt.is_some() {
465    //         break;
466    //     }
467    //     // === DEBUG ===
468    //     // eprintln!("Tried format: {:?}", fmt);
469    // }
470
471    // dt
472    formats
473        .into_iter()
474        .find_map(|fmt| Dt::from_str(s, &fmt, true, true, false).ok())
475}
476
477#[inline]
478pub(crate) fn try_unambiguous(s: &str, classification: &DateClassification) -> Option<Dt> {
479    if matches!(classification.bytes_len, 6..=8)
480        && let Some(dt) = parse_yyyy_mm(s.as_bytes())
481    {
482        return Some(dt);
483    }
484    try_compatible_formats(s, generate_unambiguous_candidates(classification))
485}