time 0.3.51

Date and time library. Fully interoperable with the standard library. Mostly compatible with #![no_std].
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
use num_conv::prelude::*;

use crate::format_description::Period;
use crate::formatting::{
    Day, IsoWeekNumber, MondayBasedWeek, OptionDay, OptionIsoWeekNumber, OptionYear, Ordinal,
    SundayBasedWeek, Year,
};
use crate::time::{Hours, Minutes, Nanoseconds, Seconds};
use crate::utc_offset::{Hours as OffsetHours, Minutes as OffsetMinutes, Seconds as OffsetSeconds};
use crate::{
    Date, Month, OffsetDateTime, PrimitiveDateTime, Time, Timestamp, UtcDateTime, UtcOffset,
    Weekday,
};

/// State used by date-providing types to cache computed values.
///
/// This is used to avoid redundant computations when multiple date components are almost certainly
/// going to be requested within the same formatting invocation.
#[derive(Debug, Default)]
pub(crate) struct DateState {
    day: OptionDay,
    month: Option<Month>,
    iso_week: OptionIsoWeekNumber,
    iso_year: OptionYear,
}

/// State used by `Timestamp` to cache computed date and time values.
///
/// `Date` and `Time` are cached separately, with the `Date`'s state being stored to allow for
/// reusing existing methods.
#[derive(Debug, Default)]
pub(crate) struct TimestampState {
    date: Option<Date>,
    time: Option<Time>,
    date_state: <Date as ComponentProvider>::State,
}

macro_rules! unimplemented_methods {
    ($(
        $(#[$meta:meta])*
        ($component:literal) $name:ident => $ret:ty;
    )*) => {
        $(
            $(#[$meta])*
            #[track_caller]
            #[expect(unused_variables, reason = "better for auto-generation of method stubs")]
            fn $name(&self, state: &mut Self::State) -> $ret {
                unimplemented!(concat!("type does not supply ", $component, " components"))
            }
        )*
    };
}

macro_rules! delegate_providers {
    (
        $target:ident {
            $($method:ident -> $return:ty)*
        }
    ) => {$(
        #[inline]
        fn $method(&self, state: &mut Self::State) -> $return {
            ComponentProvider::$method(&self.$target(), state)
        }
    )*};
    (
        $target:ident ($state:expr) {
            $($method:ident -> $return:ty)*
        }
    ) => {$(
        #[inline]
        fn $method(&self, _: &mut Self::State) -> $return {
            ComponentProvider::$method(&self.$target(), $state)
        }
    )*};
}

/// A type with the ability to provide date, time, offset, and/or timestamp components on demand.
///
/// Note that while all methods have a default body, implementations are expected to override the
/// body for all components that they provide. The default implementation exists solely for
/// convenience, avoiding the need to specify unprovided components.
pub(crate) trait ComponentProvider {
    /// The state type used by the provider, allowing for caching of computed values.
    type State: Default;

    /// Whether the type can provide date components, indicating that date-related methods can be
    /// called.
    const SUPPLIES_DATE: bool = false;
    /// Whether the type can provide time components, indicating that time-related methods can be
    /// called.
    const SUPPLIES_TIME: bool = false;
    /// Whether the type can provide offset components, indicating that offset-related methods can
    /// be called.
    const SUPPLIES_OFFSET: bool = false;
    /// Whether the type can provide timestamp components, indicating that timestamp-related methods
    /// can be called.
    const SUPPLIES_TIMESTAMP: bool = false;

    unimplemented_methods! {
        /// Obtain the day of the month.
        ("date") day => Day;
        /// Obtain the month of the year.
        ("date") month => Month;
        /// Obtain the ordinal day of the year.
        ("date") ordinal => Ordinal;
        /// Obtain the day of the week.
        ("date") weekday => Weekday;
        /// Obtain the ISO week number.
        ("date") iso_week_number => IsoWeekNumber;
        /// Obtain the Monday-based week number.
        ("date") monday_based_week => MondayBasedWeek;
        /// Obtain the Sunday-based week number.
        ("date") sunday_based_week => SundayBasedWeek;
        /// Obtain the calendar year.
        ("date") calendar_year => Year;
        /// Obtain the ISO week-based year.
        ("date") iso_year => Year;
        /// Obtain the hour within the day.
        ("time") hour => Hours;
        /// Obtain the minute within the hour.
        ("time") minute => Minutes;
        /// Obtain the period of the day (AM/PM).
        ("time") period => Period;
        /// Obtain the second within the minute.
        ("time") second => Seconds;
        /// Obtain the nanosecond within the second.
        ("time") nanosecond => Nanoseconds;
        /// Obtain whether the offset is negative.
        ("offset") offset_is_negative => bool;
        /// Obtain whether the offset is UTC.
        ("offset") offset_is_utc => bool;
        /// Obtain the hour component of the UTC offset.
        ("offset") offset_hour => OffsetHours;
        /// Obtain the minute component of the UTC offset.
        ("offset") offset_minute => OffsetMinutes;
        /// Obtain the second component of the UTC offset.
        ("offset") offset_second => OffsetSeconds;
        /// Obtain the Unix timestamp in seconds.
        ("timestamp") unix_timestamp_seconds => i64;
        /// Obtain the Unix timestamp in milliseconds.
        ("timestamp") unix_timestamp_milliseconds => i64;
        /// Obtain the Unix timestamp in microseconds.
        ("timestamp") unix_timestamp_microseconds => i128;
        /// Obtain the Unix timestamp in nanoseconds.
        ("timestamp") unix_timestamp_nanoseconds => i128;
    }
}

impl ComponentProvider for Time {
    type State = ();

    const SUPPLIES_TIME: bool = true;

    #[inline]
    fn hour(&self, _: &mut Self::State) -> Hours {
        self.as_hms_nano_ranged().0
    }

    #[inline]
    fn minute(&self, _: &mut Self::State) -> Minutes {
        self.as_hms_nano_ranged().1
    }

    #[inline]
    fn period(&self, _: &mut Self::State) -> Period {
        if (*self).hour() < 12 {
            Period::Am
        } else {
            Period::Pm
        }
    }

    #[inline]
    fn second(&self, _: &mut Self::State) -> Seconds {
        self.as_hms_nano_ranged().2
    }

    #[inline]
    fn nanosecond(&self, _: &mut Self::State) -> Nanoseconds {
        self.as_hms_nano_ranged().3
    }
}

impl ComponentProvider for Date {
    type State = DateState;

    const SUPPLIES_DATE: bool = true;

    #[inline]
    fn day(&self, state: &mut Self::State) -> Day {
        if let Some(day) = state.day.get() {
            return day;
        }

        let (_, month, day) = (*self).to_calendar_date();
        // Safety: `day` is guaranteed to be in range.
        let day = unsafe { Day::new_unchecked(day) };
        state.month = Some(month);
        state.day = OptionDay::Some(day);
        day
    }

    #[inline]
    fn month(&self, state: &mut Self::State) -> Month {
        *state.month.get_or_insert_with(|| (*self).month())
    }

    #[inline]
    fn ordinal(&self, _: &mut Self::State) -> Ordinal {
        // Safety: `self.ordinal()` is guaranteed to be in range.
        unsafe { Ordinal::new_unchecked((*self).ordinal()) }
    }

    #[inline]
    fn weekday(&self, _: &mut Self::State) -> Weekday {
        (*self).weekday()
    }

    #[inline]
    fn iso_week_number(&self, state: &mut Self::State) -> IsoWeekNumber {
        if let Some(week) = state.iso_week.get() {
            return week;
        }

        let (iso_year, iso_week) = (*self).iso_year_week();
        // Safety: `iso_week` is guaranteed to be non-zero.
        let iso_week = unsafe { IsoWeekNumber::new_unchecked(iso_week) };
        // Safety: `iso_year` is guaranteed to be in range.
        state.iso_year = OptionYear::Some(unsafe { Year::new_unchecked(iso_year) });
        state.iso_week = OptionIsoWeekNumber::Some(iso_week);
        iso_week
    }

    #[inline]
    fn monday_based_week(&self, _: &mut Self::State) -> MondayBasedWeek {
        // Safety: `self.monday_based_week()` is guaranteed to be in range.
        unsafe { MondayBasedWeek::new_unchecked((*self).monday_based_week()) }
    }

    #[inline]
    fn sunday_based_week(&self, _: &mut Self::State) -> SundayBasedWeek {
        // Safety: `self.sunday_based_week()` is guaranteed to be in range.
        unsafe { SundayBasedWeek::new_unchecked((*self).sunday_based_week()) }
    }

    #[inline]
    fn calendar_year(&self, _: &mut Self::State) -> Year {
        // Safety: `self.year()` is guaranteed to be in range.
        unsafe { Year::new_unchecked((*self).year()) }
    }

    #[inline]
    fn iso_year(&self, state: &mut Self::State) -> Year {
        if let Some(iso_year) = state.iso_year.get() {
            return iso_year;
        }

        let (iso_year, iso_week) = (*self).iso_year_week();
        // Safety: `iso_year_week` returns a valid ISO year.
        let iso_year = unsafe { Year::new_unchecked(iso_year) };
        state.iso_year = OptionYear::Some(iso_year);
        // Safety: `iso_week` is guaranteed to be non-zero.
        state.iso_week =
            OptionIsoWeekNumber::Some(unsafe { IsoWeekNumber::new_unchecked(iso_week) });
        iso_year
    }
}

impl ComponentProvider for PrimitiveDateTime {
    type State = DateState;

    const SUPPLIES_DATE: bool = true;
    const SUPPLIES_TIME: bool = true;

    delegate_providers!(date {
        day -> Day
        month -> Month
        ordinal -> Ordinal
        weekday -> Weekday
        iso_week_number -> IsoWeekNumber
        monday_based_week -> MondayBasedWeek
        sunday_based_week -> SundayBasedWeek
        calendar_year -> Year
        iso_year -> Year
    });
    delegate_providers!(time (&mut ()) {
        hour -> Hours
        minute -> Minutes
        period -> Period
        second -> Seconds
        nanosecond -> Nanoseconds
    });
}

impl ComponentProvider for UtcOffset {
    type State = ();

    const SUPPLIES_OFFSET: bool = true;

    #[inline]
    fn offset_is_negative(&self, _: &mut Self::State) -> bool {
        (*self).is_negative()
    }

    #[inline]
    fn offset_is_utc(&self, _state: &mut Self::State) -> bool {
        (*self).is_utc()
    }

    #[inline]
    fn offset_hour(&self, _: &mut Self::State) -> OffsetHours {
        (*self).as_hms_ranged().0
    }

    #[inline]
    fn offset_minute(&self, _: &mut Self::State) -> OffsetMinutes {
        (*self).as_hms_ranged().1
    }

    #[inline]
    fn offset_second(&self, _: &mut Self::State) -> OffsetSeconds {
        (*self).as_hms_ranged().2
    }
}

impl ComponentProvider for UtcDateTime {
    type State = DateState;

    const SUPPLIES_DATE: bool = true;
    const SUPPLIES_TIME: bool = true;
    const SUPPLIES_OFFSET: bool = true;
    const SUPPLIES_TIMESTAMP: bool = true;

    delegate_providers!(date {
        day -> Day
        month -> Month
        ordinal -> Ordinal
        weekday -> Weekday
        iso_week_number -> IsoWeekNumber
        monday_based_week -> MondayBasedWeek
        sunday_based_week -> SundayBasedWeek
        calendar_year -> Year
        iso_year -> Year
    });
    delegate_providers!(time (&mut ()) {
        hour -> Hours
        minute -> Minutes
        period -> Period
        second -> Seconds
        nanosecond -> Nanoseconds
    });

    #[inline]
    fn offset_is_negative(&self, _: &mut Self::State) -> bool {
        false
    }

    #[inline]
    fn offset_is_utc(&self, _state: &mut Self::State) -> bool {
        true
    }

    #[inline]
    fn offset_hour(&self, _: &mut Self::State) -> OffsetHours {
        OffsetHours::new_static::<0>()
    }

    #[inline]
    fn offset_minute(&self, _: &mut Self::State) -> OffsetMinutes {
        OffsetMinutes::new_static::<0>()
    }

    #[inline]
    fn offset_second(&self, _: &mut Self::State) -> OffsetSeconds {
        OffsetSeconds::new_static::<0>()
    }

    #[inline]
    fn unix_timestamp_seconds(&self, _: &mut Self::State) -> i64 {
        (*self).unix_timestamp()
    }

    #[inline]
    fn unix_timestamp_milliseconds(&self, state: &mut Self::State) -> i64 {
        (ComponentProvider::unix_timestamp_nanoseconds(self, state) / 1_000_000).truncate()
    }

    #[inline]
    fn unix_timestamp_microseconds(&self, state: &mut Self::State) -> i128 {
        ComponentProvider::unix_timestamp_nanoseconds(self, state) / 1_000
    }

    #[inline]
    fn unix_timestamp_nanoseconds(&self, _: &mut Self::State) -> i128 {
        (*self).unix_timestamp_nanos()
    }
}

impl ComponentProvider for OffsetDateTime {
    type State = DateState;

    const SUPPLIES_DATE: bool = true;
    const SUPPLIES_TIME: bool = true;
    const SUPPLIES_OFFSET: bool = true;
    const SUPPLIES_TIMESTAMP: bool = true;

    delegate_providers!(date {
        day -> Day
        month -> Month
        ordinal -> Ordinal
        weekday -> Weekday
        iso_week_number -> IsoWeekNumber
        monday_based_week -> MondayBasedWeek
        sunday_based_week -> SundayBasedWeek
        calendar_year -> Year
        iso_year -> Year
    });
    delegate_providers!(time (&mut ()) {
        hour -> Hours
        minute -> Minutes
        period -> Period
        second -> Seconds
        nanosecond -> Nanoseconds
    });
    delegate_providers!(offset (&mut ()) {
        offset_is_negative -> bool
        offset_is_utc -> bool
        offset_hour -> OffsetHours
        offset_minute -> OffsetMinutes
        offset_second -> OffsetSeconds
    });

    #[inline]
    fn unix_timestamp_seconds(&self, _: &mut Self::State) -> i64 {
        (*self).unix_timestamp()
    }

    #[inline]
    fn unix_timestamp_milliseconds(&self, _: &mut Self::State) -> i64 {
        ((*self).unix_timestamp_nanos() / 1_000_000) as i64
    }

    #[inline]
    fn unix_timestamp_microseconds(&self, _: &mut Self::State) -> i128 {
        (*self).unix_timestamp_nanos() / 1_000
    }

    #[inline]
    fn unix_timestamp_nanoseconds(&self, _: &mut Self::State) -> i128 {
        (*self).unix_timestamp_nanos()
    }
}

impl ComponentProvider for Timestamp {
    type State = TimestampState;

    const SUPPLIES_DATE: bool = true;
    const SUPPLIES_TIME: bool = true;
    const SUPPLIES_OFFSET: bool = true;
    const SUPPLIES_TIMESTAMP: bool = true;

    #[inline]
    fn day(&self, state: &mut Self::State) -> Day {
        let date = state.date.get_or_insert_with(|| self.date());
        ComponentProvider::day(date, &mut state.date_state)
    }

    #[inline]
    fn month(&self, state: &mut Self::State) -> Month {
        let date = state.date.get_or_insert_with(|| self.date());
        ComponentProvider::month(date, &mut state.date_state)
    }

    #[inline]
    fn ordinal(&self, state: &mut Self::State) -> Ordinal {
        let date = state.date.get_or_insert_with(|| self.date());
        ComponentProvider::ordinal(date, &mut state.date_state)
    }

    #[inline]
    fn weekday(&self, state: &mut Self::State) -> Weekday {
        let date = state.date.get_or_insert_with(|| self.date());
        ComponentProvider::weekday(date, &mut state.date_state)
    }

    #[inline]
    fn iso_week_number(&self, state: &mut Self::State) -> IsoWeekNumber {
        let date = state.date.get_or_insert_with(|| self.date());
        ComponentProvider::iso_week_number(date, &mut state.date_state)
    }

    #[inline]
    fn monday_based_week(&self, state: &mut Self::State) -> MondayBasedWeek {
        let date = state.date.get_or_insert_with(|| self.date());
        ComponentProvider::monday_based_week(date, &mut state.date_state)
    }

    #[inline]
    fn sunday_based_week(&self, state: &mut Self::State) -> SundayBasedWeek {
        let date = state.date.get_or_insert_with(|| self.date());
        ComponentProvider::sunday_based_week(date, &mut state.date_state)
    }

    #[inline]
    fn calendar_year(&self, state: &mut Self::State) -> Year {
        let date = state.date.get_or_insert_with(|| self.date());
        ComponentProvider::calendar_year(date, &mut state.date_state)
    }

    #[inline]
    fn iso_year(&self, state: &mut Self::State) -> Year {
        let date = state.date.get_or_insert_with(|| self.date());
        ComponentProvider::iso_year(date, &mut state.date_state)
    }

    #[inline]
    fn hour(&self, state: &mut Self::State) -> Hours {
        let time = state.time.get_or_insert_with(|| self.time());
        ComponentProvider::hour(time, &mut ())
    }

    #[inline]
    fn minute(&self, state: &mut Self::State) -> Minutes {
        let time = state.time.get_or_insert_with(|| self.time());
        ComponentProvider::minute(time, &mut ())
    }

    #[inline]
    fn period(&self, state: &mut Self::State) -> Period {
        let time = state.time.get_or_insert_with(|| self.time());
        ComponentProvider::period(time, &mut ())
    }

    #[inline]
    fn second(&self, state: &mut Self::State) -> Seconds {
        let time = state.time.get_or_insert_with(|| self.time());
        ComponentProvider::second(time, &mut ())
    }

    #[inline]
    fn nanosecond(&self, _: &mut Self::State) -> Nanoseconds {
        // No need to cache time here, as nanosecond is stored separately in `Timestamp` and can be
        // directly accessed.
        self.as_parts_ranged().1
    }

    #[inline]
    fn offset_is_negative(&self, _: &mut Self::State) -> bool {
        false
    }

    #[inline]
    fn offset_is_utc(&self, _: &mut Self::State) -> bool {
        true
    }

    #[inline]
    fn offset_hour(&self, _: &mut Self::State) -> OffsetHours {
        OffsetHours::new_static::<0>()
    }

    #[inline]
    fn offset_minute(&self, _: &mut Self::State) -> OffsetMinutes {
        OffsetMinutes::new_static::<0>()
    }

    #[inline]
    fn offset_second(&self, _: &mut Self::State) -> OffsetSeconds {
        OffsetSeconds::new_static::<0>()
    }

    #[inline]
    fn unix_timestamp_seconds(&self, _: &mut Self::State) -> i64 {
        self.as_seconds()
    }

    #[inline]
    fn unix_timestamp_milliseconds(&self, _: &mut Self::State) -> i64 {
        self.as_milliseconds()
    }

    #[inline]
    fn unix_timestamp_microseconds(&self, _: &mut Self::State) -> i128 {
        self.as_microseconds()
    }

    #[inline]
    fn unix_timestamp_nanoseconds(&self, _: &mut Self::State) -> i128 {
        self.as_nanoseconds()
    }
}