Skip to content

Commit

Permalink
Deny leap second if secs != 59 in from_hms_nano_opt
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Sep 12, 2023
1 parent 202af6c commit 61b7ffb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
24 changes: 12 additions & 12 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,8 @@ impl NaiveDate {

/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond.
///
/// The millisecond part can exceed 1,000
/// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// The millisecond part is allowed to exceed 1,000,000,000 in order to represent a [leap second](
/// ./struct.NaiveTime.html#leap-second-handling), but only when `sec == 59`.
///
/// # Panics
///
Expand All @@ -882,8 +882,8 @@ impl NaiveDate {

/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond.
///
/// The millisecond part can exceed 1,000
/// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// The millisecond part is allowed to exceed 1,000,000,000 in order to represent a [leap second](
/// ./struct.NaiveTime.html#leap-second-handling), but only when `sec == 59`.
///
/// # Errors
///
Expand Down Expand Up @@ -917,8 +917,8 @@ impl NaiveDate {

/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond.
///
/// The microsecond part can exceed 1,000,000
/// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// The microsecond part is allowed to exceed 1,000,000,000 in order to represent a [leap second](
/// ./struct.NaiveTime.html#leap-second-handling), but only when `sec == 59`.
///
/// # Panics
///
Expand Down Expand Up @@ -946,8 +946,8 @@ impl NaiveDate {

/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond.
///
/// The microsecond part can exceed 1,000,000
/// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// The microsecond part is allowed to exceed 1,000,000,000 in order to represent a [leap second](
/// ./struct.NaiveTime.html#leap-second-handling), but only when `sec == 59`.
///
/// # Errors
///
Expand Down Expand Up @@ -981,8 +981,8 @@ impl NaiveDate {

/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond.
///
/// The nanosecond part can exceed 1,000,000,000
/// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a [leap second](
/// ./struct.NaiveTime.html#leap-second-handling), but only when `sec == 59`.
///
/// # Panics
///
Expand All @@ -996,8 +996,8 @@ impl NaiveDate {

/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond.
///
/// The nanosecond part can exceed 1,000,000,000
/// in order to represent the [leap second](./struct.NaiveTime.html#leap-second-handling).
/// The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a [leap second](
/// ./struct.NaiveTime.html#leap-second-handling), but only when `sec == 59`.
///
/// # Errors
///
Expand Down
33 changes: 20 additions & 13 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ mod tests;
/// 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`](crate::DateTime) with fractional minutes as offset, or using
/// [`Timelike::with_nanosecond()`].
///
/// ```
/// use chrono::{FixedOffset, NaiveDate, TimeZone};
///
Expand Down Expand Up @@ -239,8 +243,8 @@ impl NaiveTime {

/// Makes a new `NaiveTime` from hour, minute and second.
///
/// No [leap second](#leap-second-handling) is allowed here;
/// use `NaiveTime::from_hms_*_opt` methods with a subsecond parameter instead.
/// The millisecond part is allowed to exceed 1,000,000,000 in order to represent a
/// [leap second](#leap-second-handling), but only when `sec == 59`.
///
/// # Errors
///
Expand Down Expand Up @@ -282,8 +286,8 @@ impl NaiveTime {

/// Makes a new `NaiveTime` from hour, minute, second and millisecond.
///
/// The millisecond part can exceed 1,000
/// in order to represent the [leap second](#leap-second-handling).
/// The millisecond part is allowed to exceed 1,000,000,000 in order to represent a
/// [leap second](#leap-second-handling), but only when `sec == 59`.
///
/// # Errors
///
Expand Down Expand Up @@ -318,8 +322,8 @@ impl NaiveTime {

/// Makes a new `NaiveTime` from hour, minute, second and microsecond.
///
/// The microsecond part can exceed 1,000,000
/// in order to represent the [leap second](#leap-second-handling).
/// The microsecond part is allowed to exceed 1,000,000,000 in order to represent a
/// [leap second](#leap-second-handling), but only when `sec == 59`.
///
/// # Panics
///
Expand All @@ -333,8 +337,8 @@ impl NaiveTime {

/// Makes a new `NaiveTime` from hour, minute, second and microsecond.
///
/// The microsecond part can exceed 1,000,000
/// in order to represent the [leap second](#leap-second-handling).
/// The microsecond part is allowed to exceed 1,000,000,000 in order to represent a
/// [leap second](#leap-second-handling), but only when `sec == 59`.
///
/// # Errors
///
Expand Down Expand Up @@ -369,8 +373,8 @@ impl NaiveTime {

/// Makes a new `NaiveTime` from hour, minute, second and nanosecond.
///
/// The nanosecond part can exceed 1,000,000,000
/// in order to represent the [leap second](#leap-second-handling).
/// The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a
/// [leap second](#leap-second-handling), but only when `sec == 59`.
///
/// # Panics
///
Expand All @@ -384,8 +388,8 @@ impl NaiveTime {

/// Makes a new `NaiveTime` from hour, minute, second and nanosecond.
///
/// The nanosecond part can exceed 1,000,000,000
/// in order to represent the [leap second](#leap-second-handling).
/// The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a
/// [leap second](#leap-second-handling), but only when `sec == 59`.
///
/// # Errors
///
Expand All @@ -409,7 +413,10 @@ impl NaiveTime {
#[inline]
#[must_use]
pub const fn from_hms_nano_opt(hour: u32, min: u32, sec: u32, nano: u32) -> Option<NaiveTime> {
if hour >= 24 || min >= 60 || sec >= 60 || nano >= 2_000_000_000 {
if (hour >= 24 || min >= 60 || sec >= 60)
|| (nano >= 1_000_000_000 && sec != 59)
|| nano >= 2_000_000_000
{
return None;
}
let secs = hour * 3600 + min * 60 + sec;
Expand Down

0 comments on commit 61b7ffb

Please sign in to comment.