chrono/naive/time/
mod.rs

1// This is a part of Chrono.
2// See README.md and LICENSE.txt for details.
3
4//! ISO 8601 time without timezone.
5
6#[cfg(any(feature = "alloc", feature = "std"))]
7use core::borrow::Borrow;
8use core::ops::{Add, AddAssign, Sub, SubAssign};
9use core::time::Duration;
10use core::{fmt, str};
11
12#[cfg(feature = "rkyv")]
13use rkyv::{Archive, Deserialize, Serialize};
14
15use crate::duration::Duration as OldDuration;
16#[cfg(any(feature = "alloc", feature = "std"))]
17use crate::format::DelayedFormat;
18use crate::format::{
19    parse, parse_and_remainder, write_hundreds, Fixed, Item, Numeric, Pad, ParseError, ParseResult,
20    Parsed, StrftimeItems,
21};
22use crate::Timelike;
23use crate::{expect, try_opt};
24
25#[cfg(feature = "rustc-serialize")]
26mod rustc_serialize;
27
28#[cfg(feature = "serde")]
29mod serde;
30
31#[cfg(test)]
32mod tests;
33
34/// ISO 8601 time without timezone.
35/// Allows for the nanosecond precision and optional leap second representation.
36///
37/// # Leap Second Handling
38///
39/// Since 1960s, the manmade atomic clock has been so accurate that
40/// it is much more accurate than Earth's own motion.
41/// It became desirable to define the civil time in terms of the atomic clock,
42/// but that risks the desynchronization of the civil time from Earth.
43/// To account for this, the designers of the Coordinated Universal Time (UTC)
44/// made that the UTC should be kept within 0.9 seconds of the observed Earth-bound time.
45/// When the mean solar day is longer than the ideal (86,400 seconds),
46/// the error slowly accumulates and it is necessary to add a **leap second**
47/// to slow the UTC down a bit.
48/// (We may also remove a second to speed the UTC up a bit, but it never happened.)
49/// The leap second, if any, follows 23:59:59 of June 30 or December 31 in the UTC.
50///
51/// Fast forward to the 21st century,
52/// we have seen 26 leap seconds from January 1972 to December 2015.
53/// Yes, 26 seconds. Probably you can read this paragraph within 26 seconds.
54/// But those 26 seconds, and possibly more in the future, are never predictable,
55/// and whether to add a leap second or not is known only before 6 months.
56/// Internet-based clocks (via NTP) do account for known leap seconds,
57/// but the system API normally doesn't (and often can't, with no network connection)
58/// and there is no reliable way to retrieve leap second information.
59///
60/// Chrono does not try to accurately implement leap seconds; it is impossible.
61/// Rather, **it allows for leap seconds but behaves as if there are *no other* leap seconds.**
62/// Various operations will ignore any possible leap second(s)
63/// except when any of the operands were actually leap seconds.
64///
65/// If you cannot tolerate this behavior,
66/// you must use a separate `TimeZone` for the International Atomic Time (TAI).
67/// TAI is like UTC but has no leap seconds, and thus slightly differs from UTC.
68/// Chrono does not yet provide such implementation, but it is planned.
69///
70/// ## Representing Leap Seconds
71///
72/// The leap second is indicated via fractional seconds more than 1 second.
73/// This makes possible to treat a leap second as the prior non-leap second
74/// if you don't care about sub-second accuracy.
75/// You should use the proper formatting to get the raw leap second.
76///
77/// All methods accepting fractional seconds will accept such values.
78///
79/// ```
80/// use chrono::{NaiveDate, NaiveTime, Utc};
81///
82/// let t = NaiveTime::from_hms_milli_opt(8, 59, 59, 1_000).unwrap();
83///
84/// let dt1 = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_micro_opt(8, 59, 59, 1_000_000).unwrap();
85///
86/// let dt2 = NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_nano_opt(23, 59, 59, 1_000_000_000).unwrap().and_local_timezone(Utc).unwrap();
87/// # let _ = (t, dt1, dt2);
88/// ```
89///
90/// Note that the leap second can happen anytime given an appropriate time zone;
91/// 2015-07-01 01:23:60 would be a proper leap second if UTC+01:24 had existed.
92/// Practically speaking, though, by the time of the first leap second on 1972-06-30,
93/// every time zone offset around the world has standardized to the 5-minute alignment.
94///
95/// ## Date And Time Arithmetics
96///
97/// As a concrete example, let's assume that `03:00:60` and `04:00:60` are leap seconds.
98/// In reality, of course, leap seconds are separated by at least 6 months.
99/// We will also use some intuitive concise notations for the explanation.
100///
101/// `Time + Duration`
102/// (short for [`NaiveTime::overflowing_add_signed`](#method.overflowing_add_signed)):
103///
104/// - `03:00:00 + 1s = 03:00:01`.
105/// - `03:00:59 + 60s = 03:01:59`.
106/// - `03:00:59 + 61s = 03:02:00`.
107/// - `03:00:59 + 1s = 03:01:00`.
108/// - `03:00:60 + 1s = 03:01:00`.
109///   Note that the sum is identical to the previous.
110/// - `03:00:60 + 60s = 03:01:59`.
111/// - `03:00:60 + 61s = 03:02:00`.
112/// - `03:00:60.1 + 0.8s = 03:00:60.9`.
113///
114/// `Time - Duration`
115/// (short for [`NaiveTime::overflowing_sub_signed`](#method.overflowing_sub_signed)):
116///
117/// - `03:00:00 - 1s = 02:59:59`.
118/// - `03:01:00 - 1s = 03:00:59`.
119/// - `03:01:00 - 60s = 03:00:00`.
120/// - `03:00:60 - 60s = 03:00:00`.
121///   Note that the result is identical to the previous.
122/// - `03:00:60.7 - 0.4s = 03:00:60.3`.
123/// - `03:00:60.7 - 0.9s = 03:00:59.8`.
124///
125/// `Time - Time`
126/// (short for [`NaiveTime::signed_duration_since`](#method.signed_duration_since)):
127///
128/// - `04:00:00 - 03:00:00 = 3600s`.
129/// - `03:01:00 - 03:00:00 = 60s`.
130/// - `03:00:60 - 03:00:00 = 60s`.
131///   Note that the difference is identical to the previous.
132/// - `03:00:60.6 - 03:00:59.4 = 1.2s`.
133/// - `03:01:00 - 03:00:59.8 = 0.2s`.
134/// - `03:01:00 - 03:00:60.5 = 0.5s`.
135///   Note that the difference is larger than the previous,
136///   even though the leap second clearly follows the previous whole second.
137/// - `04:00:60.9 - 03:00:60.1 =
138///   (04:00:60.9 - 04:00:00) + (04:00:00 - 03:01:00) + (03:01:00 - 03:00:60.1) =
139///   60.9s + 3540s + 0.9s = 3601.8s`.
140///
141/// In general,
142///
143/// - `Time + Duration` unconditionally equals to `Duration + Time`.
144///
145/// - `Time - Duration` unconditionally equals to `Time + (-Duration)`.
146///
147/// - `Time1 - Time2` unconditionally equals to `-(Time2 - Time1)`.
148///
149/// - Associativity does not generally hold, because
150///   `(Time + Duration1) - Duration2` no longer equals to `Time + (Duration1 - Duration2)`
151///   for two positive durations.
152///
153///     - As a special case, `(Time + Duration) - Duration` also does not equal to `Time`.
154///
155///     - If you can assume that all durations have the same sign, however,
156///       then the associativity holds:
157///       `(Time + Duration1) + Duration2` equals to `Time + (Duration1 + Duration2)`
158///       for two positive durations.
159///
160/// ## Reading And Writing Leap Seconds
161///
162/// The "typical" leap seconds on the minute boundary are
163/// correctly handled both in the formatting and parsing.
164/// The leap second in the human-readable representation
165/// will be represented as the second part being 60, as required by ISO 8601.
166///
167/// ```
168/// use chrono::{Utc, NaiveDate};
169///
170/// let dt = NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_000).unwrap().and_local_timezone(Utc).unwrap();
171/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60Z");
172/// ```
173///
174/// There are hypothetical leap seconds not on the minute boundary
175/// nevertheless supported by Chrono.
176/// They are allowed for the sake of completeness and consistency;
177/// there were several "exotic" time zone offsets with fractional minutes prior to UTC after all.
178/// For such cases the human-readable representation is ambiguous
179/// and would be read back to the next non-leap second.
180///
181/// ```
182/// use chrono::{DateTime, Utc, TimeZone, NaiveDate};
183///
184/// let dt = NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 56, 4, 1_000).unwrap().and_local_timezone(Utc).unwrap();
185/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:56:05Z");
186///
187/// let dt = Utc.with_ymd_and_hms(2015, 6, 30, 23, 56, 5).unwrap();
188/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:56:05Z");
189/// assert_eq!(DateTime::parse_from_rfc3339("2015-06-30T23:56:05Z").unwrap(), dt);
190/// ```
191///
192/// Since Chrono alone cannot determine any existence of leap seconds,
193/// **there is absolutely no guarantee that the leap second read has actually happened**.
194#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
195#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
196pub struct NaiveTime {
197    secs: u32,
198    frac: u32,
199}
200
201#[cfg(feature = "arbitrary")]
202impl arbitrary::Arbitrary<'_> for NaiveTime {
203    fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<NaiveTime> {
204        let secs = u.int_in_range(0..=86_399)?;
205        let nano = u.int_in_range(0..=1_999_999_999)?;
206        let time = NaiveTime::from_num_seconds_from_midnight_opt(secs, nano)
207            .expect("Could not generate a valid chrono::NaiveTime. It looks like implementation of Arbitrary for NaiveTime is erroneous.");
208        Ok(time)
209    }
210}
211
212impl NaiveTime {
213    /// Makes a new `NaiveTime` from hour, minute and second.
214    ///
215    /// No [leap second](#leap-second-handling) is allowed here;
216    /// use `NaiveTime::from_hms_*` methods with a subsecond parameter instead.
217    ///
218    /// # Panics
219    ///
220    /// Panics on invalid hour, minute and/or second.
221    #[deprecated(since = "0.4.23", note = "use `from_hms_opt()` instead")]
222    #[inline]
223    #[must_use]
224    pub const fn from_hms(hour: u32, min: u32, sec: u32) -> NaiveTime {
225        expect!(NaiveTime::from_hms_opt(hour, min, sec), "invalid time")
226    }
227
228    /// Makes a new `NaiveTime` from hour, minute and second.
229    ///
230    /// No [leap second](#leap-second-handling) is allowed here;
231    /// use `NaiveTime::from_hms_*_opt` methods with a subsecond parameter instead.
232    ///
233    /// # Errors
234    ///
235    /// Returns `None` on invalid hour, minute and/or second.
236    ///
237    /// # Example
238    ///
239    /// ```
240    /// use chrono::NaiveTime;
241    ///
242    /// let from_hms_opt = NaiveTime::from_hms_opt;
243    ///
244    /// assert!(from_hms_opt(0, 0, 0).is_some());
245    /// assert!(from_hms_opt(23, 59, 59).is_some());
246    /// assert!(from_hms_opt(24, 0, 0).is_none());
247    /// assert!(from_hms_opt(23, 60, 0).is_none());
248    /// assert!(from_hms_opt(23, 59, 60).is_none());
249    /// ```
250    #[inline]
251    #[must_use]
252    pub const fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option<NaiveTime> {
253        NaiveTime::from_hms_nano_opt(hour, min, sec, 0)
254    }
255
256    /// Makes a new `NaiveTime` from hour, minute, second and millisecond.
257    ///
258    /// The millisecond part can exceed 1,000
259    /// in order to represent the [leap second](#leap-second-handling).
260    ///
261    /// # Panics
262    ///
263    /// Panics on invalid hour, minute, second and/or millisecond.
264    #[deprecated(since = "0.4.23", note = "use `from_hms_milli_opt()` instead")]
265    #[inline]
266    #[must_use]
267    pub const fn from_hms_milli(hour: u32, min: u32, sec: u32, milli: u32) -> NaiveTime {
268        expect!(NaiveTime::from_hms_milli_opt(hour, min, sec, milli), "invalid time")
269    }
270
271    /// Makes a new `NaiveTime` from hour, minute, second and millisecond.
272    ///
273    /// The millisecond part can exceed 1,000
274    /// in order to represent the [leap second](#leap-second-handling).
275    ///
276    /// # Errors
277    ///
278    /// Returns `None` on invalid hour, minute, second and/or millisecond.
279    ///
280    /// # Example
281    ///
282    /// ```
283    /// use chrono::NaiveTime;
284    ///
285    /// let from_hmsm_opt = NaiveTime::from_hms_milli_opt;
286    ///
287    /// assert!(from_hmsm_opt(0, 0, 0, 0).is_some());
288    /// assert!(from_hmsm_opt(23, 59, 59, 999).is_some());
289    /// assert!(from_hmsm_opt(23, 59, 59, 1_999).is_some()); // a leap second after 23:59:59
290    /// assert!(from_hmsm_opt(24, 0, 0, 0).is_none());
291    /// assert!(from_hmsm_opt(23, 60, 0, 0).is_none());
292    /// assert!(from_hmsm_opt(23, 59, 60, 0).is_none());
293    /// assert!(from_hmsm_opt(23, 59, 59, 2_000).is_none());
294    /// ```
295    #[inline]
296    #[must_use]
297    pub const fn from_hms_milli_opt(
298        hour: u32,
299        min: u32,
300        sec: u32,
301        milli: u32,
302    ) -> Option<NaiveTime> {
303        let nano = try_opt!(milli.checked_mul(1_000_000));
304        NaiveTime::from_hms_nano_opt(hour, min, sec, nano)
305    }
306
307    /// Makes a new `NaiveTime` from hour, minute, second and microsecond.
308    ///
309    /// The microsecond part can exceed 1,000,000
310    /// in order to represent the [leap second](#leap-second-handling).
311    ///
312    /// # Panics
313    ///
314    /// Panics on invalid hour, minute, second and/or microsecond.
315    #[deprecated(since = "0.4.23", note = "use `from_hms_micro_opt()` instead")]
316    #[inline]
317    #[must_use]
318    pub const fn from_hms_micro(hour: u32, min: u32, sec: u32, micro: u32) -> NaiveTime {
319        expect!(NaiveTime::from_hms_micro_opt(hour, min, sec, micro), "invalid time")
320    }
321
322    /// Makes a new `NaiveTime` from hour, minute, second and microsecond.
323    ///
324    /// The microsecond part can exceed 1,000,000
325    /// in order to represent the [leap second](#leap-second-handling).
326    ///
327    /// # Errors
328    ///
329    /// Returns `None` on invalid hour, minute, second and/or microsecond.
330    ///
331    /// # Example
332    ///
333    /// ```
334    /// use chrono::NaiveTime;
335    ///
336    /// let from_hmsu_opt = NaiveTime::from_hms_micro_opt;
337    ///
338    /// assert!(from_hmsu_opt(0, 0, 0, 0).is_some());
339    /// assert!(from_hmsu_opt(23, 59, 59, 999_999).is_some());
340    /// assert!(from_hmsu_opt(23, 59, 59, 1_999_999).is_some()); // a leap second after 23:59:59
341    /// assert!(from_hmsu_opt(24, 0, 0, 0).is_none());
342    /// assert!(from_hmsu_opt(23, 60, 0, 0).is_none());
343    /// assert!(from_hmsu_opt(23, 59, 60, 0).is_none());
344    /// assert!(from_hmsu_opt(23, 59, 59, 2_000_000).is_none());
345    /// ```
346    #[inline]
347    #[must_use]
348    pub const fn from_hms_micro_opt(
349        hour: u32,
350        min: u32,
351        sec: u32,
352        micro: u32,
353    ) -> Option<NaiveTime> {
354        let nano = try_opt!(micro.checked_mul(1_000));
355        NaiveTime::from_hms_nano_opt(hour, min, sec, nano)
356    }
357
358    /// Makes a new `NaiveTime` from hour, minute, second and nanosecond.
359    ///
360    /// The nanosecond part can exceed 1,000,000,000
361    /// in order to represent the [leap second](#leap-second-handling).
362    ///
363    /// # Panics
364    ///
365    /// Panics on invalid hour, minute, second and/or nanosecond.
366    #[deprecated(since = "0.4.23", note = "use `from_hms_nano_opt()` instead")]
367    #[inline]
368    #[must_use]
369    pub const fn from_hms_nano(hour: u32, min: u32, sec: u32, nano: u32) -> NaiveTime {
370        expect!(NaiveTime::from_hms_nano_opt(hour, min, sec, nano), "invalid time")
371    }
372
373    /// Makes a new `NaiveTime` from hour, minute, second and nanosecond.
374    ///
375    /// The nanosecond part can exceed 1,000,000,000
376    /// in order to represent the [leap second](#leap-second-handling).
377    ///
378    /// # Errors
379    ///
380    /// Returns `None` on invalid hour, minute, second and/or nanosecond.
381    ///
382    /// # Example
383    ///
384    /// ```
385    /// use chrono::NaiveTime;
386    ///
387    /// let from_hmsn_opt = NaiveTime::from_hms_nano_opt;
388    ///
389    /// assert!(from_hmsn_opt(0, 0, 0, 0).is_some());
390    /// assert!(from_hmsn_opt(23, 59, 59, 999_999_999).is_some());
391    /// assert!(from_hmsn_opt(23, 59, 59, 1_999_999_999).is_some()); // a leap second after 23:59:59
392    /// assert!(from_hmsn_opt(24, 0, 0, 0).is_none());
393    /// assert!(from_hmsn_opt(23, 60, 0, 0).is_none());
394    /// assert!(from_hmsn_opt(23, 59, 60, 0).is_none());
395    /// assert!(from_hmsn_opt(23, 59, 59, 2_000_000_000).is_none());
396    /// ```
397    #[inline]
398    #[must_use]
399    pub const fn from_hms_nano_opt(hour: u32, min: u32, sec: u32, nano: u32) -> Option<NaiveTime> {
400        if hour >= 24 || min >= 60 || sec >= 60 || nano >= 2_000_000_000 {
401            return None;
402        }
403        let secs = hour * 3600 + min * 60 + sec;
404        Some(NaiveTime { secs, frac: nano })
405    }
406
407    /// Makes a new `NaiveTime` from the number of seconds since midnight and nanosecond.
408    ///
409    /// The nanosecond part can exceed 1,000,000,000
410    /// in order to represent the [leap second](#leap-second-handling).
411    ///
412    /// # Panics
413    ///
414    /// Panics on invalid number of seconds and/or nanosecond.
415    #[deprecated(since = "0.4.23", note = "use `from_num_seconds_from_midnight_opt()` instead")]
416    #[inline]
417    #[must_use]
418    pub const fn from_num_seconds_from_midnight(secs: u32, nano: u32) -> NaiveTime {
419        expect!(NaiveTime::from_num_seconds_from_midnight_opt(secs, nano), "invalid time")
420    }
421
422    /// Makes a new `NaiveTime` from the number of seconds since midnight and nanosecond.
423    ///
424    /// The nanosecond part can exceed 1,000,000,000
425    /// in order to represent the [leap second](#leap-second-handling).
426    ///
427    /// # Errors
428    ///
429    /// Returns `None` on invalid number of seconds and/or nanosecond.
430    ///
431    /// # Example
432    ///
433    /// ```
434    /// use chrono::NaiveTime;
435    ///
436    /// let from_nsecs_opt = NaiveTime::from_num_seconds_from_midnight_opt;
437    ///
438    /// assert!(from_nsecs_opt(0, 0).is_some());
439    /// assert!(from_nsecs_opt(86399, 999_999_999).is_some());
440    /// assert!(from_nsecs_opt(86399, 1_999_999_999).is_some()); // a leap second after 23:59:59
441    /// assert!(from_nsecs_opt(86_400, 0).is_none());
442    /// assert!(from_nsecs_opt(86399, 2_000_000_000).is_none());
443    /// ```
444    #[inline]
445    #[must_use]
446    pub const fn from_num_seconds_from_midnight_opt(secs: u32, nano: u32) -> Option<NaiveTime> {
447        if secs >= 86_400 || nano >= 2_000_000_000 {
448            return None;
449        }
450        Some(NaiveTime { secs, frac: nano })
451    }
452
453    /// Parses a string with the specified format string and returns a new `NaiveTime`.
454    /// See the [`format::strftime` module](../format/strftime/index.html)
455    /// on the supported escape sequences.
456    ///
457    /// # Example
458    ///
459    /// ```
460    /// use chrono::NaiveTime;
461    ///
462    /// let parse_from_str = NaiveTime::parse_from_str;
463    ///
464    /// assert_eq!(parse_from_str("23:56:04", "%H:%M:%S"),
465    ///            Ok(NaiveTime::from_hms_opt(23, 56, 4).unwrap()));
466    /// assert_eq!(parse_from_str("pm012345.6789", "%p%I%M%S%.f"),
467    ///            Ok(NaiveTime::from_hms_micro_opt(13, 23, 45, 678_900).unwrap()));
468    /// ```
469    ///
470    /// Date and offset is ignored for the purpose of parsing.
471    ///
472    /// ```
473    /// # use chrono::NaiveTime;
474    /// # let parse_from_str = NaiveTime::parse_from_str;
475    /// assert_eq!(parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
476    ///            Ok(NaiveTime::from_hms_opt(12, 34, 56).unwrap()));
477    /// ```
478    ///
479    /// [Leap seconds](#leap-second-handling) are correctly handled by
480    /// treating any time of the form `hh:mm:60` as a leap second.
481    /// (This equally applies to the formatting, so the round trip is possible.)
482    ///
483    /// ```
484    /// # use chrono::NaiveTime;
485    /// # let parse_from_str = NaiveTime::parse_from_str;
486    /// assert_eq!(parse_from_str("08:59:60.123", "%H:%M:%S%.f"),
487    ///            Ok(NaiveTime::from_hms_milli_opt(8, 59, 59, 1_123).unwrap()));
488    /// ```
489    ///
490    /// Missing seconds are assumed to be zero,
491    /// but out-of-bound times or insufficient fields are errors otherwise.
492    ///
493    /// ```
494    /// # use chrono::NaiveTime;
495    /// # let parse_from_str = NaiveTime::parse_from_str;
496    /// assert_eq!(parse_from_str("7:15", "%H:%M"),
497    ///            Ok(NaiveTime::from_hms_opt(7, 15, 0).unwrap()));
498    ///
499    /// assert!(parse_from_str("04m33s", "%Mm%Ss").is_err());
500    /// assert!(parse_from_str("12", "%H").is_err());
501    /// assert!(parse_from_str("17:60", "%H:%M").is_err());
502    /// assert!(parse_from_str("24:00:00", "%H:%M:%S").is_err());
503    /// ```
504    ///
505    /// All parsed fields should be consistent to each other, otherwise it's an error.
506    /// Here `%H` is for 24-hour clocks, unlike `%I`,
507    /// and thus can be independently determined without AM/PM.
508    ///
509    /// ```
510    /// # use chrono::NaiveTime;
511    /// # let parse_from_str = NaiveTime::parse_from_str;
512    /// assert!(parse_from_str("13:07 AM", "%H:%M %p").is_err());
513    /// ```
514    pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<NaiveTime> {
515        let mut parsed = Parsed::new();
516        parse(&mut parsed, s, StrftimeItems::new(fmt))?;
517        parsed.to_naive_time()
518    }
519
520    /// Parses a string from a user-specified format into a new `NaiveTime` value, and a slice with
521    /// the remaining portion of the string.
522    /// See the [`format::strftime` module](../format/strftime/index.html)
523    /// on the supported escape sequences.
524    ///
525    /// Similar to [`parse_from_str`](#method.parse_from_str).
526    ///
527    /// # Example
528    ///
529    /// ```rust
530    /// # use chrono::{NaiveTime};
531    /// let (time, remainder) = NaiveTime::parse_and_remainder(
532    ///     "3h4m33s trailing text", "%-Hh%-Mm%-Ss").unwrap();
533    /// assert_eq!(time, NaiveTime::from_hms_opt(3, 4, 33).unwrap());
534    /// assert_eq!(remainder, " trailing text");
535    /// ```
536    pub fn parse_and_remainder<'a>(s: &'a str, fmt: &str) -> ParseResult<(NaiveTime, &'a str)> {
537        let mut parsed = Parsed::new();
538        let remainder = parse_and_remainder(&mut parsed, s, StrftimeItems::new(fmt))?;
539        parsed.to_naive_time().map(|t| (t, remainder))
540    }
541
542    /// Adds given `Duration` to the current time, and also returns the number of *seconds*
543    /// in the integral number of days ignored from the addition.
544    ///
545    /// # Example
546    ///
547    /// ```
548    /// use chrono::{Duration, NaiveTime};
549    ///
550    /// let from_hms = |h, m, s| { NaiveTime::from_hms_opt(h, m, s).unwrap() };
551    ///
552    /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(11)),
553    ///            (from_hms(14, 4, 5), 0));
554    /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(23)),
555    ///            (from_hms(2, 4, 5), 86_400));
556    /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(-7)),
557    ///            (from_hms(20, 4, 5), -86_400));
558    /// ```
559    #[must_use]
560    pub fn overflowing_add_signed(&self, mut rhs: OldDuration) -> (NaiveTime, i64) {
561        let mut secs = self.secs;
562        let mut frac = self.frac;
563
564        // check if `self` is a leap second and adding `rhs` would escape that leap second.
565        // if it's the case, update `self` and `rhs` to involve no leap second;
566        // otherwise the addition immediately finishes.
567        if frac >= 1_000_000_000 {
568            let rfrac = 2_000_000_000 - frac;
569            if rhs >= OldDuration::nanoseconds(i64::from(rfrac)) {
570                rhs = rhs - OldDuration::nanoseconds(i64::from(rfrac));
571                secs += 1;
572                frac = 0;
573            } else if rhs < OldDuration::nanoseconds(-i64::from(frac)) {
574                rhs = rhs + OldDuration::nanoseconds(i64::from(frac));
575                frac = 0;
576            } else {
577                frac = (i64::from(frac) + rhs.num_nanoseconds().unwrap()) as u32;
578                debug_assert!(frac < 2_000_000_000);
579                return (NaiveTime { secs, frac }, 0);
580            }
581        }
582        debug_assert!(secs <= 86_400);
583        debug_assert!(frac < 1_000_000_000);
584
585        let rhssecs = rhs.num_seconds();
586        let rhsfrac = (rhs - OldDuration::seconds(rhssecs)).num_nanoseconds().unwrap();
587        debug_assert_eq!(OldDuration::seconds(rhssecs) + OldDuration::nanoseconds(rhsfrac), rhs);
588        let rhssecsinday = rhssecs % 86_400;
589        let mut morerhssecs = rhssecs - rhssecsinday;
590        let rhssecs = rhssecsinday as i32;
591        let rhsfrac = rhsfrac as i32;
592        debug_assert!(-86_400 < rhssecs && rhssecs < 86_400);
593        debug_assert_eq!(morerhssecs % 86_400, 0);
594        debug_assert!(-1_000_000_000 < rhsfrac && rhsfrac < 1_000_000_000);
595
596        let mut secs = secs as i32 + rhssecs;
597        let mut frac = frac as i32 + rhsfrac;
598        debug_assert!(-86_400 < secs && secs < 2 * 86_400);
599        debug_assert!(-1_000_000_000 < frac && frac < 2_000_000_000);
600
601        if frac < 0 {
602            frac += 1_000_000_000;
603            secs -= 1;
604        } else if frac >= 1_000_000_000 {
605            frac -= 1_000_000_000;
606            secs += 1;
607        }
608        debug_assert!((-86_400..2 * 86_400).contains(&secs));
609        debug_assert!((0..1_000_000_000).contains(&frac));
610
611        if secs < 0 {
612            secs += 86_400;
613            morerhssecs -= 86_400;
614        } else if secs >= 86_400 {
615            secs -= 86_400;
616            morerhssecs += 86_400;
617        }
618        debug_assert!((0..86_400).contains(&secs));
619
620        (NaiveTime { secs: secs as u32, frac: frac as u32 }, morerhssecs)
621    }
622
623    /// Subtracts given `Duration` from the current time, and also returns the number of *seconds*
624    /// in the integral number of days ignored from the subtraction.
625    ///
626    /// # Example
627    ///
628    /// ```
629    /// use chrono::{Duration, NaiveTime};
630    ///
631    /// let from_hms = |h, m, s| { NaiveTime::from_hms_opt(h, m, s).unwrap() };
632    ///
633    /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(2)),
634    ///            (from_hms(1, 4, 5), 0));
635    /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(17)),
636    ///            (from_hms(10, 4, 5), 86_400));
637    /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(-22)),
638    ///            (from_hms(1, 4, 5), -86_400));
639    /// ```
640    #[inline]
641    #[must_use]
642    pub fn overflowing_sub_signed(&self, rhs: OldDuration) -> (NaiveTime, i64) {
643        let (time, rhs) = self.overflowing_add_signed(-rhs);
644        (time, -rhs) // safe to negate, rhs is within +/- (2^63 / 1000)
645    }
646
647    /// Subtracts another `NaiveTime` from the current time.
648    /// Returns a `Duration` within +/- 1 day.
649    /// This does not overflow or underflow at all.
650    ///
651    /// As a part of Chrono's [leap second handling](#leap-second-handling),
652    /// the subtraction assumes that **there is no leap second ever**,
653    /// except when any of the `NaiveTime`s themselves represents a leap second
654    /// in which case the assumption becomes that
655    /// **there are exactly one (or two) leap second(s) ever**.
656    ///
657    /// # Example
658    ///
659    /// ```
660    /// use chrono::{Duration, NaiveTime};
661    ///
662    /// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
663    /// let since = NaiveTime::signed_duration_since;
664    ///
665    /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 900)),
666    ///            Duration::zero());
667    /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 875)),
668    ///            Duration::milliseconds(25));
669    /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)),
670    ///            Duration::milliseconds(975));
671    /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)),
672    ///            Duration::seconds(7));
673    /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)),
674    ///            Duration::seconds(5 * 60));
675    /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(0, 5, 7, 900)),
676    ///            Duration::seconds(3 * 3600));
677    /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)),
678    ///            Duration::seconds(-3600));
679    /// assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(2, 4, 6, 800)),
680    ///            Duration::seconds(3600 + 60 + 1) + Duration::milliseconds(100));
681    /// ```
682    ///
683    /// Leap seconds are handled, but the subtraction assumes that
684    /// there were no other leap seconds happened.
685    ///
686    /// ```
687    /// # use chrono::{Duration, NaiveTime};
688    /// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
689    /// # let since = NaiveTime::signed_duration_since;
690    /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 59, 0)),
691    ///            Duration::seconds(1));
692    /// assert_eq!(since(from_hmsm(3, 0, 59, 1_500), from_hmsm(3, 0, 59, 0)),
693    ///            Duration::milliseconds(1500));
694    /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 0, 0)),
695    ///            Duration::seconds(60));
696    /// assert_eq!(since(from_hmsm(3, 0, 0, 0), from_hmsm(2, 59, 59, 1_000)),
697    ///            Duration::seconds(1));
698    /// assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(2, 59, 59, 1_000)),
699    ///            Duration::seconds(61));
700    /// ```
701    #[must_use]
702    pub fn signed_duration_since(self, rhs: NaiveTime) -> OldDuration {
703        //     |    |    :leap|    |    |    |    |    |    |    :leap|    |
704        //     |    |    :    |    |    |    |    |    |    |    :    |    |
705        // ----+----+-----*---+----+----+----+----+----+----+-------*-+----+----
706        //          |   `rhs` |                             |    `self`
707        //          |======================================>|       |
708        //          |     |  `self.secs - rhs.secs`         |`self.frac`
709        //          |====>|   |                             |======>|
710        //      `rhs.frac`|========================================>|
711        //          |     |   |        `self - rhs`         |       |
712
713        use core::cmp::Ordering;
714
715        let secs = i64::from(self.secs) - i64::from(rhs.secs);
716        let frac = i64::from(self.frac) - i64::from(rhs.frac);
717
718        // `secs` may contain a leap second yet to be counted
719        let adjust = match self.secs.cmp(&rhs.secs) {
720            Ordering::Greater => i64::from(rhs.frac >= 1_000_000_000),
721            Ordering::Equal => 0,
722            Ordering::Less => {
723                if self.frac >= 1_000_000_000 {
724                    -1
725                } else {
726                    0
727                }
728            }
729        };
730
731        OldDuration::seconds(secs + adjust) + OldDuration::nanoseconds(frac)
732    }
733
734    /// Formats the time with the specified formatting items.
735    /// Otherwise it is the same as the ordinary [`format`](#method.format) method.
736    ///
737    /// The `Iterator` of items should be `Clone`able,
738    /// since the resulting `DelayedFormat` value may be formatted multiple times.
739    ///
740    /// # Example
741    ///
742    /// ```
743    /// use chrono::NaiveTime;
744    /// use chrono::format::strftime::StrftimeItems;
745    ///
746    /// let fmt = StrftimeItems::new("%H:%M:%S");
747    /// let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
748    /// assert_eq!(t.format_with_items(fmt.clone()).to_string(), "23:56:04");
749    /// assert_eq!(t.format("%H:%M:%S").to_string(),             "23:56:04");
750    /// ```
751    ///
752    /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
753    ///
754    /// ```
755    /// # use chrono::NaiveTime;
756    /// # use chrono::format::strftime::StrftimeItems;
757    /// # let fmt = StrftimeItems::new("%H:%M:%S").clone();
758    /// # let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
759    /// assert_eq!(format!("{}", t.format_with_items(fmt)), "23:56:04");
760    /// ```
761    #[cfg(any(feature = "alloc", feature = "std"))]
762    #[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
763    #[inline]
764    #[must_use]
765    pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
766    where
767        I: Iterator<Item = B> + Clone,
768        B: Borrow<Item<'a>>,
769    {
770        DelayedFormat::new(None, Some(*self), items)
771    }
772
773    /// Formats the time with the specified format string.
774    /// See the [`format::strftime` module](../format/strftime/index.html)
775    /// on the supported escape sequences.
776    ///
777    /// This returns a `DelayedFormat`,
778    /// which gets converted to a string only when actual formatting happens.
779    /// You may use the `to_string` method to get a `String`,
780    /// or just feed it into `print!` and other formatting macros.
781    /// (In this way it avoids the redundant memory allocation.)
782    ///
783    /// A wrong format string does *not* issue an error immediately.
784    /// Rather, converting or formatting the `DelayedFormat` fails.
785    /// You are recommended to immediately use `DelayedFormat` for this reason.
786    ///
787    /// # Example
788    ///
789    /// ```
790    /// use chrono::NaiveTime;
791    ///
792    /// let t = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
793    /// assert_eq!(t.format("%H:%M:%S").to_string(), "23:56:04");
794    /// assert_eq!(t.format("%H:%M:%S%.6f").to_string(), "23:56:04.012345");
795    /// assert_eq!(t.format("%-I:%M %p").to_string(), "11:56 PM");
796    /// ```
797    ///
798    /// The resulting `DelayedFormat` can be formatted directly via the `Display` trait.
799    ///
800    /// ```
801    /// # use chrono::NaiveTime;
802    /// # let t = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
803    /// assert_eq!(format!("{}", t.format("%H:%M:%S")), "23:56:04");
804    /// assert_eq!(format!("{}", t.format("%H:%M:%S%.6f")), "23:56:04.012345");
805    /// assert_eq!(format!("{}", t.format("%-I:%M %p")), "11:56 PM");
806    /// ```
807    #[cfg(any(feature = "alloc", feature = "std"))]
808    #[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
809    #[inline]
810    #[must_use]
811    pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
812        self.format_with_items(StrftimeItems::new(fmt))
813    }
814
815    /// Returns a triple of the hour, minute and second numbers.
816    pub(crate) fn hms(&self) -> (u32, u32, u32) {
817        let sec = self.secs % 60;
818        let mins = self.secs / 60;
819        let min = mins % 60;
820        let hour = mins / 60;
821        (hour, min, sec)
822    }
823
824    /// The earliest possible `NaiveTime`
825    pub const MIN: Self = Self { secs: 0, frac: 0 };
826    pub(super) const MAX: Self = Self { secs: 23 * 3600 + 59 * 60 + 59, frac: 999_999_999 };
827}
828
829impl Timelike for NaiveTime {
830    /// Returns the hour number from 0 to 23.
831    ///
832    /// # Example
833    ///
834    /// ```
835    /// use chrono::{NaiveTime, Timelike};
836    ///
837    /// assert_eq!(NaiveTime::from_hms_opt(0, 0, 0).unwrap().hour(), 0);
838    /// assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().hour(), 23);
839    /// ```
840    #[inline]
841    fn hour(&self) -> u32 {
842        self.hms().0
843    }
844
845    /// Returns the minute number from 0 to 59.
846    ///
847    /// # Example
848    ///
849    /// ```
850    /// use chrono::{NaiveTime, Timelike};
851    ///
852    /// assert_eq!(NaiveTime::from_hms_opt(0, 0, 0).unwrap().minute(), 0);
853    /// assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().minute(), 56);
854    /// ```
855    #[inline]
856    fn minute(&self) -> u32 {
857        self.hms().1
858    }
859
860    /// Returns the second number from 0 to 59.
861    ///
862    /// # Example
863    ///
864    /// ```
865    /// use chrono::{NaiveTime, Timelike};
866    ///
867    /// assert_eq!(NaiveTime::from_hms_opt(0, 0, 0).unwrap().second(), 0);
868    /// assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().second(), 4);
869    /// ```
870    ///
871    /// This method never returns 60 even when it is a leap second.
872    /// ([Why?](#leap-second-handling))
873    /// Use the proper [formatting method](#method.format) to get a human-readable representation.
874    ///
875    #[cfg_attr(not(feature = "std"), doc = "```ignore")]
876    #[cfg_attr(feature = "std", doc = "```")]
877    /// # use chrono::{NaiveTime, Timelike};
878    /// let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
879    /// assert_eq!(leap.second(), 59);
880    /// assert_eq!(leap.format("%H:%M:%S").to_string(), "23:59:60");
881    /// ```
882    #[inline]
883    fn second(&self) -> u32 {
884        self.hms().2
885    }
886
887    /// Returns the number of nanoseconds since the whole non-leap second.
888    /// The range from 1,000,000,000 to 1,999,999,999 represents
889    /// the [leap second](#leap-second-handling).
890    ///
891    /// # Example
892    ///
893    /// ```
894    /// use chrono::{NaiveTime, Timelike};
895    ///
896    /// assert_eq!(NaiveTime::from_hms_opt(0, 0, 0).unwrap().nanosecond(), 0);
897    /// assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().nanosecond(), 12_345_678);
898    /// ```
899    ///
900    /// Leap seconds may have seemingly out-of-range return values.
901    /// You can reduce the range with `time.nanosecond() % 1_000_000_000`, or
902    /// use the proper [formatting method](#method.format) to get a human-readable representation.
903    ///
904    #[cfg_attr(not(feature = "std"), doc = "```ignore")]
905    #[cfg_attr(feature = "std", doc = "```")]
906    /// # use chrono::{NaiveTime, Timelike};
907    /// let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
908    /// assert_eq!(leap.nanosecond(), 1_000_000_000);
909    /// assert_eq!(leap.format("%H:%M:%S%.9f").to_string(), "23:59:60.000000000");
910    /// ```
911    #[inline]
912    fn nanosecond(&self) -> u32 {
913        self.frac
914    }
915
916    /// Makes a new `NaiveTime` with the hour number changed.
917    ///
918    /// # Errors
919    ///
920    /// Returns `None` if the value for `hour` is invalid.
921    ///
922    /// # Example
923    ///
924    /// ```
925    /// use chrono::{NaiveTime, Timelike};
926    ///
927    /// let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
928    /// assert_eq!(dt.with_hour(7), Some(NaiveTime::from_hms_nano_opt(7, 56, 4, 12_345_678).unwrap()));
929    /// assert_eq!(dt.with_hour(24), None);
930    /// ```
931    #[inline]
932    fn with_hour(&self, hour: u32) -> Option<NaiveTime> {
933        if hour >= 24 {
934            return None;
935        }
936        let secs = hour * 3600 + self.secs % 3600;
937        Some(NaiveTime { secs, ..*self })
938    }
939
940    /// Makes a new `NaiveTime` with the minute number changed.
941    ///
942    /// # Errors
943    ///
944    /// Returns `None` if the value for `minute` is invalid.
945    ///
946    /// # Example
947    ///
948    /// ```
949    /// use chrono::{NaiveTime, Timelike};
950    ///
951    /// let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
952    /// assert_eq!(dt.with_minute(45), Some(NaiveTime::from_hms_nano_opt(23, 45, 4, 12_345_678).unwrap()));
953    /// assert_eq!(dt.with_minute(60), None);
954    /// ```
955    #[inline]
956    fn with_minute(&self, min: u32) -> Option<NaiveTime> {
957        if min >= 60 {
958            return None;
959        }
960        let secs = self.secs / 3600 * 3600 + min * 60 + self.secs % 60;
961        Some(NaiveTime { secs, ..*self })
962    }
963
964    /// Makes a new `NaiveTime` with the second number changed.
965    ///
966    /// As with the [`second`](#method.second) method,
967    /// the input range is restricted to 0 through 59.
968    ///
969    /// # Errors
970    ///
971    /// Returns `None` if the value for `second` is invalid.
972    ///
973    /// # Example
974    ///
975    /// ```
976    /// use chrono::{NaiveTime, Timelike};
977    ///
978    /// let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
979    /// assert_eq!(dt.with_second(17), Some(NaiveTime::from_hms_nano_opt(23, 56, 17, 12_345_678).unwrap()));
980    /// assert_eq!(dt.with_second(60), None);
981    /// ```
982    #[inline]
983    fn with_second(&self, sec: u32) -> Option<NaiveTime> {
984        if sec >= 60 {
985            return None;
986        }
987        let secs = self.secs / 60 * 60 + sec;
988        Some(NaiveTime { secs, ..*self })
989    }
990
991    /// Makes a new `NaiveTime` with nanoseconds since the whole non-leap second changed.
992    ///
993    /// As with the [`nanosecond`](#method.nanosecond) method,
994    /// the input range can exceed 1,000,000,000 for leap seconds.
995    ///
996    /// # Errors
997    ///
998    /// Returns `None` if `nanosecond >= 2,000,000,000`.
999    ///
1000    /// # Example
1001    ///
1002    /// ```
1003    /// use chrono::{NaiveTime, Timelike};
1004    ///
1005    /// let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
1006    /// assert_eq!(dt.with_nanosecond(333_333_333),
1007    ///            Some(NaiveTime::from_hms_nano_opt(23, 56, 4, 333_333_333).unwrap()));
1008    /// assert_eq!(dt.with_nanosecond(2_000_000_000), None);
1009    /// ```
1010    ///
1011    /// Leap seconds can theoretically follow *any* whole second.
1012    /// The following would be a proper leap second at the time zone offset of UTC-00:03:57
1013    /// (there are several historical examples comparable to this "non-sense" offset),
1014    /// and therefore is allowed.
1015    ///
1016    /// ```
1017    /// # use chrono::{NaiveTime, Timelike};
1018    /// # let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
1019    /// assert_eq!(dt.with_nanosecond(1_333_333_333),
1020    ///            Some(NaiveTime::from_hms_nano_opt(23, 56, 4, 1_333_333_333).unwrap()));
1021    /// ```
1022    #[inline]
1023    fn with_nanosecond(&self, nano: u32) -> Option<NaiveTime> {
1024        if nano >= 2_000_000_000 {
1025            return None;
1026        }
1027        Some(NaiveTime { frac: nano, ..*self })
1028    }
1029
1030    /// Returns the number of non-leap seconds past the last midnight.
1031    ///
1032    /// # Example
1033    ///
1034    /// ```
1035    /// use chrono::{NaiveTime, Timelike};
1036    ///
1037    /// assert_eq!(NaiveTime::from_hms_opt(1, 2, 3).unwrap().num_seconds_from_midnight(),
1038    ///            3723);
1039    /// assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().num_seconds_from_midnight(),
1040    ///            86164);
1041    /// assert_eq!(NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap().num_seconds_from_midnight(),
1042    ///            86399);
1043    /// ```
1044    #[inline]
1045    fn num_seconds_from_midnight(&self) -> u32 {
1046        self.secs // do not repeat the calculation!
1047    }
1048}
1049
1050/// An addition of `Duration` to `NaiveTime` wraps around and never overflows or underflows.
1051/// In particular the addition ignores integral number of days.
1052///
1053/// As a part of Chrono's [leap second handling], the addition assumes that **there is no leap
1054/// second ever**, except when the `NaiveTime` itself represents a leap second in which case the
1055/// assumption becomes that **there is exactly a single leap second ever**.
1056///
1057/// # Example
1058///
1059/// ```
1060/// use chrono::{Duration, NaiveTime};
1061///
1062/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
1063///
1064/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::zero(),                  from_hmsm(3, 5, 7, 0));
1065/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(1),              from_hmsm(3, 5, 8, 0));
1066/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(-1),             from_hmsm(3, 5, 6, 0));
1067/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(60 + 4),         from_hmsm(3, 6, 11, 0));
1068/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(7*60*60 - 6*60), from_hmsm(9, 59, 7, 0));
1069/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::milliseconds(80),        from_hmsm(3, 5, 7, 80));
1070/// assert_eq!(from_hmsm(3, 5, 7, 950) + Duration::milliseconds(280),     from_hmsm(3, 5, 8, 230));
1071/// assert_eq!(from_hmsm(3, 5, 7, 950) + Duration::milliseconds(-980),    from_hmsm(3, 5, 6, 970));
1072/// ```
1073///
1074/// The addition wraps around.
1075///
1076/// ```
1077/// # use chrono::{Duration, NaiveTime};
1078/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
1079/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(22*60*60), from_hmsm(1, 5, 7, 0));
1080/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(-8*60*60), from_hmsm(19, 5, 7, 0));
1081/// assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::days(800),         from_hmsm(3, 5, 7, 0));
1082/// ```
1083///
1084/// Leap seconds are handled, but the addition assumes that it is the only leap second happened.
1085///
1086/// ```
1087/// # use chrono::{Duration, NaiveTime};
1088/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
1089/// let leap = from_hmsm(3, 5, 59, 1_300);
1090/// assert_eq!(leap + Duration::zero(),             from_hmsm(3, 5, 59, 1_300));
1091/// assert_eq!(leap + Duration::milliseconds(-500), from_hmsm(3, 5, 59, 800));
1092/// assert_eq!(leap + Duration::milliseconds(500),  from_hmsm(3, 5, 59, 1_800));
1093/// assert_eq!(leap + Duration::milliseconds(800),  from_hmsm(3, 6, 0, 100));
1094/// assert_eq!(leap + Duration::seconds(10),        from_hmsm(3, 6, 9, 300));
1095/// assert_eq!(leap + Duration::seconds(-10),       from_hmsm(3, 5, 50, 300));
1096/// assert_eq!(leap + Duration::days(1),            from_hmsm(3, 5, 59, 300));
1097/// ```
1098///
1099/// [leap second handling]: crate::NaiveTime#leap-second-handling
1100impl Add<OldDuration> for NaiveTime {
1101    type Output = NaiveTime;
1102
1103    #[inline]
1104    fn add(self, rhs: OldDuration) -> NaiveTime {
1105        self.overflowing_add_signed(rhs).0
1106    }
1107}
1108
1109impl AddAssign<OldDuration> for NaiveTime {
1110    #[inline]
1111    fn add_assign(&mut self, rhs: OldDuration) {
1112        *self = self.add(rhs);
1113    }
1114}
1115
1116impl Add<Duration> for NaiveTime {
1117    type Output = NaiveTime;
1118
1119    #[inline]
1120    fn add(self, rhs: Duration) -> NaiveTime {
1121        let rhs = OldDuration::from_std(rhs)
1122            .expect("overflow converting from core::time::Duration to chrono::Duration");
1123        self.overflowing_add_signed(rhs).0
1124    }
1125}
1126
1127impl AddAssign<Duration> for NaiveTime {
1128    #[inline]
1129    fn add_assign(&mut self, rhs: Duration) {
1130        let rhs = OldDuration::from_std(rhs)
1131            .expect("overflow converting from core::time::Duration to chrono::Duration");
1132        *self += rhs;
1133    }
1134}
1135
1136/// A subtraction of `Duration` from `NaiveTime` wraps around and never overflows or underflows.
1137/// In particular the addition ignores integral number of days.
1138/// It is the same as the addition with a negated `Duration`.
1139///
1140/// As a part of Chrono's [leap second handling], the subtraction assumes that **there is no leap
1141/// second ever**, except when the `NaiveTime` itself represents a leap second in which case the
1142/// assumption becomes that **there is exactly a single leap second ever**.
1143///
1144/// # Example
1145///
1146/// ```
1147/// use chrono::{Duration, NaiveTime};
1148///
1149/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
1150///
1151/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::zero(),                  from_hmsm(3, 5, 7, 0));
1152/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(1),              from_hmsm(3, 5, 6, 0));
1153/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(60 + 5),         from_hmsm(3, 4, 2, 0));
1154/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(2*60*60 + 6*60), from_hmsm(0, 59, 7, 0));
1155/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::milliseconds(80),        from_hmsm(3, 5, 6, 920));
1156/// assert_eq!(from_hmsm(3, 5, 7, 950) - Duration::milliseconds(280),     from_hmsm(3, 5, 7, 670));
1157/// ```
1158///
1159/// The subtraction wraps around.
1160///
1161/// ```
1162/// # use chrono::{Duration, NaiveTime};
1163/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
1164/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::seconds(8*60*60), from_hmsm(19, 5, 7, 0));
1165/// assert_eq!(from_hmsm(3, 5, 7, 0) - Duration::days(800),        from_hmsm(3, 5, 7, 0));
1166/// ```
1167///
1168/// Leap seconds are handled, but the subtraction assumes that it is the only leap second happened.
1169///
1170/// ```
1171/// # use chrono::{Duration, NaiveTime};
1172/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
1173/// let leap = from_hmsm(3, 5, 59, 1_300);
1174/// assert_eq!(leap - Duration::zero(),            from_hmsm(3, 5, 59, 1_300));
1175/// assert_eq!(leap - Duration::milliseconds(200), from_hmsm(3, 5, 59, 1_100));
1176/// assert_eq!(leap - Duration::milliseconds(500), from_hmsm(3, 5, 59, 800));
1177/// assert_eq!(leap - Duration::seconds(60),       from_hmsm(3, 5, 0, 300));
1178/// assert_eq!(leap - Duration::days(1),           from_hmsm(3, 6, 0, 300));
1179/// ```
1180///
1181/// [leap second handling]: crate::NaiveTime#leap-second-handling
1182impl Sub<OldDuration> for NaiveTime {
1183    type Output = NaiveTime;
1184
1185    #[inline]
1186    fn sub(self, rhs: OldDuration) -> NaiveTime {
1187        self.overflowing_sub_signed(rhs).0
1188    }
1189}
1190
1191impl SubAssign<OldDuration> for NaiveTime {
1192    #[inline]
1193    fn sub_assign(&mut self, rhs: OldDuration) {
1194        *self = self.sub(rhs);
1195    }
1196}
1197
1198impl Sub<Duration> for NaiveTime {
1199    type Output = NaiveTime;
1200
1201    #[inline]
1202    fn sub(self, rhs: Duration) -> NaiveTime {
1203        let rhs = OldDuration::from_std(rhs)
1204            .expect("overflow converting from core::time::Duration to chrono::Duration");
1205        self.overflowing_sub_signed(rhs).0
1206    }
1207}
1208
1209impl SubAssign<Duration> for NaiveTime {
1210    #[inline]
1211    fn sub_assign(&mut self, rhs: Duration) {
1212        let rhs = OldDuration::from_std(rhs)
1213            .expect("overflow converting from core::time::Duration to chrono::Duration");
1214        *self -= rhs;
1215    }
1216}
1217
1218/// Subtracts another `NaiveTime` from the current time.
1219/// Returns a `Duration` within +/- 1 day.
1220/// This does not overflow or underflow at all.
1221///
1222/// As a part of Chrono's [leap second handling](#leap-second-handling),
1223/// the subtraction assumes that **there is no leap second ever**,
1224/// except when any of the `NaiveTime`s themselves represents a leap second
1225/// in which case the assumption becomes that
1226/// **there are exactly one (or two) leap second(s) ever**.
1227///
1228/// The implementation is a wrapper around
1229/// [`NaiveTime::signed_duration_since`](#method.signed_duration_since).
1230///
1231/// # Example
1232///
1233/// ```
1234/// use chrono::{Duration, NaiveTime};
1235///
1236/// let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
1237///
1238/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 900), Duration::zero());
1239/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 875), Duration::milliseconds(25));
1240/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 6, 925), Duration::milliseconds(975));
1241/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 0, 900), Duration::seconds(7));
1242/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 0, 7, 900), Duration::seconds(5 * 60));
1243/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(0, 5, 7, 900), Duration::seconds(3 * 3600));
1244/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(4, 5, 7, 900), Duration::seconds(-3600));
1245/// assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(2, 4, 6, 800),
1246///            Duration::seconds(3600 + 60 + 1) + Duration::milliseconds(100));
1247/// ```
1248///
1249/// Leap seconds are handled, but the subtraction assumes that
1250/// there were no other leap seconds happened.
1251///
1252/// ```
1253/// # use chrono::{Duration, NaiveTime};
1254/// # let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap() };
1255/// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 59, 0), Duration::seconds(1));
1256/// assert_eq!(from_hmsm(3, 0, 59, 1_500) - from_hmsm(3, 0, 59, 0),
1257///            Duration::milliseconds(1500));
1258/// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 0, 0), Duration::seconds(60));
1259/// assert_eq!(from_hmsm(3, 0, 0, 0) - from_hmsm(2, 59, 59, 1_000), Duration::seconds(1));
1260/// assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(2, 59, 59, 1_000),
1261///            Duration::seconds(61));
1262/// ```
1263impl Sub<NaiveTime> for NaiveTime {
1264    type Output = OldDuration;
1265
1266    #[inline]
1267    fn sub(self, rhs: NaiveTime) -> OldDuration {
1268        self.signed_duration_since(rhs)
1269    }
1270}
1271
1272/// The `Debug` output of the naive time `t` is the same as
1273/// [`t.format("%H:%M:%S%.f")`](../format/strftime/index.html).
1274///
1275/// The string printed can be readily parsed via the `parse` method on `str`.
1276///
1277/// It should be noted that, for leap seconds not on the minute boundary,
1278/// it may print a representation not distinguishable from non-leap seconds.
1279/// This doesn't matter in practice, since such leap seconds never happened.
1280/// (By the time of the first leap second on 1972-06-30,
1281/// every time zone offset around the world has standardized to the 5-minute alignment.)
1282///
1283/// # Example
1284///
1285/// ```
1286/// use chrono::NaiveTime;
1287///
1288/// assert_eq!(format!("{:?}", NaiveTime::from_hms_opt(23, 56, 4).unwrap()),              "23:56:04");
1289/// assert_eq!(format!("{:?}", NaiveTime::from_hms_milli_opt(23, 56, 4, 12).unwrap()),    "23:56:04.012");
1290/// assert_eq!(format!("{:?}", NaiveTime::from_hms_micro_opt(23, 56, 4, 1234).unwrap()),  "23:56:04.001234");
1291/// assert_eq!(format!("{:?}", NaiveTime::from_hms_nano_opt(23, 56, 4, 123456).unwrap()), "23:56:04.000123456");
1292/// ```
1293///
1294/// Leap seconds may also be used.
1295///
1296/// ```
1297/// # use chrono::NaiveTime;
1298/// assert_eq!(format!("{:?}", NaiveTime::from_hms_milli_opt(6, 59, 59, 1_500).unwrap()), "06:59:60.500");
1299/// ```
1300impl fmt::Debug for NaiveTime {
1301    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1302        let (hour, min, sec) = self.hms();
1303        let (sec, nano) = if self.frac >= 1_000_000_000 {
1304            (sec + 1, self.frac - 1_000_000_000)
1305        } else {
1306            (sec, self.frac)
1307        };
1308
1309        use core::fmt::Write;
1310        write_hundreds(f, hour as u8)?;
1311        f.write_char(':')?;
1312        write_hundreds(f, min as u8)?;
1313        f.write_char(':')?;
1314        write_hundreds(f, sec as u8)?;
1315
1316        if nano == 0 {
1317            Ok(())
1318        } else if nano % 1_000_000 == 0 {
1319            write!(f, ".{:03}", nano / 1_000_000)
1320        } else if nano % 1_000 == 0 {
1321            write!(f, ".{:06}", nano / 1_000)
1322        } else {
1323            write!(f, ".{:09}", nano)
1324        }
1325    }
1326}
1327
1328/// The `Display` output of the naive time `t` is the same as
1329/// [`t.format("%H:%M:%S%.f")`](../format/strftime/index.html).
1330///
1331/// The string printed can be readily parsed via the `parse` method on `str`.
1332///
1333/// It should be noted that, for leap seconds not on the minute boundary,
1334/// it may print a representation not distinguishable from non-leap seconds.
1335/// This doesn't matter in practice, since such leap seconds never happened.
1336/// (By the time of the first leap second on 1972-06-30,
1337/// every time zone offset around the world has standardized to the 5-minute alignment.)
1338///
1339/// # Example
1340///
1341/// ```
1342/// use chrono::NaiveTime;
1343///
1344/// assert_eq!(format!("{}", NaiveTime::from_hms_opt(23, 56, 4).unwrap()),              "23:56:04");
1345/// assert_eq!(format!("{}", NaiveTime::from_hms_milli_opt(23, 56, 4, 12).unwrap()),    "23:56:04.012");
1346/// assert_eq!(format!("{}", NaiveTime::from_hms_micro_opt(23, 56, 4, 1234).unwrap()),  "23:56:04.001234");
1347/// assert_eq!(format!("{}", NaiveTime::from_hms_nano_opt(23, 56, 4, 123456).unwrap()), "23:56:04.000123456");
1348/// ```
1349///
1350/// Leap seconds may also be used.
1351///
1352/// ```
1353/// # use chrono::NaiveTime;
1354/// assert_eq!(format!("{}", NaiveTime::from_hms_milli_opt(6, 59, 59, 1_500).unwrap()), "06:59:60.500");
1355/// ```
1356impl fmt::Display for NaiveTime {
1357    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1358        fmt::Debug::fmt(self, f)
1359    }
1360}
1361
1362/// Parsing a `str` into a `NaiveTime` uses the same format,
1363/// [`%H:%M:%S%.f`](../format/strftime/index.html), as in `Debug` and `Display`.
1364///
1365/// # Example
1366///
1367/// ```
1368/// use chrono::NaiveTime;
1369///
1370/// let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
1371/// assert_eq!("23:56:04".parse::<NaiveTime>(), Ok(t));
1372///
1373/// let t = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
1374/// assert_eq!("23:56:4.012345678".parse::<NaiveTime>(), Ok(t));
1375///
1376/// let t = NaiveTime::from_hms_nano_opt(23, 59, 59, 1_234_567_890).unwrap(); // leap second
1377/// assert_eq!("23:59:60.23456789".parse::<NaiveTime>(), Ok(t));
1378///
1379/// // Seconds are optional
1380/// let t = NaiveTime::from_hms_opt(23, 56, 0).unwrap();
1381/// assert_eq!("23:56".parse::<NaiveTime>(), Ok(t));
1382///
1383/// assert!("foo".parse::<NaiveTime>().is_err());
1384/// ```
1385impl str::FromStr for NaiveTime {
1386    type Err = ParseError;
1387
1388    fn from_str(s: &str) -> ParseResult<NaiveTime> {
1389        const HOUR_AND_MINUTE: &[Item<'static>] = &[
1390            Item::Numeric(Numeric::Hour, Pad::Zero),
1391            Item::Space(""),
1392            Item::Literal(":"),
1393            Item::Numeric(Numeric::Minute, Pad::Zero),
1394        ];
1395        const SECOND_AND_NANOS: &[Item<'static>] = &[
1396            Item::Space(""),
1397            Item::Literal(":"),
1398            Item::Numeric(Numeric::Second, Pad::Zero),
1399            Item::Fixed(Fixed::Nanosecond),
1400            Item::Space(""),
1401        ];
1402        const TRAILING_WHITESPACE: [Item<'static>; 1] = [Item::Space("")];
1403
1404        let mut parsed = Parsed::new();
1405        let s = parse_and_remainder(&mut parsed, s, HOUR_AND_MINUTE.iter())?;
1406        // Seconds are optional, don't fail if parsing them doesn't succeed.
1407        let s = parse_and_remainder(&mut parsed, s, SECOND_AND_NANOS.iter()).unwrap_or(s);
1408        parse(&mut parsed, s, TRAILING_WHITESPACE.iter())?;
1409        parsed.to_naive_time()
1410    }
1411}
1412
1413/// The default value for a NaiveTime is midnight, 00:00:00 exactly.
1414///
1415/// # Example
1416///
1417/// ```rust
1418/// use chrono::NaiveTime;
1419///
1420/// let default_time = NaiveTime::default();
1421/// assert_eq!(default_time, NaiveTime::from_hms_opt(0, 0, 0).unwrap());
1422/// ```
1423impl Default for NaiveTime {
1424    fn default() -> Self {
1425        NaiveTime::from_hms_opt(0, 0, 0).unwrap()
1426    }
1427}
1428
1429#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
1430fn test_encodable_json<F, E>(to_string: F)
1431where
1432    F: Fn(&NaiveTime) -> Result<String, E>,
1433    E: ::std::fmt::Debug,
1434{
1435    assert_eq!(
1436        to_string(&NaiveTime::from_hms_opt(0, 0, 0).unwrap()).ok(),
1437        Some(r#""00:00:00""#.into())
1438    );
1439    assert_eq!(
1440        to_string(&NaiveTime::from_hms_milli_opt(0, 0, 0, 950).unwrap()).ok(),
1441        Some(r#""00:00:00.950""#.into())
1442    );
1443    assert_eq!(
1444        to_string(&NaiveTime::from_hms_milli_opt(0, 0, 59, 1_000).unwrap()).ok(),
1445        Some(r#""00:00:60""#.into())
1446    );
1447    assert_eq!(
1448        to_string(&NaiveTime::from_hms_opt(0, 1, 2).unwrap()).ok(),
1449        Some(r#""00:01:02""#.into())
1450    );
1451    assert_eq!(
1452        to_string(&NaiveTime::from_hms_nano_opt(3, 5, 7, 98765432).unwrap()).ok(),
1453        Some(r#""03:05:07.098765432""#.into())
1454    );
1455    assert_eq!(
1456        to_string(&NaiveTime::from_hms_opt(7, 8, 9).unwrap()).ok(),
1457        Some(r#""07:08:09""#.into())
1458    );
1459    assert_eq!(
1460        to_string(&NaiveTime::from_hms_micro_opt(12, 34, 56, 789).unwrap()).ok(),
1461        Some(r#""12:34:56.000789""#.into())
1462    );
1463    assert_eq!(
1464        to_string(&NaiveTime::from_hms_nano_opt(23, 59, 59, 1_999_999_999).unwrap()).ok(),
1465        Some(r#""23:59:60.999999999""#.into())
1466    );
1467}
1468
1469#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
1470fn test_decodable_json<F, E>(from_str: F)
1471where
1472    F: Fn(&str) -> Result<NaiveTime, E>,
1473    E: ::std::fmt::Debug,
1474{
1475    assert_eq!(from_str(r#""00:00:00""#).ok(), Some(NaiveTime::from_hms_opt(0, 0, 0).unwrap()));
1476    assert_eq!(from_str(r#""0:0:0""#).ok(), Some(NaiveTime::from_hms_opt(0, 0, 0).unwrap()));
1477    assert_eq!(
1478        from_str(r#""00:00:00.950""#).ok(),
1479        Some(NaiveTime::from_hms_milli_opt(0, 0, 0, 950).unwrap())
1480    );
1481    assert_eq!(
1482        from_str(r#""0:0:0.95""#).ok(),
1483        Some(NaiveTime::from_hms_milli_opt(0, 0, 0, 950).unwrap())
1484    );
1485    assert_eq!(
1486        from_str(r#""00:00:60""#).ok(),
1487        Some(NaiveTime::from_hms_milli_opt(0, 0, 59, 1_000).unwrap())
1488    );
1489    assert_eq!(from_str(r#""00:01:02""#).ok(), Some(NaiveTime::from_hms_opt(0, 1, 2).unwrap()));
1490    assert_eq!(
1491        from_str(r#""03:05:07.098765432""#).ok(),
1492        Some(NaiveTime::from_hms_nano_opt(3, 5, 7, 98765432).unwrap())
1493    );
1494    assert_eq!(from_str(r#""07:08:09""#).ok(), Some(NaiveTime::from_hms_opt(7, 8, 9).unwrap()));
1495    assert_eq!(
1496        from_str(r#""12:34:56.000789""#).ok(),
1497        Some(NaiveTime::from_hms_micro_opt(12, 34, 56, 789).unwrap())
1498    );
1499    assert_eq!(
1500        from_str(r#""23:59:60.999999999""#).ok(),
1501        Some(NaiveTime::from_hms_nano_opt(23, 59, 59, 1_999_999_999).unwrap())
1502    );
1503    assert_eq!(
1504        from_str(r#""23:59:60.9999999999997""#).ok(), // excess digits are ignored
1505        Some(NaiveTime::from_hms_nano_opt(23, 59, 59, 1_999_999_999).unwrap())
1506    );
1507
1508    // bad formats
1509    assert!(from_str(r#""""#).is_err());
1510    assert!(from_str(r#""000000""#).is_err());
1511    assert!(from_str(r#""00:00:61""#).is_err());
1512    assert!(from_str(r#""00:60:00""#).is_err());
1513    assert!(from_str(r#""24:00:00""#).is_err());
1514    assert!(from_str(r#""23:59:59,1""#).is_err());
1515    assert!(from_str(r#""012:34:56""#).is_err());
1516    assert!(from_str(r#""hh:mm:ss""#).is_err());
1517    assert!(from_str(r#"0"#).is_err());
1518    assert!(from_str(r#"86399"#).is_err());
1519    assert!(from_str(r#"{}"#).is_err());
1520    // pre-0.3.0 rustc-serialize format is now invalid
1521    assert!(from_str(r#"{"secs":0,"frac":0}"#).is_err());
1522    assert!(from_str(r#"null"#).is_err());
1523}