[][src]Trait chrono::Timelike

pub trait Timelike: Sized {
    fn hour(&self) -> u32;
fn minute(&self) -> u32;
fn second(&self) -> u32;
fn nanosecond(&self) -> u32;
fn with_hour(&self, hour: u32) -> Option<Self>;
fn with_minute(&self, min: u32) -> Option<Self>;
fn with_second(&self, sec: u32) -> Option<Self>;
fn with_nanosecond(&self, nano: u32) -> Option<Self>; fn hour12(&self) -> (bool, u32) { ... }
fn num_seconds_from_midnight(&self) -> u32 { ... } }

The common set of methods for time component.

Required methods

fn hour(&self) -> u32

Returns the hour number from 0 to 23.

fn minute(&self) -> u32

Returns the minute number from 0 to 59.

fn second(&self) -> u32

Returns the second number from 0 to 59.

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.

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

Makes a new value with the hour number changed.

Returns None when the resulting value would be invalid.

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

Makes a new value with the minute number changed.

Returns None when the resulting value would be invalid.

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

Makes a new value with the second number changed.

Returns None when the resulting value would be invalid. As with the second method, the input range is restricted to 0 through 59.

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

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

Returns None when the resulting value would be invalid. As with the nanosecond method, the input range can exceed 1,000,000,000 for leap seconds.

Loading content...

Provided methods

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.

fn num_seconds_from_midnight(&self) -> u32

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

Loading content...

Implementors

impl Timelike for NaiveDateTime[src]

fn hour(&self) -> u32[src]

Returns the hour number from 0 to 23.

See also the NaiveTime::hour method.

Example

use chrono::{NaiveDate, NaiveDateTime, Timelike};

let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
assert_eq!(dt.hour(), 12);

fn minute(&self) -> u32[src]

Returns the minute number from 0 to 59.

See also the NaiveTime::minute method.

Example

use chrono::{NaiveDate, NaiveDateTime, Timelike};

let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
assert_eq!(dt.minute(), 34);

fn second(&self) -> u32[src]

Returns the second number from 0 to 59.

See also the NaiveTime::second method.

Example

use chrono::{NaiveDate, NaiveDateTime, Timelike};

let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
assert_eq!(dt.second(), 56);

fn nanosecond(&self) -> u32[src]

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.

See also the NaiveTime::nanosecond method.

Example

use chrono::{NaiveDate, NaiveDateTime, Timelike};

let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
assert_eq!(dt.nanosecond(), 789_000_000);

fn with_hour(&self, hour: u32) -> Option<NaiveDateTime>[src]

Makes a new NaiveDateTime with the hour number changed.

Returns None when the resulting NaiveDateTime would be invalid.

See also the NaiveTime::with_hour method.

Example

use chrono::{NaiveDate, NaiveDateTime, Timelike};

let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
assert_eq!(dt.with_hour(7),
           Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(7, 34, 56, 789)));
assert_eq!(dt.with_hour(24), None);

fn with_minute(&self, min: u32) -> Option<NaiveDateTime>[src]

Makes a new NaiveDateTime with the minute number changed.

Returns None when the resulting NaiveDateTime would be invalid.

See also the NaiveTime::with_minute method.

Example

use chrono::{NaiveDate, NaiveDateTime, Timelike};

let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
assert_eq!(dt.with_minute(45),
           Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 45, 56, 789)));
assert_eq!(dt.with_minute(60), None);

fn with_second(&self, sec: u32) -> Option<NaiveDateTime>[src]

Makes a new NaiveDateTime with the second number changed.

Returns None when the resulting NaiveDateTime would be invalid. As with the second method, the input range is restricted to 0 through 59.

See also the NaiveTime::with_second method.

Example

use chrono::{NaiveDate, NaiveDateTime, Timelike};

let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
assert_eq!(dt.with_second(17),
           Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 17, 789)));
assert_eq!(dt.with_second(60), None);

fn with_nanosecond(&self, nano: u32) -> Option<NaiveDateTime>[src]

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

Returns None when the resulting NaiveDateTime would be invalid. As with the nanosecond method, the input range can exceed 1,000,000,000 for leap seconds.

See also the NaiveTime::with_nanosecond method.

Example

use chrono::{NaiveDate, NaiveDateTime, Timelike};

let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms_milli(12, 34, 56, 789);
assert_eq!(dt.with_nanosecond(333_333_333),
           Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_nano(12, 34, 56, 333_333_333)));
assert_eq!(dt.with_nanosecond(1_333_333_333), // leap second
           Some(NaiveDate::from_ymd(2015, 9, 8).and_hms_nano(12, 34, 56, 1_333_333_333)));
assert_eq!(dt.with_nanosecond(2_000_000_000), None);

impl Timelike for NaiveTime[src]

fn hour(&self) -> u32[src]

Returns the hour number from 0 to 23.

Example

use chrono::{NaiveTime, Timelike};

assert_eq!(NaiveTime::from_hms(0, 0, 0).hour(), 0);
assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).hour(), 23);

fn minute(&self) -> u32[src]

Returns the minute number from 0 to 59.

Example

use chrono::{NaiveTime, Timelike};

assert_eq!(NaiveTime::from_hms(0, 0, 0).minute(), 0);
assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).minute(), 56);

fn second(&self) -> u32[src]

Returns the second number from 0 to 59.

Example

use chrono::{NaiveTime, Timelike};

assert_eq!(NaiveTime::from_hms(0, 0, 0).second(), 0);
assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).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(23, 59, 59, 1_000);
assert_eq!(leap.second(), 59);
assert_eq!(leap.format("%H:%M:%S").to_string(), "23:59:60");

fn nanosecond(&self) -> u32[src]

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(0, 0, 0).nanosecond(), 0);
assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).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(23, 59, 59, 1_000);
assert_eq!(leap.nanosecond(), 1_000_000_000);
assert_eq!(leap.format("%H:%M:%S%.9f").to_string(), "23:59:60.000000000");

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

Makes a new NaiveTime with the hour number changed.

Returns None when the resulting NaiveTime would be invalid.

Example

use chrono::{NaiveTime, Timelike};

let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
assert_eq!(dt.with_hour(7), Some(NaiveTime::from_hms_nano(7, 56, 4, 12_345_678)));
assert_eq!(dt.with_hour(24), None);

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

Makes a new NaiveTime with the minute number changed.

Returns None when the resulting NaiveTime would be invalid.

Example

use chrono::{NaiveTime, Timelike};

let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
assert_eq!(dt.with_minute(45), Some(NaiveTime::from_hms_nano(23, 45, 4, 12_345_678)));
assert_eq!(dt.with_minute(60), None);

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

Makes a new NaiveTime with the second number changed.

Returns None when the resulting NaiveTime would be invalid. As with the second method, the input range is restricted to 0 through 59.

Example

use chrono::{NaiveTime, Timelike};

let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
assert_eq!(dt.with_second(17), Some(NaiveTime::from_hms_nano(23, 56, 17, 12_345_678)));
assert_eq!(dt.with_second(60), None);

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

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

Returns None when the resulting NaiveTime would be invalid. As with the nanosecond method, the input range can exceed 1,000,000,000 for leap seconds.

Example

use chrono::{NaiveTime, Timelike};

let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678);
assert_eq!(dt.with_nanosecond(333_333_333),
           Some(NaiveTime::from_hms_nano(23, 56, 4, 333_333_333)));
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.

assert_eq!(dt.with_nanosecond(1_333_333_333),
           Some(NaiveTime::from_hms_nano(23, 56, 4, 1_333_333_333)));

fn num_seconds_from_midnight(&self) -> u32[src]

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

Example

use chrono::{NaiveTime, Timelike};

assert_eq!(NaiveTime::from_hms(1, 2, 3).num_seconds_from_midnight(),
           3723);
assert_eq!(NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).num_seconds_from_midnight(),
           86164);
assert_eq!(NaiveTime::from_hms_milli(23, 59, 59, 1_000).num_seconds_from_midnight(),
           86399);

impl<Tz: TimeZone> Timelike for DateTime<Tz>[src]

Loading content...