jiff/civil/
time.rs

1use core::time::Duration as UnsignedDuration;
2
3use crate::{
4    civil::{Date, DateTime},
5    duration::{Duration, SDuration},
6    error::{err, Error, ErrorContext},
7    fmt::{
8        self,
9        temporal::{self, DEFAULT_DATETIME_PARSER},
10    },
11    shared::util::itime::{ITime, ITimeNanosecond, ITimeSecond},
12    util::{
13        rangeint::{self, Composite, RFrom, RInto, TryRFrom},
14        round::increment,
15        t::{
16            self, CivilDayNanosecond, CivilDaySecond, Hour, Microsecond,
17            Millisecond, Minute, Nanosecond, Second, SubsecNanosecond, C,
18        },
19    },
20    RoundMode, SignedDuration, Span, SpanRound, Unit, Zoned,
21};
22
23/// A representation of civil "wall clock" time.
24///
25/// Conceptually, a `Time` value corresponds to the typical hours and minutes
26/// that you might see on a clock. This type also contains the second and
27/// fractional subsecond (to nanosecond precision) associated with a time.
28///
29/// # Civil time
30///
31/// A `Time` value behaves as if it corresponds precisely to a single
32/// nanosecond within a day, where all days have `86,400` seconds. That is,
33/// any given `Time` value corresponds to a nanosecond in the inclusive range
34/// `[0, 86399999999999]`, where `0` corresponds to `00:00:00.000000000`
35/// ([`Time::MIN`]) and `86399999999999` corresponds to `23:59:59.999999999`
36/// ([`Time::MAX`]). Moreover, in civil time, all hours have the same number of
37/// minutes, all minutes have the same number of seconds and all seconds have
38/// the same number of nanoseconds.
39///
40/// # Parsing and printing
41///
42/// The `Time` type provides convenient trait implementations of
43/// [`std::str::FromStr`] and [`std::fmt::Display`]:
44///
45/// ```
46/// use jiff::civil::Time;
47///
48/// let t: Time = "15:22:45".parse()?;
49/// assert_eq!(t.to_string(), "15:22:45");
50///
51/// # Ok::<(), Box<dyn std::error::Error>>(())
52/// ```
53///
54/// A civil `Time` can also be parsed from something that _contains_ a
55/// time, but with perhaps other data (such as an offset or time zone):
56///
57/// ```
58/// use jiff::civil::Time;
59///
60/// let t: Time = "2024-06-19T15:22:45-04[America/New_York]".parse()?;
61/// assert_eq!(t.to_string(), "15:22:45");
62///
63/// # Ok::<(), Box<dyn std::error::Error>>(())
64/// ```
65///
66/// For more information on the specific format supported, see the
67/// [`fmt::temporal`](crate::fmt::temporal) module documentation.
68///
69/// # Default value
70///
71/// For convenience, this type implements the `Default` trait. Its default
72/// value is midnight. i.e., `00:00:00.000000000`.
73///
74/// # Leap seconds
75///
76/// Jiff does not support leap seconds. Jiff behaves as if they don't exist.
77/// The only exception is that if one parses a time with a second component
78/// of `60`, then it is automatically constrained to `59`:
79///
80/// ```
81/// use jiff::civil::{Time, time};
82///
83/// let t: Time = "23:59:60".parse()?;
84/// assert_eq!(t, time(23, 59, 59, 0));
85///
86/// # Ok::<(), Box<dyn std::error::Error>>(())
87/// ```
88///
89/// # Comparisons
90///
91/// The `Time` type provides both `Eq` and `Ord` trait implementations to
92/// facilitate easy comparisons. When a time `t1` occurs before a time `t2`,
93/// then `t1 < t2`. For example:
94///
95/// ```
96/// use jiff::civil::time;
97///
98/// let t1 = time(7, 30, 1, 0);
99/// let t2 = time(8, 10, 0, 0);
100/// assert!(t1 < t2);
101/// ```
102///
103/// As mentioned above, `Time` values are not associated with timezones, and
104/// thus transitions such as DST are not taken into account when comparing
105/// `Time` values.
106///
107/// # Arithmetic
108///
109/// This type provides routines for adding and subtracting spans of time, as
110/// well as computing the span of time between two `Time` values.
111///
112/// For adding or subtracting spans of time, one can use any of the following
113/// routines:
114///
115/// * [`Time::wrapping_add`] or [`Time::wrapping_sub`] for wrapping arithmetic.
116/// * [`Time::checked_add`] or [`Time::checked_sub`] for checked arithmetic.
117/// * [`Time::saturating_add`] or [`Time::saturating_sub`] for saturating
118/// arithmetic.
119///
120/// Additionally, wrapping arithmetic is available via the `Add` and `Sub`
121/// trait implementations:
122///
123/// ```
124/// use jiff::{civil::time, ToSpan};
125///
126/// let t = time(20, 10, 1, 0);
127/// let span = 1.hours().minutes(49).seconds(59);
128/// assert_eq!(t + span, time(22, 0, 0, 0));
129///
130/// // Overflow will result in wrap-around unless using checked
131/// // arithmetic explicitly.
132/// let t = time(23, 59, 59, 999_999_999);
133/// assert_eq!(time(0, 0, 0, 0), t + 1.nanoseconds());
134/// ```
135///
136/// Wrapping arithmetic is used by default because it corresponds to how clocks
137/// showing the time of day behave in practice.
138///
139/// One can compute the span of time between two times using either
140/// [`Time::until`] or [`Time::since`]. It's also possible to subtract two
141/// `Time` values directly via a `Sub` trait implementation:
142///
143/// ```
144/// use jiff::{civil::time, ToSpan};
145///
146/// let time1 = time(22, 0, 0, 0);
147/// let time2 = time(20, 10, 1, 0);
148/// assert_eq!(
149///     time1 - time2,
150///     1.hours().minutes(49).seconds(59).fieldwise(),
151/// );
152/// ```
153///
154/// The `until` and `since` APIs are polymorphic and allow re-balancing and
155/// rounding the span returned. For example, the default largest unit is hours
156/// (as exemplified above), but we can ask for smaller units:
157///
158/// ```
159/// use jiff::{civil::time, ToSpan, Unit};
160///
161/// let time1 = time(23, 30, 0, 0);
162/// let time2 = time(7, 0, 0, 0);
163/// assert_eq!(
164///     time1.since((Unit::Minute, time2))?,
165///     990.minutes().fieldwise(),
166/// );
167///
168/// # Ok::<(), Box<dyn std::error::Error>>(())
169/// ```
170///
171/// Or even round the span returned:
172///
173/// ```
174/// use jiff::{civil::{TimeDifference, time}, RoundMode, ToSpan, Unit};
175///
176/// let time1 = time(23, 30, 0, 0);
177/// let time2 = time(23, 35, 59, 0);
178/// assert_eq!(
179///     time1.until(
180///         TimeDifference::new(time2).smallest(Unit::Minute),
181///     )?,
182///     5.minutes().fieldwise(),
183/// );
184/// // `TimeDifference` uses truncation as a rounding mode by default,
185/// // but you can set the rounding mode to break ties away from zero:
186/// assert_eq!(
187///     time1.until(
188///         TimeDifference::new(time2)
189///             .smallest(Unit::Minute)
190///             .mode(RoundMode::HalfExpand),
191///     )?,
192///     // Rounds up to 6 minutes.
193///     6.minutes().fieldwise(),
194/// );
195///
196/// # Ok::<(), Box<dyn std::error::Error>>(())
197/// ```
198///
199/// # Rounding
200///
201/// A `Time` can be rounded based on a [`TimeRound`] configuration of smallest
202/// units, rounding increment and rounding mode. Here's an example showing how
203/// to round to the nearest third hour:
204///
205/// ```
206/// use jiff::{civil::{TimeRound, time}, Unit};
207///
208/// let t = time(16, 27, 29, 999_999_999);
209/// assert_eq!(
210///     t.round(TimeRound::new().smallest(Unit::Hour).increment(3))?,
211///     time(15, 0, 0, 0),
212/// );
213/// // Or alternatively, make use of the `From<(Unit, i64)> for TimeRound`
214/// // trait implementation:
215/// assert_eq!(t.round((Unit::Hour, 3))?, time(15, 0, 0, 0));
216///
217/// # Ok::<(), Box<dyn std::error::Error>>(())
218/// ```
219///
220/// See [`Time::round`] for more details.
221#[derive(Clone, Copy, Eq, Hash, PartialEq, PartialOrd, Ord)]
222pub struct Time {
223    hour: Hour,
224    minute: Minute,
225    second: Second,
226    subsec_nanosecond: SubsecNanosecond,
227}
228
229impl Time {
230    /// The minimum representable time value.
231    ///
232    /// This corresponds to `00:00:00.000000000`.
233    pub const MIN: Time = Time::midnight();
234
235    /// The maximum representable time value.
236    ///
237    /// This corresponds to `23:59:59.999999999`.
238    pub const MAX: Time = Time::constant(23, 59, 59, 999_999_999);
239
240    /// Creates a new `Time` value from its component hour, minute, second and
241    /// fractional subsecond (up to nanosecond precision) values.
242    ///
243    /// To set the component values of a time after creating it, use
244    /// [`TimeWith`] via [`Time::with`] to build a new [`Time`] from the fields
245    /// of an existing time.
246    ///
247    /// # Errors
248    ///
249    /// This returns an error unless *all* of the following conditions are
250    /// true:
251    ///
252    /// * `0 <= hour <= 23`
253    /// * `0 <= minute <= 59`
254    /// * `0 <= second <= 59`
255    /// * `0 <= subsec_nanosecond <= 999,999,999`
256    ///
257    /// # Example
258    ///
259    /// This shows an example of a valid time:
260    ///
261    /// ```
262    /// use jiff::civil::Time;
263    ///
264    /// let t = Time::new(21, 30, 5, 123_456_789).unwrap();
265    /// assert_eq!(t.hour(), 21);
266    /// assert_eq!(t.minute(), 30);
267    /// assert_eq!(t.second(), 5);
268    /// assert_eq!(t.millisecond(), 123);
269    /// assert_eq!(t.microsecond(), 456);
270    /// assert_eq!(t.nanosecond(), 789);
271    /// ```
272    ///
273    /// This shows an example of an invalid time:
274    ///
275    /// ```
276    /// use jiff::civil::Time;
277    ///
278    /// assert!(Time::new(21, 30, 60, 0).is_err());
279    /// ```
280    #[inline]
281    pub fn new(
282        hour: i8,
283        minute: i8,
284        second: i8,
285        subsec_nanosecond: i32,
286    ) -> Result<Time, Error> {
287        let hour = Hour::try_new("hour", hour)?;
288        let minute = Minute::try_new("minute", minute)?;
289        let second = Second::try_new("second", second)?;
290        let subsec_nanosecond =
291            SubsecNanosecond::try_new("subsec_nanosecond", subsec_nanosecond)?;
292        Ok(Time::new_ranged(hour, minute, second, subsec_nanosecond))
293    }
294
295    /// Creates a new `Time` value in a `const` context.
296    ///
297    /// # Panics
298    ///
299    /// This panics if the given values do not correspond to a valid `Time`.
300    /// All of the following conditions must be true:
301    ///
302    /// * `0 <= hour <= 23`
303    /// * `0 <= minute <= 59`
304    /// * `0 <= second <= 59`
305    /// * `0 <= subsec_nanosecond <= 999,999,999`
306    ///
307    /// Similarly, when used in a const context, invalid parameters will
308    /// prevent your Rust program from compiling.
309    ///
310    /// # Example
311    ///
312    /// This shows an example of a valid time in a `const` context:
313    ///
314    /// ```
315    /// use jiff::civil::Time;
316    ///
317    /// const BEDTIME: Time = Time::constant(21, 30, 5, 123_456_789);
318    /// assert_eq!(BEDTIME.hour(), 21);
319    /// assert_eq!(BEDTIME.minute(), 30);
320    /// assert_eq!(BEDTIME.second(), 5);
321    /// assert_eq!(BEDTIME.millisecond(), 123);
322    /// assert_eq!(BEDTIME.microsecond(), 456);
323    /// assert_eq!(BEDTIME.nanosecond(), 789);
324    /// assert_eq!(BEDTIME.subsec_nanosecond(), 123_456_789);
325    /// ```
326    #[inline]
327    pub const fn constant(
328        hour: i8,
329        minute: i8,
330        second: i8,
331        subsec_nanosecond: i32,
332    ) -> Time {
333        if !Hour::contains(hour) {
334            panic!("invalid hour");
335        }
336        if !Minute::contains(minute) {
337            panic!("invalid minute");
338        }
339        if !Second::contains(second) {
340            panic!("invalid second");
341        }
342        if !SubsecNanosecond::contains(subsec_nanosecond) {
343            panic!("invalid nanosecond");
344        }
345        let hour = Hour::new_unchecked(hour);
346        let minute = Minute::new_unchecked(minute);
347        let second = Second::new_unchecked(second);
348        let subsec_nanosecond =
349            SubsecNanosecond::new_unchecked(subsec_nanosecond);
350        Time { hour, minute, second, subsec_nanosecond }
351    }
352
353    /// Returns the first moment of time in a day.
354    ///
355    /// Specifically, this has the `hour`, `minute`, `second`, `millisecond`,
356    /// `microsecond` and `nanosecond` fields all set to `0`.
357    ///
358    /// # Example
359    ///
360    /// ```
361    /// use jiff::civil::Time;
362    ///
363    /// let t = Time::midnight();
364    /// assert_eq!(t.hour(), 0);
365    /// assert_eq!(t.minute(), 0);
366    /// assert_eq!(t.second(), 0);
367    /// assert_eq!(t.millisecond(), 0);
368    /// assert_eq!(t.microsecond(), 0);
369    /// assert_eq!(t.nanosecond(), 0);
370    /// ```
371    #[inline]
372    pub const fn midnight() -> Time {
373        Time::constant(0, 0, 0, 0)
374    }
375
376    /// Create a builder for constructing a `Time` from the fields of this
377    /// time.
378    ///
379    /// See the methods on [`TimeWith`] for the different ways one can set the
380    /// fields of a new `Time`.
381    ///
382    /// # Example
383    ///
384    /// Unlike [`Date`], a [`Time`] is valid for all possible valid values
385    /// of its fields. That is, there is no way for two valid field values
386    /// to combine into an invalid `Time`. So, for `Time`, this builder does
387    /// have as much of a benefit versus an API design with methods like
388    /// `Time::with_hour` and `Time::with_minute`. Nevertheless, this builder
389    /// permits settings multiple fields at the same time and performing only
390    /// one validity check. Moreover, this provides a consistent API with other
391    /// date and time types in this crate.
392    ///
393    /// ```
394    /// use jiff::civil::time;
395    ///
396    /// let t1 = time(0, 0, 24, 0);
397    /// let t2 = t1.with().hour(15).minute(30).millisecond(10).build()?;
398    /// assert_eq!(t2, time(15, 30, 24, 10_000_000));
399    ///
400    /// # Ok::<(), Box<dyn std::error::Error>>(())
401    /// ```
402    #[inline]
403    pub fn with(self) -> TimeWith {
404        TimeWith::new(self)
405    }
406
407    /// Returns the "hour" component of this time.
408    ///
409    /// The value returned is guaranteed to be in the range `0..=23`.
410    ///
411    /// # Example
412    ///
413    /// ```
414    /// use jiff::civil::time;
415    ///
416    /// let t = time(13, 35, 56, 123_456_789);
417    /// assert_eq!(t.hour(), 13);
418    /// ```
419    #[inline]
420    pub fn hour(self) -> i8 {
421        self.hour_ranged().get()
422    }
423
424    /// Returns the "minute" component of this time.
425    ///
426    /// The value returned is guaranteed to be in the range `0..=59`.
427    ///
428    /// # Example
429    ///
430    /// ```
431    /// use jiff::civil::time;
432    ///
433    /// let t = time(13, 35, 56, 123_456_789);
434    /// assert_eq!(t.minute(), 35);
435    /// ```
436    #[inline]
437    pub fn minute(self) -> i8 {
438        self.minute_ranged().get()
439    }
440
441    /// Returns the "second" component of this time.
442    ///
443    /// The value returned is guaranteed to be in the range `0..=59`.
444    ///
445    /// # Example
446    ///
447    /// ```
448    /// use jiff::civil::time;
449    ///
450    /// let t = time(13, 35, 56, 123_456_789);
451    /// assert_eq!(t.second(), 56);
452    /// ```
453    #[inline]
454    pub fn second(self) -> i8 {
455        self.second_ranged().get()
456    }
457
458    /// Returns the "millisecond" component of this time.
459    ///
460    /// The value returned is guaranteed to be in the range `0..=999`.
461    ///
462    /// # Example
463    ///
464    /// ```
465    /// use jiff::civil::time;
466    ///
467    /// let t = time(13, 35, 56, 123_456_789);
468    /// assert_eq!(t.millisecond(), 123);
469    /// ```
470    #[inline]
471    pub fn millisecond(self) -> i16 {
472        self.millisecond_ranged().get()
473    }
474
475    /// Returns the "microsecond" component of this time.
476    ///
477    /// The value returned is guaranteed to be in the range `0..=999`.
478    ///
479    /// # Example
480    ///
481    /// ```
482    /// use jiff::civil::time;
483    ///
484    /// let t = time(13, 35, 56, 123_456_789);
485    /// assert_eq!(t.microsecond(), 456);
486    /// ```
487    #[inline]
488    pub fn microsecond(self) -> i16 {
489        self.microsecond_ranged().get()
490    }
491
492    /// Returns the "nanosecond" component of this time.
493    ///
494    /// The value returned is guaranteed to be in the range `0..=999`.
495    ///
496    /// # Example
497    ///
498    /// ```
499    /// use jiff::civil::time;
500    ///
501    /// let t = time(13, 35, 56, 123_456_789);
502    /// assert_eq!(t.nanosecond(), 789);
503    /// ```
504    #[inline]
505    pub fn nanosecond(self) -> i16 {
506        self.nanosecond_ranged().get()
507    }
508
509    /// Returns the fractional nanosecond for this `Time` value.
510    ///
511    /// If you want to set this value on `Time`, then use
512    /// [`TimeWith::subsec_nanosecond`] via [`Time::with`].
513    ///
514    /// The value returned is guaranteed to be in the range `0..=999_999_999`.
515    ///
516    /// # Example
517    ///
518    /// This shows the relationship between constructing a `Time` value
519    /// with routines like `with().millisecond()` and accessing the entire
520    /// fractional part as a nanosecond:
521    ///
522    /// ```
523    /// use jiff::civil::time;
524    ///
525    /// let t = time(15, 21, 35, 0).with().millisecond(987).build()?;
526    /// assert_eq!(t.subsec_nanosecond(), 987_000_000);
527    ///
528    /// # Ok::<(), Box<dyn std::error::Error>>(())
529    /// ```
530    ///
531    /// # Example: nanoseconds from a timestamp
532    ///
533    /// This shows how the fractional nanosecond part of a `Time` value
534    /// manifests from a specific timestamp.
535    ///
536    /// ```
537    /// use jiff::Timestamp;
538    ///
539    /// // 1,234 nanoseconds after the Unix epoch.
540    /// let zdt = Timestamp::new(0, 1_234)?.in_tz("UTC")?;
541    /// let time = zdt.datetime().time();
542    /// assert_eq!(time.subsec_nanosecond(), 1_234);
543    ///
544    /// // 1,234 nanoseconds before the Unix epoch.
545    /// let zdt = Timestamp::new(0, -1_234)?.in_tz("UTC")?;
546    /// let time = zdt.datetime().time();
547    /// // The nanosecond is equal to `1_000_000_000 - 1_234`.
548    /// assert_eq!(time.subsec_nanosecond(), 999998766);
549    /// // Looking at the other components of the time value might help.
550    /// assert_eq!(time.hour(), 23);
551    /// assert_eq!(time.minute(), 59);
552    /// assert_eq!(time.second(), 59);
553    ///
554    /// # Ok::<(), Box<dyn std::error::Error>>(())
555    /// ```
556    #[inline]
557    pub fn subsec_nanosecond(self) -> i32 {
558        self.subsec_nanosecond_ranged().get()
559    }
560
561    /// Given a [`Date`], this constructs a [`DateTime`] value with its time
562    /// component equal to this time.
563    ///
564    /// This is a convenience function for [`DateTime::from_parts`].
565    ///
566    /// # Example
567    ///
568    /// ```
569    /// use jiff::civil::{DateTime, date, time};
570    ///
571    /// let d = date(2010, 3, 14);
572    /// let t = time(2, 30, 0, 0);
573    /// assert_eq!(DateTime::from_parts(d, t), t.to_datetime(d));
574    /// ```
575    #[inline]
576    pub const fn to_datetime(self, date: Date) -> DateTime {
577        DateTime::from_parts(date, self)
578    }
579
580    /// A convenience function for constructing a [`DateTime`] from this time
581    /// on the date given by its components.
582    ///
583    /// # Example
584    ///
585    /// ```
586    /// use jiff::civil::time;
587    ///
588    /// assert_eq!(
589    ///     time(2, 30, 0, 0).on(2010, 3, 14).to_string(),
590    ///     "2010-03-14T02:30:00",
591    /// );
592    /// ```
593    ///
594    /// One can also flip the order by making use of [`Date::at`]:
595    ///
596    /// ```
597    /// use jiff::civil::date;
598    ///
599    /// assert_eq!(
600    ///     date(2010, 3, 14).at(2, 30, 0, 0).to_string(),
601    ///     "2010-03-14T02:30:00",
602    /// );
603    /// ```
604    #[inline]
605    pub const fn on(self, year: i16, month: i8, day: i8) -> DateTime {
606        DateTime::from_parts(Date::constant(year, month, day), self)
607    }
608
609    /// Add the given span to this time and wrap around on overflow.
610    ///
611    /// This operation accepts three different duration types: [`Span`],
612    /// [`SignedDuration`] or [`std::time::Duration`]. This is achieved via
613    /// `From` trait implementations for the [`TimeArithmetic`] type.
614    ///
615    /// # Properties
616    ///
617    /// Given times `t1` and `t2`, and a span `s`, with `t2 = t1 + s`, it
618    /// follows then that `t1 = t2 - s` for all values of `t1` and `s` that sum
619    /// to `t2`.
620    ///
621    /// In short, subtracting the given span from the sum returned by this
622    /// function is guaranteed to result in precisely the original time.
623    ///
624    /// # Example: available via addition operator
625    ///
626    /// This routine can be used via the `+` operator.
627    ///
628    /// ```
629    /// use jiff::{civil::time, ToSpan};
630    ///
631    /// let t = time(20, 10, 1, 0);
632    /// assert_eq!(
633    ///     t + 1.hours().minutes(49).seconds(59),
634    ///     time(22, 0, 0, 0),
635    /// );
636    /// ```
637    ///
638    /// # Example: add nanoseconds to a `Time`
639    ///
640    /// ```
641    /// use jiff::{civil::time, ToSpan};
642    ///
643    /// let t = time(22, 35, 1, 0);
644    /// assert_eq!(
645    ///     time(22, 35, 3, 500_000_000),
646    ///     t.wrapping_add(2_500_000_000i64.nanoseconds()),
647    /// );
648    /// ```
649    ///
650    /// # Example: add span with multiple units
651    ///
652    /// ```
653    /// use jiff::{civil::time, ToSpan};
654    ///
655    /// let t = time(20, 10, 1, 0);
656    /// assert_eq!(
657    ///     time(22, 0, 0, 0),
658    ///     t.wrapping_add(1.hours().minutes(49).seconds(59)),
659    /// );
660    /// ```
661    ///
662    /// # Example: adding an empty span is a no-op
663    ///
664    /// ```
665    /// use jiff::{civil::time, Span};
666    ///
667    /// let t = time(20, 10, 1, 0);
668    /// assert_eq!(t, t.wrapping_add(Span::new()));
669    /// ```
670    ///
671    /// # Example: addition wraps on overflow
672    ///
673    /// ```
674    /// use jiff::{civil::time, SignedDuration, ToSpan};
675    ///
676    /// let t = time(23, 59, 59, 999_999_999);
677    /// assert_eq!(
678    ///     t.wrapping_add(1.nanoseconds()),
679    ///     time(0, 0, 0, 0),
680    /// );
681    /// assert_eq!(
682    ///     t.wrapping_add(SignedDuration::from_nanos(1)),
683    ///     time(0, 0, 0, 0),
684    /// );
685    /// assert_eq!(
686    ///     t.wrapping_add(std::time::Duration::from_nanos(1)),
687    ///     time(0, 0, 0, 0),
688    /// );
689    /// ```
690    ///
691    /// Similarly, if there are any non-zero units greater than hours in the
692    /// given span, then they also result in wrapping behavior (i.e., they are
693    /// ignored):
694    ///
695    /// ```
696    /// use jiff::{civil::time, ToSpan};
697    ///
698    /// // doesn't matter what our time value is in this example
699    /// let t = time(0, 0, 0, 0);
700    /// assert_eq!(t, t.wrapping_add(1.days()));
701    /// ```
702    #[inline]
703    pub fn wrapping_add<A: Into<TimeArithmetic>>(self, duration: A) -> Time {
704        let duration: TimeArithmetic = duration.into();
705        duration.wrapping_add(self)
706    }
707
708    #[inline]
709    fn wrapping_add_span(self, span: Span) -> Time {
710        let mut sum = self.to_nanosecond().without_bounds();
711        sum = sum.wrapping_add(
712            span.get_hours_ranged()
713                .without_bounds()
714                .wrapping_mul(t::NANOS_PER_HOUR),
715        );
716        sum = sum.wrapping_add(
717            span.get_minutes_ranged()
718                .without_bounds()
719                .wrapping_mul(t::NANOS_PER_MINUTE),
720        );
721        sum = sum.wrapping_add(
722            span.get_seconds_ranged()
723                .without_bounds()
724                .wrapping_mul(t::NANOS_PER_SECOND),
725        );
726        sum = sum.wrapping_add(
727            span.get_milliseconds_ranged()
728                .without_bounds()
729                .wrapping_mul(t::NANOS_PER_MILLI),
730        );
731        sum = sum.wrapping_add(
732            span.get_microseconds_ranged()
733                .without_bounds()
734                .wrapping_mul(t::NANOS_PER_MICRO),
735        );
736        sum = sum.wrapping_add(span.get_nanoseconds_ranged().without_bounds());
737        let civil_day_nanosecond = sum % t::NANOS_PER_CIVIL_DAY;
738        Time::from_nanosecond(civil_day_nanosecond.rinto())
739    }
740
741    #[inline]
742    fn wrapping_add_signed_duration(self, duration: SignedDuration) -> Time {
743        let start = t::NoUnits128::rfrom(self.to_nanosecond());
744        let duration = t::NoUnits128::new_unchecked(duration.as_nanos());
745        let end = start.wrapping_add(duration) % t::NANOS_PER_CIVIL_DAY;
746        Time::from_nanosecond(end.rinto())
747    }
748
749    #[inline]
750    fn wrapping_add_unsigned_duration(
751        self,
752        duration: UnsignedDuration,
753    ) -> Time {
754        let start = t::NoUnits128::rfrom(self.to_nanosecond());
755        // OK because 96-bit unsigned integer can't overflow i128.
756        let duration = i128::try_from(duration.as_nanos()).unwrap();
757        let duration = t::NoUnits128::new_unchecked(duration);
758        let duration = duration % t::NANOS_PER_CIVIL_DAY;
759        let end = start.wrapping_add(duration) % t::NANOS_PER_CIVIL_DAY;
760        Time::from_nanosecond(end.rinto())
761    }
762
763    /// This routine is identical to [`Time::wrapping_add`] with the duration
764    /// negated.
765    ///
766    /// # Example
767    ///
768    /// ```
769    /// use jiff::{civil::time, SignedDuration, ToSpan};
770    ///
771    /// let t = time(0, 0, 0, 0);
772    /// assert_eq!(
773    ///     t.wrapping_sub(1.nanoseconds()),
774    ///     time(23, 59, 59, 999_999_999),
775    /// );
776    /// assert_eq!(
777    ///     t.wrapping_sub(SignedDuration::from_nanos(1)),
778    ///     time(23, 59, 59, 999_999_999),
779    /// );
780    /// assert_eq!(
781    ///     t.wrapping_sub(std::time::Duration::from_nanos(1)),
782    ///     time(23, 59, 59, 999_999_999),
783    /// );
784    ///
785    /// assert_eq!(
786    ///     t.wrapping_sub(SignedDuration::MIN),
787    ///     time(15, 30, 8, 999_999_999),
788    /// );
789    /// assert_eq!(
790    ///     t.wrapping_sub(SignedDuration::MAX),
791    ///     time(8, 29, 52, 1),
792    /// );
793    /// assert_eq!(
794    ///     t.wrapping_sub(std::time::Duration::MAX),
795    ///     time(16, 59, 44, 1),
796    /// );
797    /// ```
798    #[inline]
799    pub fn wrapping_sub<A: Into<TimeArithmetic>>(self, duration: A) -> Time {
800        let duration: TimeArithmetic = duration.into();
801        duration.wrapping_sub(self)
802    }
803
804    #[inline]
805    fn wrapping_sub_unsigned_duration(
806        self,
807        duration: UnsignedDuration,
808    ) -> Time {
809        let start = t::NoUnits128::rfrom(self.to_nanosecond());
810        // OK because 96-bit unsigned integer can't overflow i128.
811        let duration = i128::try_from(duration.as_nanos()).unwrap();
812        let duration = t::NoUnits128::new_unchecked(duration);
813        let end = start.wrapping_sub(duration) % t::NANOS_PER_CIVIL_DAY;
814        Time::from_nanosecond(end.rinto())
815    }
816
817    /// Add the given span to this time and return an error if the result would
818    /// otherwise overflow.
819    ///
820    /// This operation accepts three different duration types: [`Span`],
821    /// [`SignedDuration`] or [`std::time::Duration`]. This is achieved via
822    /// `From` trait implementations for the [`TimeArithmetic`] type.
823    ///
824    /// # Properties
825    ///
826    /// Given a time `t1` and a span `s`, and assuming `t2 = t1 + s` exists, it
827    /// follows then that `t1 = t2 - s` for all values of `t1` and `s` that sum
828    /// to a valid `t2`.
829    ///
830    /// In short, subtracting the given span from the sum returned by this
831    /// function is guaranteed to result in precisely the original time.
832    ///
833    /// # Errors
834    ///
835    /// If the sum would overflow the minimum or maximum timestamp values, then
836    /// an error is returned.
837    ///
838    /// If the given span has any non-zero units greater than hours, then an
839    /// error is returned.
840    ///
841    /// # Example: add nanoseconds to a `Time`
842    ///
843    /// ```
844    /// use jiff::{civil::time, ToSpan};
845    ///
846    /// let t = time(22, 35, 1, 0);
847    /// assert_eq!(
848    ///     time(22, 35, 3, 500_000_000),
849    ///     t.checked_add(2_500_000_000i64.nanoseconds())?,
850    /// );
851    /// # Ok::<(), Box<dyn std::error::Error>>(())
852    /// ```
853    ///
854    /// # Example: add span with multiple units
855    ///
856    /// ```
857    /// use jiff::{civil::time, ToSpan};
858    ///
859    /// let t = time(20, 10, 1, 0);
860    /// assert_eq!(
861    ///     time(22, 0, 0, 0),
862    ///     t.checked_add(1.hours().minutes(49).seconds(59))?,
863    /// );
864    /// # Ok::<(), Box<dyn std::error::Error>>(())
865    /// ```
866    ///
867    /// # Example: adding an empty span is a no-op
868    ///
869    /// ```
870    /// use jiff::{civil::time, Span};
871    ///
872    /// let t = time(20, 10, 1, 0);
873    /// assert_eq!(t, t.checked_add(Span::new())?);
874    ///
875    /// # Ok::<(), Box<dyn std::error::Error>>(())
876    /// ```
877    ///
878    /// # Example: error on overflow
879    ///
880    /// ```
881    /// use jiff::{civil::time, ToSpan};
882    ///
883    /// // okay
884    /// let t = time(23, 59, 59, 999_999_998);
885    /// assert_eq!(
886    ///     t.with().nanosecond(999).build()?,
887    ///     t.checked_add(1.nanoseconds())?,
888    /// );
889    ///
890    /// // not okay
891    /// let t = time(23, 59, 59, 999_999_999);
892    /// assert!(t.checked_add(1.nanoseconds()).is_err());
893    ///
894    /// # Ok::<(), Box<dyn std::error::Error>>(())
895    /// ```
896    ///
897    /// Similarly, if there are any non-zero units greater than hours in the
898    /// given span, then they also result in overflow (and thus an error):
899    ///
900    /// ```
901    /// use jiff::{civil::time, ToSpan};
902    ///
903    /// // doesn't matter what our time value is in this example
904    /// let t = time(0, 0, 0, 0);
905    /// assert!(t.checked_add(1.days()).is_err());
906    /// ```
907    ///
908    /// # Example: adding absolute durations
909    ///
910    /// This shows how to add signed and unsigned absolute durations to a
911    /// `Time`. As with adding a `Span`, any overflow that occurs results in
912    /// an error.
913    ///
914    /// ```
915    /// use std::time::Duration;
916    ///
917    /// use jiff::{civil::time, SignedDuration};
918    ///
919    /// let t = time(23, 0, 0, 0);
920    ///
921    /// let dur = SignedDuration::from_mins(30);
922    /// assert_eq!(t.checked_add(dur)?, time(23, 30, 0, 0));
923    /// assert_eq!(t.checked_add(-dur)?, time(22, 30, 0, 0));
924    ///
925    /// let dur = Duration::new(0, 1);
926    /// assert_eq!(t.checked_add(dur)?, time(23, 0, 0, 1));
927    ///
928    /// # Ok::<(), Box<dyn std::error::Error>>(())
929    /// ```
930    #[inline]
931    pub fn checked_add<A: Into<TimeArithmetic>>(
932        self,
933        duration: A,
934    ) -> Result<Time, Error> {
935        let duration: TimeArithmetic = duration.into();
936        duration.checked_add(self)
937    }
938
939    #[inline]
940    fn checked_add_span(self, span: Span) -> Result<Time, Error> {
941        let (time, span) = self.overflowing_add(span)?;
942        if let Some(err) = span.smallest_non_time_non_zero_unit_error() {
943            return Err(err);
944        }
945        Ok(time)
946    }
947
948    #[inline]
949    fn checked_add_duration(
950        self,
951        duration: SignedDuration,
952    ) -> Result<Time, Error> {
953        let original = duration;
954        let start = t::NoUnits128::rfrom(self.to_nanosecond());
955        let duration = t::NoUnits128::new_unchecked(duration.as_nanos());
956        // This can never fail because the maximum duration fits into a
957        // 96-bit integer, and adding any 96-bit integer to any 64-bit
958        // integer can never overflow a 128-bit integer.
959        let end = start.try_checked_add("nanoseconds", duration).unwrap();
960        let end = CivilDayNanosecond::try_rfrom("nanoseconds", end)
961            .with_context(|| {
962                err!(
963                    "adding signed duration {duration:?}, equal to
964                     {nanos} nanoseconds, to {time} overflowed",
965                    duration = original,
966                    nanos = original.as_nanos(),
967                    time = self,
968                )
969            })?;
970        Ok(Time::from_nanosecond(end))
971    }
972
973    /// This routine is identical to [`Time::checked_add`] with the duration
974    /// negated.
975    ///
976    /// # Errors
977    ///
978    /// This has the same error conditions as [`Time::checked_add`].
979    ///
980    /// # Example
981    ///
982    /// ```
983    /// use std::time::Duration;
984    ///
985    /// use jiff::{civil::time, SignedDuration, ToSpan};
986    ///
987    /// let t = time(22, 0, 0, 0);
988    /// assert_eq!(
989    ///     t.checked_sub(1.hours().minutes(49).seconds(59))?,
990    ///     time(20, 10, 1, 0),
991    /// );
992    /// assert_eq!(
993    ///     t.checked_sub(SignedDuration::from_hours(1))?,
994    ///     time(21, 0, 0, 0),
995    /// );
996    /// assert_eq!(
997    ///     t.checked_sub(Duration::from_secs(60 * 60))?,
998    ///     time(21, 0, 0, 0),
999    /// );
1000    /// # Ok::<(), Box<dyn std::error::Error>>(())
1001    /// ```
1002    #[inline]
1003    pub fn checked_sub<A: Into<TimeArithmetic>>(
1004        self,
1005        duration: A,
1006    ) -> Result<Time, Error> {
1007        let duration: TimeArithmetic = duration.into();
1008        duration.checked_neg().and_then(|ta| ta.checked_add(self))
1009    }
1010
1011    /// This routine is identical to [`Time::checked_add`], except the
1012    /// result saturates on overflow. That is, instead of overflow, either
1013    /// [`Time::MIN`] or [`Time::MAX`] is returned.
1014    ///
1015    /// # Example
1016    ///
1017    /// ```
1018    /// use jiff::{civil::{Time, time}, SignedDuration, ToSpan};
1019    ///
1020    /// // no saturation
1021    /// let t = time(23, 59, 59, 999_999_998);
1022    /// assert_eq!(
1023    ///     t.with().nanosecond(999).build()?,
1024    ///     t.saturating_add(1.nanoseconds()),
1025    /// );
1026    ///
1027    /// // saturates
1028    /// let t = time(23, 59, 59, 999_999_999);
1029    /// assert_eq!(Time::MAX, t.saturating_add(1.nanoseconds()));
1030    /// assert_eq!(Time::MAX, t.saturating_add(SignedDuration::MAX));
1031    /// assert_eq!(Time::MIN, t.saturating_add(SignedDuration::MIN));
1032    /// assert_eq!(Time::MAX, t.saturating_add(std::time::Duration::MAX));
1033    ///
1034    /// # Ok::<(), Box<dyn std::error::Error>>(())
1035    /// ```
1036    ///
1037    /// Similarly, if there are any non-zero units greater than hours in the
1038    /// given span, then they also result in overflow (and thus saturation):
1039    ///
1040    /// ```
1041    /// use jiff::{civil::{Time, time}, ToSpan};
1042    ///
1043    /// // doesn't matter what our time value is in this example
1044    /// let t = time(0, 0, 0, 0);
1045    /// assert_eq!(Time::MAX, t.saturating_add(1.days()));
1046    /// ```
1047    #[inline]
1048    pub fn saturating_add<A: Into<TimeArithmetic>>(self, duration: A) -> Time {
1049        let duration: TimeArithmetic = duration.into();
1050        self.checked_add(duration).unwrap_or_else(|_| {
1051            if duration.is_negative() {
1052                Time::MIN
1053            } else {
1054                Time::MAX
1055            }
1056        })
1057    }
1058
1059    /// This routine is identical to [`Time::saturating_add`] with the duration
1060    /// negated.
1061    ///
1062    /// # Example
1063    ///
1064    /// ```
1065    /// use jiff::{civil::{Time, time}, SignedDuration, ToSpan};
1066    ///
1067    /// // no saturation
1068    /// let t = time(0, 0, 0, 1);
1069    /// assert_eq!(
1070    ///     t.with().nanosecond(0).build()?,
1071    ///     t.saturating_sub(1.nanoseconds()),
1072    /// );
1073    ///
1074    /// // saturates
1075    /// let t = time(0, 0, 0, 0);
1076    /// assert_eq!(Time::MIN, t.saturating_sub(1.nanoseconds()));
1077    /// assert_eq!(Time::MIN, t.saturating_sub(SignedDuration::MAX));
1078    /// assert_eq!(Time::MAX, t.saturating_sub(SignedDuration::MIN));
1079    /// assert_eq!(Time::MIN, t.saturating_sub(std::time::Duration::MAX));
1080    ///
1081    /// # Ok::<(), Box<dyn std::error::Error>>(())
1082    /// ```
1083    #[inline]
1084    pub fn saturating_sub<A: Into<TimeArithmetic>>(self, duration: A) -> Time {
1085        let duration: TimeArithmetic = duration.into();
1086        let Ok(duration) = duration.checked_neg() else { return Time::MIN };
1087        self.saturating_add(duration)
1088    }
1089
1090    /// Adds the given span to the this time value, and returns the resulting
1091    /// time with any overflowing amount in the span returned.
1092    ///
1093    /// This isn't part of the public API because it seems a little odd, and
1094    /// I'm unsure of its use case. Overall this routine is a bit specialized
1095    /// and I'm not sure how generally useful it is. But it is used in crucial
1096    /// points in other parts of this crate.
1097    ///
1098    /// If you want this public, please file an issue and discuss your use
1099    /// case: https://github.com/BurntSushi/jiff/issues/new
1100    #[inline]
1101    pub(crate) fn overflowing_add(
1102        self,
1103        span: Span,
1104    ) -> Result<(Time, Span), Error> {
1105        if let Some(err) = span.smallest_non_time_non_zero_unit_error() {
1106            return Err(err);
1107        }
1108        let span_nanos = span.to_invariant_nanoseconds();
1109        let time_nanos = self.to_nanosecond();
1110        let sum = span_nanos + time_nanos;
1111        let days = t::SpanDays::try_new(
1112            "overflowing-days",
1113            sum.div_floor(t::NANOS_PER_CIVIL_DAY),
1114        )?;
1115        let time_nanos = sum.rem_floor(t::NANOS_PER_CIVIL_DAY);
1116        let time = Time::from_nanosecond(time_nanos.rinto());
1117        Ok((time, Span::new().days_ranged(days)))
1118    }
1119
1120    /// Like `overflowing_add`, but with `SignedDuration`.
1121    ///
1122    /// This is used for datetime arithmetic, when adding to the time
1123    /// component overflows into days (always 24 hours).
1124    #[inline]
1125    pub(crate) fn overflowing_add_duration(
1126        self,
1127        duration: SignedDuration,
1128    ) -> Result<(Time, SignedDuration), Error> {
1129        if self.subsec_nanosecond() != 0 || duration.subsec_nanos() != 0 {
1130            return self.overflowing_add_duration_general(duration);
1131        }
1132        let start = t::NoUnits::rfrom(self.to_second());
1133        let duration_secs = t::NoUnits::new_unchecked(duration.as_secs());
1134        // This can fail if the duration is near its min or max values, and
1135        // thus we fall back to the more general (but slower) implementation
1136        // that uses 128-bit integers.
1137        let Some(sum) = start.checked_add(duration_secs) else {
1138            return self.overflowing_add_duration_general(duration);
1139        };
1140        let days = t::SpanDays::try_new(
1141            "overflowing-days",
1142            sum.div_floor(t::SECONDS_PER_CIVIL_DAY),
1143        )?;
1144        let time_secs = sum.rem_floor(t::SECONDS_PER_CIVIL_DAY);
1145        let time = Time::from_second(time_secs.rinto());
1146        // OK because of the constraint imposed by t::SpanDays.
1147        let hours = i64::from(days).checked_mul(24).unwrap();
1148        Ok((time, SignedDuration::from_hours(hours)))
1149    }
1150
1151    /// Like `overflowing_add`, but with `SignedDuration`.
1152    ///
1153    /// This is used for datetime arithmetic, when adding to the time
1154    /// component overflows into days (always 24 hours).
1155    #[inline(never)]
1156    #[cold]
1157    fn overflowing_add_duration_general(
1158        self,
1159        duration: SignedDuration,
1160    ) -> Result<(Time, SignedDuration), Error> {
1161        let start = t::NoUnits128::rfrom(self.to_nanosecond());
1162        let duration = t::NoUnits96::new_unchecked(duration.as_nanos());
1163        // This can never fail because the maximum duration fits into a
1164        // 96-bit integer, and adding any 96-bit integer to any 64-bit
1165        // integer can never overflow a 128-bit integer.
1166        let sum = start.try_checked_add("nanoseconds", duration).unwrap();
1167        let days = t::SpanDays::try_new(
1168            "overflowing-days",
1169            sum.div_floor(t::NANOS_PER_CIVIL_DAY),
1170        )?;
1171        let time_nanos = sum.rem_floor(t::NANOS_PER_CIVIL_DAY);
1172        let time = Time::from_nanosecond(time_nanos.rinto());
1173        // OK because of the constraint imposed by t::SpanDays.
1174        let hours = i64::from(days).checked_mul(24).unwrap();
1175        Ok((time, SignedDuration::from_hours(hours)))
1176    }
1177
1178    /// Returns a span representing the elapsed time from this time until
1179    /// the given `other` time.
1180    ///
1181    /// When `other` is earlier than this time, the span returned will be
1182    /// negative.
1183    ///
1184    /// Depending on the input provided, the span returned is rounded. It may
1185    /// also be balanced down to smaller units than the default. By default,
1186    /// the span returned is balanced such that the biggest possible unit is
1187    /// hours.
1188    ///
1189    /// This operation is configured by providing a [`TimeDifference`]
1190    /// value. Since this routine accepts anything that implements
1191    /// `Into<TimeDifference>`, once can pass a `Time` directly. One
1192    /// can also pass a `(Unit, Time)`, where `Unit` is treated as
1193    /// [`TimeDifference::largest`].
1194    ///
1195    /// # Properties
1196    ///
1197    /// As long as no rounding is requested, it is guaranteed that adding the
1198    /// span returned to the `other` time will always equal this time.
1199    ///
1200    /// # Errors
1201    ///
1202    /// An error can occur if `TimeDifference` is misconfigured. For example,
1203    /// if the smallest unit provided is bigger than the largest unit, or if
1204    /// the largest unit is bigger than [`Unit::Hour`].
1205    ///
1206    /// It is guaranteed that if one provides a time with the default
1207    /// [`TimeDifference`] configuration, then this routine will never fail.
1208    ///
1209    /// # Examples
1210    ///
1211    /// ```
1212    /// use jiff::{civil::time, ToSpan};
1213    ///
1214    /// let t1 = time(22, 35, 1, 0);
1215    /// let t2 = time(22, 35, 3, 500_000_000);
1216    /// assert_eq!(t1.until(t2)?, 2.seconds().milliseconds(500).fieldwise());
1217    /// // Flipping the dates is fine, but you'll get a negative span.
1218    /// assert_eq!(t2.until(t1)?, -2.seconds().milliseconds(500).fieldwise());
1219    ///
1220    /// # Ok::<(), Box<dyn std::error::Error>>(())
1221    /// ```
1222    ///
1223    /// # Example: using smaller units
1224    ///
1225    /// This example shows how to contract the span returned to smaller units.
1226    /// This makes use of a `From<(Unit, Time)> for TimeDifference`
1227    /// trait implementation.
1228    ///
1229    /// ```
1230    /// use jiff::{civil::time, Unit, ToSpan};
1231    ///
1232    /// let t1 = time(3, 24, 30, 3500);
1233    /// let t2 = time(15, 30, 0, 0);
1234    ///
1235    /// // The default limits spans to using "hours" as the biggest unit.
1236    /// let span = t1.until(t2)?;
1237    /// assert_eq!(span.to_string(), "PT12H5M29.9999965S");
1238    ///
1239    /// // But we can ask for smaller units, like capping the biggest unit
1240    /// // to minutes instead of hours.
1241    /// let span = t1.until((Unit::Minute, t2))?;
1242    /// assert_eq!(span.to_string(), "PT725M29.9999965S");
1243    ///
1244    /// # Ok::<(), Box<dyn std::error::Error>>(())
1245    /// ```
1246    #[inline]
1247    pub fn until<A: Into<TimeDifference>>(
1248        self,
1249        other: A,
1250    ) -> Result<Span, Error> {
1251        let args: TimeDifference = other.into();
1252        let span = args.until_with_largest_unit(self)?;
1253        if args.rounding_may_change_span() {
1254            span.round(args.round)
1255        } else {
1256            Ok(span)
1257        }
1258    }
1259
1260    /// This routine is identical to [`Time::until`], but the order of the
1261    /// parameters is flipped.
1262    ///
1263    /// # Errors
1264    ///
1265    /// This has the same error conditions as [`Time::until`].
1266    ///
1267    /// # Example
1268    ///
1269    /// This routine can be used via the `-` operator. Since the default
1270    /// configuration is used and because a `Span` can represent the difference
1271    /// between any two possible times, it will never panic.
1272    ///
1273    /// ```
1274    /// use jiff::{civil::time, ToSpan};
1275    ///
1276    /// let earlier = time(1, 0, 0, 0);
1277    /// let later = time(22, 30, 0, 0);
1278    /// assert_eq!(later - earlier, 21.hours().minutes(30).fieldwise());
1279    /// ```
1280    #[inline]
1281    pub fn since<A: Into<TimeDifference>>(
1282        self,
1283        other: A,
1284    ) -> Result<Span, Error> {
1285        let args: TimeDifference = other.into();
1286        let span = -args.until_with_largest_unit(self)?;
1287        if args.rounding_may_change_span() {
1288            span.round(args.round)
1289        } else {
1290            Ok(span)
1291        }
1292    }
1293
1294    /// Returns an absolute duration representing the elapsed time from this
1295    /// time until the given `other` time.
1296    ///
1297    /// When `other` occurs before this time, then the duration returned will
1298    /// be negative.
1299    ///
1300    /// Unlike [`Time::until`], this returns a duration corresponding to a
1301    /// 96-bit integer of nanoseconds between two times. In this case of
1302    /// computing durations between civil times where all days are assumed to
1303    /// be 24 hours long, the duration returned will always be less than 24
1304    /// hours.
1305    ///
1306    /// # Fallibility
1307    ///
1308    /// This routine never panics or returns an error. Since there are no
1309    /// configuration options that can be incorrectly provided, no error is
1310    /// possible when calling this routine. In contrast, [`Time::until`] can
1311    /// return an error in some cases due to misconfiguration. But like this
1312    /// routine, [`Time::until`] never panics or returns an error in its
1313    /// default configuration.
1314    ///
1315    /// # When should I use this versus [`Time::until`]?
1316    ///
1317    /// See the type documentation for [`SignedDuration`] for the section on
1318    /// when one should use [`Span`] and when one should use `SignedDuration`.
1319    /// In short, use `Span` (and therefore `Time::until`) unless you have a
1320    /// specific reason to do otherwise.
1321    ///
1322    /// # Example
1323    ///
1324    /// ```
1325    /// use jiff::{civil::time, SignedDuration};
1326    ///
1327    /// let t1 = time(22, 35, 1, 0);
1328    /// let t2 = time(22, 35, 3, 500_000_000);
1329    /// assert_eq!(t1.duration_until(t2), SignedDuration::new(2, 500_000_000));
1330    /// // Flipping the time is fine, but you'll get a negative duration.
1331    /// assert_eq!(t2.duration_until(t1), -SignedDuration::new(2, 500_000_000));
1332    /// ```
1333    ///
1334    /// # Example: difference with [`Time::until`]
1335    ///
1336    /// Since the difference between two civil times is always expressed in
1337    /// units of hours or smaller, and units of hours or smaller are always
1338    /// uniform, there is no "expressive" difference between this routine and
1339    /// `Time::until`. The only difference is that this routine returns a
1340    /// `SignedDuration` and `Time::until` returns a [`Span`]. Moreover, since
1341    /// the difference is always less than 24 hours, the return values can
1342    /// always be infallibly and losslessly converted between each other:
1343    ///
1344    /// ```
1345    /// use jiff::{civil::time, SignedDuration, Span};
1346    ///
1347    /// let t1 = time(22, 35, 1, 0);
1348    /// let t2 = time(22, 35, 3, 500_000_000);
1349    /// let dur = t1.duration_until(t2);
1350    /// // Guaranteed to never fail because the duration
1351    /// // between two civil times never exceeds the limits
1352    /// // of a `Span`.
1353    /// let span = Span::try_from(dur).unwrap();
1354    /// assert_eq!(span, Span::new().seconds(2).milliseconds(500).fieldwise());
1355    /// // Guaranteed to succeed and always return the original
1356    /// // duration because the units are always hours or smaller,
1357    /// // and thus uniform. This means a relative datetime is
1358    /// // never required to do this conversion.
1359    /// let dur = SignedDuration::try_from(span).unwrap();
1360    /// assert_eq!(dur, SignedDuration::new(2, 500_000_000));
1361    /// ```
1362    ///
1363    /// This conversion guarantee also applies to [`Time::until`] since it
1364    /// always returns a balanced span. That is, it never returns spans like
1365    /// `1 second 1000 milliseconds`. (Those cannot be losslessly converted to
1366    /// a `SignedDuration` since a `SignedDuration` is only represented as a
1367    /// single 96-bit integer of nanoseconds.)
1368    ///
1369    /// # Example: getting an unsigned duration
1370    ///
1371    /// If you're looking to find the duration between two times as a
1372    /// [`std::time::Duration`], you'll need to use this method to get a
1373    /// [`SignedDuration`] and then convert it to a `std::time::Duration`:
1374    ///
1375    /// ```
1376    /// use std::time::Duration;
1377    ///
1378    /// use jiff::{civil::time, SignedDuration, Span};
1379    ///
1380    /// let t1 = time(22, 35, 1, 0);
1381    /// let t2 = time(22, 35, 3, 500_000_000);
1382    /// let dur = Duration::try_from(t1.duration_until(t2))?;;
1383    /// assert_eq!(dur, Duration::new(2, 500_000_000));
1384    ///
1385    /// // Note that unsigned durations cannot represent all
1386    /// // possible differences! If the duration would be negative,
1387    /// // then the conversion fails:
1388    /// assert!(Duration::try_from(t2.duration_until(t1)).is_err());
1389    ///
1390    /// # Ok::<(), Box<dyn std::error::Error>>(())
1391    /// ```
1392    #[inline]
1393    pub fn duration_until(self, other: Time) -> SignedDuration {
1394        SignedDuration::time_until(self, other)
1395    }
1396
1397    /// This routine is identical to [`Time::duration_until`], but the order of
1398    /// the parameters is flipped.
1399    ///
1400    /// # Example
1401    ///
1402    /// ```
1403    /// use jiff::{civil::time, SignedDuration};
1404    ///
1405    /// let earlier = time(1, 0, 0, 0);
1406    /// let later = time(22, 30, 0, 0);
1407    /// assert_eq!(
1408    ///     later.duration_since(earlier),
1409    ///     SignedDuration::from_secs((21 * 60 * 60) + (30 * 60)),
1410    /// );
1411    /// ```
1412    #[inline]
1413    pub fn duration_since(self, other: Time) -> SignedDuration {
1414        SignedDuration::time_until(other, self)
1415    }
1416
1417    /// Rounds this time according to the [`TimeRound`] configuration given.
1418    ///
1419    /// The principal option is [`TimeRound::smallest`], which allows one
1420    /// to configure the smallest units in the returned time. Rounding
1421    /// is what determines whether that unit should keep its current value
1422    /// or whether it should be incremented. Moreover, the amount it should
1423    /// be incremented can be configured via [`TimeRound::increment`].
1424    /// Finally, the rounding strategy itself can be configured via
1425    /// [`TimeRound::mode`].
1426    ///
1427    /// Note that this routine is generic and accepts anything that
1428    /// implements `Into<TimeRound>`. Some notable implementations are:
1429    ///
1430    /// * `From<Unit> for Round`, which will automatically create a
1431    /// `TimeRound::new().smallest(unit)` from the unit provided.
1432    /// * `From<(Unit, i64)> for Round`, which will automatically create a
1433    /// `TimeRound::new().smallest(unit).increment(number)` from the unit
1434    /// and increment provided.
1435    ///
1436    /// # Errors
1437    ///
1438    /// This returns an error if the smallest unit configured on the given
1439    /// [`TimeRound`] is bigger than hours.
1440    ///
1441    /// The rounding increment must divide evenly into the next highest unit
1442    /// after the smallest unit configured (and must not be equivalent to it).
1443    /// For example, if the smallest unit is [`Unit::Nanosecond`], then *some*
1444    /// of the valid values for the rounding increment are `1`, `2`, `4`, `5`,
1445    /// `100` and `500`. Namely, any integer that divides evenly into `1,000`
1446    /// nanoseconds since there are `1,000` nanoseconds in the next highest
1447    /// unit (microseconds).
1448    ///
1449    /// This can never fail because of overflow for any input. The only
1450    /// possible errors are "configuration" errors.
1451    ///
1452    /// # Example
1453    ///
1454    /// This is a basic example that demonstrates rounding a datetime to the
1455    /// nearest second. This also demonstrates calling this method with the
1456    /// smallest unit directly, instead of constructing a `TimeRound` manually.
1457    ///
1458    /// ```
1459    /// use jiff::{civil::time, Unit};
1460    ///
1461    /// let t = time(15, 45, 10, 123_456_789);
1462    /// assert_eq!(
1463    ///     t.round(Unit::Second)?,
1464    ///     time(15, 45, 10, 0),
1465    /// );
1466    /// let t = time(15, 45, 10, 500_000_001);
1467    /// assert_eq!(
1468    ///     t.round(Unit::Second)?,
1469    ///     time(15, 45, 11, 0),
1470    /// );
1471    ///
1472    /// # Ok::<(), Box<dyn std::error::Error>>(())
1473    /// ```
1474    ///
1475    /// # Example: changing the rounding mode
1476    ///
1477    /// The default rounding mode is [`RoundMode::HalfExpand`], which
1478    /// breaks ties by rounding away from zero. But other modes like
1479    /// [`RoundMode::Trunc`] can be used too:
1480    ///
1481    /// ```
1482    /// use jiff::{civil::{TimeRound, time}, RoundMode, Unit};
1483    ///
1484    /// let t = time(15, 45, 10, 999_999_999);
1485    /// assert_eq!(
1486    ///     t.round(Unit::Second)?,
1487    ///     time(15, 45, 11, 0),
1488    /// );
1489    /// // The default will round up to the next second for any fraction
1490    /// // greater than or equal to 0.5. But truncation will always round
1491    /// // toward zero.
1492    /// assert_eq!(
1493    ///     t.round(
1494    ///         TimeRound::new().smallest(Unit::Second).mode(RoundMode::Trunc),
1495    ///     )?,
1496    ///     time(15, 45, 10, 0),
1497    /// );
1498    ///
1499    /// # Ok::<(), Box<dyn std::error::Error>>(())
1500    /// ```
1501    ///
1502    /// # Example: rounding to the nearest 5 minute increment
1503    ///
1504    /// ```
1505    /// use jiff::{civil::time, Unit};
1506    ///
1507    /// // rounds down
1508    /// let t = time(15, 27, 29, 999_999_999);
1509    /// assert_eq!(t.round((Unit::Minute, 5))?, time(15, 25, 0, 0));
1510    /// // rounds up
1511    /// let t = time(15, 27, 30, 0);
1512    /// assert_eq!(t.round((Unit::Minute, 5))?, time(15, 30, 0, 0));
1513    ///
1514    /// # Ok::<(), Box<dyn std::error::Error>>(())
1515    /// ```
1516    ///
1517    /// # Example: rounding wraps around on overflow
1518    ///
1519    /// This example demonstrates that it's possible for this operation to
1520    /// overflow, and as a result, have the time wrap around.
1521    ///
1522    /// ```
1523    /// use jiff::{civil::Time, Unit};
1524    ///
1525    /// let t = Time::MAX;
1526    /// assert_eq!(t.round(Unit::Hour)?, Time::MIN);
1527    ///
1528    /// # Ok::<(), Box<dyn std::error::Error>>(())
1529    /// ```
1530    #[inline]
1531    pub fn round<R: Into<TimeRound>>(self, options: R) -> Result<Time, Error> {
1532        let options: TimeRound = options.into();
1533        options.round(self)
1534    }
1535
1536    /// Return an iterator of periodic times determined by the given span.
1537    ///
1538    /// The given span may be negative, in which case, the iterator will move
1539    /// backwards through time. The iterator won't stop until either the span
1540    /// itself overflows, or it would otherwise exceed the minimum or maximum
1541    /// `Time` value.
1542    ///
1543    /// # Example: visiting every third hour
1544    ///
1545    /// This shows how to visit each third hour of a 24 hour time interval:
1546    ///
1547    /// ```
1548    /// use jiff::{civil::{Time, time}, ToSpan};
1549    ///
1550    /// let start = Time::MIN;
1551    /// let mut every_third_hour = vec![];
1552    /// for t in start.series(3.hours()) {
1553    ///     every_third_hour.push(t);
1554    /// }
1555    /// assert_eq!(every_third_hour, vec![
1556    ///     time(0, 0, 0, 0),
1557    ///     time(3, 0, 0, 0),
1558    ///     time(6, 0, 0, 0),
1559    ///     time(9, 0, 0, 0),
1560    ///     time(12, 0, 0, 0),
1561    ///     time(15, 0, 0, 0),
1562    ///     time(18, 0, 0, 0),
1563    ///     time(21, 0, 0, 0),
1564    /// ]);
1565    /// ```
1566    ///
1567    /// Or go backwards every 6.5 hours:
1568    ///
1569    /// ```
1570    /// use jiff::{civil::{Time, time}, ToSpan};
1571    ///
1572    /// let start = time(23, 0, 0, 0);
1573    /// let times: Vec<Time> = start.series(-6.hours().minutes(30)).collect();
1574    /// assert_eq!(times, vec![
1575    ///     time(23, 0, 0, 0),
1576    ///     time(16, 30, 0, 0),
1577    ///     time(10, 0, 0, 0),
1578    ///     time(3, 30, 0, 0),
1579    /// ]);
1580    /// ```
1581    #[inline]
1582    pub fn series(self, period: Span) -> TimeSeries {
1583        TimeSeries { start: self, period, step: 0 }
1584    }
1585}
1586
1587/// Parsing and formatting using a "printf"-style API.
1588impl Time {
1589    /// Parses a civil time in `input` matching the given `format`.
1590    ///
1591    /// The format string uses a "printf"-style API where conversion
1592    /// specifiers can be used as place holders to match components of
1593    /// a datetime. For details on the specifiers supported, see the
1594    /// [`fmt::strtime`] module documentation.
1595    ///
1596    /// # Errors
1597    ///
1598    /// This returns an error when parsing failed. This might happen because
1599    /// the format string itself was invalid, or because the input didn't match
1600    /// the format string.
1601    ///
1602    /// This also returns an error if there wasn't sufficient information to
1603    /// construct a civil time. For example, if an offset wasn't parsed.
1604    ///
1605    /// # Example
1606    ///
1607    /// This example shows how to parse a civil time:
1608    ///
1609    /// ```
1610    /// use jiff::civil::Time;
1611    ///
1612    /// // Parse with a 12-hour clock.
1613    /// let time = Time::strptime("%I:%M%P", "4:30pm")?;
1614    /// assert_eq!(time.to_string(), "16:30:00");
1615    ///
1616    /// # Ok::<(), Box<dyn std::error::Error>>(())
1617    /// ```
1618    #[inline]
1619    pub fn strptime(
1620        format: impl AsRef<[u8]>,
1621        input: impl AsRef<[u8]>,
1622    ) -> Result<Time, Error> {
1623        fmt::strtime::parse(format, input).and_then(|tm| tm.to_time())
1624    }
1625
1626    /// Formats this civil time according to the given `format`.
1627    ///
1628    /// The format string uses a "printf"-style API where conversion
1629    /// specifiers can be used as place holders to format components of
1630    /// a datetime. For details on the specifiers supported, see the
1631    /// [`fmt::strtime`] module documentation.
1632    ///
1633    /// # Errors and panics
1634    ///
1635    /// While this routine itself does not error or panic, using the value
1636    /// returned may result in a panic if formatting fails. See the
1637    /// documentation on [`fmt::strtime::Display`] for more information.
1638    ///
1639    /// To format in a way that surfaces errors without panicking, use either
1640    /// [`fmt::strtime::format`] or [`fmt::strtime::BrokenDownTime::format`].
1641    ///
1642    /// # Example
1643    ///
1644    /// This example shows how to format a civil time in a 12 hour clock with
1645    /// no padding for the hour:
1646    ///
1647    /// ```
1648    /// use jiff::civil::time;
1649    ///
1650    /// let t = time(16, 30, 59, 0);
1651    /// let string = t.strftime("%-I:%M%P").to_string();
1652    /// assert_eq!(string, "4:30pm");
1653    /// ```
1654    ///
1655    /// Note that one can round a `Time` before formatting. For example, to
1656    /// round to the nearest minute:
1657    ///
1658    /// ```
1659    /// use jiff::{civil::time, Unit};
1660    ///
1661    /// let t = time(16, 30, 59, 0);
1662    /// let string = t.round(Unit::Minute)?.strftime("%-I:%M%P").to_string();
1663    /// assert_eq!(string, "4:31pm");
1664    ///
1665    /// # Ok::<(), Box<dyn std::error::Error>>(())
1666    /// ```
1667    #[inline]
1668    pub fn strftime<'f, F: 'f + ?Sized + AsRef<[u8]>>(
1669        &self,
1670        format: &'f F,
1671    ) -> fmt::strtime::Display<'f> {
1672        fmt::strtime::Display { fmt: format.as_ref(), tm: (*self).into() }
1673    }
1674}
1675
1676/// Crate internal APIs.
1677///
1678/// Many of these are mirrors of the public API, but on ranged types. These
1679/// are often much more convenient to use in composition with other parts of
1680/// the crate that also use ranged integer types. And this often permits the
1681/// routines to be infallible and (possibly) zero-cost.
1682impl Time {
1683    #[inline]
1684    pub(crate) fn new_ranged(
1685        hour: impl RInto<Hour>,
1686        minute: impl RInto<Minute>,
1687        second: impl RInto<Second>,
1688        subsec_nanosecond: impl RInto<SubsecNanosecond>,
1689    ) -> Time {
1690        Time {
1691            hour: hour.rinto(),
1692            minute: minute.rinto(),
1693            second: second.rinto(),
1694            subsec_nanosecond: subsec_nanosecond.rinto(),
1695        }
1696    }
1697
1698    /// Set the fractional parts of this time to the given units via ranged
1699    /// types.
1700    #[inline]
1701    fn with_subsec_parts_ranged(
1702        self,
1703        millisecond: impl RInto<Millisecond>,
1704        microsecond: impl RInto<Microsecond>,
1705        nanosecond: impl RInto<Nanosecond>,
1706    ) -> Time {
1707        let millisecond = SubsecNanosecond::rfrom(millisecond.rinto());
1708        let microsecond = SubsecNanosecond::rfrom(microsecond.rinto());
1709        let nanosecond = SubsecNanosecond::rfrom(nanosecond.rinto());
1710        let mut subsec_nanosecond =
1711            millisecond * t::MICROS_PER_MILLI * t::NANOS_PER_MICRO;
1712        subsec_nanosecond += microsecond * t::NANOS_PER_MICRO;
1713        subsec_nanosecond += nanosecond;
1714        Time { subsec_nanosecond: subsec_nanosecond.rinto(), ..self }
1715    }
1716
1717    #[inline]
1718    pub(crate) fn hour_ranged(self) -> Hour {
1719        self.hour
1720    }
1721
1722    #[inline]
1723    pub(crate) fn minute_ranged(self) -> Minute {
1724        self.minute
1725    }
1726
1727    #[inline]
1728    pub(crate) fn second_ranged(self) -> Second {
1729        self.second
1730    }
1731
1732    #[inline]
1733    pub(crate) fn millisecond_ranged(self) -> Millisecond {
1734        let micros = self.subsec_nanosecond_ranged() / t::NANOS_PER_MICRO;
1735        let millis = micros / t::MICROS_PER_MILLI;
1736        millis.rinto()
1737    }
1738
1739    #[inline]
1740    pub(crate) fn microsecond_ranged(self) -> Microsecond {
1741        let micros = self.subsec_nanosecond_ranged() / t::NANOS_PER_MICRO;
1742        let only_micros = micros % t::MICROS_PER_MILLI;
1743        only_micros.rinto()
1744    }
1745
1746    #[inline]
1747    pub(crate) fn nanosecond_ranged(self) -> Nanosecond {
1748        let only_nanos = self.subsec_nanosecond_ranged() % t::NANOS_PER_MICRO;
1749        only_nanos.rinto()
1750    }
1751
1752    #[inline]
1753    pub(crate) fn subsec_nanosecond_ranged(self) -> SubsecNanosecond {
1754        self.subsec_nanosecond
1755    }
1756
1757    #[inline]
1758    pub(crate) fn until_nanoseconds(self, other: Time) -> t::SpanNanoseconds {
1759        let t1 = t::SpanNanoseconds::rfrom(self.to_nanosecond());
1760        let t2 = t::SpanNanoseconds::rfrom(other.to_nanosecond());
1761        t2 - t1
1762    }
1763
1764    /// Converts this time value to the number of seconds that has elapsed
1765    /// since `00:00:00`. This completely ignores seconds. Callers should
1766    /// likely ensure that the fractional second component is zero.
1767    ///
1768    /// The maximum possible value that can be returned represents the time
1769    /// `23:59:59`.
1770    #[inline]
1771    pub(crate) fn to_second(&self) -> CivilDaySecond {
1772        self.to_itime().map(|x| x.to_second().second).to_rint()
1773    }
1774
1775    /// Converts the given second to a time value. The second should correspond
1776    /// to the number of seconds that have elapsed since `00:00:00`. The
1777    /// fractional second component of the `Time` returned is always `0`.
1778    #[cfg_attr(feature = "perf-inline", inline(always))]
1779    pub(crate) fn from_second(second: CivilDaySecond) -> Time {
1780        let second = rangeint::composite!((second) => {
1781            ITimeSecond { second }
1782        });
1783        Time::from_itime(second.map(|x| x.to_time()))
1784    }
1785
1786    /// Converts this time value to the number of nanoseconds that has elapsed
1787    /// since `00:00:00.000000000`.
1788    ///
1789    /// The maximum possible value that can be returned represents the time
1790    /// `23:59:59.999999999`.
1791    #[inline]
1792    pub(crate) fn to_nanosecond(&self) -> CivilDayNanosecond {
1793        self.to_itime().map(|x| x.to_nanosecond().nanosecond).to_rint()
1794    }
1795
1796    /// Converts the given nanosecond to a time value. The nanosecond should
1797    /// correspond to the number of nanoseconds that have elapsed since
1798    /// `00:00:00.000000000`.
1799    #[cfg_attr(feature = "perf-inline", inline(always))]
1800    pub(crate) fn from_nanosecond(nanosecond: CivilDayNanosecond) -> Time {
1801        let nano = rangeint::composite!((nanosecond) => {
1802            ITimeNanosecond { nanosecond }
1803        });
1804        Time::from_itime(nano.map(|x| x.to_time()))
1805    }
1806
1807    #[inline]
1808    pub(crate) fn to_itime(&self) -> Composite<ITime> {
1809        rangeint::composite! {
1810            (
1811                hour = self.hour,
1812                minute = self.minute,
1813                second = self.second,
1814                subsec_nanosecond = self.subsec_nanosecond,
1815            ) => {
1816                ITime { hour, minute, second, subsec_nanosecond }
1817            }
1818        }
1819    }
1820
1821    #[inline]
1822    pub(crate) fn from_itime(itime: Composite<ITime>) -> Time {
1823        let (hour, minute, second, subsec_nanosecond) = rangeint::uncomposite!(
1824            itime,
1825            c => (c.hour, c.minute, c.second, c.subsec_nanosecond),
1826        );
1827        Time {
1828            hour: hour.to_rint(),
1829            minute: minute.to_rint(),
1830            second: second.to_rint(),
1831            subsec_nanosecond: subsec_nanosecond.to_rint(),
1832        }
1833    }
1834
1835    #[inline]
1836    pub(crate) const fn to_itime_const(&self) -> ITime {
1837        ITime {
1838            hour: self.hour.get_unchecked(),
1839            minute: self.minute.get_unchecked(),
1840            second: self.second.get_unchecked(),
1841            subsec_nanosecond: self.subsec_nanosecond.get_unchecked(),
1842        }
1843    }
1844}
1845
1846impl Default for Time {
1847    #[inline]
1848    fn default() -> Time {
1849        Time::midnight()
1850    }
1851}
1852
1853/// Converts a `Time` into a human readable time string.
1854///
1855/// (This `Debug` representation currently emits the same string as the
1856/// `Display` representation, but this is not a guarantee.)
1857///
1858/// Options currently supported:
1859///
1860/// * [`std::fmt::Formatter::precision`] can be set to control the precision
1861/// of the fractional second component.
1862///
1863/// # Example
1864///
1865/// ```
1866/// use jiff::civil::time;
1867///
1868/// let t = time(7, 0, 0, 123_000_000);
1869/// assert_eq!(format!("{t:.6?}"), "07:00:00.123000");
1870/// // Precision values greater than 9 are clamped to 9.
1871/// assert_eq!(format!("{t:.300?}"), "07:00:00.123000000");
1872/// // A precision of 0 implies the entire fractional
1873/// // component is always truncated.
1874/// assert_eq!(format!("{t:.0?}"), "07:00:00");
1875///
1876/// # Ok::<(), Box<dyn std::error::Error>>(())
1877/// ```
1878impl core::fmt::Debug for Time {
1879    #[inline]
1880    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1881        core::fmt::Display::fmt(self, f)
1882    }
1883}
1884
1885/// Converts a `Time` into an ISO 8601 compliant string.
1886///
1887/// # Formatting options supported
1888///
1889/// * [`std::fmt::Formatter::precision`] can be set to control the precision
1890/// of the fractional second component. When not set, the minimum precision
1891/// required to losslessly render the value is used.
1892///
1893/// # Example
1894///
1895/// ```
1896/// use jiff::civil::time;
1897///
1898/// // No fractional seconds:
1899/// let t = time(7, 0, 0, 0);
1900/// assert_eq!(format!("{t}"), "07:00:00");
1901///
1902/// // With fractional seconds:
1903/// let t = time(7, 0, 0, 123_000_000);
1904/// assert_eq!(format!("{t}"), "07:00:00.123");
1905///
1906/// # Ok::<(), Box<dyn std::error::Error>>(())
1907/// ```
1908///
1909/// # Example: setting the precision
1910///
1911/// ```
1912/// use jiff::civil::time;
1913///
1914/// let t = time(7, 0, 0, 123_000_000);
1915/// assert_eq!(format!("{t:.6}"), "07:00:00.123000");
1916/// // Precision values greater than 9 are clamped to 9.
1917/// assert_eq!(format!("{t:.300}"), "07:00:00.123000000");
1918/// // A precision of 0 implies the entire fractional
1919/// // component is always truncated.
1920/// assert_eq!(format!("{t:.0}"), "07:00:00");
1921///
1922/// # Ok::<(), Box<dyn std::error::Error>>(())
1923/// ```
1924impl core::fmt::Display for Time {
1925    #[inline]
1926    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1927        use crate::fmt::StdFmtWrite;
1928
1929        let precision =
1930            f.precision().map(|p| u8::try_from(p).unwrap_or(u8::MAX));
1931        temporal::DateTimePrinter::new()
1932            .precision(precision)
1933            .print_time(self, StdFmtWrite(f))
1934            .map_err(|_| core::fmt::Error)
1935    }
1936}
1937
1938impl core::str::FromStr for Time {
1939    type Err = Error;
1940
1941    #[inline]
1942    fn from_str(string: &str) -> Result<Time, Error> {
1943        DEFAULT_DATETIME_PARSER.parse_time(string)
1944    }
1945}
1946
1947/// Adds a span of time. This uses wrapping arithmetic.
1948///
1949/// For checked arithmetic, see [`Time::checked_add`].
1950impl core::ops::Add<Span> for Time {
1951    type Output = Time;
1952
1953    #[inline]
1954    fn add(self, rhs: Span) -> Time {
1955        self.wrapping_add(rhs)
1956    }
1957}
1958
1959/// Adds a span of time in place. This uses wrapping arithmetic.
1960///
1961/// For checked arithmetic, see [`Time::checked_add`].
1962impl core::ops::AddAssign<Span> for Time {
1963    #[inline]
1964    fn add_assign(&mut self, rhs: Span) {
1965        *self = *self + rhs;
1966    }
1967}
1968
1969/// Subtracts a span of time. This uses wrapping arithmetic.
1970///
1971/// For checked arithmetic, see [`Time::checked_sub`].
1972impl core::ops::Sub<Span> for Time {
1973    type Output = Time;
1974
1975    #[inline]
1976    fn sub(self, rhs: Span) -> Time {
1977        self.wrapping_sub(rhs)
1978    }
1979}
1980
1981/// Subtracts a span of time in place. This uses wrapping arithmetic.
1982///
1983/// For checked arithmetic, see [`Time::checked_sub`].
1984impl core::ops::SubAssign<Span> for Time {
1985    #[inline]
1986    fn sub_assign(&mut self, rhs: Span) {
1987        *self = *self - rhs;
1988    }
1989}
1990
1991/// Computes the span of time between two times.
1992///
1993/// This will return a negative span when the time being subtracted is greater.
1994///
1995/// Since this uses the default configuration for calculating a span between
1996/// two times (no rounding and largest units is hours), this will never panic
1997/// or fail in any way.
1998///
1999/// To configure the largest unit or enable rounding, use [`Time::since`].
2000impl core::ops::Sub for Time {
2001    type Output = Span;
2002
2003    #[inline]
2004    fn sub(self, rhs: Time) -> Span {
2005        self.since(rhs).expect("since never fails when given Time")
2006    }
2007}
2008
2009/// Adds a signed duration of time. This uses wrapping arithmetic.
2010///
2011/// For checked arithmetic, see [`Time::checked_add`].
2012impl core::ops::Add<SignedDuration> for Time {
2013    type Output = Time;
2014
2015    #[inline]
2016    fn add(self, rhs: SignedDuration) -> Time {
2017        self.wrapping_add(rhs)
2018    }
2019}
2020
2021/// Adds a signed duration of time in place. This uses wrapping arithmetic.
2022///
2023/// For checked arithmetic, see [`Time::checked_add`].
2024impl core::ops::AddAssign<SignedDuration> for Time {
2025    #[inline]
2026    fn add_assign(&mut self, rhs: SignedDuration) {
2027        *self = *self + rhs;
2028    }
2029}
2030
2031/// Subtracts a signed duration of time. This uses wrapping arithmetic.
2032///
2033/// For checked arithmetic, see [`Time::checked_sub`].
2034impl core::ops::Sub<SignedDuration> for Time {
2035    type Output = Time;
2036
2037    #[inline]
2038    fn sub(self, rhs: SignedDuration) -> Time {
2039        self.wrapping_sub(rhs)
2040    }
2041}
2042
2043/// Subtracts a signed duration of time in place. This uses wrapping arithmetic.
2044///
2045/// For checked arithmetic, see [`Time::checked_sub`].
2046impl core::ops::SubAssign<SignedDuration> for Time {
2047    #[inline]
2048    fn sub_assign(&mut self, rhs: SignedDuration) {
2049        *self = *self - rhs;
2050    }
2051}
2052
2053/// Adds an unsigned duration of time. This uses wrapping arithmetic.
2054///
2055/// For checked arithmetic, see [`Time::checked_add`].
2056impl core::ops::Add<UnsignedDuration> for Time {
2057    type Output = Time;
2058
2059    #[inline]
2060    fn add(self, rhs: UnsignedDuration) -> Time {
2061        self.wrapping_add(rhs)
2062    }
2063}
2064
2065/// Adds an unsigned duration of time in place. This uses wrapping arithmetic.
2066///
2067/// For checked arithmetic, see [`Time::checked_add`].
2068impl core::ops::AddAssign<UnsignedDuration> for Time {
2069    #[inline]
2070    fn add_assign(&mut self, rhs: UnsignedDuration) {
2071        *self = *self + rhs;
2072    }
2073}
2074
2075/// Subtracts an unsigned duration of time. This uses wrapping arithmetic.
2076///
2077/// For checked arithmetic, see [`Time::checked_sub`].
2078impl core::ops::Sub<UnsignedDuration> for Time {
2079    type Output = Time;
2080
2081    #[inline]
2082    fn sub(self, rhs: UnsignedDuration) -> Time {
2083        self.wrapping_sub(rhs)
2084    }
2085}
2086
2087/// Subtracts an unsigned duration of time in place. This uses wrapping
2088/// arithmetic.
2089///
2090/// For checked arithmetic, see [`Time::checked_sub`].
2091impl core::ops::SubAssign<UnsignedDuration> for Time {
2092    #[inline]
2093    fn sub_assign(&mut self, rhs: UnsignedDuration) {
2094        *self = *self - rhs;
2095    }
2096}
2097
2098impl From<DateTime> for Time {
2099    #[inline]
2100    fn from(dt: DateTime) -> Time {
2101        dt.time()
2102    }
2103}
2104
2105impl From<Zoned> for Time {
2106    #[inline]
2107    fn from(zdt: Zoned) -> Time {
2108        zdt.datetime().time()
2109    }
2110}
2111
2112impl<'a> From<&'a Zoned> for Time {
2113    #[inline]
2114    fn from(zdt: &'a Zoned) -> Time {
2115        zdt.datetime().time()
2116    }
2117}
2118
2119#[cfg(feature = "serde")]
2120impl serde_core::Serialize for Time {
2121    #[inline]
2122    fn serialize<S: serde_core::Serializer>(
2123        &self,
2124        serializer: S,
2125    ) -> Result<S::Ok, S::Error> {
2126        serializer.collect_str(self)
2127    }
2128}
2129
2130#[cfg(feature = "serde")]
2131impl<'de> serde_core::Deserialize<'de> for Time {
2132    #[inline]
2133    fn deserialize<D: serde_core::Deserializer<'de>>(
2134        deserializer: D,
2135    ) -> Result<Time, D::Error> {
2136        use serde_core::de;
2137
2138        struct TimeVisitor;
2139
2140        impl<'de> de::Visitor<'de> for TimeVisitor {
2141            type Value = Time;
2142
2143            fn expecting(
2144                &self,
2145                f: &mut core::fmt::Formatter,
2146            ) -> core::fmt::Result {
2147                f.write_str("a time string")
2148            }
2149
2150            #[inline]
2151            fn visit_bytes<E: de::Error>(
2152                self,
2153                value: &[u8],
2154            ) -> Result<Time, E> {
2155                DEFAULT_DATETIME_PARSER
2156                    .parse_time(value)
2157                    .map_err(de::Error::custom)
2158            }
2159
2160            #[inline]
2161            fn visit_str<E: de::Error>(self, value: &str) -> Result<Time, E> {
2162                self.visit_bytes(value.as_bytes())
2163            }
2164        }
2165
2166        deserializer.deserialize_str(TimeVisitor)
2167    }
2168}
2169
2170#[cfg(test)]
2171impl quickcheck::Arbitrary for Time {
2172    fn arbitrary(g: &mut quickcheck::Gen) -> Time {
2173        let hour = Hour::arbitrary(g);
2174        let minute = Minute::arbitrary(g);
2175        let second = Second::arbitrary(g);
2176        let subsec_nanosecond = SubsecNanosecond::arbitrary(g);
2177        Time::new_ranged(hour, minute, second, subsec_nanosecond)
2178    }
2179
2180    fn shrink(&self) -> alloc::boxed::Box<dyn Iterator<Item = Time>> {
2181        alloc::boxed::Box::new(
2182            (
2183                self.hour_ranged(),
2184                self.minute_ranged(),
2185                self.second_ranged(),
2186                self.subsec_nanosecond_ranged(),
2187            )
2188                .shrink()
2189                .map(
2190                    |(hour, minute, second, subsec_nanosecond)| {
2191                        Time::new_ranged(
2192                            hour,
2193                            minute,
2194                            second,
2195                            subsec_nanosecond,
2196                        )
2197                    },
2198                ),
2199        )
2200    }
2201}
2202
2203/// An iterator over periodic times, created by [`Time::series`].
2204///
2205/// It is exhausted when the next value would exceed the limits of a [`Span`]
2206/// or [`Time`] value.
2207///
2208/// This iterator is created by [`Time::series`].
2209#[derive(Clone, Debug)]
2210pub struct TimeSeries {
2211    start: Time,
2212    period: Span,
2213    step: i64,
2214}
2215
2216impl Iterator for TimeSeries {
2217    type Item = Time;
2218
2219    #[inline]
2220    fn next(&mut self) -> Option<Time> {
2221        let span = self.period.checked_mul(self.step).ok()?;
2222        self.step = self.step.checked_add(1)?;
2223        let time = self.start.checked_add(span).ok()?;
2224        Some(time)
2225    }
2226}
2227
2228impl core::iter::FusedIterator for TimeSeries {}
2229
2230/// Options for [`Time::checked_add`] and [`Time::checked_sub`].
2231///
2232/// This type provides a way to ergonomically add one of a few different
2233/// duration types to a [`Time`].
2234///
2235/// The main way to construct values of this type is with its `From` trait
2236/// implementations:
2237///
2238/// * `From<Span> for TimeArithmetic` adds (or subtracts) the given span to the
2239/// receiver time.
2240/// * `From<SignedDuration> for TimeArithmetic` adds (or subtracts)
2241/// the given signed duration to the receiver time.
2242/// * `From<std::time::Duration> for TimeArithmetic` adds (or subtracts)
2243/// the given unsigned duration to the receiver time.
2244///
2245/// # Example
2246///
2247/// ```
2248/// use std::time::Duration;
2249///
2250/// use jiff::{civil::time, SignedDuration, ToSpan};
2251///
2252/// let t = time(0, 0, 0, 0);
2253/// assert_eq!(t.checked_add(2.hours())?, time(2, 0, 0, 0));
2254/// assert_eq!(t.checked_add(SignedDuration::from_hours(2))?, time(2, 0, 0, 0));
2255/// assert_eq!(t.checked_add(Duration::from_secs(2 * 60 * 60))?, time(2, 0, 0, 0));
2256///
2257/// # Ok::<(), Box<dyn std::error::Error>>(())
2258/// ```
2259#[derive(Clone, Copy, Debug)]
2260pub struct TimeArithmetic {
2261    duration: Duration,
2262}
2263
2264impl TimeArithmetic {
2265    #[inline]
2266    fn wrapping_add(self, time: Time) -> Time {
2267        match self.duration {
2268            Duration::Span(span) => time.wrapping_add_span(span),
2269            Duration::Signed(sdur) => time.wrapping_add_signed_duration(sdur),
2270            Duration::Unsigned(udur) => {
2271                time.wrapping_add_unsigned_duration(udur)
2272            }
2273        }
2274    }
2275
2276    #[inline]
2277    fn wrapping_sub(self, time: Time) -> Time {
2278        match self.duration {
2279            Duration::Span(span) => time.wrapping_add_span(span.negate()),
2280            Duration::Signed(sdur) => {
2281                if let Some(sdur) = sdur.checked_neg() {
2282                    time.wrapping_add_signed_duration(sdur)
2283                } else {
2284                    let udur = UnsignedDuration::new(
2285                        i64::MIN.unsigned_abs(),
2286                        sdur.subsec_nanos().unsigned_abs(),
2287                    );
2288                    time.wrapping_add_unsigned_duration(udur)
2289                }
2290            }
2291            Duration::Unsigned(udur) => {
2292                time.wrapping_sub_unsigned_duration(udur)
2293            }
2294        }
2295    }
2296
2297    #[inline]
2298    fn checked_add(self, time: Time) -> Result<Time, Error> {
2299        match self.duration.to_signed()? {
2300            SDuration::Span(span) => time.checked_add_span(span),
2301            SDuration::Absolute(sdur) => time.checked_add_duration(sdur),
2302        }
2303    }
2304
2305    #[inline]
2306    fn checked_neg(self) -> Result<TimeArithmetic, Error> {
2307        let duration = self.duration.checked_neg()?;
2308        Ok(TimeArithmetic { duration })
2309    }
2310
2311    #[inline]
2312    fn is_negative(&self) -> bool {
2313        self.duration.is_negative()
2314    }
2315}
2316
2317impl From<Span> for TimeArithmetic {
2318    fn from(span: Span) -> TimeArithmetic {
2319        let duration = Duration::from(span);
2320        TimeArithmetic { duration }
2321    }
2322}
2323
2324impl From<SignedDuration> for TimeArithmetic {
2325    fn from(sdur: SignedDuration) -> TimeArithmetic {
2326        let duration = Duration::from(sdur);
2327        TimeArithmetic { duration }
2328    }
2329}
2330
2331impl From<UnsignedDuration> for TimeArithmetic {
2332    fn from(udur: UnsignedDuration) -> TimeArithmetic {
2333        let duration = Duration::from(udur);
2334        TimeArithmetic { duration }
2335    }
2336}
2337
2338impl<'a> From<&'a Span> for TimeArithmetic {
2339    fn from(span: &'a Span) -> TimeArithmetic {
2340        TimeArithmetic::from(*span)
2341    }
2342}
2343
2344impl<'a> From<&'a SignedDuration> for TimeArithmetic {
2345    fn from(sdur: &'a SignedDuration) -> TimeArithmetic {
2346        TimeArithmetic::from(*sdur)
2347    }
2348}
2349
2350impl<'a> From<&'a UnsignedDuration> for TimeArithmetic {
2351    fn from(udur: &'a UnsignedDuration) -> TimeArithmetic {
2352        TimeArithmetic::from(*udur)
2353    }
2354}
2355
2356/// Options for [`Time::since`] and [`Time::until`].
2357///
2358/// This type provides a way to configure the calculation of spans between two
2359/// [`Time`] values. In particular, both `Time::since` and `Time::until` accept
2360/// anything that implements `Into<TimeDifference>`. There are a few key trait
2361/// implementations that make this convenient:
2362///
2363/// * `From<Time> for TimeDifference` will construct a configuration consisting
2364/// of just the time. So for example, `time1.until(time2)` will return the span
2365/// from `time1` to `time2`.
2366/// * `From<DateTime> for TimeDifference` will construct a configuration
2367/// consisting of just the time from the given datetime. So for example,
2368/// `time.since(datetime)` returns the span from `datetime.time()` to `time`.
2369/// * `From<(Unit, Time)>` is a convenient way to specify the largest units
2370/// that should be present on the span returned. By default, the largest units
2371/// are hours. Using this trait implementation is equivalent to
2372/// `TimeDifference::new(time).largest(unit)`.
2373/// * `From<(Unit, DateTime)>` is like the one above, but with the time from
2374/// the given datetime.
2375///
2376/// One can also provide a `TimeDifference` value directly. Doing so
2377/// is necessary to use the rounding features of calculating a span. For
2378/// example, setting the smallest unit (defaults to [`Unit::Nanosecond`]), the
2379/// rounding mode (defaults to [`RoundMode::Trunc`]) and the rounding increment
2380/// (defaults to `1`). The defaults are selected such that no rounding occurs.
2381///
2382/// Rounding a span as part of calculating it is provided as a convenience.
2383/// Callers may choose to round the span as a distinct step via
2384/// [`Span::round`].
2385///
2386/// # Example
2387///
2388/// This example shows how to round a span between two datetimes to the nearest
2389/// half-hour, with ties breaking away from zero.
2390///
2391/// ```
2392/// use jiff::{civil::{Time, TimeDifference}, RoundMode, ToSpan, Unit};
2393///
2394/// let t1 = "08:14:00.123456789".parse::<Time>()?;
2395/// let t2 = "15:00".parse::<Time>()?;
2396/// let span = t1.until(
2397///     TimeDifference::new(t2)
2398///         .smallest(Unit::Minute)
2399///         .mode(RoundMode::HalfExpand)
2400///         .increment(30),
2401/// )?;
2402/// assert_eq!(span, 7.hours().fieldwise());
2403///
2404/// // One less minute, and because of the HalfExpand mode, the span would
2405/// // get rounded down.
2406/// let t2 = "14:59".parse::<Time>()?;
2407/// let span = t1.until(
2408///     TimeDifference::new(t2)
2409///         .smallest(Unit::Minute)
2410///         .mode(RoundMode::HalfExpand)
2411///         .increment(30),
2412/// )?;
2413/// assert_eq!(span, 6.hours().minutes(30).fieldwise());
2414///
2415/// # Ok::<(), Box<dyn std::error::Error>>(())
2416/// ```
2417#[derive(Clone, Copy, Debug)]
2418pub struct TimeDifference {
2419    time: Time,
2420    round: SpanRound<'static>,
2421}
2422
2423impl TimeDifference {
2424    /// Create a new default configuration for computing the span between
2425    /// the given time and some other time (specified as the receiver in
2426    /// [`Time::since`] or [`Time::until`]).
2427    #[inline]
2428    pub fn new(time: Time) -> TimeDifference {
2429        // We use truncation rounding by default since it seems that's
2430        // what is generally expected when computing the difference between
2431        // datetimes.
2432        //
2433        // See: https://github.com/tc39/proposal-temporal/issues/1122
2434        let round = SpanRound::new().mode(RoundMode::Trunc);
2435        TimeDifference { time, round }
2436    }
2437
2438    /// Set the smallest units allowed in the span returned.
2439    ///
2440    /// # Errors
2441    ///
2442    /// The smallest units must be no greater than the largest units. If this
2443    /// is violated, then computing a span with this configuration will result
2444    /// in an error.
2445    ///
2446    /// # Example
2447    ///
2448    /// This shows how to round a span between two times to units no less than
2449    /// seconds.
2450    ///
2451    /// ```
2452    /// use jiff::{civil::{Time, TimeDifference}, RoundMode, ToSpan, Unit};
2453    ///
2454    /// let t1 = "08:14:02.5001".parse::<Time>()?;
2455    /// let t2 = "08:30:03.0001".parse::<Time>()?;
2456    /// let span = t1.until(
2457    ///     TimeDifference::new(t2)
2458    ///         .smallest(Unit::Second)
2459    ///         .mode(RoundMode::HalfExpand),
2460    /// )?;
2461    /// assert_eq!(span, 16.minutes().seconds(1).fieldwise());
2462    ///
2463    /// # Ok::<(), Box<dyn std::error::Error>>(())
2464    /// ```
2465    #[inline]
2466    pub fn smallest(self, unit: Unit) -> TimeDifference {
2467        TimeDifference { round: self.round.smallest(unit), ..self }
2468    }
2469
2470    /// Set the largest units allowed in the span returned.
2471    ///
2472    /// When a largest unit is not specified, computing a span between times
2473    /// behaves as if it were set to [`Unit::Hour`].
2474    ///
2475    /// # Errors
2476    ///
2477    /// The largest units, when set, must be at least as big as the smallest
2478    /// units (which defaults to [`Unit::Nanosecond`]). If this is violated,
2479    /// then computing a span with this configuration will result in an error.
2480    ///
2481    /// # Example
2482    ///
2483    /// This shows how to round a span between two times to units no
2484    /// bigger than seconds.
2485    ///
2486    /// ```
2487    /// use jiff::{civil::{Time, TimeDifference}, ToSpan, Unit};
2488    ///
2489    /// let t1 = "08:14".parse::<Time>()?;
2490    /// let t2 = "08:30".parse::<Time>()?;
2491    /// let span = t1.until(TimeDifference::new(t2).largest(Unit::Second))?;
2492    /// assert_eq!(span, 960.seconds().fieldwise());
2493    ///
2494    /// # Ok::<(), Box<dyn std::error::Error>>(())
2495    /// ```
2496    #[inline]
2497    pub fn largest(self, unit: Unit) -> TimeDifference {
2498        TimeDifference { round: self.round.largest(unit), ..self }
2499    }
2500
2501    /// Set the rounding mode.
2502    ///
2503    /// This defaults to [`RoundMode::Trunc`] since it's plausible that
2504    /// rounding "up" in the context of computing the span between two times
2505    /// could be surprising in a number of cases. The [`RoundMode::HalfExpand`]
2506    /// mode corresponds to typical rounding you might have learned about in
2507    /// school. But a variety of other rounding modes exist.
2508    ///
2509    /// # Example
2510    ///
2511    /// This shows how to always round "up" towards positive infinity.
2512    ///
2513    /// ```
2514    /// use jiff::{civil::{Time, TimeDifference}, RoundMode, ToSpan, Unit};
2515    ///
2516    /// let t1 = "08:10".parse::<Time>()?;
2517    /// let t2 = "08:11".parse::<Time>()?;
2518    /// let span = t1.until(
2519    ///     TimeDifference::new(t2)
2520    ///         .smallest(Unit::Hour)
2521    ///         .mode(RoundMode::Ceil),
2522    /// )?;
2523    /// // Only one minute elapsed, but we asked to always round up!
2524    /// assert_eq!(span, 1.hour().fieldwise());
2525    ///
2526    /// // Since `Ceil` always rounds toward positive infinity, the behavior
2527    /// // flips for a negative span.
2528    /// let span = t1.since(
2529    ///     TimeDifference::new(t2)
2530    ///         .smallest(Unit::Hour)
2531    ///         .mode(RoundMode::Ceil),
2532    /// )?;
2533    /// assert_eq!(span, 0.hour().fieldwise());
2534    ///
2535    /// # Ok::<(), Box<dyn std::error::Error>>(())
2536    /// ```
2537    #[inline]
2538    pub fn mode(self, mode: RoundMode) -> TimeDifference {
2539        TimeDifference { round: self.round.mode(mode), ..self }
2540    }
2541
2542    /// Set the rounding increment for the smallest unit.
2543    ///
2544    /// The default value is `1`. Other values permit rounding the smallest
2545    /// unit to the nearest integer increment specified. For example, if the
2546    /// smallest unit is set to [`Unit::Minute`], then a rounding increment of
2547    /// `30` would result in rounding in increments of a half hour. That is,
2548    /// the only minute value that could result would be `0` or `30`.
2549    ///
2550    /// # Errors
2551    ///
2552    /// The rounding increment must divide evenly into the next highest unit
2553    /// after the smallest unit configured (and must not be equivalent to it).
2554    /// For example, if the smallest unit is [`Unit::Nanosecond`], then *some*
2555    /// of the valid values for the rounding increment are `1`, `2`, `4`, `5`,
2556    /// `100` and `500`. Namely, any integer that divides evenly into `1,000`
2557    /// nanoseconds since there are `1,000` nanoseconds in the next highest
2558    /// unit (microseconds).
2559    ///
2560    /// The error will occur when computing the span, and not when setting
2561    /// the increment here.
2562    ///
2563    /// # Example
2564    ///
2565    /// This shows how to round the span between two times to the nearest 5
2566    /// minute increment.
2567    ///
2568    /// ```
2569    /// use jiff::{civil::{Time, TimeDifference}, RoundMode, ToSpan, Unit};
2570    ///
2571    /// let t1 = "08:19".parse::<Time>()?;
2572    /// let t2 = "12:52".parse::<Time>()?;
2573    /// let span = t1.until(
2574    ///     TimeDifference::new(t2)
2575    ///         .smallest(Unit::Minute)
2576    ///         .increment(5)
2577    ///         .mode(RoundMode::HalfExpand),
2578    /// )?;
2579    /// assert_eq!(span, 4.hour().minutes(35).fieldwise());
2580    ///
2581    /// # Ok::<(), Box<dyn std::error::Error>>(())
2582    /// ```
2583    #[inline]
2584    pub fn increment(self, increment: i64) -> TimeDifference {
2585        TimeDifference { round: self.round.increment(increment), ..self }
2586    }
2587
2588    /// Returns true if and only if this configuration could change the span
2589    /// via rounding.
2590    #[inline]
2591    fn rounding_may_change_span(&self) -> bool {
2592        self.round.rounding_may_change_span_ignore_largest()
2593    }
2594
2595    /// Returns the span of time from `t1` to the time in this configuration.
2596    /// The biggest units allowed are determined by the `smallest` and
2597    /// `largest` settings, but defaults to `Unit::Hour`.
2598    #[inline]
2599    fn until_with_largest_unit(&self, t1: Time) -> Result<Span, Error> {
2600        let t2 = self.time;
2601        if t1 == t2 {
2602            return Ok(Span::new());
2603        }
2604        let largest = self.round.get_largest().unwrap_or(Unit::Hour);
2605        if largest > Unit::Hour {
2606            return Err(err!(
2607                "rounding the span between two times must use hours \
2608                 or smaller for its units, but found {units}",
2609                units = largest.plural(),
2610            ));
2611        }
2612        let start = t1.to_nanosecond();
2613        let end = t2.to_nanosecond();
2614        let span =
2615            Span::from_invariant_nanoseconds(largest, (end - start).rinto())
2616                .expect("difference in civil times is always in bounds");
2617        Ok(span)
2618    }
2619}
2620
2621impl From<Time> for TimeDifference {
2622    #[inline]
2623    fn from(time: Time) -> TimeDifference {
2624        TimeDifference::new(time)
2625    }
2626}
2627
2628impl From<DateTime> for TimeDifference {
2629    #[inline]
2630    fn from(dt: DateTime) -> TimeDifference {
2631        TimeDifference::from(Time::from(dt))
2632    }
2633}
2634
2635impl From<Zoned> for TimeDifference {
2636    #[inline]
2637    fn from(zdt: Zoned) -> TimeDifference {
2638        TimeDifference::from(Time::from(zdt))
2639    }
2640}
2641
2642impl<'a> From<&'a Zoned> for TimeDifference {
2643    #[inline]
2644    fn from(zdt: &'a Zoned) -> TimeDifference {
2645        TimeDifference::from(zdt.datetime())
2646    }
2647}
2648
2649impl From<(Unit, Time)> for TimeDifference {
2650    #[inline]
2651    fn from((largest, time): (Unit, Time)) -> TimeDifference {
2652        TimeDifference::from(time).largest(largest)
2653    }
2654}
2655
2656impl From<(Unit, DateTime)> for TimeDifference {
2657    #[inline]
2658    fn from((largest, dt): (Unit, DateTime)) -> TimeDifference {
2659        TimeDifference::from((largest, Time::from(dt)))
2660    }
2661}
2662
2663impl From<(Unit, Zoned)> for TimeDifference {
2664    #[inline]
2665    fn from((largest, zdt): (Unit, Zoned)) -> TimeDifference {
2666        TimeDifference::from((largest, Time::from(zdt)))
2667    }
2668}
2669
2670impl<'a> From<(Unit, &'a Zoned)> for TimeDifference {
2671    #[inline]
2672    fn from((largest, zdt): (Unit, &'a Zoned)) -> TimeDifference {
2673        TimeDifference::from((largest, zdt.datetime()))
2674    }
2675}
2676
2677/// Options for [`Time::round`].
2678///
2679/// This type provides a way to configure the rounding of a civil time.
2680/// In particular, `Time::round` accepts anything that implements the
2681/// `Into<TimeRound>` trait. There are some trait implementations that
2682/// therefore make calling `Time::round` in some common cases more ergonomic:
2683///
2684/// * `From<Unit> for TimeRound` will construct a rounding configuration that
2685/// rounds to the unit given. Specifically, `TimeRound::new().smallest(unit)`.
2686/// * `From<(Unit, i64)> for TimeRound` is like the one above, but also
2687/// specifies the rounding increment for [`TimeRound::increment`].
2688///
2689/// Note that in the default configuration, no rounding occurs.
2690///
2691/// # Example
2692///
2693/// This example shows how to round a time to the nearest second:
2694///
2695/// ```
2696/// use jiff::{civil::{Time, time}, Unit};
2697///
2698/// let t: Time = "16:24:59.5".parse()?;
2699/// assert_eq!(
2700///     t.round(Unit::Second)?,
2701///     // The second rounds up and causes minutes to increase.
2702///     time(16, 25, 0, 0),
2703/// );
2704///
2705/// # Ok::<(), Box<dyn std::error::Error>>(())
2706/// ```
2707///
2708/// The above makes use of the fact that `Unit` implements
2709/// `Into<TimeRound>`. If you want to change the rounding mode to, say,
2710/// truncation, then you'll need to construct a `TimeRound` explicitly
2711/// since there are no convenience `Into` trait implementations for
2712/// [`RoundMode`].
2713///
2714/// ```
2715/// use jiff::{civil::{Time, TimeRound, time}, RoundMode, Unit};
2716///
2717/// let t: Time = "2024-06-20 16:24:59.5".parse()?;
2718/// assert_eq!(
2719///     t.round(
2720///         TimeRound::new().smallest(Unit::Second).mode(RoundMode::Trunc),
2721///     )?,
2722///     // The second just gets truncated as if it wasn't there.
2723///     time(16, 24, 59, 0),
2724/// );
2725///
2726/// # Ok::<(), Box<dyn std::error::Error>>(())
2727/// ```
2728#[derive(Clone, Copy, Debug)]
2729pub struct TimeRound {
2730    smallest: Unit,
2731    mode: RoundMode,
2732    increment: i64,
2733}
2734
2735impl TimeRound {
2736    /// Create a new default configuration for rounding a [`Time`].
2737    #[inline]
2738    pub fn new() -> TimeRound {
2739        TimeRound {
2740            smallest: Unit::Nanosecond,
2741            mode: RoundMode::HalfExpand,
2742            increment: 1,
2743        }
2744    }
2745
2746    /// Set the smallest units allowed in the time returned after rounding.
2747    ///
2748    /// Any units below the smallest configured unit will be used, along with
2749    /// the rounding increment and rounding mode, to determine the value of the
2750    /// smallest unit. For example, when rounding `03:25:30` to the
2751    /// nearest minute, the `30` second unit will result in rounding the minute
2752    /// unit of `25` up to `26` and zeroing out everything below minutes.
2753    ///
2754    /// This defaults to [`Unit::Nanosecond`].
2755    ///
2756    /// # Errors
2757    ///
2758    /// The smallest units must be no greater than [`Unit::Hour`].
2759    ///
2760    /// # Example
2761    ///
2762    /// ```
2763    /// use jiff::{civil::{TimeRound, time}, Unit};
2764    ///
2765    /// let t = time(3, 25, 30, 0);
2766    /// assert_eq!(
2767    ///     t.round(TimeRound::new().smallest(Unit::Minute))?,
2768    ///     time(3, 26, 0, 0),
2769    /// );
2770    /// // Or, utilize the `From<Unit> for TimeRound` impl:
2771    /// assert_eq!(t.round(Unit::Minute)?, time(3, 26, 0, 0));
2772    ///
2773    /// # Ok::<(), Box<dyn std::error::Error>>(())
2774    /// ```
2775    #[inline]
2776    pub fn smallest(self, unit: Unit) -> TimeRound {
2777        TimeRound { smallest: unit, ..self }
2778    }
2779
2780    /// Set the rounding mode.
2781    ///
2782    /// This defaults to [`RoundMode::HalfExpand`], which rounds away from
2783    /// zero. It matches the kind of rounding you might have been taught in
2784    /// school.
2785    ///
2786    /// # Example
2787    ///
2788    /// This shows how to always round times up towards positive infinity.
2789    ///
2790    /// ```
2791    /// use jiff::{civil::{Time, TimeRound, time}, RoundMode, Unit};
2792    ///
2793    /// let t: Time = "03:25:01".parse()?;
2794    /// assert_eq!(
2795    ///     t.round(
2796    ///         TimeRound::new()
2797    ///             .smallest(Unit::Minute)
2798    ///             .mode(RoundMode::Ceil),
2799    ///     )?,
2800    ///     time(3, 26, 0, 0),
2801    /// );
2802    ///
2803    /// # Ok::<(), Box<dyn std::error::Error>>(())
2804    /// ```
2805    #[inline]
2806    pub fn mode(self, mode: RoundMode) -> TimeRound {
2807        TimeRound { mode, ..self }
2808    }
2809
2810    /// Set the rounding increment for the smallest unit.
2811    ///
2812    /// The default value is `1`. Other values permit rounding the smallest
2813    /// unit to the nearest integer increment specified. For example, if the
2814    /// smallest unit is set to [`Unit::Minute`], then a rounding increment of
2815    /// `30` would result in rounding in increments of a half hour. That is,
2816    /// the only minute value that could result would be `0` or `30`.
2817    ///
2818    /// # Errors
2819    ///
2820    /// The rounding increment must divide evenly into the
2821    /// next highest unit above the smallest unit set. The rounding increment
2822    /// must also not be equal to the next highest unit. For example, if the
2823    /// smallest unit is [`Unit::Nanosecond`], then *some* of the valid values
2824    /// for the rounding increment are `1`, `2`, `4`, `5`, `100` and `500`.
2825    /// Namely, any integer that divides evenly into `1,000` nanoseconds since
2826    /// there are `1,000` nanoseconds in the next highest unit (microseconds).
2827    ///
2828    /// # Example
2829    ///
2830    /// This example shows how to round a time to the nearest 10 minute
2831    /// increment.
2832    ///
2833    /// ```
2834    /// use jiff::{civil::{Time, TimeRound, time}, RoundMode, Unit};
2835    ///
2836    /// let t: Time = "03:24:59".parse()?;
2837    /// assert_eq!(t.round((Unit::Minute, 10))?, time(3, 20, 0, 0));
2838    ///
2839    /// # Ok::<(), Box<dyn std::error::Error>>(())
2840    /// ```
2841    #[inline]
2842    pub fn increment(self, increment: i64) -> TimeRound {
2843        TimeRound { increment, ..self }
2844    }
2845
2846    /// Does the actual rounding.
2847    pub(crate) fn round(&self, t: Time) -> Result<Time, Error> {
2848        let increment = increment::for_time(self.smallest, self.increment)?;
2849        let nanos = t.to_nanosecond();
2850        let rounded = self.mode.round_by_unit_in_nanoseconds(
2851            nanos,
2852            self.smallest,
2853            increment,
2854        );
2855        let limit =
2856            t::NoUnits128::rfrom(t::CivilDayNanosecond::MAX_SELF) + C(1);
2857        Ok(Time::from_nanosecond((rounded % limit).rinto()))
2858    }
2859}
2860
2861impl Default for TimeRound {
2862    #[inline]
2863    fn default() -> TimeRound {
2864        TimeRound::new()
2865    }
2866}
2867
2868impl From<Unit> for TimeRound {
2869    #[inline]
2870    fn from(unit: Unit) -> TimeRound {
2871        TimeRound::default().smallest(unit)
2872    }
2873}
2874
2875impl From<(Unit, i64)> for TimeRound {
2876    #[inline]
2877    fn from((unit, increment): (Unit, i64)) -> TimeRound {
2878        TimeRound::from(unit).increment(increment)
2879    }
2880}
2881
2882/// A builder for setting the fields on a [`Time`].
2883///
2884/// This builder is constructed via [`Time::with`].
2885///
2886/// # Example
2887///
2888/// Unlike [`Date`], a [`Time`] is valid for all possible valid values of its
2889/// fields. That is, there is no way for two valid field values to combine
2890/// into an invalid `Time`. So, for `Time`, this builder does have as much of
2891/// a benefit versus an API design with methods like `Time::with_hour` and
2892/// `Time::with_minute`. Nevertheless, this builder permits settings multiple
2893/// fields at the same time and performing only one validity check. Moreover,
2894/// this provides a consistent API with other date and time types in this
2895/// crate.
2896///
2897/// ```
2898/// use jiff::civil::time;
2899///
2900/// let t1 = time(0, 0, 24, 0);
2901/// let t2 = t1.with().hour(15).minute(30).millisecond(10).build()?;
2902/// assert_eq!(t2, time(15, 30, 24, 10_000_000));
2903///
2904/// # Ok::<(), Box<dyn std::error::Error>>(())
2905/// ```
2906#[derive(Clone, Copy, Debug)]
2907pub struct TimeWith {
2908    original: Time,
2909    hour: Option<i8>,
2910    minute: Option<i8>,
2911    second: Option<i8>,
2912    millisecond: Option<i16>,
2913    microsecond: Option<i16>,
2914    nanosecond: Option<i16>,
2915    subsec_nanosecond: Option<i32>,
2916}
2917
2918impl TimeWith {
2919    #[inline]
2920    fn new(original: Time) -> TimeWith {
2921        TimeWith {
2922            original,
2923            hour: None,
2924            minute: None,
2925            second: None,
2926            millisecond: None,
2927            microsecond: None,
2928            nanosecond: None,
2929            subsec_nanosecond: None,
2930        }
2931    }
2932
2933    /// Create a new `Time` from the fields set on this configuration.
2934    ///
2935    /// An error occurs when the fields combine to an invalid time. This only
2936    /// occurs when at least one field has an invalid value, or if at least
2937    /// one of `millisecond`, `microsecond` or `nanosecond` is set _and_
2938    /// `subsec_nanosecond` is set. Otherwise, if all fields are valid, then
2939    /// the entire `Time` is guaranteed to be valid.
2940    ///
2941    /// For any fields not set on this configuration, the values are taken from
2942    /// the [`Time`] that originally created this configuration. When no values
2943    /// are set, this routine is guaranteed to succeed and will always return
2944    /// the original time without modification.
2945    ///
2946    /// # Example
2947    ///
2948    /// This creates a time but with its fractional nanosecond component
2949    /// stripped:
2950    ///
2951    /// ```
2952    /// use jiff::civil::time;
2953    ///
2954    /// let t = time(14, 27, 30, 123_456_789);
2955    /// assert_eq!(t.with().subsec_nanosecond(0).build()?, time(14, 27, 30, 0));
2956    ///
2957    /// # Ok::<(), Box<dyn std::error::Error>>(())
2958    /// ```
2959    ///
2960    /// # Example: error for invalid time
2961    ///
2962    /// ```
2963    /// use jiff::civil::time;
2964    ///
2965    /// let t = time(14, 27, 30, 0);
2966    /// assert!(t.with().hour(24).build().is_err());
2967    /// ```
2968    ///
2969    /// # Example: error for ambiguous sub-second value
2970    ///
2971    /// ```
2972    /// use jiff::civil::time;
2973    ///
2974    /// let t = time(14, 27, 30, 123_456_789);
2975    /// // Setting both the individual sub-second fields and the entire
2976    /// // fractional component could lead to a misleading configuration. So
2977    /// // if it's done, it results in an error in all cases. Callers must
2978    /// // choose one or the other.
2979    /// assert!(t.with().microsecond(1).subsec_nanosecond(0).build().is_err());
2980    /// ```
2981    #[inline]
2982    pub fn build(self) -> Result<Time, Error> {
2983        let hour = match self.hour {
2984            None => self.original.hour_ranged(),
2985            Some(hour) => Hour::try_new("hour", hour)?,
2986        };
2987        let minute = match self.minute {
2988            None => self.original.minute_ranged(),
2989            Some(minute) => Minute::try_new("minute", minute)?,
2990        };
2991        let second = match self.second {
2992            None => self.original.second_ranged(),
2993            Some(second) => Second::try_new("second", second)?,
2994        };
2995        let millisecond = match self.millisecond {
2996            None => self.original.millisecond_ranged(),
2997            Some(millisecond) => {
2998                Millisecond::try_new("millisecond", millisecond)?
2999            }
3000        };
3001        let microsecond = match self.microsecond {
3002            None => self.original.microsecond_ranged(),
3003            Some(microsecond) => {
3004                Microsecond::try_new("microsecond", microsecond)?
3005            }
3006        };
3007        let nanosecond = match self.nanosecond {
3008            None => self.original.nanosecond_ranged(),
3009            Some(nanosecond) => Nanosecond::try_new("nanosecond", nanosecond)?,
3010        };
3011        let subsec_nanosecond = match self.subsec_nanosecond {
3012            None => self.original.subsec_nanosecond_ranged(),
3013            Some(subsec_nanosecond) => {
3014                if self.millisecond.is_some() {
3015                    return Err(err!(
3016                        "cannot set both TimeWith::millisecond \
3017                         and TimeWith::subsec_nanosecond",
3018                    ));
3019                }
3020                if self.microsecond.is_some() {
3021                    return Err(err!(
3022                        "cannot set both TimeWith::microsecond \
3023                         and TimeWith::subsec_nanosecond",
3024                    ));
3025                }
3026                if self.nanosecond.is_some() {
3027                    return Err(err!(
3028                        "cannot set both TimeWith::nanosecond \
3029                         and TimeWith::subsec_nanosecond",
3030                    ));
3031                }
3032                SubsecNanosecond::try_new(
3033                    "subsec_nanosecond",
3034                    subsec_nanosecond,
3035                )?
3036            }
3037        };
3038        if self.subsec_nanosecond.is_some() {
3039            Ok(Time::new_ranged(hour, minute, second, subsec_nanosecond))
3040        } else {
3041            Ok(Time::new_ranged(hour, minute, second, C(0))
3042                .with_subsec_parts_ranged(
3043                    millisecond,
3044                    microsecond,
3045                    nanosecond,
3046                ))
3047        }
3048    }
3049
3050    /// Set the hour field on a [`Time`].
3051    ///
3052    /// One can access this value via [`Time::hour`].
3053    ///
3054    /// This overrides any previous hour settings.
3055    ///
3056    /// # Errors
3057    ///
3058    /// This returns an error when [`TimeWith::build`] is called if the given
3059    /// hour is outside the range `0..=23`.
3060    ///
3061    /// # Example
3062    ///
3063    /// ```
3064    /// use jiff::civil::time;
3065    ///
3066    /// let t1 = time(15, 21, 59, 0);
3067    /// assert_eq!(t1.hour(), 15);
3068    /// let t2 = t1.with().hour(3).build()?;
3069    /// assert_eq!(t2.hour(), 3);
3070    ///
3071    /// # Ok::<(), Box<dyn std::error::Error>>(())
3072    /// ```
3073    #[inline]
3074    pub fn hour(self, hour: i8) -> TimeWith {
3075        TimeWith { hour: Some(hour), ..self }
3076    }
3077
3078    /// Set the minute field on a [`Time`].
3079    ///
3080    /// One can access this value via [`Time::minute`].
3081    ///
3082    /// This overrides any previous minute settings.
3083    ///
3084    /// # Errors
3085    ///
3086    /// This returns an error when [`TimeWith::build`] is called if the given
3087    /// minute is outside the range `0..=59`.
3088    ///
3089    /// # Example
3090    ///
3091    /// ```
3092    /// use jiff::civil::time;
3093    ///
3094    /// let t1 = time(15, 21, 59, 0);
3095    /// assert_eq!(t1.minute(), 21);
3096    /// let t2 = t1.with().minute(3).build()?;
3097    /// assert_eq!(t2.minute(), 3);
3098    ///
3099    /// # Ok::<(), Box<dyn std::error::Error>>(())
3100    /// ```
3101    #[inline]
3102    pub fn minute(self, minute: i8) -> TimeWith {
3103        TimeWith { minute: Some(minute), ..self }
3104    }
3105
3106    /// Set the second field on a [`Time`].
3107    ///
3108    /// One can access this value via [`Time::second`].
3109    ///
3110    /// This overrides any previous second settings.
3111    ///
3112    /// # Errors
3113    ///
3114    /// This returns an error when [`TimeWith::build`] is called if the given
3115    /// second is outside the range `0..=59`.
3116    ///
3117    /// # Example
3118    ///
3119    /// ```
3120    /// use jiff::civil::time;
3121    ///
3122    /// let t1 = time(15, 21, 59, 0);
3123    /// assert_eq!(t1.second(), 59);
3124    /// let t2 = t1.with().second(3).build()?;
3125    /// assert_eq!(t2.second(), 3);
3126    ///
3127    /// # Ok::<(), Box<dyn std::error::Error>>(())
3128    /// ```
3129    #[inline]
3130    pub fn second(self, second: i8) -> TimeWith {
3131        TimeWith { second: Some(second), ..self }
3132    }
3133
3134    /// Set the millisecond field on a [`Time`].
3135    ///
3136    /// One can access this value via [`Time::millisecond`].
3137    ///
3138    /// This overrides any previous millisecond settings.
3139    ///
3140    /// Note that this only sets the millisecond component. It does
3141    /// not change the microsecond or nanosecond components. To set
3142    /// the fractional second component to nanosecond precision, use
3143    /// [`TimeWith::subsec_nanosecond`].
3144    ///
3145    /// # Errors
3146    ///
3147    /// This returns an error when [`TimeWith::build`] is called if the given
3148    /// millisecond is outside the range `0..=999`, or if both this and
3149    /// [`TimeWith::subsec_nanosecond`] are set.
3150    ///
3151    /// # Example
3152    ///
3153    /// This shows the relationship between [`Time::millisecond`] and
3154    /// [`Time::subsec_nanosecond`]:
3155    ///
3156    /// ```
3157    /// use jiff::civil::time;
3158    ///
3159    /// let t = time(15, 21, 35, 0).with().millisecond(123).build()?;
3160    /// assert_eq!(t.subsec_nanosecond(), 123_000_000);
3161    ///
3162    /// # Ok::<(), Box<dyn std::error::Error>>(())
3163    /// ```
3164    #[inline]
3165    pub fn millisecond(self, millisecond: i16) -> TimeWith {
3166        TimeWith { millisecond: Some(millisecond), ..self }
3167    }
3168
3169    /// Set the microsecond field on a [`Time`].
3170    ///
3171    /// One can access this value via [`Time::microsecond`].
3172    ///
3173    /// This overrides any previous microsecond settings.
3174    ///
3175    /// Note that this only sets the microsecond component. It does
3176    /// not change the millisecond or nanosecond components. To set
3177    /// the fractional second component to nanosecond precision, use
3178    /// [`TimeWith::subsec_nanosecond`].
3179    ///
3180    /// # Errors
3181    ///
3182    /// This returns an error when [`TimeWith::build`] is called if the given
3183    /// microsecond is outside the range `0..=999`, or if both this and
3184    /// [`TimeWith::subsec_nanosecond`] are set.
3185    ///
3186    /// # Example
3187    ///
3188    /// This shows the relationship between [`Time::microsecond`] and
3189    /// [`Time::subsec_nanosecond`]:
3190    ///
3191    /// ```
3192    /// use jiff::civil::time;
3193    ///
3194    /// let t = time(15, 21, 35, 0).with().microsecond(123).build()?;
3195    /// assert_eq!(t.subsec_nanosecond(), 123_000);
3196    ///
3197    /// # Ok::<(), Box<dyn std::error::Error>>(())
3198    /// ```
3199    #[inline]
3200    pub fn microsecond(self, microsecond: i16) -> TimeWith {
3201        TimeWith { microsecond: Some(microsecond), ..self }
3202    }
3203
3204    /// Set the nanosecond field on a [`Time`].
3205    ///
3206    /// One can access this value via [`Time::nanosecond`].
3207    ///
3208    /// This overrides any previous nanosecond settings.
3209    ///
3210    /// Note that this only sets the nanosecond component. It does
3211    /// not change the millisecond or microsecond components. To set
3212    /// the fractional second component to nanosecond precision, use
3213    /// [`TimeWith::subsec_nanosecond`].
3214    ///
3215    /// # Errors
3216    ///
3217    /// This returns an error when [`TimeWith::build`] is called if the given
3218    /// nanosecond is outside the range `0..=999`, or if both this and
3219    /// [`TimeWith::subsec_nanosecond`] are set.
3220    ///
3221    /// # Example
3222    ///
3223    /// This shows the relationship between [`Time::nanosecond`] and
3224    /// [`Time::subsec_nanosecond`]:
3225    ///
3226    /// ```
3227    /// use jiff::civil::time;
3228    ///
3229    /// let t = time(15, 21, 35, 0).with().nanosecond(123).build()?;
3230    /// assert_eq!(t.subsec_nanosecond(), 123);
3231    ///
3232    /// # Ok::<(), Box<dyn std::error::Error>>(())
3233    /// ```
3234    #[inline]
3235    pub fn nanosecond(self, nanosecond: i16) -> TimeWith {
3236        TimeWith { nanosecond: Some(nanosecond), ..self }
3237    }
3238
3239    /// Set the subsecond nanosecond field on a [`Time`].
3240    ///
3241    /// If you want to access this value on `Time`, then use
3242    /// [`Time::subsec_nanosecond`].
3243    ///
3244    /// This overrides any previous subsecond nanosecond settings.
3245    ///
3246    /// Note that this sets the entire fractional second component to
3247    /// nanosecond precision, and overrides any individual millisecond,
3248    /// microsecond or nanosecond settings. To set individual components,
3249    /// use [`TimeWith::millisecond`], [`TimeWith::microsecond`] or
3250    /// [`TimeWith::nanosecond`].
3251    ///
3252    /// # Errors
3253    ///
3254    /// This returns an error when [`TimeWith::build`] is called if the given
3255    /// subsecond nanosecond is outside the range `0..=999,999,999`, or if both
3256    /// this and one of [`TimeWith::millisecond`], [`TimeWith::microsecond`] or
3257    /// [`TimeWith::nanosecond`] are set.
3258    ///
3259    /// # Example
3260    ///
3261    /// This shows the relationship between constructing a `Time` value with
3262    /// subsecond nanoseconds and its individual subsecond fields:
3263    ///
3264    /// ```
3265    /// use jiff::civil::time;
3266    ///
3267    /// let t1 = time(15, 21, 35, 0);
3268    /// let t2 = t1.with().subsec_nanosecond(123_456_789).build()?;
3269    /// assert_eq!(t2.millisecond(), 123);
3270    /// assert_eq!(t2.microsecond(), 456);
3271    /// assert_eq!(t2.nanosecond(), 789);
3272    ///
3273    /// # Ok::<(), Box<dyn std::error::Error>>(())
3274    /// ```
3275    #[inline]
3276    pub fn subsec_nanosecond(self, subsec_nanosecond: i32) -> TimeWith {
3277        TimeWith { subsec_nanosecond: Some(subsec_nanosecond), ..self }
3278    }
3279}
3280
3281#[cfg(test)]
3282mod tests {
3283    use std::io::Cursor;
3284
3285    use crate::{civil::time, span::span_eq, ToSpan};
3286
3287    use super::*;
3288
3289    #[test]
3290    fn min() {
3291        let t = Time::MIN;
3292        assert_eq!(t.hour(), 0);
3293        assert_eq!(t.minute(), 0);
3294        assert_eq!(t.second(), 0);
3295        assert_eq!(t.subsec_nanosecond(), 0);
3296    }
3297
3298    #[test]
3299    fn max() {
3300        let t = Time::MAX;
3301        assert_eq!(t.hour(), 23);
3302        assert_eq!(t.minute(), 59);
3303        assert_eq!(t.second(), 59);
3304        assert_eq!(t.subsec_nanosecond(), 999_999_999);
3305    }
3306
3307    #[test]
3308    fn invalid() {
3309        assert!(Time::new(24, 0, 0, 0).is_err());
3310        assert!(Time::new(23, 60, 0, 0).is_err());
3311        assert!(Time::new(23, 59, 60, 0).is_err());
3312        assert!(Time::new(23, 59, 61, 0).is_err());
3313        assert!(Time::new(-1, 0, 0, 0).is_err());
3314        assert!(Time::new(0, -1, 0, 0).is_err());
3315        assert!(Time::new(0, 0, -1, 0).is_err());
3316
3317        assert!(Time::new(0, 0, 0, 1_000_000_000).is_err());
3318        assert!(Time::new(0, 0, 0, -1).is_err());
3319        assert!(Time::new(23, 59, 59, 1_000_000_000).is_err());
3320        assert!(Time::new(23, 59, 59, -1).is_err());
3321    }
3322
3323    #[test]
3324    fn rounding_cross_midnight() {
3325        let t1 = time(23, 59, 59, 999_999_999);
3326
3327        let t2 = t1.round(Unit::Nanosecond).unwrap();
3328        assert_eq!(t2, t1);
3329
3330        let t2 = t1.round(Unit::Millisecond).unwrap();
3331        assert_eq!(t2, time(0, 0, 0, 0));
3332
3333        let t2 = t1.round(Unit::Microsecond).unwrap();
3334        assert_eq!(t2, time(0, 0, 0, 0));
3335
3336        let t2 = t1.round(Unit::Millisecond).unwrap();
3337        assert_eq!(t2, time(0, 0, 0, 0));
3338
3339        let t2 = t1.round(Unit::Second).unwrap();
3340        assert_eq!(t2, time(0, 0, 0, 0));
3341
3342        let t2 = t1.round(Unit::Minute).unwrap();
3343        assert_eq!(t2, time(0, 0, 0, 0));
3344
3345        let t2 = t1.round(Unit::Hour).unwrap();
3346        assert_eq!(t2, time(0, 0, 0, 0));
3347
3348        let t1 = time(22, 15, 0, 0);
3349        assert_eq!(
3350            time(22, 30, 0, 0),
3351            t1.round(TimeRound::new().smallest(Unit::Minute).increment(30))
3352                .unwrap()
3353        );
3354    }
3355
3356    #[cfg(not(miri))]
3357    quickcheck::quickcheck! {
3358        fn prop_ordering_same_as_civil_nanosecond(
3359            civil_nanosecond1: CivilDayNanosecond,
3360            civil_nanosecond2: CivilDayNanosecond
3361        ) -> bool {
3362            let t1 = Time::from_nanosecond(civil_nanosecond1);
3363            let t2 = Time::from_nanosecond(civil_nanosecond2);
3364            t1.cmp(&t2) == civil_nanosecond1.cmp(&civil_nanosecond2)
3365        }
3366
3367        fn prop_checked_add_then_sub(
3368            time: Time,
3369            nano_span: CivilDayNanosecond
3370        ) -> quickcheck::TestResult {
3371            let span = Span::new().nanoseconds(nano_span.get());
3372            let Ok(sum) = time.checked_add(span) else {
3373                return quickcheck::TestResult::discard()
3374            };
3375            let diff = sum.checked_sub(span).unwrap();
3376            quickcheck::TestResult::from_bool(time == diff)
3377        }
3378
3379        fn prop_wrapping_add_then_sub(
3380            time: Time,
3381            nano_span: CivilDayNanosecond
3382        ) -> bool {
3383            let span = Span::new().nanoseconds(nano_span.get());
3384            let sum = time.wrapping_add(span);
3385            let diff = sum.wrapping_sub(span);
3386            time == diff
3387        }
3388
3389        fn prop_checked_add_equals_wrapping_add(
3390            time: Time,
3391            nano_span: CivilDayNanosecond
3392        ) -> quickcheck::TestResult {
3393            let span = Span::new().nanoseconds(nano_span.get());
3394            let Ok(sum_checked) = time.checked_add(span) else {
3395                return quickcheck::TestResult::discard()
3396            };
3397            let sum_wrapped = time.wrapping_add(span);
3398            quickcheck::TestResult::from_bool(sum_checked == sum_wrapped)
3399        }
3400
3401        fn prop_checked_sub_equals_wrapping_sub(
3402            time: Time,
3403            nano_span: CivilDayNanosecond
3404        ) -> quickcheck::TestResult {
3405            let span = Span::new().nanoseconds(nano_span.get());
3406            let Ok(diff_checked) = time.checked_sub(span) else {
3407                return quickcheck::TestResult::discard()
3408            };
3409            let diff_wrapped = time.wrapping_sub(span);
3410            quickcheck::TestResult::from_bool(diff_checked == diff_wrapped)
3411        }
3412
3413        fn prop_until_then_add(t1: Time, t2: Time) -> bool {
3414            let span = t1.until(t2).unwrap();
3415            t1.checked_add(span).unwrap() == t2
3416        }
3417
3418        fn prop_until_then_sub(t1: Time, t2: Time) -> bool {
3419            let span = t1.until(t2).unwrap();
3420            t2.checked_sub(span).unwrap() == t1
3421        }
3422
3423        fn prop_since_then_add(t1: Time, t2: Time) -> bool {
3424            let span = t1.since(t2).unwrap();
3425            t2.checked_add(span).unwrap() == t1
3426        }
3427
3428        fn prop_since_then_sub(t1: Time, t2: Time) -> bool {
3429            let span = t1.since(t2).unwrap();
3430            t1.checked_sub(span).unwrap() == t2
3431        }
3432
3433        fn prop_until_is_since_negated(t1: Time, t2: Time) -> bool {
3434            t1.until(t2).unwrap().get_nanoseconds()
3435                == t1.since(t2).unwrap().negate().get_nanoseconds()
3436        }
3437    }
3438
3439    #[test]
3440    fn overflowing_add() {
3441        let t1 = time(23, 30, 0, 0);
3442        let (t2, span) = t1.overflowing_add(5.hours()).unwrap();
3443        assert_eq!(t2, time(4, 30, 0, 0));
3444        span_eq!(span, 1.days());
3445    }
3446
3447    #[test]
3448    fn overflowing_add_overflows() {
3449        let t1 = time(23, 30, 0, 0);
3450        let span = Span::new()
3451            .hours(t::SpanHours::MAX_REPR)
3452            .minutes(t::SpanMinutes::MAX_REPR)
3453            .seconds(t::SpanSeconds::MAX_REPR)
3454            .milliseconds(t::SpanMilliseconds::MAX_REPR)
3455            .microseconds(t::SpanMicroseconds::MAX_REPR)
3456            .nanoseconds(t::SpanNanoseconds::MAX_REPR);
3457        assert!(t1.overflowing_add(span).is_err());
3458    }
3459
3460    #[test]
3461    fn time_size() {
3462        #[cfg(debug_assertions)]
3463        {
3464            assert_eq!(24, core::mem::size_of::<Time>());
3465        }
3466        #[cfg(not(debug_assertions))]
3467        {
3468            assert_eq!(8, core::mem::size_of::<Time>());
3469        }
3470    }
3471
3472    // This test checks that a wrapping subtraction with the minimum signed
3473    // duration is as expected.
3474    #[test]
3475    fn wrapping_sub_signed_duration_min() {
3476        let max = -SignedDuration::MIN.as_nanos();
3477        let got = time(15, 30, 8, 999_999_999).to_nanosecond();
3478        let expected = max.rem_euclid(t::NANOS_PER_CIVIL_DAY.bound());
3479        assert_eq!(i128::from(got.get()), expected);
3480    }
3481
3482    // This test checks that a wrapping subtraction with the maximum signed
3483    // duration is as expected.
3484    #[test]
3485    fn wrapping_sub_signed_duration_max() {
3486        let max = -SignedDuration::MAX.as_nanos();
3487        let got = time(8, 29, 52, 1).to_nanosecond();
3488        let expected = max.rem_euclid(t::NANOS_PER_CIVIL_DAY.bound());
3489        assert_eq!(i128::from(got.get()), expected);
3490    }
3491
3492    // This test checks that a wrapping subtraction with the maximum unsigned
3493    // duration is as expected.
3494    #[test]
3495    fn wrapping_sub_unsigned_duration_max() {
3496        let max =
3497            -i128::try_from(std::time::Duration::MAX.as_nanos()).unwrap();
3498        let got = time(16, 59, 44, 1).to_nanosecond();
3499        let expected = max.rem_euclid(t::NANOS_PER_CIVIL_DAY.bound());
3500        assert_eq!(i128::from(got.get()), expected);
3501    }
3502
3503    /// # `serde` deserializer compatibility test
3504    ///
3505    /// Serde YAML used to be unable to deserialize `jiff` types,
3506    /// as deserializing from bytes is not supported by the deserializer.
3507    ///
3508    /// - <https://github.com/BurntSushi/jiff/issues/138>
3509    /// - <https://github.com/BurntSushi/jiff/discussions/148>
3510    #[test]
3511    fn civil_time_deserialize_yaml() {
3512        let expected = time(16, 35, 4, 987654321);
3513
3514        let deserialized: Time =
3515            serde_yaml::from_str("16:35:04.987654321").unwrap();
3516
3517        assert_eq!(deserialized, expected);
3518
3519        let deserialized: Time =
3520            serde_yaml::from_slice("16:35:04.987654321".as_bytes()).unwrap();
3521
3522        assert_eq!(deserialized, expected);
3523
3524        let cursor = Cursor::new(b"16:35:04.987654321");
3525        let deserialized: Time = serde_yaml::from_reader(cursor).unwrap();
3526
3527        assert_eq!(deserialized, expected);
3528    }
3529}