-
Notifications
You must be signed in to change notification settings - Fork 543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't allow strange leap seconds in initialization methods #1283
Conversation
Codecov Report
@@ Coverage Diff @@
## 0.4.x #1283 +/- ##
==========================================
- Coverage 91.41% 91.33% -0.09%
==========================================
Files 38 38
Lines 16957 16971 +14
==========================================
- Hits 15502 15501 -1
- Misses 1455 1470 +15
... and 4 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
259473e
to
27d90ad
Compare
src/naive/date.rs
Outdated
@@ -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]( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"allowed exceed" -> "allowed to exceed" for all of these?
Nit: please try to keep the first line of your commit messages within ~72 characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, fixed.
src/naive/time/mod.rs
Outdated
@@ -458,7 +458,7 @@ impl NaiveTime { | |||
#[inline] | |||
#[must_use] | |||
pub const fn from_num_seconds_from_midnight_opt(secs: u32, nano: u32) -> Option<NaiveTime> { | |||
if secs >= 86_400 || nano >= 2_000_000_000 { | |||
if secs >= 86_400 || (nano >= 1_000_000_000 && secs % 60 != 59) || nano >= 2_000_000_000 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would order the leap second conditional last here, since it is the most complex.
5dba138
to
d05eb11
Compare
I forgot the |
d05eb11
to
8a64c8f
Compare
As discussed in #944.
A leap second that is not on a minute boundary never happens in reality. It still makes sense to be able to represent them. But the less trivial it is to create them, the less confusion they can cause (?).
We have to add an extra check in
NaiveTime::from_hms_nano_opt
andNaiveTime::from_num_seconds_from_midnight_opt
.This also effects the following methods:
DateTime<Tz>::from_timestamp
(brand-new 😄)NaiveTime::from_hms_milli
NaiveTime::from_hms_milli_opt
NaiveTime::from_hms_micro
NaiveTime::from_hms_micro_opt
NaiveTime::from_hms_nano
NaiveTime::from_hms_nano_opt
NaiveTime::from_num_seconds_from_midnight
NaiveDate::and_hms_milli
NaiveDate::and_hms_milli_opt
NaiveDate::and_hms_micro
NaiveDate::and_hms_micro_opt
NaiveDate::and_hms_nano
NaiveDate::and_hms_nano_opt
NaiveDateTime::from_timestamp
NaiveDateTime::from_timestamp_opt
TimeZone::timestamp
TimeZone::timestamp_opt
A strange leap-second can still be created from an existing value by using a
DateTime
with a fractional minute offset, and by changing a time withTimelike::with_nanosecond
.