Skip to content

Commit

Permalink
Don't allow leap seconds that are not on a minute boundary in `from_h…
Browse files Browse the repository at this point in the history
…ms_nano_opt`
  • Loading branch information
pitdicker committed Sep 12, 2023
1 parent bf007c7 commit 0da66de
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 @@ -861,8 +861,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 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 @@ -876,8 +876,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 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 @@ -911,8 +911,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 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 @@ -940,8 +940,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 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 @@ -975,8 +975,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 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 @@ -990,8 +990,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 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 @@ -234,8 +238,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 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 @@ -277,8 +281,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 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 @@ -313,8 +317,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 exceed 1,000,000,000 in order to represent a
/// [leap second](#leap-second-handling), but only when `sec == 59`.
///
/// # Panics
///
Expand All @@ -328,8 +332,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 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 @@ -364,8 +368,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 exceed 1,000,000,000 in order to represent a
/// [leap second](#leap-second-handling), but only when `sec == 59`.
///
/// # Panics
///
Expand All @@ -379,8 +383,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 exceed 1,000,000,000 in order to represent a
/// [leap second](#leap-second-handling), but only when `sec == 59`.
///
/// # Errors
///
Expand All @@ -404,7 +408,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 0da66de

Please sign in to comment.