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