deep_time/dt/from_str.rs
1use crate::{
2 ATTOS_PER_SEC_I128, Dt, DtErr, DtErrKind, Parts, SEC_PER_DAY, SEC_PER_MONTH, SEC_PER_WEEK,
3 SEC_PER_YEAR, Scale, StrPTimeFmt, an_err,
4};
5use core::str::FromStr;
6
7#[cfg(feature = "parse")]
8use crate::ParseCfg;
9
10#[cfg(feature = "parse")]
11impl FromStr for Dt {
12 type Err = DtErr;
13
14 #[inline]
15 fn from_str(s: &str) -> Result<Self, DtErr> {
16 Dt::from_str_parse(s, &ParseCfg::DEFAULT)
17 }
18}
19
20#[cfg(not(feature = "parse"))]
21impl FromStr for Dt {
22 type Err = DtErr;
23
24 #[inline]
25 fn from_str(s: &str) -> Result<Self, DtErr> {
26 Self::from_str_iso(s)
27 }
28}
29
30struct ParsedComponent {
31 unit: u8,
32 signed_int: i64,
33 frac_digits: usize,
34 frac_num: i64,
35}
36
37impl Dt {
38 /// Parses a date/time string.
39 ///
40 /// - When the `parse` feature is enabled: uses the smart auto-parser.
41 /// - When the `parse` feature is disabled: falls back to CCSDS format.
42 ///
43 /// ## Examples
44 ///
45 /// ```rust
46 /// use deep_time::{Dt, Scale};
47 ///
48 /// // uses impl FromStr but Dt::parse provides the same functionality
49 /// let x: Dt = "2000-01-01 12:00:00".parse().unwrap();
50 ///
51 /// let ymd = x.to_ymd();
52 /// assert_eq!(ymd.yr(), 2000);
53 /// assert_eq!(ymd.mo(), 1);
54 /// assert_eq!(ymd.day(), 1);
55 /// assert_eq!(ymd.hr(), 12);
56 /// assert_eq!(ymd.min(), 0);
57 /// assert_eq!(ymd.sec(), 0);
58 /// assert_eq!(ymd.attos(), 0);
59 /// ```
60 ///
61 /// ## See also
62 ///
63 /// - [`Dt::from_str_parse`](../struct.Dt.html#method.from_str_parse)
64 /// - [`Dt::from_str_iso`](../struct.Dt.html#method.from_str_iso)
65 #[inline(always)]
66 pub fn parse(s: &str) -> Result<Self, DtErr> {
67 #[cfg(feature = "parse")]
68 {
69 Self::from_str_parse(s, &ParseCfg::DEFAULT)
70 }
71 #[cfg(not(feature = "parse"))]
72 {
73 Self::from_str_iso(s)
74 }
75 }
76
77 /// Parser equivalent to `strptime` with a provided format string.
78 ///
79 /// The returned [`Dt`] will be on the `TAI` time scale, converted from whatever
80 /// optional time scale (`%L`) was provided in the input. If no time scale was
81 /// provided then it's converted from `UTC` -> `TAI`.
82 ///
83 /// The result is that the [`Dt`]'s `scale` field will be `TAI` and its `target`
84 /// field will be whatever time scale it was converted from (`UTC` if no time
85 /// scale was in the input).
86 ///
87 /// ## Parameters
88 ///
89 /// - `fmt`: The format string containing `%` directives.
90 /// - `input`: The string to parse.
91 /// - `inp_can_end_before_fmt`: If `true`, the input may end before the format
92 /// string is fully consumed (extra format specifiers are ignored).
93 /// - `fmt_can_end_before_inp`: If `true`, the format may end before the input
94 /// is fully consumed (trailing characters in the input are allowed).
95 /// - `allow_partial_date`: If `true`, a missing month/day will be defaulted
96 /// to `1` instead of returning a [`DtErrKind::Incomplete`](../error/enum.DtErrKind.html#variant.Incomplete) error.
97 ///
98 /// ## Supported Directives
99 ///
100 /// The format string supports literal characters and the following `%` directives.
101 /// Literal non-whitespace characters must match the input exactly.
102 /// Whitespace in the format matches (and consumes) any leading ASCII whitespace in the input.
103 ///
104 /// Many directives accept **format extensions** right after `%`:
105 /// - **Flags**: `-` (no pad), `_` (space pad), `0` (zero pad), `^`/`#` (treated as default)
106 /// - **Width**: 1–3 digits (affects numeric field width / padding expectations)
107 /// - **Colons** (only for `%z`): `:`, `::`, `:::` to control offset format
108 ///
109 /// ### Year / Century / Unbounded
110 /// - `%Y` — Four-digit year (e.g. `2024`). Supports sign, flags, and width.
111 /// - `%y` — Two-digit year (`00`–`99`; `00`–`68` → 2000+, `69`–`99` → 1900s).
112 /// - `%C` — Century (`00`–`99`).
113 /// - `%G` — Four-digit ISO week-based year.
114 /// - `%g` — Two-digit ISO week-based year (same century rule as `%y`).
115 /// - `%*` — **Unbounded year** (arbitrary length, supports negative years). *Library extension.*
116 ///
117 /// ### Month
118 /// - `%m` — Month number `01`–`12`.
119 /// - `%B` — Full English month name (e.g. `January`).
120 /// - `%b`, `%h` — Abbreviated English month name (3 letters, e.g. `Jan`).
121 ///
122 /// ### Day
123 /// - `%d`, `%e` — Day of month `01`–`31` (`%e` allows space padding).
124 /// - `%j` — Day of year `001`–`366`.
125 ///
126 /// ### Time of day
127 /// - `%H`, `%k` — Hour `00`–`23` (24-hour clock; `%k` allows space padding).
128 /// - `%I`, `%l` — Hour `01`–`12` (12-hour clock).
129 /// - `%M` — Minute `00`–`59`.
130 /// - `%S` — Second `00`–`60` (leap second allowed).
131 /// - `%f`, `%N` — Fractional seconds (up to 18 digits = attoseconds).
132 /// Width controls precision (`%3f` = ms, `%6N` = µs, `%9f` = ns, etc.).
133 /// Both accept an optional leading `.` in the input.
134 /// - `%.f`, `%.N`, `%.3f`, `%.6N`, ... — Same fractional parsing, but the
135 /// dot before the fraction is **optional** in the input (consumes literal `.` if present).
136 /// - `%P`, `%p` — `AM`/`PM` indicator (case-insensitive).
137 ///
138 /// ### Weekday / Week number
139 /// - `%A` — Full English weekday name (e.g. `Monday`).
140 /// - `%a` — Abbreviated English weekday name (3 letters, e.g. `Mon`).
141 /// - `%u` — Weekday number Monday=`1` … Sunday=`7`.
142 /// - `%w` — Weekday number Sunday=`0` … Saturday=`6`.
143 /// - `%U` — Week number (Sunday-first week), `00`–`53`.
144 /// - `%W` — Week number (Monday-first week), `00`–`53`.
145 /// - `%V` — ISO 8601 week number `01`–`53`.
146 ///
147 /// ### Timezone, Offset & Scale
148 /// - `%z` — Timezone offset. Colon count selects format:
149 /// - `%z` → `±HH[MM[SS]]` (minutes/seconds optional)
150 /// - `%:z` → `±HH:MM` (minutes required)
151 /// - `%::z` → `±HH:MM:SS` (seconds optional)
152 /// - `%:::z` → `±HH:MM:SS` (more flexible)
153 /// - `%Q` — IANA timezone name (e.g. `America/New_York`) **or** numeric offset
154 /// (if input starts with `+`/`-`). *Library extension.*
155 /// - `%L` — Time scale abbreviation (e.g. `TAI`, `UTC`, `GPS`). See [`Scale`].
156 /// *Library extension.*
157 ///
158 /// ### Shortcuts (compound directives)
159 /// - `%F` — Equivalent to `%Y-%m-%d` (ISO date).
160 /// - `%D` — Equivalent to `%m/%d/%y` (US date).
161 /// - `%T` — Equivalent to `%H:%M:%S`.
162 /// - `%R` — Equivalent to `%H:%M`.
163 ///
164 /// ### Other
165 /// - `%%` — Literal `%` character.
166 /// - `%s` — Unix timestamp (seconds since 1970-01-01 00:00 UTC, can be negative).
167 /// This directive greedily consumes any fractional seconds.
168 /// - `%J` — Seconds since 2000-01-01 12:00 TAI (2000-01-01 noon epoch), can be
169 /// negative.
170 /// This directive greedily consumes any fractional seconds.
171 /// - `%n`, `%t` — Any whitespace (consumes it from input).
172 ///
173 /// ### Unsupported / Unknown
174 /// - `%c`, `%r`, `%x`, `%X`, `%Z` → [`DtErrKind::UnsupportedItem`]
175 /// - Any other unknown directive character → [`DtErrKind::UnknownItem`]
176 ///
177 /// ## Errors
178 ///
179 /// Returns a [`DtErr`] if either the strptime-style parser or the subsequent
180 /// conversion from [`Parts`] to [`Dt`] fails.
181 ///
182 /// ### Format string errors
183 ///
184 /// - [`DtErrKind::TruncatedDirective`] — A `%` appeared at the end of the format
185 /// string, or after flags/width/colons with no directive character following it.
186 /// - [`DtErrKind::UnexpectedEnd`] — A `%` was followed only by extensions with no
187 /// directive character.
188 /// - [`DtErrKind::InvalidFractional`] — A `%.` fractional directive was followed by
189 /// an invalid character (not `f` or `N`).
190 /// - [`DtErrKind::ExpectedFractional`] — A `%.` fractional directive was started
191 /// but no directive character followed the dot.
192 /// - [`DtErrKind::UnsupportedItem`] — The format contains `%c`, `%r`, `%x`, `%X`,
193 /// or `%Z`.
194 /// - [`DtErrKind::UnknownItem`] — The format contains an unrecognized `%` directive.
195 ///
196 /// ### Input parsing errors
197 ///
198 /// - [`DtErrKind::UnexpectedEnd`] — The input ended before a required value could
199 /// be parsed.
200 /// - `Expected*` variants:
201 /// - [`DtErrKind::ExpectedYear`], [`DtErrKind::ExpectedCentury`],
202 /// [`DtErrKind::ExpectedMonth`], [`DtErrKind::ExpectedDay`],
203 /// [`DtErrKind::ExpectedDayOfYear`], [`DtErrKind::ExpectedHour`],
204 /// [`DtErrKind::ExpectedMinute`], [`DtErrKind::ExpectedSecond`],
205 /// [`DtErrKind::ExpectedFractional`], [`DtErrKind::ExpectedTimestamp`],
206 /// [`DtErrKind::ExpectedWeekNumber`], [`DtErrKind::ExpectedMonWeekday`],
207 /// [`DtErrKind::ExpectedSunWeekday`], [`DtErrKind::ExpectedMonWeek`],
208 /// [`DtErrKind::ExpectedSunWeek`]
209 /// - Out-of-range errors:
210 /// - [`DtErrKind::MonthOutOfRange`], [`DtErrKind::DayOutOfRange`],
211 /// [`DtErrKind::DayOfYearOutOfRange`], [`DtErrKind::HourOutOfRange`],
212 /// [`DtErrKind::MinuteOutOfRange`], [`DtErrKind::SecondOutOfRange`],
213 /// [`DtErrKind::IsoWeekOutOfRange`], [`DtErrKind::MonWeekdayOutOfRange`],
214 /// [`DtErrKind::SunWeekdayOutOfRange`]
215 /// - [`DtErrKind::MismatchedLiteral`] — A literal character in the format string
216 /// did not match the input.
217 /// - Name errors: [`DtErrKind::InvalidMonthName`], [`DtErrKind::InvalidWeekdayName`],
218 /// [`DtErrKind::InvalidMeridiem`].
219 ///
220 /// ### Timezone and Offset errors
221 ///
222 /// - [`DtErrKind::OffsetMissingSign`] — A timezone offset (`%z` / `%Q`) did not
223 /// start with `+` or `-`.
224 /// - [`DtErrKind::InvalidOffsetHour`] — Invalid hour value in a timezone offset.
225 /// - [`DtErrKind::InvalidOffsetMinute`] — Invalid minute value in a timezone offset.
226 /// - [`DtErrKind::InvalidOffsetSecond`] — Invalid second value in a timezone offset.
227 /// - [`DtErrKind::InvalidOffsetColons`] — Incorrect number of colons or missing
228 /// required colon in a timezone offset.
229 /// - [`DtErrKind::InvalidOffset`] — General failure while parsing a numeric
230 /// timezone offset.
231 /// - [`DtErrKind::InvalidTimeZone`] — Invalid or unparseable IANA timezone name
232 /// (used by the `%Q` directive).
233 ///
234 /// ### Post-processing / validation errors
235 ///
236 /// - [`DtErrKind::TrailingCharacters`] — The input contained trailing characters
237 /// after parsing and `fmt_can_end_before_inp` was `false`.
238 /// - [`DtErrKind::Incomplete`] — Required date components (month or day) were
239 /// missing and `allow_partial_date` was `false`.
240 ///
241 /// ### Conversion to [`Dt`] errors
242 ///
243 /// These errors can occur *after* successful parsing, inside [`Parts::to_dt`]:
244 ///
245 /// - [`DtErrKind::InvalidDate`] or [`DtErrKind::InvalidInput`] — Unable to
246 /// construct a valid date from the parsed components.
247 /// - Out-of-range or conflicting field errors (e.g. [`DtErrKind::DayOfYearOutOfRange`],
248 /// [`DtErrKind::IsoWeekOutOfRange`], [`DtErrKind::WeekOutOfRange`], etc.).
249 /// - [`DtErrKind::InvalidItem`] — ISO week 53 requested for a year that does not
250 /// contain 53 ISO weeks.
251 /// - Feature-dependent errors (when `jiff-tz` is involved):
252 /// - [`DtErrKind::InvalidTimeZone`], [`DtErrKind::InvalidNumber`],
253 /// [`DtErrKind::InvalidBytes`].
254 ///
255 /// The error kind is available via [`DtErr::kind()`].
256 #[inline(always)]
257 pub fn from_str(
258 s: &str,
259 fmt: &str,
260 inp_can_end_before_fmt: bool,
261 fmt_can_end_before_inp: bool,
262 allow_partial_date: bool,
263 ) -> Result<Dt, DtErr> {
264 Parts::from_str(
265 fmt,
266 s,
267 inp_can_end_before_fmt,
268 fmt_can_end_before_inp,
269 allow_partial_date,
270 )?
271 .to_dt()
272 }
273
274 /// Parses and validates a `strptime`-style format string into a reusable [`StrPTimeFmt`].
275 ///
276 /// The format is checked once for syntax errors and unsupported directives,
277 /// then stored in a compact fixed-size buffer. The resulting `StrPTimeFmt` is
278 /// `Copy`, cheap to clone, and can be used repeatedly with [`StrPTimeFmt::to_dt`]
279 /// and [`StrPTimeFmt::to_str`] without re-validating.
280 ///
281 /// Only ASCII formats up to 256 bytes are accepted.
282 ///
283 /// ## Parameters
284 ///
285 /// - `strptime_fmt`: The format string using `%` directives (e.g. `"%Y-%m-%d %H:%M:%S"`,
286 /// `"%F %T"`, `"%Y-%m-%dT%H:%M:%S%.3fZ"`).
287 ///
288 /// ## Errors
289 ///
290 /// Returns [`DtErr`] if the format is:
291 /// - Longer than 256 bytes
292 /// - Not valid ASCII
293 /// - Contains unknown, unsupported, or malformed directives
294 #[inline(always)]
295 pub fn parse_fmt(strptime_fmt: &str) -> Result<StrPTimeFmt, DtErr> {
296 StrPTimeFmt::new(strptime_fmt)
297 }
298
299 /// Generalized no alloc parser.
300 ///
301 /// - Only supports ASCII characters.
302 /// - This function is considerably faster than all other string parsing methods if
303 /// your date-time string is in one of the supported formats.
304 /// - Timezones beyond UTC aliases require the `jiff-tz` feature, which requires `std`.
305 ///
306 /// ## Returns
307 ///
308 /// - If there is NOT a trailing time scale in the input and the format of the input
309 /// is a typical datetime iso e.g. `2000-01-01T17:00:00` then the time scale is
310 /// assumed to be `UTC` and the [`Dt`] goes through a `UTC` -> `TAI` conversion
311 /// (adding leap seconds).
312 /// - If there is NOT a trailing time scale in the input and the format of the input
313 /// is a seconds count, jd, or mjd then the time scale is assumed to be `TAI` and
314 /// no conversion happens.
315 /// - If there IS a trailing time scale in the input then the input goes through
316 /// a time scale conversion (regardless of input format) of the provided time
317 /// scale -> `TAI`. If the trailing time scale is `TAI` then no conversion occurs.
318 ///
319 /// A [`Dt`] of the `TAI` time scale is returned.
320 ///
321 /// ## Supported formats
322 ///
323 /// An **optional** library time scale right on the end of the input, e.g. `TAI` is
324 /// supported for all of the below formats.
325 ///
326 /// ### ISO
327 ///
328 /// #### Format examples:
329 ///
330 /// - **`+2000-01-01T17:00:00 -0500 [America/New_York] TAI`**.
331 /// - **`2024 Apr 18, 14:30:25 [America/New_York]`**. Abbreviated or full month
332 /// - **`2024-109 14:30:25 [America/New_York]`**. Day of year
333 ///
334 /// #### Notes:
335 ///
336 /// - If a time is included then some kind of date-time separator e.g. `T` or space is
337 /// required.
338 /// - Supports both calendar (`%Y-%m-%d`) and day-of-year (`%Y-%j`) formats.
339 /// - Treats years digits literally as shown, for example `99-01-01` would be
340 /// the year 99 AD not 1999.
341 /// - Supported **optional** components:
342 /// - Time components after a date e.g. `T12:00:00`.
343 /// - Offset after time components or directly after the date e.g. `+0200` or
344 /// `2023-01-01+05:00`.
345 /// - Timezone name, **requires square brackets** and **requires `jiff-tz`**
346 /// feature, after time or offset e.g. `T12:00:00 [America/New_York]`.
347 ///
348 /// ### Seconds since J2000 Noon
349 ///
350 /// #### Format examples:
351 ///
352 /// - **`SEC 1234.567 TDB`**.
353 ///
354 /// #### Notes:
355 ///
356 /// - `sec` prefix is required but case-**in**sensitive.
357 /// - Fractional seconds are optional.
358 ///
359 /// ### JD
360 ///
361 /// #### Format examples:
362 ///
363 /// - **`JD 2451545.0 TAI`**.
364 ///
365 /// #### Notes:
366 ///
367 /// - `jd` prefix is required but case-**in**sensitive.
368 /// - Fractional days are optional.
369 ///
370 /// ### MJD
371 ///
372 /// #### Format examples:
373 ///
374 /// - **`MJD 51544.5 TT`**.
375 ///
376 /// #### Notes:
377 ///
378 /// - `mjd` prefix is required but case-**in**sensitive.
379 /// - Fractional days are optional.
380 ///
381 /// ## See also
382 ///
383 /// - [`Parts::from_str_iso`](../struct.Parts.html#method.from_str_iso)
384 #[inline(always)]
385 pub fn from_str_iso(s: &str) -> Result<Self, DtErr> {
386 Parts::from_str_iso(s)?.to_dt()
387 }
388
389 /// Parses a decimal seconds string (with optional fractional part) as seconds
390 /// since
391 /// [`Dt::ZERO`](../struct.Dt.html#associatedconstant.ZERO)
392 /// on the chosen time scale.
393 ///
394 /// The returned [`Dt`] is on the `TAI` time [`Scale`], having been converted
395 /// to `TAI` from whatever the **trailing** scale is, or if no scale is provided
396 /// then no conversion takes place.
397 ///
398 /// Leading non-numeric characters are skipped until a number start is found
399 /// (`+`, `-`, `.`, or digit).
400 ///
401 /// - Fractional seconds are limited to the first 18 digits (attosecond
402 /// precision); extra digits are truncated.
403 /// - Oversized integer parts saturate instead of failing.
404 /// - Inputs longer than [`STRTIME_SIZE`](../constants/constant.STRTIME_SIZE.html) are rejected.
405 /// - Returns `None` only for completely unparseable input (empty, sign/dot
406 /// only, no digits after skipping, etc.).
407 ///
408 /// ## Examples
409 ///
410 /// ```rust
411 /// use deep_time::{Dt, Scale};
412 ///
413 /// let d = Dt::from_str_sec_f("1700000000.123456789012345678", Some(Scale::TAI)).unwrap();
414 /// assert_eq!(d.to_sec64(), 1700000000);
415 ///
416 /// // Leading junk is skipped
417 /// let d = Dt::from_str_sec_f("ts= -0.00123 suffix", Some(Scale::TAI)).unwrap();
418 /// assert!(d.to_attos() < 0);
419 ///
420 /// // Pure negative fraction
421 /// let d = Dt::from_str_sec_f("-.5", Some(Scale::TT)).unwrap();
422 /// assert!(d.to_attos() < 0);
423 ///
424 /// // Scale parsed from trailing abbreviation when passing None
425 /// let d = Dt::from_str_sec_f("42.75 GPS", None).unwrap();
426 /// assert_eq!(d.target, Scale::GPS);
427 ///
428 /// // 1 attosecond
429 /// let d = Dt::from_str_sec_f("0.000000000000000001", Some(Scale::TAI)).unwrap();
430 /// assert_eq!(d.to_attos() % 1_000_000_000_000_000_000, 1);
431 /// ```
432 pub fn from_str_sec_f(s: &str, scale: Option<Scale>) -> Option<Dt> {
433 let parsed = Parts::parse_str_f(s.as_bytes(), scale)?;
434
435 let int_attos = (parsed.int_u as i128) * ATTOS_PER_SEC_I128;
436 let signed_attos = if parsed.negative {
437 -int_attos - (parsed.frac_attos as i128)
438 } else {
439 int_attos + (parsed.frac_attos as i128)
440 };
441
442 Some(Dt::from_attos(signed_attos, parsed.scale))
443 }
444
445 /// Parses a decimal Julian Date string (with optional fractional part).
446 ///
447 /// The returned [`Dt`] is on the `TAI` time [`Scale`], having been converted
448 /// to `TAI` from whatever the **trailing** scale is, or if no scale is provided
449 /// then no conversion takes place.
450 ///
451 /// Leading junk is skipped the same way as [`Dt::from_str_sec_f`].
452 /// Fractional day precision up to 18 digits.
453 ///
454 /// Returns `None` for unparseable input.
455 ///
456 /// JD 2451545.0 is the library epoch (2000-01-01 noon).
457 ///
458 /// ## Examples
459 ///
460 /// ```rust
461 /// use deep_time::{Dt, Scale};
462 ///
463 /// let d = Dt::from_str_jd_f("2451545.0", Some(Scale::TAI)).unwrap();
464 /// assert_eq!(d.to_jd(), (2_451_545, 0));
465 ///
466 /// let d = Dt::from_str_jd_f("2451545.25 TT", None).unwrap();
467 /// assert_eq!(d.target, Scale::TT);
468 ///
469 /// let d = Dt::from_str_jd_f("2451544.5", Some(Scale::TAI)).unwrap();
470 /// assert!(d.to_attos() < 0);
471 /// ```
472 pub fn from_str_jd_f(s: &str, scale: Option<Scale>) -> Option<Dt> {
473 Parts::from_str_jd_f(s, scale).and_then(|p| p.to_dt().ok())
474 }
475
476 /// Parses a decimal Modified Julian Date string (with optional fractional part).
477 ///
478 /// The returned [`Dt`] is on the `TAI` time [`Scale`], having been converted
479 /// to `TAI` from whatever the **trailing** scale is, or if no scale is provided
480 /// then no conversion takes place.
481 ///
482 /// Leading junk is skipped the same way as [`Dt::from_str_sec_f`].
483 /// Fractional day precision up to 18 digits.
484 ///
485 /// Returns `None` for unparseable input.
486 ///
487 /// MJD 51544.5 is the library epoch (2000-01-01 noon).
488 ///
489 /// ## Examples
490 ///
491 /// ```rust
492 /// use deep_time::{Dt, Scale};
493 ///
494 /// let d = Dt::from_str_mjd_f("51544.5", Some(Scale::TAI)).unwrap();
495 /// assert_eq!(d.to_jd(), (2_451_545, 0));
496 ///
497 /// let d = Dt::from_str_mjd_f("51544.25 TT", None).unwrap();
498 /// assert_eq!(d.target, Scale::TT);
499 ///
500 /// let d = Dt::from_str_mjd_f("51543.5", Some(Scale::TAI)).unwrap();
501 /// assert!(d.to_attos() < 0);
502 /// ```
503 pub fn from_str_mjd_f(s: &str, scale: Option<Scale>) -> Option<Dt> {
504 Parts::from_str_mjd_f(s, scale).and_then(|p| p.to_dt().ok())
505 }
506
507 /// Parses an ISO 8601 duration string into a [`Dt`] representing a pure time interval.
508 ///
509 /// Supports the full `PnYnMnDTnHnMnS` format (case-insensitive), including:
510 /// - Optional leading `+` or `-` sign
511 /// - `P` / `p` prefix (required)
512 /// - Optional `T` / `t` separator between date and time parts
513 /// - Weeks (`W` / `w`)
514 /// - Fractional seconds with up to 9 digits of precision (nanosecond resolution;
515 /// the parsed value is scaled to attosecond resolution in the resulting [`Dt`]).
516 ///
517 /// The returned [`Dt`] is a **duration** (signed interval) on the TAI scale.
518 /// It can be added to/subtracted from other `Dt` values, multiplied/divided,
519 /// rounded, etc.
520 ///
521 /// ## Not Reference-Time Aware
522 ///
523 /// This parser is **not reference-time aware**. Calendar units (`Y`, `M`) are
524 /// converted to a fixed number of seconds using standard average lengths
525 /// rather than being resolved against a specific date. This makes parsing
526 /// fast and allocation-free, but `P1M` always represents exactly the same
527 /// duration regardless of context.
528 ///
529 /// ## Parameters
530 ///
531 /// - `s`: The ISO 8601 duration string (e.g. `"P1Y2M3DT4H5M6.123456789012345678S"`,
532 /// `"-PT30M"`, `"P7W"`, `"+P1DT12H"`).
533 ///
534 /// ## Errors
535 ///
536 /// Returns a [`DtErr`] if parsing fails. The error kind is available via
537 /// [`DtErr::kind()`].
538 ///
539 /// ### Input / structure errors
540 ///
541 /// - [`DtErrKind::Empty`] — The input string is empty.
542 /// - [`DtErrKind::MustStartWith`] — Missing `P` / `p` prefix (after optional leading sign).
543 /// - [`DtErrKind::InvalidSyntax`] — Invalid syntax, e.g. `T` with no following time part,
544 /// or more than one `T`/`t` separator.
545 /// - [`DtErrKind::TrailingCharacters`] — Additional components appear after a fractional
546 /// seconds value (only the final `S` component may carry a fraction).
547 ///
548 /// ### Component parsing errors
549 ///
550 /// - [`DtErrKind::ExpectedValue`] — Expected a numeric value for a component but found none.
551 /// - [`DtErrKind::ExpectedFractional`] — A `.` or `,` was present for a fractional part
552 /// but no digits followed.
553 /// - [`DtErrKind::ExpectedUnit`] — A number was parsed but no unit designator
554 /// (`Y`/`M`/`W`/`D`/`H`/`S` etc.) followed it.
555 /// - [`DtErrKind::InvalidNumber`] — A numeric component could not be parsed as an `i64`
556 /// (typically too large).
557 /// - [`DtErrKind::InvalidBytes`] — Internal UTF-8 conversion failure while reading a number
558 /// (should not occur for valid ASCII input).
559 /// - [`DtErrKind::InvalidFractional`] — The fractional part digits could not be parsed as an integer.
560 /// - [`DtErrKind::FracOutOfRange`] — More than 9 digits were supplied for fractional seconds.
561 /// - [`DtErrKind::InvalidItem`] — A fractional part was supplied on a unit other than seconds.
562 ///
563 /// ### Unit and range errors
564 ///
565 /// - [`DtErrKind::UnknownItem`] — An unknown unit designator character was used.
566 /// - [`DtErrKind::YearOutOfRange`], [`DtErrKind::MonthOutOfRange`],
567 /// [`DtErrKind::WeekOutOfRange`], [`DtErrKind::DayOutOfRange`] — The component value
568 /// (after sign) overflows when multiplied by the corresponding fixed-length constant
569 /// (checked arithmetic).
570 pub fn from_iso_duration(s: &str) -> Result<Dt, DtErr> {
571 let len = s.len();
572 if len == 0 {
573 return Err(an_err!(DtErrKind::Empty));
574 }
575
576 let b = s.as_bytes();
577 let mut i = 0usize;
578
579 // Optional leading sign (+ or -)
580 let mut sign: i64 = 1;
581 if i < len && matches!(b[i], b'+' | b'-') {
582 if b[i] == b'-' {
583 sign = -1;
584 }
585 i += 1;
586 }
587
588 // Must start with P/p
589 if i >= len || !matches!(b[i], b'P' | b'p') {
590 return Err(an_err!(DtErrKind::MustStartWith));
591 }
592 i += 1;
593
594 // Find the (single) T/t separator
595 let t_pos = b[i..]
596 .iter()
597 .position(|&c| matches!(c, b'T' | b't'))
598 .map(|p| i + p);
599
600 let (date_part, time_part) = match t_pos {
601 Some(pos) => {
602 if pos == len - 1 {
603 return Err(an_err!(DtErrKind::InvalidSyntax));
604 }
605 if b[pos + 1..].iter().any(|&c| matches!(c, b'T' | b't')) {
606 return Err(an_err!(DtErrKind::InvalidSyntax));
607 }
608 (&b[i..pos], &b[pos + 1..])
609 }
610 None => (&b[i..], &[] as &[u8]),
611 };
612
613 let mut has_fraction = false;
614 let mut total_nanos: i128 = 0;
615
616 // Both date and time parts now use the same fixed-length logic
617 Self::parse_duration_part(date_part, &mut total_nanos, true, sign, &mut has_fraction)?;
618 Self::parse_duration_part(time_part, &mut total_nanos, false, sign, &mut has_fraction)?;
619
620 // Convert accumulated nanoseconds to attoseconds and build Dt
621 let total_attos = total_nanos * 1_000_000_000i128;
622 Ok(Dt::span(total_attos))
623 }
624
625 /// Parses a single component (number + optional fraction + unit) from the slice,
626 /// advancing the index `i`. Returns `None` when the slice is exhausted.
627 fn parse_next_component(
628 chars: &[u8],
629 i: &mut usize,
630 sign: i64,
631 has_fraction: &mut bool,
632 ) -> Result<Option<ParsedComponent>, DtErr> {
633 if *i >= chars.len() {
634 return Ok(None);
635 }
636
637 if *has_fraction {
638 return Err(an_err!(DtErrKind::TrailingCharacters));
639 }
640
641 // Parse integer part
642 let start = *i;
643 while *i < chars.len() && chars[*i].is_ascii_digit() {
644 *i += 1;
645 }
646 if start == *i {
647 return Err(an_err!(DtErrKind::ExpectedValue));
648 }
649
650 let int_str = core::str::from_utf8(&chars[start..*i])
651 .map_err(|e| an_err!(DtErrKind::InvalidBytes, "{}", e))?;
652 let int: i64 = int_str.parse().map_err(|e: core::num::ParseIntError| {
653 an_err!(DtErrKind::InvalidNumber, "{}: {}", int_str, e)
654 })?;
655
656 // Parse optional fraction
657 let mut frac_num: i64 = 0;
658 let mut frac_digits: usize = 0;
659 if *i < chars.len() && matches!(chars[*i], b'.' | b',') {
660 *i += 1;
661 let frac_start = *i;
662 while *i < chars.len() && chars[*i].is_ascii_digit() {
663 *i += 1;
664 }
665 frac_digits = *i - frac_start;
666 if frac_digits == 0 {
667 return Err(an_err!(DtErrKind::ExpectedFractional));
668 }
669 if frac_digits > 9 {
670 return Err(an_err!(DtErrKind::FracOutOfRange));
671 }
672
673 let frac_str = core::str::from_utf8(&chars[frac_start..*i])
674 .map_err(|e| an_err!(DtErrKind::InvalidBytes, "{}", e))?;
675 frac_num = frac_str.parse().map_err(|e: core::num::ParseIntError| {
676 an_err!(DtErrKind::InvalidFractional, "{}: {}", frac_str, e)
677 })?;
678 }
679
680 // Unit must follow
681 if *i >= chars.len() {
682 return Err(an_err!(DtErrKind::ExpectedUnit));
683 }
684 let unit = chars[*i];
685 *i += 1;
686
687 // Only seconds support a fractional part
688 if frac_digits > 0 {
689 if !matches!(unit, b'S' | b's') {
690 return Err(an_err!(DtErrKind::InvalidItem));
691 }
692 *has_fraction = true;
693 }
694
695 let signed_int = (int as i128 * sign as i128) as i64;
696
697 Ok(Some(ParsedComponent {
698 unit,
699 signed_int,
700 frac_digits,
701 frac_num,
702 }))
703 }
704
705 /// Helper that parses **one section** of an ISO duration (date or time part)
706 /// and accumulates nanoseconds into `total_nanos`.
707 ///
708 /// Years, months, weeks, and days are converted using the fixed-length
709 /// constants (the only sensible semantics for a pure `Dt`).
710 fn parse_duration_part(
711 chars: &[u8],
712 total_nanos: &mut i128,
713 is_date: bool,
714 sign: i64,
715 has_fraction: &mut bool,
716 ) -> Result<(), DtErr> {
717 let mut i = 0;
718 while let Some(comp) = Self::parse_next_component(chars, &mut i, sign, has_fraction)? {
719 let contrib_nanos = match (is_date, comp.unit) {
720 (true, b'Y' | b'y') => {
721 let total_secs = (comp.signed_int as i128)
722 .checked_mul(SEC_PER_YEAR)
723 .ok_or_else(|| an_err!(DtErrKind::YearOutOfRange))?;
724 total_secs * 1_000_000_000i128
725 }
726 (true, b'M' | b'm') => {
727 let total_secs = (comp.signed_int as i128)
728 .checked_mul(SEC_PER_MONTH)
729 .ok_or_else(|| an_err!(DtErrKind::MonthOutOfRange))?;
730 total_secs * 1_000_000_000i128
731 }
732 (true, b'W' | b'w') => {
733 let total_secs = (comp.signed_int as i128)
734 .checked_mul(SEC_PER_WEEK as i128)
735 .ok_or_else(|| an_err!(DtErrKind::WeekOutOfRange))?;
736 total_secs * 1_000_000_000i128
737 }
738 (true, b'D' | b'd') => {
739 let total_secs = (comp.signed_int as i128)
740 .checked_mul(SEC_PER_DAY)
741 .ok_or_else(|| an_err!(DtErrKind::DayOutOfRange))?;
742 total_secs * 1_000_000_000i128
743 }
744 (false, b'H' | b'h') => (comp.signed_int as i128) * 3_600_000_000_000i128,
745 (false, b'M' | b'm') => (comp.signed_int as i128) * 60_000_000_000i128,
746 (false, b'S' | b's') => {
747 let mut sec_nanos = (comp.signed_int as i128) * 1_000_000_000i128;
748 if comp.frac_digits > 0 {
749 let frac_ns = (comp.frac_num as i128 * sign as i128 * 1_000_000_000i128)
750 / 10i128.pow(comp.frac_digits as u32);
751 sec_nanos += frac_ns;
752 }
753 sec_nanos
754 }
755 _ => {
756 return Err(an_err!(DtErrKind::UnknownItem, "{}", comp.unit as char));
757 }
758 };
759
760 *total_nanos = total_nanos.saturating_add(contrib_nanos);
761 }
762 Ok(())
763 }
764
765 /// Parses a media-style duration string.
766 ///
767 /// Accepts formats like:
768 /// - `"0:45"`, `"9:41"`
769 /// - `"1:23:45"`
770 /// - `"1:07:54:30"`
771 /// - `"-1:23:45"`
772 ///
773 /// ## Errors
774 ///
775 /// Returns a [`DtErr`] if the input cannot be parsed as a valid media-style
776 /// duration. The error kind is available via [`DtErr::kind`].
777 ///
778 /// This function uses saturating arithmetic, so it never returns range or
779 /// overflow errors.
780 ///
781 /// ### Input / structure errors
782 ///
783 /// - [`DtErrKind::Empty`] — The string is empty or contains only ASCII whitespace.
784 /// - [`DtErrKind::InvalidInput`] — A single minus sign with nothing after it.
785 /// - [`DtErrKind::InvalidSyntax`] — The input does not contain exactly 2, 3, or 4
786 /// colon-separated numeric components.
787 /// - [`DtErrKind::TrailingCharacters`] — Non-whitespace characters remain after
788 /// the final numeric component.
789 ///
790 /// ### Parsing errors
791 ///
792 /// - [`DtErrKind::ExpectedValue`] — A component was expected to begin with a digit
793 /// (either at the start of the string or immediately after a `:`) but did not.
794 ///
795 /// ## See also
796 ///
797 /// - [`Dt::to_str_media_duration`](../struct.Dt.html#method.to_str_media_duration)
798 /// - [`Dt::to_str_lite_media_duration`](../struct.Dt.html#method.to_str_lite_media_duration)
799 pub fn from_str_media_duration(input: &str) -> Result<Dt, DtErr> {
800 let bytes = input.as_bytes();
801 let len = bytes.len();
802 let mut pos: usize = 0;
803
804 // Skip leading whitespace
805 while pos < len && bytes[pos].is_ascii_whitespace() {
806 pos += 1;
807 }
808
809 if pos == len {
810 return Err(an_err!(DtErrKind::Empty));
811 }
812
813 // Optional single leading minus
814 let negative = if bytes[pos] == b'-' {
815 pos += 1;
816 if pos == len {
817 return Err(an_err!(DtErrKind::InvalidInput));
818 }
819 true
820 } else {
821 false
822 };
823
824 // Parse up to 4 numeric components separated by ':'
825 let mut components: [i128; 4] = [0; 4];
826 let mut count: usize = 0;
827
828 loop {
829 if count >= 4 {
830 break;
831 }
832
833 // Parse one number
834 if pos >= len || !bytes[pos].is_ascii_digit() {
835 return Err(an_err!(DtErrKind::ExpectedValue));
836 }
837
838 let mut value: i128 = 0;
839 while pos < len && bytes[pos].is_ascii_digit() {
840 value = value
841 .saturating_mul(10)
842 .saturating_add((bytes[pos] - b'0') as i128);
843 pos += 1;
844 }
845
846 components[count] = value;
847 count += 1;
848
849 // Check for more components
850 if pos >= len || bytes[pos] != b':' {
851 break;
852 }
853
854 pos += 1; // consume ':'
855
856 // Reject trailing ':' with no number after it
857 if pos >= len || !bytes[pos].is_ascii_digit() {
858 return Err(an_err!(DtErrKind::ExpectedValue));
859 }
860 }
861
862 if !(2..=4).contains(&count) {
863 return Err(an_err!(DtErrKind::InvalidSyntax));
864 }
865
866 // Skip trailing whitespace
867 while pos < len && bytes[pos].is_ascii_whitespace() {
868 pos += 1;
869 }
870
871 if pos != len {
872 return Err(an_err!(DtErrKind::TrailingCharacters));
873 }
874
875 // Convert to total seconds
876 let total_secs: i128 = match count {
877 2 => components[0] * 60 + components[1], // M:SS
878 3 => components[0] * 3600 + components[1] * 60 + components[2], // H:MM:SS
879 4 => components[0] * 86400 + components[1] * 3600 + components[2] * 60 + components[3], // D:H:MM:SS
880 _ => unreachable!(),
881 };
882
883 let total_secs = if negative { -total_secs } else { total_secs };
884 let attos = total_secs.saturating_mul(ATTOS_PER_SEC_I128);
885
886 Ok(Dt::span(attos))
887 }
888}