From 5d476d3b74bb8c1ae2044f4406581912b3c68257 Mon Sep 17 00:00:00 2001 From: Moritz Oberhauser Date: Sat, 17 Feb 2024 18:22:55 +0100 Subject: [PATCH] Return Result for NaiveDate --- src/datetime/mod.rs | 47 ++++++++++++++++++++++----------------- src/naive/date/mod.rs | 42 +++++++++++++++++----------------- src/naive/datetime/mod.rs | 26 +++++++++++----------- src/naive/time/mod.rs | 35 +++++++++++++++-------------- src/traits.rs | 32 +++++++++++++------------- 5 files changed, 95 insertions(+), 87 deletions(-) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 86ccf975fe..42af19c1a6 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -29,7 +29,7 @@ use crate::offset::{FixedOffset, Offset, TimeZone, Utc}; use crate::try_opt; #[cfg(any(feature = "clock", feature = "std"))] use crate::OutOfRange; -use crate::{Datelike, Months, TimeDelta, Timelike, Weekday}; +use crate::{Datelike, Error, Months, TimeDelta, Timelike, Weekday}; #[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))] use rkyv::{Archive, Deserialize, Serialize}; @@ -669,13 +669,19 @@ impl From> for DateTime { } /// Maps the local datetime to other datetime with given conversion function. -fn map_local(dt: &DateTime, mut f: F) -> Option> +fn map_local(dt: &DateTime, mut f: F) -> Result, Error> where - F: FnMut(NaiveDateTime) -> Option, + F: FnMut(NaiveDateTime) -> Result, { - f(dt.overflowing_naive_local()) - .and_then(|datetime| dt.timezone().from_local_datetime(&datetime).single()) - .filter(|dt| dt >= &DateTime::::MIN_UTC && dt <= &DateTime::::MAX_UTC) + let dt = f(dt.overflowing_naive_local()).and_then(|datetime| { + dt.timezone().from_local_datetime(&datetime).single().ok_or_else(|| Error::InvalidArgument) + })?; + + if dt < DateTime::::MIN_UTC || dt > DateTime::::MAX_UTC { + return Err(Error::OutOfRange); + } + + Ok(dt) } impl DateTime { @@ -933,7 +939,7 @@ impl Datelike for DateTime { /// - When the `NaiveDateTime` would be out of range. /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. - fn with_year(&self, year: i32) -> Option> { + fn with_year(&self, year: i32) -> Result, Error> { map_local(self, |datetime| datetime.with_year(year)) } @@ -949,7 +955,7 @@ impl Datelike for DateTime { /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. #[inline] - fn with_month(&self, month: u32) -> Option> { + fn with_month(&self, month: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_month(month)) } @@ -965,7 +971,7 @@ impl Datelike for DateTime { /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. #[inline] - fn with_month0(&self, month0: u32) -> Option> { + fn with_month0(&self, month0: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_month0(month0)) } @@ -981,7 +987,7 @@ impl Datelike for DateTime { /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. #[inline] - fn with_day(&self, day: u32) -> Option> { + fn with_day(&self, day: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_day(day)) } @@ -997,7 +1003,7 @@ impl Datelike for DateTime { /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. #[inline] - fn with_day0(&self, day0: u32) -> Option> { + fn with_day0(&self, day0: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_day0(day0)) } @@ -1013,7 +1019,7 @@ impl Datelike for DateTime { /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. #[inline] - fn with_ordinal(&self, ordinal: u32) -> Option> { + fn with_ordinal(&self, ordinal: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_ordinal(ordinal)) } @@ -1029,7 +1035,7 @@ impl Datelike for DateTime { /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. #[inline] - fn with_ordinal0(&self, ordinal0: u32) -> Option> { + fn with_ordinal0(&self, ordinal0: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_ordinal0(ordinal0)) } } @@ -1058,12 +1064,12 @@ impl Timelike for DateTime { /// /// # Errors /// - /// Returns `None` if: + /// Returns `Err` if: /// - The value for `hour` is invalid. /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. #[inline] - fn with_hour(&self, hour: u32) -> Option> { + fn with_hour(&self, hour: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_hour(hour)) } @@ -1073,11 +1079,12 @@ impl Timelike for DateTime { /// /// # Errors /// + /// Returns `Err` if: /// - The value for `minute` is invalid. /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. #[inline] - fn with_minute(&self, min: u32) -> Option> { + fn with_minute(&self, min: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_minute(min)) } @@ -1090,18 +1097,18 @@ impl Timelike for DateTime { /// /// # Errors /// - /// Returns `None` if: + /// Returns `Err` if: /// - The value for `second` is invalid. /// - The local time at the resulting date does not exist or is ambiguous, for example during a /// daylight saving time transition. #[inline] - fn with_second(&self, sec: u32) -> Option> { + fn with_second(&self, sec: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_second(sec)) } /// Makes a new `DateTime` with nanoseconds since the whole non-leap second changed. /// - /// Returns `None` when the resulting `NaiveDateTime` would be invalid. + /// Returns `Err` when the resulting `NaiveDateTime` would be invalid. /// As with the [`NaiveDateTime::nanosecond`] method, /// the input range can exceed 1,000,000,000 for leap seconds. /// @@ -1111,7 +1118,7 @@ impl Timelike for DateTime { /// /// Returns `None` if `nanosecond >= 2,000,000,000`. #[inline] - fn with_nanosecond(&self, nano: u32) -> Option> { + fn with_nanosecond(&self, nano: u32) -> Result, Error> { map_local(self, |datetime| datetime.with_nanosecond(nano)) } } diff --git a/src/naive/date/mod.rs b/src/naive/date/mod.rs index 822694db51..48c120eae7 100644 --- a/src/naive/date/mod.rs +++ b/src/naive/date/mod.rs @@ -793,13 +793,13 @@ impl NaiveDate { /// /// Returns `None` when the resulting `NaiveDate` would be invalid. #[inline] - const fn with_mdf(&self, mdf: Mdf) -> Option { + const fn with_mdf(&self, mdf: Mdf) -> Result { debug_assert!(self.year_flags().0 == mdf.year_flags().0); match mdf.ordinal() { Ok(ordinal) => { - Some(NaiveDate::from_yof((self.yof() & !ORDINAL_MASK) | (ordinal << 4) as i32)) + Ok(NaiveDate::from_yof((self.yof() & !ORDINAL_MASK) | (ordinal << 4) as i32)) } - Err(_) => None, // Non-existing date + Err(e) => Err(e), // Non-existing date } } @@ -1455,7 +1455,7 @@ impl Datelike for NaiveDate { /// assert!(NaiveDate::from_ymd(2016, 2, 29).unwrap().with_year(2020).is_some()); /// ``` #[inline] - fn with_year(&self, year: i32) -> Option { + fn with_year(&self, year: i32) -> Result { // we need to operate with `mdf` since we should keep the month and day number as is let mdf = self.mdf(); @@ -1463,7 +1463,7 @@ impl Datelike for NaiveDate { let flags = YearFlags::from_year(year); let mdf = mdf.with_flags(flags); - ok!(NaiveDate::from_mdf(year, mdf)) + NaiveDate::from_mdf(year, mdf) } /// Makes a new `NaiveDate` with the month number (starting from 1) changed. @@ -1483,8 +1483,8 @@ impl Datelike for NaiveDate { /// assert_eq!(NaiveDate::from_ymd(2015, 9, 30).unwrap().with_month(2), None); // no February 30 /// ``` #[inline] - fn with_month(&self, month: u32) -> Option { - self.with_mdf(self.mdf().with_month(month).ok()?) + fn with_month(&self, month: u32) -> Result { + self.with_mdf(self.mdf().with_month(month)?) } /// Makes a new `NaiveDate` with the month number (starting from 0) changed. @@ -1505,9 +1505,9 @@ impl Datelike for NaiveDate { /// assert_eq!(NaiveDate::from_ymd(2015, 9, 30).unwrap().with_month0(1), None); // no February 30 /// ``` #[inline] - fn with_month0(&self, month0: u32) -> Option { - let month = month0.checked_add(1)?; - self.with_mdf(self.mdf().with_month(month).ok()?) + fn with_month0(&self, month0: u32) -> Result { + let month = month0.checked_add(1).ok_or(Error::InvalidArgument)?; + self.with_mdf(self.mdf().with_month(month)?) } /// Makes a new `NaiveDate` with the day of month (starting from 1) changed. @@ -1527,8 +1527,8 @@ impl Datelike for NaiveDate { /// None); // no September 31 /// ``` #[inline] - fn with_day(&self, day: u32) -> Option { - self.with_mdf(self.mdf().with_day(day).ok()?) + fn with_day(&self, day: u32) -> Result { + self.with_mdf(self.mdf().with_day(day)?) } /// Makes a new `NaiveDate` with the day of month (starting from 0) changed. @@ -1548,9 +1548,9 @@ impl Datelike for NaiveDate { /// None); // no September 31 /// ``` #[inline] - fn with_day0(&self, day0: u32) -> Option { - let day = day0.checked_add(1)?; - self.with_mdf(self.mdf().with_day(day).ok()?) + fn with_day0(&self, day0: u32) -> Result { + let day = day0.checked_add(1).ok_or(Error::InvalidArgument)?; + self.with_mdf(self.mdf().with_day(day)?) } /// Makes a new `NaiveDate` with the day of year (starting from 1) changed. @@ -1576,14 +1576,14 @@ impl Datelike for NaiveDate { /// Some(NaiveDate::from_ymd(2016, 12, 31).unwrap())); /// ``` #[inline] - fn with_ordinal(&self, ordinal: u32) -> Option { + fn with_ordinal(&self, ordinal: u32) -> Result { if ordinal == 0 || ordinal > 366 { - return None; + return Err(Error::InvalidArgument); } let yof = (self.yof() & !ORDINAL_MASK) | (ordinal << 4) as i32; match yof & OL_MASK <= MAX_OL { - true => Some(NaiveDate::from_yof(yof)), - false => None, // Does not exist: Ordinal 366 in a common year. + true => Ok(NaiveDate::from_yof(yof)), + false => Err(Error::DoesNotExist), // Does not exist: Ordinal 366 in a common year. } } @@ -1610,8 +1610,8 @@ impl Datelike for NaiveDate { /// Some(NaiveDate::from_ymd(2016, 12, 31).unwrap())); /// ``` #[inline] - fn with_ordinal0(&self, ordinal0: u32) -> Option { - let ordinal = ordinal0.checked_add(1)?; + fn with_ordinal0(&self, ordinal0: u32) -> Result { + let ordinal = ordinal0.checked_add(1).ok_or(Error::InvalidArgument)?; self.with_ordinal(ordinal) } } diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index b7e958614d..d705f36975 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -21,8 +21,8 @@ use crate::naive::{Days, IsoWeek, NaiveDate, NaiveTime}; use crate::offset::Utc; use crate::time_delta::NANOS_PER_SEC; use crate::{ - expect, ok, try_opt, DateTime, Datelike, FixedOffset, LocalResult, Months, TimeDelta, TimeZone, - Timelike, Weekday, + expect, ok, try_opt, DateTime, Datelike, Error, FixedOffset, LocalResult, Months, TimeDelta, + TimeZone, Timelike, Weekday, }; /// Tools to help serializing/deserializing `NaiveDateTime`s @@ -1238,7 +1238,7 @@ impl Datelike for NaiveDateTime { /// assert_eq!(dt.with_year(-308), Some(NaiveDate::from_ymd(-308, 9, 25).unwrap().and_hms(12, 34, 56).unwrap())); /// ``` #[inline] - fn with_year(&self, year: i32) -> Option { + fn with_year(&self, year: i32) -> Result { self.date.with_year(year).map(|d| NaiveDateTime { date: d, ..*self }) } @@ -1261,7 +1261,7 @@ impl Datelike for NaiveDateTime { /// assert_eq!(dt.with_month(2), None); // no February 30 /// ``` #[inline] - fn with_month(&self, month: u32) -> Option { + fn with_month(&self, month: u32) -> Result { self.date.with_month(month).map(|d| NaiveDateTime { date: d, ..*self }) } @@ -1285,7 +1285,7 @@ impl Datelike for NaiveDateTime { /// assert_eq!(dt.with_month0(1), None); // no February 30 /// ``` #[inline] - fn with_month0(&self, month0: u32) -> Option { + fn with_month0(&self, month0: u32) -> Result { self.date.with_month0(month0).map(|d| NaiveDateTime { date: d, ..*self }) } @@ -1307,7 +1307,7 @@ impl Datelike for NaiveDateTime { /// assert_eq!(dt.with_day(31), None); // no September 31 /// ``` #[inline] - fn with_day(&self, day: u32) -> Option { + fn with_day(&self, day: u32) -> Result { self.date.with_day(day).map(|d| NaiveDateTime { date: d, ..*self }) } @@ -1329,7 +1329,7 @@ impl Datelike for NaiveDateTime { /// assert_eq!(dt.with_day0(30), None); // no September 31 /// ``` #[inline] - fn with_day0(&self, day0: u32) -> Option { + fn with_day0(&self, day0: u32) -> Result { self.date.with_day0(day0).map(|d| NaiveDateTime { date: d, ..*self }) } @@ -1359,7 +1359,7 @@ impl Datelike for NaiveDateTime { /// Some(NaiveDate::from_ymd(2016, 12, 31).unwrap().and_hms(12, 34, 56).unwrap())); /// ``` #[inline] - fn with_ordinal(&self, ordinal: u32) -> Option { + fn with_ordinal(&self, ordinal: u32) -> Result { self.date.with_ordinal(ordinal).map(|d| NaiveDateTime { date: d, ..*self }) } @@ -1389,7 +1389,7 @@ impl Datelike for NaiveDateTime { /// Some(NaiveDate::from_ymd(2016, 12, 31).unwrap().and_hms(12, 34, 56).unwrap())); /// ``` #[inline] - fn with_ordinal0(&self, ordinal0: u32) -> Option { + fn with_ordinal0(&self, ordinal0: u32) -> Result { self.date.with_ordinal0(ordinal0).map(|d| NaiveDateTime { date: d, ..*self }) } } @@ -1484,7 +1484,7 @@ impl Timelike for NaiveDateTime { /// assert_eq!(dt.with_hour(24), None); /// ``` #[inline] - fn with_hour(&self, hour: u32) -> Option { + fn with_hour(&self, hour: u32) -> Result { self.time.with_hour(hour).map(|t| NaiveDateTime { time: t, ..*self }) } @@ -1507,7 +1507,7 @@ impl Timelike for NaiveDateTime { /// assert_eq!(dt.with_minute(60), None); /// ``` #[inline] - fn with_minute(&self, min: u32) -> Option { + fn with_minute(&self, min: u32) -> Result { self.time.with_minute(min).map(|t| NaiveDateTime { time: t, ..*self }) } @@ -1533,7 +1533,7 @@ impl Timelike for NaiveDateTime { /// assert_eq!(dt.with_second(60), None); /// ``` #[inline] - fn with_second(&self, sec: u32) -> Option { + fn with_second(&self, sec: u32) -> Result { self.time.with_second(sec).map(|t| NaiveDateTime { time: t, ..*self }) } @@ -1562,7 +1562,7 @@ impl Timelike for NaiveDateTime { /// assert_eq!(dt.with_nanosecond(2_000_000_000), None); /// ``` #[inline] - fn with_nanosecond(&self, nano: u32) -> Option { + fn with_nanosecond(&self, nano: u32) -> Result { self.time.with_nanosecond(nano).map(|t| NaiveDateTime { time: t, ..*self }) } } diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index b3907ad2f6..be276331da 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -900,17 +900,18 @@ impl Timelike for NaiveTime { /// ``` /// use chrono::{NaiveTime, Timelike}; /// - /// let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).unwrap(); - /// assert_eq!(dt.with_hour(7), Some(NaiveTime::from_hms_nano(7, 56, 4, 12_345_678).unwrap())); - /// assert_eq!(dt.with_hour(24), None); + /// let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678)?; + /// assert_eq!(dt.with_hour(7), Ok(NaiveTime::from_hms_nano(7, 56, 4, 12_345_678)?)); + /// assert_eq!(dt.with_hour(24), Err(Error::InvalidArgument)); + /// Ok(()) /// ``` #[inline] - fn with_hour(&self, hour: u32) -> Option { + fn with_hour(&self, hour: u32) -> Result { if hour >= 24 { - return None; + return Err(Error::InvalidArgument); } let secs = hour * 3600 + self.secs % 3600; - Some(NaiveTime { secs, ..*self }) + Ok(NaiveTime { secs, ..*self }) } /// Makes a new `NaiveTime` with the minute number changed. @@ -929,12 +930,12 @@ impl Timelike for NaiveTime { /// assert_eq!(dt.with_minute(60), None); /// ``` #[inline] - fn with_minute(&self, min: u32) -> Option { + fn with_minute(&self, min: u32) -> Result { if min >= 60 { - return None; + return Err(Error::InvalidArgument); } let secs = self.secs / 3600 * 3600 + min * 60 + self.secs % 60; - Some(NaiveTime { secs, ..*self }) + Ok(NaiveTime { secs, ..*self }) } /// Makes a new `NaiveTime` with the second number changed. @@ -952,16 +953,16 @@ impl Timelike for NaiveTime { /// use chrono::{NaiveTime, Timelike}; /// /// let dt = NaiveTime::from_hms_nano(23, 56, 4, 12_345_678).unwrap(); - /// assert_eq!(dt.with_second(17), Some(NaiveTime::from_hms_nano(23, 56, 17, 12_345_678).unwrap())); - /// assert_eq!(dt.with_second(60), None); + /// assert_eq!(dt.with_second(17), Ok(NaiveTime::from_hms_nano(23, 56, 17, 12_345_678))); + /// assert_eq!(dt.with_second(60), Err(_)); /// ``` #[inline] - fn with_second(&self, sec: u32) -> Option { + fn with_second(&self, sec: u32) -> Result { if sec >= 60 { - return None; + return Err(Error::InvalidArgument); } let secs = self.secs / 60 * 60 + sec; - Some(NaiveTime { secs, ..*self }) + Ok(NaiveTime { secs, ..*self }) } /// Makes a new `NaiveTime` with nanoseconds since the whole non-leap second changed. @@ -996,11 +997,11 @@ impl Timelike for NaiveTime { /// assert_eq!(strange_leap_second.nanosecond(), 1_333_333_333); /// ``` #[inline] - fn with_nanosecond(&self, nano: u32) -> Option { + fn with_nanosecond(&self, nano: u32) -> Result { if nano >= 2_000_000_000 { - return None; + return Err(Error::InvalidArgument); } - Some(NaiveTime { frac: nano, ..*self }) + Ok(NaiveTime { frac: nano, ..*self }) } /// Returns the number of non-leap seconds past the last midnight. diff --git a/src/traits.rs b/src/traits.rs index 92f4b4107f..68ced09819 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1,4 +1,4 @@ -use crate::{IsoWeek, Weekday}; +use crate::{Error, IsoWeek, Weekday}; /// The common set of methods for date component. /// @@ -116,7 +116,7 @@ pub trait Datelike: Sized { /// NaiveDate::from_yo(2023, 100).unwrap() // result is 2023-101 /// ); /// ``` - fn with_year(&self, year: i32) -> Option; + fn with_year(&self, year: i32) -> Result; /// Makes a new value with the month number (starting from 1) changed. /// @@ -161,7 +161,7 @@ pub trait Datelike: Sized { /// let d = NaiveDate::from_ymd(2020, 2, 29).unwrap(); /// assert_eq!(with_year_month_fixed(d, 2019, 1), NaiveDate::from_ymd(2019, 1, 29)); /// ``` - fn with_month(&self, month: u32) -> Option; + fn with_month(&self, month: u32) -> Result; /// Makes a new value with the month number (starting from 0) changed. /// @@ -175,7 +175,7 @@ pub trait Datelike: Sized { /// - The value for `month0` is out of range. /// /// [`DateTime`]: crate::DateTime - fn with_month0(&self, month0: u32) -> Option; + fn with_month0(&self, month0: u32) -> Result; /// Makes a new value with the day of month (starting from 1) changed. /// @@ -189,7 +189,7 @@ pub trait Datelike: Sized { /// - The value for `day` is out of range. /// /// [`DateTime`]: crate::DateTime - fn with_day(&self, day: u32) -> Option; + fn with_day(&self, day: u32) -> Result; /// Makes a new value with the day of month (starting from 0) changed. /// @@ -203,7 +203,7 @@ pub trait Datelike: Sized { /// - The value for `day0` is out of range. /// /// [`DateTime`]: crate::DateTime - fn with_day0(&self, day0: u32) -> Option; + fn with_day0(&self, day0: u32) -> Result; /// Makes a new value with the day of year (starting from 1) changed. /// @@ -217,7 +217,7 @@ pub trait Datelike: Sized { /// - The value for `ordinal` is out of range. /// /// [`DateTime`]: crate::DateTime - fn with_ordinal(&self, ordinal: u32) -> Option; + fn with_ordinal(&self, ordinal: u32) -> Result; /// Makes a new value with the day of year (starting from 0) changed. /// @@ -231,7 +231,7 @@ pub trait Datelike: Sized { /// - The value for `ordinal0` is out of range. /// /// [`DateTime`]: crate::DateTime - fn with_ordinal0(&self, ordinal0: u32) -> Option; + fn with_ordinal0(&self, ordinal0: u32) -> Result; /// Counts the days in the proleptic Gregorian calendar, with January 1, Year 1 (CE) as day 1. /// @@ -293,27 +293,27 @@ pub trait Timelike: Sized { /// Makes a new value with the hour number changed. /// - /// Returns `None` when the resulting value would be invalid. - fn with_hour(&self, hour: u32) -> Option; + /// Returns `Err` when the resulting value would be invalid. + fn with_hour(&self, hour: u32) -> Result; /// Makes a new value with the minute number changed. /// - /// Returns `None` when the resulting value would be invalid. - fn with_minute(&self, min: u32) -> Option; + /// Returns `Err` when the resulting value would be invalid. + fn with_minute(&self, min: u32) -> Result; /// Makes a new value with the second number changed. /// - /// Returns `None` when the resulting value would be invalid. + /// Returns `Err` when the resulting value would be invalid. /// As with the [`second`](#tymethod.second) method, /// the input range is restricted to 0 through 59. - fn with_second(&self, sec: u32) -> Option; + fn with_second(&self, sec: u32) -> Result; /// Makes a new value with nanoseconds since the whole non-leap second changed. /// - /// Returns `None` when the resulting value would be invalid. + /// Returns `Err` when the resulting value would be invalid. /// As with the [`nanosecond`](#tymethod.nanosecond) method, /// the input range can exceed 1,000,000,000 for leap seconds. - fn with_nanosecond(&self, nano: u32) -> Option; + fn with_nanosecond(&self, nano: u32) -> Result; /// Returns the number of non-leap seconds past the last midnight. ///