Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

TimeTrait

Fully qualified path: chrono::time::TimeTrait

pub trait TimeTrait

Trait constants

MIN

The earliest possible NaiveTime

Fully qualified path: chrono::time::TimeTrait::MIN

const MIN: Time;

MAX

Fully qualified path: chrono::time::TimeTrait::MAX

const MAX: Time;

Trait functions

from_hms_opt

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());

Fully qualified path: chrono::time::TimeTrait::from_hms_opt

fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option<Time>

from_num_seconds_from_midnight_opt

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());

Fully qualified path: chrono::time::TimeTrait::from_num_seconds_from_midnight_opt

fn from_num_seconds_from_midnight_opt(secs: u32) -> Option<Time>

overflowing_add_signed

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)
);

Fully qualified path: chrono::time::TimeTrait::overflowing_add_signed

fn overflowing_add_signed(self: @Time, rhs: TimeDelta) -> (Time, i64)

overflowing_sub_signed

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)
);

Fully qualified path: chrono::time::TimeTrait::overflowing_sub_signed

fn overflowing_sub_signed(self: @Time, rhs: TimeDelta) -> (Time, i64)

signed_duration_since

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.

 use chrono::{TimeDelta, NaiveTime};
 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, 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());

Fully qualified path: chrono::time::TimeTrait::signed_duration_since

fn signed_duration_since(self: @Time, rhs: Time) -> TimeDelta

hms

Returns a triple of the hour, minute and second numbers.

Fully qualified path: chrono::time::TimeTrait::hms

fn hms(self: @Time) -> (u32, u32, u32)