sea_orm::entity::prelude

Struct Time

Source
pub struct Time { /* private fields */ }
Expand description

ISO 8601 time without timezone. Allows for the nanosecond precision and optional leap second representation.

ยงLeap Second Handling

Since 1960s, the manmade atomic clock has been so accurate that it is much more accurate than Earthโ€™s own motion. It became desirable to define the civil time in terms of the atomic clock, but that risks the desynchronization of the civil time from Earth. To account for this, the designers of the Coordinated Universal Time (UTC) made that the UTC should be kept within 0.9 seconds of the observed Earth-bound time. When the mean solar day is longer than the ideal (86,400 seconds), the error slowly accumulates and it is necessary to add a leap second to slow the UTC down a bit. (We may also remove a second to speed the UTC up a bit, but it never happened.) The leap second, if any, follows 23:59:59 of June 30 or December 31 in the UTC.

Fast forward to the 21st century, we have seen 26 leap seconds from January 1972 to December 2015. Yes, 26 seconds. Probably you can read this paragraph within 26 seconds. But those 26 seconds, and possibly more in the future, are never predictable, and whether to add a leap second or not is known only before 6 months. Internet-based clocks (via NTP) do account for known leap seconds, but the system API normally doesnโ€™t (and often canโ€™t, with no network connection) and there is no reliable way to retrieve leap second information.

Chrono does not try to accurately implement leap seconds; it is impossible. Rather, it allows for leap seconds but behaves as if there are no other leap seconds. Various operations will ignore any possible leap second(s) except when any of the operands were actually leap seconds.

If you cannot tolerate this behavior, you must use a separate TimeZone for the International Atomic Time (TAI). TAI is like UTC but has no leap seconds, and thus slightly differs from UTC. Chrono does not yet provide such implementation, but it is planned.

ยงRepresenting Leap Seconds

The leap second is indicated via fractional seconds more than 1 second. This makes possible to treat a leap second as the prior non-leap second if you donโ€™t care about sub-second accuracy. You should use the proper formatting to get the raw leap second.

All methods accepting fractional seconds will accept such values.

use chrono::{NaiveDate, NaiveTime};

let t = NaiveTime::from_hms_milli_opt(8, 59, 59, 1_000).unwrap();

let dt1 = NaiveDate::from_ymd_opt(2015, 7, 1)
    .unwrap()
    .and_hms_micro_opt(8, 59, 59, 1_000_000)
    .unwrap();

let dt2 = NaiveDate::from_ymd_opt(2015, 6, 30)
    .unwrap()
    .and_hms_nano_opt(23, 59, 59, 1_000_000_000)
    .unwrap()
    .and_utc();

Note that the leap second can happen anytime given an appropriate time zone; 2015-07-01 01:23:60 would be a proper leap second if UTC+01:24 had existed. Practically speaking, though, by the time of the first leap second on 1972-06-30, every time zone offset around the world has standardized to the 5-minute alignment.

ยงDate And Time Arithmetics

As a concrete example, letโ€™s assume that 03:00:60 and 04:00:60 are leap seconds. In reality, of course, leap seconds are separated by at least 6 months. We will also use some intuitive concise notations for the explanation.

Time + TimeDelta (short for NaiveTime::overflowing_add_signed):

  • 03:00:00 + 1s = 03:00:01.
  • 03:00:59 + 60s = 03:01:59.
  • 03:00:59 + 61s = 03:02:00.
  • 03:00:59 + 1s = 03:01:00.
  • 03:00:60 + 1s = 03:01:00. Note that the sum is identical to the previous.
  • 03:00:60 + 60s = 03:01:59.
  • 03:00:60 + 61s = 03:02:00.
  • 03:00:60.1 + 0.8s = 03:00:60.9.

Time - TimeDelta (short for NaiveTime::overflowing_sub_signed):

  • 03:00:00 - 1s = 02:59:59.
  • 03:01:00 - 1s = 03:00:59.
  • 03:01:00 - 60s = 03:00:00.
  • 03:00:60 - 60s = 03:00:00. Note that the result is identical to the previous.
  • 03:00:60.7 - 0.4s = 03:00:60.3.
  • 03:00:60.7 - 0.9s = 03:00:59.8.

Time - Time (short for NaiveTime::signed_duration_since):

  • 04:00:00 - 03:00:00 = 3600s.
  • 03:01:00 - 03:00:00 = 60s.
  • 03:00:60 - 03:00:00 = 60s. Note that the difference is identical to the previous.
  • 03:00:60.6 - 03:00:59.4 = 1.2s.
  • 03:01:00 - 03:00:59.8 = 0.2s.
  • 03:01:00 - 03:00:60.5 = 0.5s. Note that the difference is larger than the previous, even though the leap second clearly follows the previous whole second.
  • 04:00:60.9 - 03:00:60.1 = (04:00:60.9 - 04:00:00) + (04:00:00 - 03:01:00) + (03:01:00 - 03:00:60.1) = 60.9s + 3540s + 0.9s = 3601.8s.

In general,

  • Time + TimeDelta unconditionally equals to TimeDelta + Time.

  • Time - TimeDelta unconditionally equals to Time + (-TimeDelta).

  • Time1 - Time2 unconditionally equals to -(Time2 - Time1).

  • Associativity does not generally hold, because (Time + TimeDelta1) - TimeDelta2 no longer equals to Time + (TimeDelta1 - TimeDelta2) for two positive durations.

    • As a special case, (Time + TimeDelta) - TimeDelta also does not equal to Time.

    • If you can assume that all durations have the same sign, however, then the associativity holds: (Time + TimeDelta1) + TimeDelta2 equals to Time + (TimeDelta1 + TimeDelta2) for two positive durations.

ยงReading And Writing Leap Seconds

The โ€œtypicalโ€ leap seconds on the minute boundary are correctly handled both in the formatting and parsing. The leap second in the human-readable representation will be represented as the second part being 60, as required by ISO 8601.

use chrono::NaiveDate;

let dt = NaiveDate::from_ymd_opt(2015, 6, 30)
    .unwrap()
    .and_hms_milli_opt(23, 59, 59, 1_000)
    .unwrap()
    .and_utc();
assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60Z");

There are hypothetical leap seconds not on the minute boundary nevertheless supported by Chrono. They are allowed for the sake of completeness and consistency; there were several โ€œexoticโ€ time zone offsets with fractional minutes prior to UTC after all. For such cases the human-readable representation is ambiguous and would be read back to the next non-leap second.

A NaiveTime with a leap second that is not on a minute boundary can only be created from a DateTime with fractional minutes as offset, or using Timelike::with_nanosecond().

use chrono::{FixedOffset, NaiveDate, TimeZone};

let paramaribo_pre1945 = FixedOffset::east_opt(-13236).unwrap(); // -03:40:36
let leap_sec_2015 =
    NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_000).unwrap();
let dt1 = paramaribo_pre1945.from_utc_datetime(&leap_sec_2015);
assert_eq!(format!("{:?}", dt1), "2015-06-30T20:19:24-03:40:36");
assert_eq!(format!("{:?}", dt1.time()), "20:19:24");

let next_sec = NaiveDate::from_ymd_opt(2015, 7, 1).unwrap().and_hms_opt(0, 0, 0).unwrap();
let dt2 = paramaribo_pre1945.from_utc_datetime(&next_sec);
assert_eq!(format!("{:?}", dt2), "2015-06-30T20:19:24-03:40:36");
assert_eq!(format!("{:?}", dt2.time()), "20:19:24");

assert!(dt1.time() != dt2.time());
assert!(dt1.time().to_string() == dt2.time().to_string());

Since Chrono alone cannot determine any existence of leap seconds, there is absolutely no guarantee that the leap second read has actually happened.

Implementationsยง

Sourceยง

impl NaiveTime

Source

pub const MIN: NaiveTime = _

The earliest possible NaiveTime

Source

pub const fn from_hms(hour: u32, min: u32, sec: u32) -> NaiveTime

๐Ÿ‘ŽDeprecated since 0.4.23: use from_hms_opt() instead

Makes a new NaiveTime from hour, minute and second.

No leap second is allowed here; use NaiveTime::from_hms_* methods with a subsecond parameter instead.

ยงPanics

Panics on invalid hour, minute and/or second.

Source

pub const fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option<NaiveTime>

Makes a new NaiveTime from hour, minute and second.

The millisecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

ยงErrors

Returns None on invalid hour, minute and/or second.

ยงExample
use chrono::NaiveTime;

let from_hms_opt = NaiveTime::from_hms_opt;

assert!(from_hms_opt(0, 0, 0).is_some());
assert!(from_hms_opt(23, 59, 59).is_some());
assert!(from_hms_opt(24, 0, 0).is_none());
assert!(from_hms_opt(23, 60, 0).is_none());
assert!(from_hms_opt(23, 59, 60).is_none());
Source

pub const fn from_hms_milli( hour: u32, min: u32, sec: u32, milli: u32, ) -> NaiveTime

๐Ÿ‘ŽDeprecated since 0.4.23: use from_hms_milli_opt() instead

Makes a new NaiveTime from hour, minute, second and millisecond.

The millisecond part can exceed 1,000 in order to represent the leap second.

ยงPanics

Panics on invalid hour, minute, second and/or millisecond.

Source

pub const fn from_hms_milli_opt( hour: u32, min: u32, sec: u32, milli: u32, ) -> Option<NaiveTime>

Makes a new NaiveTime from hour, minute, second and millisecond.

The millisecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

ยงErrors

Returns None on invalid hour, minute, second and/or millisecond.

ยงExample
use chrono::NaiveTime;

let from_hmsm_opt = NaiveTime::from_hms_milli_opt;

assert!(from_hmsm_opt(0, 0, 0, 0).is_some());
assert!(from_hmsm_opt(23, 59, 59, 999).is_some());
assert!(from_hmsm_opt(23, 59, 59, 1_999).is_some()); // a leap second after 23:59:59
assert!(from_hmsm_opt(24, 0, 0, 0).is_none());
assert!(from_hmsm_opt(23, 60, 0, 0).is_none());
assert!(from_hmsm_opt(23, 59, 60, 0).is_none());
assert!(from_hmsm_opt(23, 59, 59, 2_000).is_none());
Source

pub const fn from_hms_micro( hour: u32, min: u32, sec: u32, micro: u32, ) -> NaiveTime

๐Ÿ‘ŽDeprecated since 0.4.23: use from_hms_micro_opt() instead

Makes a new NaiveTime from hour, minute, second and microsecond.

The microsecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

ยงPanics

Panics on invalid hour, minute, second and/or microsecond.

Source

pub const fn from_hms_micro_opt( hour: u32, min: u32, sec: u32, micro: u32, ) -> Option<NaiveTime>

Makes a new NaiveTime from hour, minute, second and microsecond.

The microsecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

ยงErrors

Returns None on invalid hour, minute, second and/or microsecond.

ยงExample
use chrono::NaiveTime;

let from_hmsu_opt = NaiveTime::from_hms_micro_opt;

assert!(from_hmsu_opt(0, 0, 0, 0).is_some());
assert!(from_hmsu_opt(23, 59, 59, 999_999).is_some());
assert!(from_hmsu_opt(23, 59, 59, 1_999_999).is_some()); // a leap second after 23:59:59
assert!(from_hmsu_opt(24, 0, 0, 0).is_none());
assert!(from_hmsu_opt(23, 60, 0, 0).is_none());
assert!(from_hmsu_opt(23, 59, 60, 0).is_none());
assert!(from_hmsu_opt(23, 59, 59, 2_000_000).is_none());
Source

pub const fn from_hms_nano( hour: u32, min: u32, sec: u32, nano: u32, ) -> NaiveTime

๐Ÿ‘ŽDeprecated since 0.4.23: use from_hms_nano_opt() instead

Makes a new NaiveTime from hour, minute, second and nanosecond.

The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

ยงPanics

Panics on invalid hour, minute, second and/or nanosecond.

Source

pub const fn from_hms_nano_opt( hour: u32, min: u32, sec: u32, nano: u32, ) -> Option<NaiveTime>

Makes a new NaiveTime from hour, minute, second and nanosecond.

The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

ยงErrors

Returns None on invalid hour, minute, second and/or nanosecond.

ยงExample
use chrono::NaiveTime;

let from_hmsn_opt = NaiveTime::from_hms_nano_opt;

assert!(from_hmsn_opt(0, 0, 0, 0).is_some());
assert!(from_hmsn_opt(23, 59, 59, 999_999_999).is_some());
assert!(from_hmsn_opt(23, 59, 59, 1_999_999_999).is_some()); // a leap second after 23:59:59
assert!(from_hmsn_opt(24, 0, 0, 0).is_none());
assert!(from_hmsn_opt(23, 60, 0, 0).is_none());
assert!(from_hmsn_opt(23, 59, 60, 0).is_none());
assert!(from_hmsn_opt(23, 59, 59, 2_000_000_000).is_none());
Source

pub const fn from_num_seconds_from_midnight(secs: u32, nano: u32) -> NaiveTime

๐Ÿ‘ŽDeprecated since 0.4.23: use from_num_seconds_from_midnight_opt() instead

Makes a new NaiveTime from the number of seconds since midnight and nanosecond.

The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when secs % 60 == 59.

ยงPanics

Panics on invalid number of seconds and/or nanosecond.

Source

pub const fn from_num_seconds_from_midnight_opt( secs: u32, nano: u32, ) -> Option<NaiveTime>

Makes a new NaiveTime from the number of seconds since midnight and nanosecond.

The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when secs % 60 == 59.

ยงErrors

Returns None on invalid number of seconds and/or nanosecond.

ยงExample
use chrono::NaiveTime;

let from_nsecs_opt = NaiveTime::from_num_seconds_from_midnight_opt;

assert!(from_nsecs_opt(0, 0).is_some());
assert!(from_nsecs_opt(86399, 999_999_999).is_some());
assert!(from_nsecs_opt(86399, 1_999_999_999).is_some()); // a leap second after 23:59:59
assert!(from_nsecs_opt(86_400, 0).is_none());
assert!(from_nsecs_opt(86399, 2_000_000_000).is_none());
Source

pub fn parse_from_str(s: &str, fmt: &str) -> Result<NaiveTime, ParseError>

Parses a string with the specified format string and returns a new NaiveTime. See the format::strftime module on the supported escape sequences.

ยงExample
use chrono::NaiveTime;

let parse_from_str = NaiveTime::parse_from_str;

assert_eq!(
    parse_from_str("23:56:04", "%H:%M:%S"),
    Ok(NaiveTime::from_hms_opt(23, 56, 4).unwrap())
);
assert_eq!(
    parse_from_str("pm012345.6789", "%p%I%M%S%.f"),
    Ok(NaiveTime::from_hms_micro_opt(13, 23, 45, 678_900).unwrap())
);

Date and offset is ignored for the purpose of parsing.

assert_eq!(
    parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
    Ok(NaiveTime::from_hms_opt(12, 34, 56).unwrap())
);

Leap seconds are correctly handled by treating any time of the form hh:mm:60 as a leap second. (This equally applies to the formatting, so the round trip is possible.)

assert_eq!(
    parse_from_str("08:59:60.123", "%H:%M:%S%.f"),
    Ok(NaiveTime::from_hms_milli_opt(8, 59, 59, 1_123).unwrap())
);

Missing seconds are assumed to be zero, but out-of-bound times or insufficient fields are errors otherwise.

assert_eq!(parse_from_str("7:15", "%H:%M"), Ok(NaiveTime::from_hms_opt(7, 15, 0).unwrap()));

assert!(parse_from_str("04m33s", "%Mm%Ss").is_err());
assert!(parse_from_str("12", "%H").is_err());
assert!(parse_from_str("17:60", "%H:%M").is_err());
assert!(parse_from_str("24:00:00", "%H:%M:%S").is_err());

All parsed fields should be consistent to each other, otherwise itโ€™s an error. Here %H is for 24-hour clocks, unlike %I, and thus can be independently determined without AM/PM.

assert!(parse_from_str("13:07 AM", "%H:%M %p").is_err());
Source

pub fn parse_and_remainder<'a>( s: &'a str, fmt: &str, ) -> Result<(NaiveTime, &'a str), ParseError>

Parses a string from a user-specified format into a new NaiveTime value, and a slice with the remaining portion of the string. See the format::strftime module on the supported escape sequences.

Similar to parse_from_str.

ยงExample
let (time, remainder) =
    NaiveTime::parse_and_remainder("3h4m33s trailing text", "%-Hh%-Mm%-Ss").unwrap();
assert_eq!(time, NaiveTime::from_hms_opt(3, 4, 33).unwrap());
assert_eq!(remainder, " trailing text");
Source

pub const fn overflowing_add_signed(&self, rhs: TimeDelta) -> (NaiveTime, i64)

Adds given TimeDelta to the current time, and also returns the number of seconds in the integral number of days ignored from the addition.

ยงExample
use chrono::{NaiveTime, TimeDelta};

let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();

assert_eq!(
    from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(11).unwrap()),
    (from_hms(14, 4, 5), 0)
);
assert_eq!(
    from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(23).unwrap()),
    (from_hms(2, 4, 5), 86_400)
);
assert_eq!(
    from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(-7).unwrap()),
    (from_hms(20, 4, 5), -86_400)
);
Source

pub const fn overflowing_sub_signed(&self, rhs: TimeDelta) -> (NaiveTime, i64)

Subtracts given TimeDelta from the current time, and also returns the number of seconds in the integral number of days ignored from the subtraction.

ยงExample
use chrono::{NaiveTime, TimeDelta};

let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();

assert_eq!(
    from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(2).unwrap()),
    (from_hms(1, 4, 5), 0)
);
assert_eq!(
    from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(17).unwrap()),
    (from_hms(10, 4, 5), 86_400)
);
assert_eq!(
    from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(-22).unwrap()),
    (from_hms(1, 4, 5), -86_400)
);
Source

pub const fn signed_duration_since(self, rhs: NaiveTime) -> TimeDelta

Subtracts another NaiveTime from the current time. Returns a TimeDelta within +/- 1 day. This does not overflow or underflow at all.

As a part of Chronoโ€™s leap second handling, the subtraction assumes that there is no leap second ever, except when any of the NaiveTimes themselves represents a leap second in which case the assumption becomes that there are exactly one (or two) leap second(s) ever.

ยงExample
use chrono::{NaiveTime, TimeDelta};

let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();
let since = NaiveTime::signed_duration_since;

assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 900)), TimeDelta::zero());
assert_eq!(
    since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 875)),
    TimeDelta::try_milliseconds(25).unwrap()
);
assert_eq!(
    since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)),
    TimeDelta::try_milliseconds(975).unwrap()
);
assert_eq!(
    since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)),
    TimeDelta::try_seconds(7).unwrap()
);
assert_eq!(
    since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)),
    TimeDelta::try_seconds(5 * 60).unwrap()
);
assert_eq!(
    since(from_hmsm(3, 5, 7, 900), from_hmsm(0, 5, 7, 900)),
    TimeDelta::try_seconds(3 * 3600).unwrap()
);
assert_eq!(
    since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)),
    TimeDelta::try_seconds(-3600).unwrap()
);
assert_eq!(
    since(from_hmsm(3, 5, 7, 900), from_hmsm(2, 4, 6, 800)),
    TimeDelta::try_seconds(3600 + 60 + 1).unwrap() + TimeDelta::try_milliseconds(100).unwrap()
);

Leap seconds are handled, but the subtraction assumes that there were no other leap seconds happened.

assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 59, 0)),
           TimeDelta::try_seconds(1).unwrap());
assert_eq!(since(from_hmsm(3, 0, 59, 1_500), from_hmsm(3, 0, 59, 0)),
           TimeDelta::try_milliseconds(1500).unwrap());
assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 0, 0)),
           TimeDelta::try_seconds(60).unwrap());
assert_eq!(since(from_hmsm(3, 0, 0, 0), from_hmsm(2, 59, 59, 1_000)),
           TimeDelta::try_seconds(1).unwrap());
assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(2, 59, 59, 1_000)),
           TimeDelta::try_seconds(61).unwrap());
Source

pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
where I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>,

Formats the time with the specified formatting items. Otherwise it is the same as the ordinary format method.

The Iterator of items should be Cloneable, since the resulting DelayedFormat value may be formatted multiple times.

ยงExample
use chrono::format::strftime::StrftimeItems;
use chrono::NaiveTime;

let fmt = StrftimeItems::new("%H:%M:%S");
let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
assert_eq!(t.format_with_items(fmt.clone()).to_string(), "23:56:04");
assert_eq!(t.format("%H:%M:%S").to_string(), "23:56:04");

The resulting DelayedFormat can be formatted directly via the Display trait.

assert_eq!(format!("{}", t.format_with_items(fmt)), "23:56:04");
Source

pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>

Formats the time with the specified format string. See the format::strftime module on the supported escape sequences.

This returns a DelayedFormat, which gets converted to a string only when actual formatting happens. You may use the to_string method to get a String, or just feed it into print! and other formatting macros. (In this way it avoids the redundant memory allocation.)

A wrong format string does not issue an error immediately. Rather, converting or formatting the DelayedFormat fails. You are recommended to immediately use DelayedFormat for this reason.

ยงExample
use chrono::NaiveTime;

let t = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
assert_eq!(t.format("%H:%M:%S").to_string(), "23:56:04");
assert_eq!(t.format("%H:%M:%S%.6f").to_string(), "23:56:04.012345");
assert_eq!(t.format("%-I:%M %p").to_string(), "11:56 PM");

The resulting DelayedFormat can be formatted directly via the Display trait.

assert_eq!(format!("{}", t.format("%H:%M:%S")), "23:56:04");
assert_eq!(format!("{}", t.format("%H:%M:%S%.6f")), "23:56:04.012345");
assert_eq!(format!("{}", t.format("%-I:%M %p")), "11:56 PM");

Trait Implementationsยง

Sourceยง

impl Add<Duration> for NaiveTime

Add std::time::Duration to NaiveTime.

This wraps around and never overflows or underflows. In particular the addition ignores integral number of days.

Sourceยง

type Output = NaiveTime

The resulting type after applying the + operator.
Sourceยง

fn add(self, rhs: Duration) -> NaiveTime

Performs the + operation. Read more
Sourceยง

impl Add<FixedOffset> for NaiveTime

Add FixedOffset to NaiveTime.

This wraps around and never overflows or underflows. In particular the addition ignores integral number of days.

Sourceยง

type Output = NaiveTime

The resulting type after applying the + operator.
Sourceยง

fn add(self, rhs: FixedOffset) -> NaiveTime

Performs the + operation. Read more
Sourceยง

impl Add<TimeDelta> for NaiveTime

Add TimeDelta to NaiveTime.

This wraps around and never overflows or underflows. In particular the addition ignores integral number of days.

As a part of Chronoโ€™s leap second handling, the addition assumes that there is no leap second ever, except when the NaiveTime itself represents a leap second in which case the assumption becomes that there is exactly a single leap second ever.

ยงExample

use chrono::{NaiveTime, TimeDelta};

let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();

assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::zero(), from_hmsm(3, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(1).unwrap(), from_hmsm(3, 5, 8, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(-1).unwrap(), from_hmsm(3, 5, 6, 0));
assert_eq!(
    from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(60 + 4).unwrap(),
    from_hmsm(3, 6, 11, 0)
);
assert_eq!(
    from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(7 * 60 * 60 - 6 * 60).unwrap(),
    from_hmsm(9, 59, 7, 0)
);
assert_eq!(
    from_hmsm(3, 5, 7, 0) + TimeDelta::try_milliseconds(80).unwrap(),
    from_hmsm(3, 5, 7, 80)
);
assert_eq!(
    from_hmsm(3, 5, 7, 950) + TimeDelta::try_milliseconds(280).unwrap(),
    from_hmsm(3, 5, 8, 230)
);
assert_eq!(
    from_hmsm(3, 5, 7, 950) + TimeDelta::try_milliseconds(-980).unwrap(),
    from_hmsm(3, 5, 6, 970)
);

The addition wraps around.

assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(22*60*60).unwrap(), from_hmsm(1, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(-8*60*60).unwrap(), from_hmsm(19, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_days(800).unwrap(), from_hmsm(3, 5, 7, 0));

Leap seconds are handled, but the addition assumes that it is the only leap second happened.

let leap = from_hmsm(3, 5, 59, 1_300);
assert_eq!(leap + TimeDelta::zero(), from_hmsm(3, 5, 59, 1_300));
assert_eq!(leap + TimeDelta::try_milliseconds(-500).unwrap(), from_hmsm(3, 5, 59, 800));
assert_eq!(leap + TimeDelta::try_milliseconds(500).unwrap(), from_hmsm(3, 5, 59, 1_800));
assert_eq!(leap + TimeDelta::try_milliseconds(800).unwrap(), from_hmsm(3, 6, 0, 100));
assert_eq!(leap + TimeDelta::try_seconds(10).unwrap(), from_hmsm(3, 6, 9, 300));
assert_eq!(leap + TimeDelta::try_seconds(-10).unwrap(), from_hmsm(3, 5, 50, 300));
assert_eq!(leap + TimeDelta::try_days(1).unwrap(), from_hmsm(3, 5, 59, 300));
Sourceยง

type Output = NaiveTime

The resulting type after applying the + operator.
Sourceยง

fn add(self, rhs: TimeDelta) -> NaiveTime

Performs the + operation. Read more
Sourceยง

impl AddAssign<Duration> for NaiveTime

Add-assign std::time::Duration to NaiveTime.

This wraps around and never overflows or underflows. In particular the addition ignores integral number of days.

Sourceยง

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. Read more
Sourceยง

impl AddAssign<TimeDelta> for NaiveTime

Add-assign TimeDelta to NaiveTime.

This wraps around and never overflows or underflows. In particular the addition ignores integral number of days.

Sourceยง

fn add_assign(&mut self, rhs: TimeDelta)

Performs the += operation. Read more
Sourceยง

impl Clone for NaiveTime

Sourceยง

fn clone(&self) -> NaiveTime

Returns a copy of the value. Read more
1.0.0 ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl Debug for NaiveTime

The Debug output of the naive time t is the same as t.format("%H:%M:%S%.f").

The string printed can be readily parsed via the parse method on str.

It should be noted that, for leap seconds not on the minute boundary, it may print a representation not distinguishable from non-leap seconds. This doesnโ€™t matter in practice, since such leap seconds never happened. (By the time of the first leap second on 1972-06-30, every time zone offset around the world has standardized to the 5-minute alignment.)

ยงExample

use chrono::NaiveTime;

assert_eq!(format!("{:?}", NaiveTime::from_hms_opt(23, 56, 4).unwrap()), "23:56:04");
assert_eq!(
    format!("{:?}", NaiveTime::from_hms_milli_opt(23, 56, 4, 12).unwrap()),
    "23:56:04.012"
);
assert_eq!(
    format!("{:?}", NaiveTime::from_hms_micro_opt(23, 56, 4, 1234).unwrap()),
    "23:56:04.001234"
);
assert_eq!(
    format!("{:?}", NaiveTime::from_hms_nano_opt(23, 56, 4, 123456).unwrap()),
    "23:56:04.000123456"
);

Leap seconds may also be used.

assert_eq!(
    format!("{:?}", NaiveTime::from_hms_milli_opt(6, 59, 59, 1_500).unwrap()),
    "06:59:60.500"
);
Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Sourceยง

impl<'r> Decode<'r, MySql> for NaiveTime

Decode from a TIME value.

ยงErrors

Returns an error if the TIME value is negative or exceeds 23:59:59.999999.

Sourceยง

fn decode( value: MySqlValueRef<'r>, ) -> Result<NaiveTime, Box<dyn Error + Sync + Send>>

Decode a new value of this type using a raw value from the database.
Sourceยง

impl<'r> Decode<'r, Postgres> for NaiveTime

Sourceยง

fn decode( value: PgValueRef<'r>, ) -> Result<NaiveTime, Box<dyn Error + Sync + Send>>

Decode a new value of this type using a raw value from the database.
Sourceยง

impl<'r> Decode<'r, Sqlite> for NaiveTime

Sourceยง

fn decode( value: SqliteValueRef<'r>, ) -> Result<NaiveTime, Box<dyn Error + Sync + Send>>

Decode a new value of this type using a raw value from the database.
Sourceยง

impl Default for NaiveTime

The default value for a NaiveTime is midnight, 00:00:00 exactly.

ยงExample

use chrono::NaiveTime;

let default_time = NaiveTime::default();
assert_eq!(default_time, NaiveTime::from_hms_opt(0, 0, 0).unwrap());
Sourceยง

fn default() -> NaiveTime

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl<'de> Deserialize<'de> for NaiveTime

Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<NaiveTime, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl Display for NaiveTime

The Display output of the naive time t is the same as t.format("%H:%M:%S%.f").

The string printed can be readily parsed via the parse method on str.

It should be noted that, for leap seconds not on the minute boundary, it may print a representation not distinguishable from non-leap seconds. This doesnโ€™t matter in practice, since such leap seconds never happened. (By the time of the first leap second on 1972-06-30, every time zone offset around the world has standardized to the 5-minute alignment.)

ยงExample

use chrono::NaiveTime;

assert_eq!(format!("{}", NaiveTime::from_hms_opt(23, 56, 4).unwrap()), "23:56:04");
assert_eq!(
    format!("{}", NaiveTime::from_hms_milli_opt(23, 56, 4, 12).unwrap()),
    "23:56:04.012"
);
assert_eq!(
    format!("{}", NaiveTime::from_hms_micro_opt(23, 56, 4, 1234).unwrap()),
    "23:56:04.001234"
);
assert_eq!(
    format!("{}", NaiveTime::from_hms_nano_opt(23, 56, 4, 123456).unwrap()),
    "23:56:04.000123456"
);

Leap seconds may also be used.

assert_eq!(
    format!("{}", NaiveTime::from_hms_milli_opt(6, 59, 59, 1_500).unwrap()),
    "06:59:60.500"
);
Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Sourceยง

impl Encode<'_, MySql> for NaiveTime

Sourceยง

fn encode_by_ref( &self, buf: &mut Vec<u8>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

Writes the value of self into buf without moving self. Read more
Sourceยง

fn size_hint(&self) -> usize

Sourceยง

fn encode( self, buf: &mut <DB as Database>::ArgumentBuffer<'q>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
where Self: Sized,

Writes the value of self into buf in the expected format for the database.
Sourceยง

fn produces(&self) -> Option<<DB as Database>::TypeInfo>

Sourceยง

impl Encode<'_, Postgres> for NaiveTime

Sourceยง

fn encode_by_ref( &self, buf: &mut PgArgumentBuffer, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

Writes the value of self into buf without moving self. Read more
Sourceยง

fn size_hint(&self) -> usize

Sourceยง

fn encode( self, buf: &mut <DB as Database>::ArgumentBuffer<'q>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
where Self: Sized,

Writes the value of self into buf in the expected format for the database.
Sourceยง

fn produces(&self) -> Option<<DB as Database>::TypeInfo>

Sourceยง

impl Encode<'_, Sqlite> for NaiveTime

Sourceยง

fn encode_by_ref( &self, buf: &mut Vec<SqliteArgumentValue<'_>>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

Writes the value of self into buf without moving self. Read more
Sourceยง

fn encode( self, buf: &mut <DB as Database>::ArgumentBuffer<'q>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
where Self: Sized,

Writes the value of self into buf in the expected format for the database.
Sourceยง

fn produces(&self) -> Option<<DB as Database>::TypeInfo>

Sourceยง

fn size_hint(&self) -> usize

Sourceยง

impl From<NaiveTime> for Value

Sourceยง

fn from(x: NaiveTime) -> Value

Converts to this type from the input type.
Sourceยง

impl FromStr for NaiveTime

Parsing a str into a NaiveTime uses the same format, %H:%M:%S%.f, as in Debug and Display.

ยงExample

use chrono::NaiveTime;

let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap();
assert_eq!("23:56:04".parse::<NaiveTime>(), Ok(t));

let t = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
assert_eq!("23:56:4.012345678".parse::<NaiveTime>(), Ok(t));

let t = NaiveTime::from_hms_nano_opt(23, 59, 59, 1_234_567_890).unwrap(); // leap second
assert_eq!("23:59:60.23456789".parse::<NaiveTime>(), Ok(t));

// Seconds are optional
let t = NaiveTime::from_hms_opt(23, 56, 0).unwrap();
assert_eq!("23:56".parse::<NaiveTime>(), Ok(t));

assert!("foo".parse::<NaiveTime>().is_err());
Sourceยง

type Err = ParseError

The associated error which can be returned from parsing.
Sourceยง

fn from_str(s: &str) -> Result<NaiveTime, ParseError>

Parses a string s to return a value of this type. Read more
Sourceยง

impl Hash for NaiveTime

Sourceยง

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 ยท Sourceยง

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Sourceยง

impl IntoActiveValue<NaiveTime> for Time

Sourceยง

fn into_active_value(self) -> ActiveValue<Time>

Method to perform the conversion
Sourceยง

impl Nullable for NaiveTime

Sourceยง

impl Ord for NaiveTime

Sourceยง

fn cmp(&self, other: &NaiveTime) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 ยท Sourceยง

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 ยท Sourceยง

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 ยท Sourceยง

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Sourceยง

impl PartialEq for NaiveTime

Sourceยง

fn eq(&self, other: &NaiveTime) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl PartialOrd for NaiveTime

Sourceยง

fn partial_cmp(&self, other: &NaiveTime) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 ยท Sourceยง

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 ยท Sourceยง

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 ยท Sourceยง

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 ยท Sourceยง

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Sourceยง

impl PgHasArrayType for NaiveTime

Sourceยง

impl Serialize for NaiveTime

Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Sub<Duration> for NaiveTime

Subtract std::time::Duration from NaiveTime.

This wraps around and never overflows or underflows. In particular the subtraction ignores integral number of days.

Sourceยง

type Output = NaiveTime

The resulting type after applying the - operator.
Sourceยง

fn sub(self, rhs: Duration) -> NaiveTime

Performs the - operation. Read more
Sourceยง

impl Sub<FixedOffset> for NaiveTime

Subtract FixedOffset from NaiveTime.

This wraps around and never overflows or underflows. In particular the subtraction ignores integral number of days.

Sourceยง

type Output = NaiveTime

The resulting type after applying the - operator.
Sourceยง

fn sub(self, rhs: FixedOffset) -> NaiveTime

Performs the - operation. Read more
Sourceยง

impl Sub<TimeDelta> for NaiveTime

Subtract TimeDelta from NaiveTime.

This wraps around and never overflows or underflows. In particular the subtraction ignores integral number of days. This is the same as addition with a negated TimeDelta.

As a part of Chronoโ€™s leap second handling, the subtraction assumes that there is no leap second ever, except when the NaiveTime itself represents a leap second in which case the assumption becomes that there is exactly a single leap second ever.

ยงExample

use chrono::{NaiveTime, TimeDelta};

let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();

assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::zero(), from_hmsm(3, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(1).unwrap(), from_hmsm(3, 5, 6, 0));
assert_eq!(
    from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(60 + 5).unwrap(),
    from_hmsm(3, 4, 2, 0)
);
assert_eq!(
    from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(2 * 60 * 60 + 6 * 60).unwrap(),
    from_hmsm(0, 59, 7, 0)
);
assert_eq!(
    from_hmsm(3, 5, 7, 0) - TimeDelta::try_milliseconds(80).unwrap(),
    from_hmsm(3, 5, 6, 920)
);
assert_eq!(
    from_hmsm(3, 5, 7, 950) - TimeDelta::try_milliseconds(280).unwrap(),
    from_hmsm(3, 5, 7, 670)
);

The subtraction wraps around.

assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(8*60*60).unwrap(), from_hmsm(19, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::try_days(800).unwrap(), from_hmsm(3, 5, 7, 0));

Leap seconds are handled, but the subtraction assumes that it is the only leap second happened.

let leap = from_hmsm(3, 5, 59, 1_300);
assert_eq!(leap - TimeDelta::zero(), from_hmsm(3, 5, 59, 1_300));
assert_eq!(leap - TimeDelta::try_milliseconds(200).unwrap(), from_hmsm(3, 5, 59, 1_100));
assert_eq!(leap - TimeDelta::try_milliseconds(500).unwrap(), from_hmsm(3, 5, 59, 800));
assert_eq!(leap - TimeDelta::try_seconds(60).unwrap(), from_hmsm(3, 5, 0, 300));
assert_eq!(leap - TimeDelta::try_days(1).unwrap(), from_hmsm(3, 6, 0, 300));
Sourceยง

type Output = NaiveTime

The resulting type after applying the - operator.
Sourceยง

fn sub(self, rhs: TimeDelta) -> NaiveTime

Performs the - operation. Read more
Sourceยง

impl Sub for NaiveTime

Subtracts another NaiveTime from the current time. Returns a TimeDelta within +/- 1 day. This does not overflow or underflow at all.

As a part of Chronoโ€™s leap second handling, the subtraction assumes that there is no leap second ever, except when any of the NaiveTimes themselves represents a leap second in which case the assumption becomes that there are exactly one (or two) leap second(s) ever.

The implementation is a wrapper around NaiveTime::signed_duration_since.

ยงExample

use chrono::{NaiveTime, TimeDelta};

let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();

assert_eq!(from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 900), TimeDelta::zero());
assert_eq!(
    from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 7, 875),
    TimeDelta::try_milliseconds(25).unwrap()
);
assert_eq!(
    from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 6, 925),
    TimeDelta::try_milliseconds(975).unwrap()
);
assert_eq!(
    from_hmsm(3, 5, 7, 900) - from_hmsm(3, 5, 0, 900),
    TimeDelta::try_seconds(7).unwrap()
);
assert_eq!(
    from_hmsm(3, 5, 7, 900) - from_hmsm(3, 0, 7, 900),
    TimeDelta::try_seconds(5 * 60).unwrap()
);
assert_eq!(
    from_hmsm(3, 5, 7, 900) - from_hmsm(0, 5, 7, 900),
    TimeDelta::try_seconds(3 * 3600).unwrap()
);
assert_eq!(
    from_hmsm(3, 5, 7, 900) - from_hmsm(4, 5, 7, 900),
    TimeDelta::try_seconds(-3600).unwrap()
);
assert_eq!(
    from_hmsm(3, 5, 7, 900) - from_hmsm(2, 4, 6, 800),
    TimeDelta::try_seconds(3600 + 60 + 1).unwrap() + TimeDelta::try_milliseconds(100).unwrap()
);

Leap seconds are handled, but the subtraction assumes that there were no other leap seconds happened.

assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 59, 0), TimeDelta::try_seconds(1).unwrap());
assert_eq!(from_hmsm(3, 0, 59, 1_500) - from_hmsm(3, 0, 59, 0),
           TimeDelta::try_milliseconds(1500).unwrap());
assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(3, 0, 0, 0), TimeDelta::try_seconds(60).unwrap());
assert_eq!(from_hmsm(3, 0, 0, 0) - from_hmsm(2, 59, 59, 1_000), TimeDelta::try_seconds(1).unwrap());
assert_eq!(from_hmsm(3, 0, 59, 1_000) - from_hmsm(2, 59, 59, 1_000),
           TimeDelta::try_seconds(61).unwrap());
Sourceยง

type Output = TimeDelta

The resulting type after applying the - operator.
Sourceยง

fn sub(self, rhs: NaiveTime) -> TimeDelta

Performs the - operation. Read more
Sourceยง

impl SubAssign<Duration> for NaiveTime

Subtract-assign std::time::Duration from NaiveTime.

This wraps around and never overflows or underflows. In particular the subtraction ignores integral number of days.

Sourceยง

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. Read more
Sourceยง

impl SubAssign<TimeDelta> for NaiveTime

Subtract-assign TimeDelta from NaiveTime.

This wraps around and never overflows or underflows. In particular the subtraction ignores integral number of days.

Sourceยง

fn sub_assign(&mut self, rhs: TimeDelta)

Performs the -= operation. Read more
Sourceยง

impl Timelike for NaiveTime

Sourceยง

fn hour(&self) -> u32

Returns the hour number from 0 to 23.

ยงExample
use chrono::{NaiveTime, Timelike};

assert_eq!(NaiveTime::from_hms_opt(0, 0, 0).unwrap().hour(), 0);
assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().hour(), 23);
Sourceยง

fn minute(&self) -> u32

Returns the minute number from 0 to 59.

ยงExample
use chrono::{NaiveTime, Timelike};

assert_eq!(NaiveTime::from_hms_opt(0, 0, 0).unwrap().minute(), 0);
assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().minute(), 56);
Sourceยง

fn second(&self) -> u32

Returns the second number from 0 to 59.

ยงExample
use chrono::{NaiveTime, Timelike};

assert_eq!(NaiveTime::from_hms_opt(0, 0, 0).unwrap().second(), 0);
assert_eq!(NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().second(), 4);

This method never returns 60 even when it is a leap second. (Why?) Use the proper formatting method to get a human-readable representation.

let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
assert_eq!(leap.second(), 59);
assert_eq!(leap.format("%H:%M:%S").to_string(), "23:59:60");
Sourceยง

fn nanosecond(&self) -> u32

Returns the number of nanoseconds since the whole non-leap second. The range from 1,000,000,000 to 1,999,999,999 represents the leap second.

ยงExample
use chrono::{NaiveTime, Timelike};

assert_eq!(NaiveTime::from_hms_opt(0, 0, 0).unwrap().nanosecond(), 0);
assert_eq!(
    NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().nanosecond(),
    12_345_678
);

Leap seconds may have seemingly out-of-range return values. You can reduce the range with time.nanosecond() % 1_000_000_000, or use the proper formatting method to get a human-readable representation.

let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
assert_eq!(leap.nanosecond(), 1_000_000_000);
assert_eq!(leap.format("%H:%M:%S%.9f").to_string(), "23:59:60.000000000");
Sourceยง

fn with_hour(&self, hour: u32) -> Option<NaiveTime>

Makes a new NaiveTime with the hour number changed.

ยงErrors

Returns None if the value for hour is invalid.

ยงExample
use chrono::{NaiveTime, Timelike};

let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
assert_eq!(dt.with_hour(7), Some(NaiveTime::from_hms_nano_opt(7, 56, 4, 12_345_678).unwrap()));
assert_eq!(dt.with_hour(24), None);
Sourceยง

fn with_minute(&self, min: u32) -> Option<NaiveTime>

Makes a new NaiveTime with the minute number changed.

ยงErrors

Returns None if the value for minute is invalid.

ยงExample
use chrono::{NaiveTime, Timelike};

let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
assert_eq!(
    dt.with_minute(45),
    Some(NaiveTime::from_hms_nano_opt(23, 45, 4, 12_345_678).unwrap())
);
assert_eq!(dt.with_minute(60), None);
Sourceยง

fn with_second(&self, sec: u32) -> Option<NaiveTime>

Makes a new NaiveTime with the second number changed.

As with the second method, the input range is restricted to 0 through 59.

ยงErrors

Returns None if the value for second is invalid.

ยงExample
use chrono::{NaiveTime, Timelike};

let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
assert_eq!(
    dt.with_second(17),
    Some(NaiveTime::from_hms_nano_opt(23, 56, 17, 12_345_678).unwrap())
);
assert_eq!(dt.with_second(60), None);
Sourceยง

fn with_nanosecond(&self, nano: u32) -> Option<NaiveTime>

Makes a new NaiveTime with nanoseconds since the whole non-leap second changed.

As with the nanosecond method, the input range can exceed 1,000,000,000 for leap seconds.

ยงErrors

Returns None if nanosecond >= 2,000,000,000.

ยงExample
use chrono::{NaiveTime, Timelike};

let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
assert_eq!(
    dt.with_nanosecond(333_333_333),
    Some(NaiveTime::from_hms_nano_opt(23, 56, 4, 333_333_333).unwrap())
);
assert_eq!(dt.with_nanosecond(2_000_000_000), None);

Leap seconds can theoretically follow any whole second. The following would be a proper leap second at the time zone offset of UTC-00:03:57 (there are several historical examples comparable to this โ€œnon-senseโ€ offset), and therefore is allowed.

let dt = NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap();
let strange_leap_second = dt.with_nanosecond(1_333_333_333).unwrap();
assert_eq!(strange_leap_second.nanosecond(), 1_333_333_333);
Sourceยง

fn num_seconds_from_midnight(&self) -> u32

Returns the number of non-leap seconds past the last midnight.

ยงExample
use chrono::{NaiveTime, Timelike};

assert_eq!(NaiveTime::from_hms_opt(1, 2, 3).unwrap().num_seconds_from_midnight(), 3723);
assert_eq!(
    NaiveTime::from_hms_nano_opt(23, 56, 4, 12_345_678).unwrap().num_seconds_from_midnight(),
    86164
);
assert_eq!(
    NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap().num_seconds_from_midnight(),
    86399
);
Sourceยง

fn hour12(&self) -> (bool, u32)

Returns the hour number from 1 to 12 with a boolean flag, which is false for AM and true for PM.
Sourceยง

impl TryFrom<MySqlTime> for NaiveTime

Sourceยง

type Error = Box<dyn Error + Sync + Send>

The type returned in the event of a conversion error.
Sourceยง

fn try_from( time: MySqlTime, ) -> Result<NaiveTime, <NaiveTime as TryFrom<MySqlTime>>::Error>

Performs the conversion.
Sourceยง

impl TryFromU64 for NaiveTime

Sourceยง

fn try_from_u64(_: u64) -> Result<Self, DbErr>

The method to convert the type to a u64
Sourceยง

impl TryGetable for NaiveTime

Sourceยง

fn try_get_by<I: ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError>

Get a value from the query result with an ColIdx
Sourceยง

fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError>

Get a value from the query result with prefixed column name
Sourceยง

fn try_get_by_index( res: &QueryResult, index: usize, ) -> Result<Self, TryGetError>

Get a value from the query result based on the order in the select expressions
Sourceยง

impl Type<MySql> for NaiveTime

Sourceยง

fn type_info() -> MySqlTypeInfo

Returns the canonical SQL type for this Rust type. Read more
Sourceยง

fn compatible(ty: &<DB as Database>::TypeInfo) -> bool

Determines if this Rust type is compatible with the given SQL type. Read more
Sourceยง

impl Type<Postgres> for NaiveTime

Sourceยง

fn type_info() -> PgTypeInfo

Returns the canonical SQL type for this Rust type. Read more
Sourceยง

fn compatible(ty: &<DB as Database>::TypeInfo) -> bool

Determines if this Rust type is compatible with the given SQL type. Read more
Sourceยง

impl Type<Sqlite> for NaiveTime

Sourceยง

fn type_info() -> SqliteTypeInfo

Returns the canonical SQL type for this Rust type. Read more
Sourceยง

fn compatible(ty: &SqliteTypeInfo) -> bool

Determines if this Rust type is compatible with the given SQL type. Read more
Sourceยง

impl ValueType for NaiveTime

Sourceยง

impl Copy for NaiveTime

Sourceยง

impl Eq for NaiveTime

Sourceยง

impl NotU8 for NaiveTime

Sourceยง

impl StructuralPartialEq for NaiveTime

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dst: *mut T)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Sourceยง

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Sourceยง

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Sourceยง

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Sourceยง

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Sourceยง

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Sourceยง

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Sourceยง

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Sourceยง

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Sourceยง

impl<T> ExprTrait for T
where T: Into<SimpleExpr>,

Sourceยง

fn as_enum<N>(self, type_name: N) -> SimpleExpr
where N: IntoIden,

Express a AS enum expression. Read more
Sourceยง

fn binary<O, R>(self, op: O, right: R) -> SimpleExpr
where O: Into<BinOper>, R: Into<SimpleExpr>,

Create any binary operation Read more
Sourceยง

fn cast_as<N>(self, type_name: N) -> SimpleExpr
where N: IntoIden,

Express a CAST AS expression. Read more
Sourceยง

fn unary(self, op: UnOper) -> SimpleExpr

Apply any unary operator to the expression. Read more
Sourceยง

fn add<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic addition operation. Read more
Sourceยง

fn and<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Sourceยง

fn between<A, B>(self, a: A, b: B) -> SimpleExpr
where A: Into<SimpleExpr>, B: Into<SimpleExpr>,

Express a BETWEEN expression. Read more
Sourceยง

fn div<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic division operation. Read more
Sourceยง

fn eq<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an equal (=) expression. Read more
Sourceยง

fn equals<C>(self, col: C) -> SimpleExpr
where C: IntoColumnRef,

Express a equal expression between two table columns, you will mainly use this to relate identical value between two table columns. Read more
Sourceยง

fn gt<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a greater than (>) expression. Read more
Sourceยง

fn gte<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a greater than or equal (>=) expression. Read more
Sourceยง

fn in_subquery(self, sel: SelectStatement) -> SimpleExpr

Express a IN sub-query expression. Read more
Sourceยง

fn in_tuples<V, I>(self, v: I) -> SimpleExpr
where V: IntoValueTuple, I: IntoIterator<Item = V>,

Express a IN sub expression. Read more
Sourceยง

fn is<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a IS expression. Read more
Sourceยง

fn is_in<V, I>(self, v: I) -> SimpleExpr
where V: Into<SimpleExpr>, I: IntoIterator<Item = V>,

Express a IN expression. Read more
Sourceยง

fn is_not<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a IS NOT expression. Read more
Sourceยง

fn is_not_in<V, I>(self, v: I) -> SimpleExpr
where V: Into<SimpleExpr>, I: IntoIterator<Item = V>,

Express a NOT IN expression. Read more
Sourceยง

fn is_not_null(self) -> SimpleExpr

Express a IS NOT NULL expression. Read more
Sourceยง

fn is_null(self) -> SimpleExpr

Express a IS NULL expression. Read more
Sourceยง

fn left_shift<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a bitwise left shift. Read more
Sourceยง

fn like<L>(self, like: L) -> SimpleExpr
where L: IntoLikeExpr,

Express a LIKE expression. Read more
Sourceยง

fn lt<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a less than (<) expression. Read more
Sourceยง

fn lte<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a less than or equal (<=) expression. Read more
Sourceยง

fn modulo<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic modulo operation. Read more
Sourceยง

fn mul<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic multiplication operation. Read more
Sourceยง

fn ne<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a not equal (<>) expression. Read more
Sourceยง

fn not(self) -> SimpleExpr

Negates an expression with NOT. Read more
Sourceยง

fn not_between<A, B>(self, a: A, b: B) -> SimpleExpr
where A: Into<SimpleExpr>, B: Into<SimpleExpr>,

Express a NOT BETWEEN expression. Read more
Sourceยง

fn not_equals<C>(self, col: C) -> SimpleExpr
where C: IntoColumnRef,

Express a not equal expression between two table columns, you will mainly use this to relate identical value between two table columns. Read more
Sourceยง

fn not_in_subquery(self, sel: SelectStatement) -> SimpleExpr

Express a NOT IN sub-query expression. Read more
Sourceยง

fn not_like<L>(self, like: L) -> SimpleExpr
where L: IntoLikeExpr,

Express a NOT LIKE expression. Read more
Sourceยง

fn or<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a logical OR operation. Read more
Sourceยง

fn right_shift<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a bitwise right shift. Read more
Sourceยง

fn sub<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic subtraction operation. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<V> FromValueTuple for V
where V: Into<Value> + ValueType,

Sourceยง

impl<T> Instrument for T

Sourceยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Sourceยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> IntoEither for T

Sourceยง

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Sourceยง

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Sourceยง

impl<V> IntoValueTuple for V
where V: Into<Value>,

Sourceยง

impl<V> PrimaryKeyArity for V
where V: TryGetable,

Sourceยง

const ARITY: usize = 1usize

Arity of the Primary Key
Sourceยง

impl<T> Same for T

Sourceยง

type Output = T

Should always be Self
Sourceยง

impl<T> SubsecRound for T
where T: Add<TimeDelta, Output = T> + Sub<TimeDelta, Output = T> + Timelike,

Sourceยง

fn round_subsecs(self, digits: u16) -> T

Return a copy rounded to the specified number of subsecond digits. With 9 or more digits, self is returned unmodified. Halfway values are rounded up (away from zero). Read more
Sourceยง

fn trunc_subsecs(self, digits: u16) -> T

Return a copy truncated to the specified number of subsecond digits. With 9 or more digits, self is returned unmodified. Read more
Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Sourceยง

impl<T> ToString for T
where T: Display + ?Sized,

Sourceยง

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T> TryGetableMany for T
where T: TryGetable,

Sourceยง

fn try_get_many( res: &QueryResult, pre: &str, cols: &[String], ) -> Result<T, TryGetError>

Get a tuple value from the query result with prefixed column name
Sourceยง

fn try_get_many_by_index(res: &QueryResult) -> Result<T, TryGetError>

Get a tuple value from the query result based on the order in the select expressions
Sourceยง

fn find_by_statement<C>( stmt: Statement, ) -> SelectorRaw<SelectGetableValue<Self, C>>

Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Sourceยง

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Sourceยง

fn vzip(self) -> V

Sourceยง

impl<T> WithSubscriber for T

Sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,