deep-time 0.1.0-beta.7

High-precision, no-std, no-alloc date-time library, leap-seconds, time scales, relativistic time, and a powerful date & duration parser
Documentation
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use crate::Lang;
use crate::{Dt, DtErr, DtErrKind, LiteStr, STRFTIME_SIZE, YmdHms, an_err, tz::offset_for_utc};

#[cfg(feature = "alloc")]
use {crate::ATTOS_PER_SEC, alloc::string::String};

#[cfg(feature = "alloc")]
impl Dt {
    /// Converts this `Dt` to an ISO 8601 duration string
    /// (e.g. `"PT1H23M45.6789S"`, `"-PT0.5S"`, `"PT0.000000000000000001S"`, or `"PT0S"`).
    ///
    /// - This method is only available when the **`alloc`** feature is enabled.
    /// - It returns `alloc::string::String` (no_std + alloc compatible).
    /// - Performs no time scale conversions prior to output.
    pub fn to_iso_duration(&self) -> String {
        if self.is_zero() {
            return String::from("PT0S");
        }

        let total = self.to_attos();
        let negative = total < 0;
        let mut attos = total.unsigned_abs();

        let mut s = String::with_capacity(48);
        if negative {
            s.push('-');
        }
        s.push_str("PT");

        const A_PER_S: u128 = ATTOS_PER_SEC as u128;
        const A_PER_M: u128 = A_PER_S * 60;
        const A_PER_H: u128 = A_PER_M * 60;

        let hours = attos / A_PER_H;
        attos %= A_PER_H;
        let minutes = attos / A_PER_M;
        attos %= A_PER_M;
        let seconds = attos / A_PER_S;
        let frac_attos = attos % A_PER_S;

        if hours > 0 {
            s.push_str(&alloc::format!("{}", hours));
            s.push('H');
        }
        if minutes > 0 {
            s.push_str(&alloc::format!("{}", minutes));
            s.push('M');
        }

        if seconds > 0 || frac_attos > 0 {
            s.push_str(&alloc::format!("{}", seconds));

            if frac_attos != 0 {
                let frac_str = alloc::format!("{frac_attos:018}");
                let trimmed = frac_str.trim_end_matches('0');
                s.push('.');
                s.push_str(trimmed);
            }

            s.push('S');
        }

        s
    }

    /// Formats this [`Dt`] into a String. Requires the `"alloc"` feature.
    ///
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Lang, Scale};
    ///
    /// let x = Dt::from_ymd(2000, 1, 1, 0, 0, 0, 0, Scale::UTC);
    /// let s = x.to_str("%F", Lang::En).unwrap();
    ///
    /// println!("{}", s);
    /// ```
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers
    /// or if the internal formatting buffer overflows (extremely unlikely
    /// with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str_in_offset`](../struct.Dt.html#method.to_str_in_offset)
    /// - [`Dt::to_str_in_tz`](../struct.Dt.html#method.to_str_in_tz)
    #[inline(always)]
    pub fn to_str(&self, fmt: &str, lang: Lang) -> Result<String, DtErr> {
        self.to_str_in_offset(fmt, 0, lang)
    }

    /// Formats this [`Dt`] into a String, applying a fixed offset. Requires the
    /// `"alloc"` feature.
    ///
    /// - A copy of the [`Dt`] is adjusted by the given `secs` offset **before**
    ///   formatting, and the offset is stored so that `%z` / `%:z` format directives
    ///   will reflect it.
    /// - No IANA timezone name or abbreviation is set.
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Lang, Scale};
    ///
    /// let x = Dt::from_ymd(2000, 1, 1, 0, 0, 0, 0, Scale::UTC);
    ///
    /// // offset of minus one hour
    /// let s = x.to_str_in_offset("%F", -3600, Lang::En).unwrap();
    ///
    /// println!("{}", s);
    /// ```
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers
    /// or if the internal formatting buffer overflows (extremely unlikely
    /// with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str`](../struct.Dt.html#method.to_str)
    /// - [`Dt::to_str_in_tz`](../struct.Dt.html#method.to_str_in_tz)
    #[inline(always)]
    pub fn to_str_in_offset(&self, fmt: &str, secs: i32, lang: Lang) -> Result<String, DtErr> {
        self.ymd_with_offset(secs)
            .to_str(fmt, Some(secs), None, None, lang)
    }

    /// Formats this [`Dt`] into a string, time adjusted to the given IANA timezone. Requires
    /// the `"alloc"` feature.
    ///
    /// Use this method when you want full IANA-aware formatting (`%Q`, `%Z`, `%z`, etc.).
    ///
    /// - A copy of the [`Dt`] is adjusted by the offset at the [`Dt`]'s time for the given
    ///   IANA timezone. This is so that the formatter will have:
    ///     - Accurate wall time for the timezone.
    ///     - Correct numeric offset (for `%z` / `%:z`).
    ///     - Timezone abbreviation (for `%Z`). These **do not** round-trip (the parser
    ///       does not parse them).
    ///     - Full IANA timezone name (for `%Q` / `%:Q`).
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Examples
    ///
    /// You can offset an output that wasn't originally from a zoned input:
    ///
    /// ```rust
    /// # #[cfg(all(feature = "tz", feature = "parse"))]
    /// # {
    /// use deep_time::{Dt, Lang, Scale};
    ///
    /// let x: Dt = "2000-01-01 12:00:00".parse().unwrap();
    /// let s = x.to_str_in_tz("%A, %B %d, %Y %H:%M:%S %Q", "America/New_York", Lang::En).unwrap();
    /// assert_eq!(s, "Saturday, January 01, 2000 07:00:00 America/New_York");
    /// # }
    /// ```
    ///
    /// You can also return to a zoned output from a zoned input:
    ///
    /// ```rust
    /// # #[cfg(all(feature = "tz", feature = "parse"))]
    /// # {
    /// use deep_time::{Dt, Lang, Scale};
    ///
    /// let x: Dt = "Saturday, January 01, 2000 07:00:00 America/New_York".parse().unwrap();
    /// let s = x.to_str_in_tz("%A, %B %d, %Y %H:%M:%S %Q", "America/New_York", Lang::En).unwrap();
    /// assert_eq!(s, "Saturday, January 01, 2000 07:00:00 America/New_York");
    /// # }
    /// ```
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers
    /// or if the internal formatting buffer overflows (extremely unlikely
    /// with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str`](../struct.Dt.html#method.to_str)
    /// - [`Dt::to_str_in_offset`](../struct.Dt.html#method.to_str_in_offset)
    #[inline(always)]
    pub fn to_str_in_tz(&self, fmt: &str, tz_name: &str, lang: Lang) -> Result<String, DtErr> {
        let (ymd, offset, abbrev) = self.ymd_with_tz(tz_name, true)?;
        ymd.to_str(
            fmt,
            Some(offset),
            Some(LiteStr::new(tz_name)),
            Some(abbrev),
            lang,
        )
    }

    /// **RFC 9557** / Temporal format with IANA timezone name in brackets.
    ///
    /// - Automatically trims trailing zeros in the fractional part.
    /// - Example: `"2020-06-15T14:30:00-04:00[America/New_York]"`
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    #[inline(always)]
    pub fn to_str_rfc9557(&self, tz_name: &str) -> Result<String, DtErr> {
        self.to_str_in_tz("%Y-%m-%dT%H:%M:%S%.~f%:z[%Q]", tz_name, Lang::En)
    }

    /// Returns this instant as an **RFC 3339** / ISO 8601 timestamp with a
    /// `Z` suffix.
    ///
    /// - Default = 9 digits (nanoseconds) but **automatically trims trailing zeros**.
    /// - If fractional part is zero → no decimal point at all (e.g. `...45Z`).
    /// - Example: `"2024-03-14T15:30:45.123Z"`
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    #[inline(always)]
    pub fn to_str_rfc3339(&self) -> Result<String, DtErr> {
        self.to_str_rfc3339_nf(9)
    }

    /// Same as [`Dt::to_str_rfc3339`](../struct.Dt.html#method.to_str_rfc3339) but
    /// with a configurable maximum number of fractional digits (0–18). Trailing zeros are
    /// always trimmed.
    ///
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    pub fn to_str_rfc3339_nf(&self, max_precision: usize) -> Result<String, DtErr> {
        let prec = max_precision.min(18);
        // Uses the formatter with the `~` "trim trailing zeros" flag.
        // The formatter already handles:
        //   - correct 4-digit years (with sign) for |yr| < 10000
        //   - full-width years otherwise
        //   - suppressing the decimal point entirely when the trimmed fraction is zero
        let fmt = alloc::format!("%Y-%m-%dT%H:%M:%S%.{}~fZ", prec);
        self.to_str_in_offset(&fmt, 0, Lang::En)
    }

    /// **ISO 8601 / RFC 3339** with **actual offset** (modern `+00:00` style).
    ///
    /// - Uses colon-separated offset (`%:z`) instead of forcing `Z`.
    /// - Still trims trailing zeros in the fractional part.
    /// - Example: `"2025-04-16T14:30:45.123+00:00"`
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    #[inline(always)]
    pub fn to_str_iso8601(&self) -> Result<String, DtErr> {
        self.to_str_in_offset("%Y-%m-%dT%H:%M:%S%.~f%:z", 0, Lang::En)
    }

    /// **Compact ISO 8601 basic format** (no separators).
    ///
    /// - Example: `"20250416T143045.123456789Z"`
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    #[inline(always)]
    pub fn to_str_iso8601_basic(&self) -> Result<String, DtErr> {
        self.to_str_in_offset("%Y%m%dT%H%M%S%.~fZ", 0, Lang::En)
    }

    /// **ISO 8601 week date**.
    ///
    /// - Example: `"2025-W16-3"` (year-week-day)
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    #[inline(always)]
    pub fn to_str_iso_week_date(&self) -> Result<String, DtErr> {
        self.to_str_in_offset("%G-W%V-%u", 0, Lang::En)
    }

    /// Just the **ISO date** part (no time).
    ///
    /// - Example: `"2025-04-16"`
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    #[inline(always)]
    pub fn to_str_iso_date(&self) -> Result<String, DtErr> {
        self.to_str_in_offset("%Y-%m-%d", 0, Lang::En)
    }

    /// Just the **time** part with fractional seconds (trimmed).
    ///
    /// - Example: `"14:30:45.123456789"`
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    #[inline(always)]
    pub fn to_str_iso_time(&self) -> Result<String, DtErr> {
        self.to_str_in_offset("%H:%M:%S%.~f", 0, Lang::En)
    }

    /// **HTTP-date** format (RFC 7231 / RFC 1123) — **always in GMT**.
    ///
    /// - Example: `"Wed, 16 Apr 2025 14:30:45 GMT"`
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    #[inline(always)]
    pub fn to_str_http(&self, lang: Lang) -> Result<String, DtErr> {
        self.to_str_in_offset("%a, %d %b %Y %H:%M:%S GMT", 0, lang)
    }

    /// **RFC 2822** date format (used in email `Date` headers).
    ///
    /// - Example: `"Wed, 16 Apr 2025 14:30:45 +0000"`
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    #[inline(always)]
    pub fn to_str_rfc2822(&self, lang: Lang) -> Result<String, DtErr> {
        self.to_str_in_offset("%a, %d %b %Y %H:%M:%S %z", 0, lang)
    }

    /// Formats this [`Dt`] into a `String`, attaching an offset **as a label only**.
    ///
    /// - The actual datetime components are **not** shifted or adjusted.
    /// - The given `offset` is used **only** for `%z` / `%:z` format directives.
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers
    /// or if the internal formatting buffer overflows (extremely unlikely
    /// with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str_in_offset`](../struct.Dt.html#method.to_str_in_offset) —
    ///   shifts the datetime by the offset
    #[inline(always)]
    pub fn to_str_with_offset_label(
        &self,
        fmt: &str,
        offset: i32,
        lang: Lang,
    ) -> Result<String, DtErr> {
        self.to_ymd().to_str(fmt, Some(offset), None, None, lang)
    }

    /// Formats this [`Dt`] into a `String`, attaching a timezone **as a label only**.
    ///
    /// - The actual datetime components are **not** shifted or adjusted.
    /// - The timezone is used to provide correct values for `%z`, `%:z`, `%Z`, `%Q`, and `%:Q`.
    /// - The timezone abbreviation is automatically looked up from tzdata.
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers,
    /// if the timezone name is invalid, or if the internal formatting buffer
    /// overflows (extremely unlikely with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str_in_tz`](../struct.Dt.html#method.to_str_in_tz) —
    ///   shifts the datetime into the given timezone
    #[inline(always)]
    pub fn to_str_with_tz_label(
        &self,
        fmt: &str,
        tz_name: &str,
        lang: Lang,
    ) -> Result<String, DtErr> {
        let (ymd, offset, abbrev) = self.ymd_with_tz(tz_name, false)?;
        ymd.to_str(
            fmt,
            Some(offset),
            Some(LiteStr::new(tz_name)),
            Some(abbrev),
            lang,
        )
    }
}

impl Dt {
    /// Formats this [`Dt`] into a fixed-size binary string.
    ///
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Lang, Scale};
    ///
    /// let x = Dt::from_ymd(2000, 1, 1, 0, 0, 0, 0, Scale::UTC);
    /// let b = x.to_str_lite("%F", Lang::En).unwrap();
    /// let s = b.as_str().unwrap();
    ///
    /// println!("{}", s);
    /// ```
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers
    /// or if the internal formatting buffer overflows (extremely unlikely
    /// with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str_lite_in_offset`](../struct.Dt.html#method.to_str_lite_in_offset)
    /// - [`Dt::to_str_lite_in_tz`](../struct.Dt.html#method.to_str_lite_in_tz)
    #[inline(always)]
    pub fn to_str_lite(&self, fmt: &str, lang: Lang) -> Result<LiteStr<STRFTIME_SIZE>, DtErr> {
        self.to_ymd().to_str_lite(fmt, None, None, None, lang)
    }

    /// Formats this [`Dt`] into a fixed-size binary string, applying a fixed UTC offset.
    ///
    /// - A copy of the [`Dt`] is adjusted by the given `secs` offset **before**
    ///   formatting, and the offset is stored so that `%z` / `%:z` format directives
    ///   will reflect it.
    /// - No IANA timezone name or abbreviation is set.
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Lang, Scale};
    ///
    /// let x = Dt::from_ymd(2000, 1, 1, 0, 0, 0, 0, Scale::UTC);
    ///
    /// // offset of minus one hour
    /// let b = x.to_str_lite_in_offset("%F", -3600, Lang::En).unwrap();
    /// let s = b.as_str().unwrap();
    ///
    /// println!("{}", s);
    /// ```
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers
    /// or if the internal formatting buffer overflows (extremely unlikely
    /// with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str_lite`](../struct.Dt.html#method.to_str_lite)
    /// - [`Dt::to_str_lite_in_tz`](../struct.Dt.html#method.to_str_lite_in_tz)
    #[inline(always)]
    pub fn to_str_lite_in_offset(
        &self,
        fmt: &str,
        secs: i32,
        lang: Lang,
    ) -> Result<LiteStr<STRFTIME_SIZE>, DtErr> {
        self.ymd_with_offset(secs)
            .to_str_lite(fmt, Some(secs), None, None, lang)
    }

    /// Formats this [`Dt`] into a fixed-size binary string, time adjusted to the given
    /// IANA timezone.
    ///
    /// Use this method when you want full IANA-aware formatting (`%Q`, `%Z`, `%z`, etc.).
    ///
    /// - A copy of the [`Dt`] is adjusted by the offset at the [`Dt`]'s time for the given
    ///   IANA timezone. This is so that the formatter will have:
    ///     - Accurate wall time for the timezone.
    ///     - Correct numeric offset (for `%z` / `%:z`).
    ///     - Timezone abbreviation (for `%Z`). These **do not** round-trip.
    ///     - Full IANA timezone name (for `%Q` / `%:Q`).
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "tz")]
    /// # {
    /// use deep_time::{Dt, Lang, Scale};
    ///
    /// let x = Dt::from_ymd(2000, 1, 1, 0, 0, 0, 0, Scale::UTC);
    ///
    /// let b = x.to_str_lite_in_tz("%F", "America/New_York", Lang::En).unwrap();
    /// let s = b.as_str().unwrap();
    ///
    /// println!("{}", s);
    /// # }
    /// ```
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers
    /// or if the internal formatting buffer overflows (extremely unlikely
    /// with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str_lite`](../struct.Dt.html#method.to_str_lite)
    /// - [`Dt::to_str_lite_in_offset`](../struct.Dt.html#method.to_str_lite_in_offset)
    #[inline(always)]
    pub fn to_str_lite_in_tz(
        &self,
        fmt: &str,
        tz_name: &str,
        lang: Lang,
    ) -> Result<LiteStr<STRFTIME_SIZE>, DtErr> {
        let (ymd, offset, abbrev) = self.ymd_with_tz(tz_name, true)?;
        ymd.to_str_lite(
            fmt,
            Some(offset),
            Some(LiteStr::new(tz_name)),
            Some(abbrev),
            lang,
        )
    }

    /// Formats this [`Dt`] into a `LiteStr`, attaching an offset **as a label only**.
    ///
    /// - The actual datetime components are **not** shifted or adjusted.
    /// - The given `offset` is used **only** for `%z` / `%:z` format directives.
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers
    /// or if the internal formatting buffer overflows (extremely unlikely
    /// with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str_lite_in_offset`](../struct.Dt.html#method.to_str_lite_in_offset) —
    ///   shifts the datetime by the offset
    #[inline(always)]
    pub fn to_str_lite_with_offset_label(
        &self,
        fmt: &str,
        offset: i32,
        lang: Lang,
    ) -> Result<LiteStr<STRFTIME_SIZE>, DtErr> {
        self.to_ymd()
            .to_str_lite(fmt, Some(offset), None, None, lang)
    }

    /// Formats this [`Dt`] into a `LiteStr`, attaching a timezone **as a label only**.
    ///
    /// - The actual datetime components are **not** shifted or adjusted.
    /// - The timezone is used to provide correct values for `%z`, `%:z`, `%Z`, `%Q`, and `%:Q`.
    /// - The timezone abbreviation is automatically looked up from tzdata.
    /// - Converts from this [`Dt`]'s current time `scale` to its `target`
    ///   time scale before producing the result.
    ///
    /// ## Errors
    ///
    /// Returns [`DtErr`] if the format string contains invalid specifiers,
    /// if the timezone name is invalid, or if the internal formatting buffer
    /// overflows (extremely unlikely with [`STRFTIME_SIZE`]).
    ///
    /// ## See also
    ///
    /// - [`Dt::to_str_lite_in_tz`](../struct.Dt.html#method.to_str_lite_in_tz) —
    ///   shifts the datetime into the given timezone
    #[inline(always)]
    pub fn to_str_lite_with_tz_label(
        &self,
        fmt: &str,
        tz_name: &str,
        lang: Lang,
    ) -> Result<LiteStr<STRFTIME_SIZE>, DtErr> {
        let (ymd, offset, abbrev) = self.ymd_with_tz(tz_name, false)?;
        ymd.to_str_lite(
            fmt,
            Some(offset),
            Some(LiteStr::new(tz_name)),
            Some(abbrev),
            lang,
        )
    }

    /// Returns `(is_negative, hours, minutes)`.
    #[inline]
    pub(crate) const fn sec_as_hhmm(seconds: i32) -> (bool, u8, u8) {
        let total = seconds.saturating_abs();
        let hours = (total / 3600) as u8;
        let minutes = ((total % 3600) / 60) as u8;
        (seconds < 0, hours, minutes)
    }

    pub(crate) fn ymd_with_offset(&self, secs: i32) -> YmdHms {
        if secs != 0 {
            self.add_sec(secs as i128).to_ymd()
        } else {
            self.to_ymd()
        }
    }

    pub(crate) fn ymd_with_tz(
        &self,
        tz_name: &str,
        apply_offset: bool,
    ) -> Result<(YmdHms, i32, LiteStr<49>), DtErr> {
        // Look up offset + abbrev at that exact UTC instant
        let unix_sec = self.to_unix().to_sec64();
        let (offset_secs, abbrev) = match offset_for_utc(tz_name, unix_sec) {
            Some(info) => (info.offset, info.abbrev),
            None => return Err(an_err!(DtErrKind::InvalidTimezoneOffset, "{}", tz_name)),
        };
        let ab = LiteStr::new(abbrev);
        let ymd = if offset_secs != 0 && apply_offset {
            self.add_sec(offset_secs as i128).to_ymd()
        } else {
            self.to_ymd()
        };

        Ok((ymd, offset_secs, ab))
    }
}