This commit is contained in:
Jordan Petridis 2017-10-09 15:30:08 +03:00
commit 1286ad12d3
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
11 changed files with 118 additions and 114 deletions

View File

@ -362,26 +362,30 @@ pub fn format<'a, I>(w: &mut fmt::Formatter, date: Option<&NaiveDate>, time: Opt
(d.ordinal() as i32 - d.weekday().num_days_from_monday() as i32 + 7) / 7; (d.ordinal() as i32 - d.weekday().num_days_from_monday() as i32 + 7) / 7;
let (width, v) = match spec { let (width, v) = match spec {
Year => (4, date.map(|d| d.year() as i64)), Year => (4, date.map(|d| i64::from(d.year()))),
YearDiv100 => (2, date.map(|d| div_floor(d.year() as i64, 100))), YearDiv100 => (2, date.map(|d| div_floor(i64::from(d.year()), 100))),
YearMod100 => (2, date.map(|d| mod_floor(d.year() as i64, 100))), YearMod100 => (2, date.map(|d| mod_floor(i64::from(d.year()), 100))),
IsoYear => (4, date.map(|d| d.iso_week().year() as i64)), IsoYear => (4, date.map(|d| i64::from(d.iso_week().year()))),
IsoYearDiv100 => (2, date.map(|d| div_floor(d.iso_week().year() as i64, 100))), IsoYearDiv100 => (2, date.map(|d| div_floor(
IsoYearMod100 => (2, date.map(|d| mod_floor(d.iso_week().year() as i64, 100))), i64::from(d.iso_week().year()), 100))),
Month => (2, date.map(|d| d.month() as i64)), IsoYearMod100 => (2, date.map(|d| mod_floor(
Day => (2, date.map(|d| d.day() as i64)), i64::from(d.iso_week().year()), 100))),
WeekFromSun => (2, date.map(|d| week_from_sun(d) as i64)), Month => (2, date.map(|d| i64::from(d.month()))),
WeekFromMon => (2, date.map(|d| week_from_mon(d) as i64)), Day => (2, date.map(|d| i64::from(d.day()))),
IsoWeek => (2, date.map(|d| d.iso_week().week() as i64)), WeekFromSun => (2, date.map(|d| i64::from(week_from_sun(d)))),
NumDaysFromSun => (1, date.map(|d| d.weekday().num_days_from_sunday() as i64)), WeekFromMon => (2, date.map(|d| i64::from(week_from_mon(d)))),
WeekdayFromMon => (1, date.map(|d| d.weekday().number_from_monday() as i64)), IsoWeek => (2, date.map(|d| i64::from(d.iso_week().week()))),
Ordinal => (3, date.map(|d| d.ordinal() as i64)), NumDaysFromSun => (1, date.map(|d| i64::from(d.weekday()
Hour => (2, time.map(|t| t.hour() as i64)), .num_days_from_sunday()))),
Hour12 => (2, time.map(|t| t.hour12().1 as i64)), WeekdayFromMon => (1, date.map(|d| i64::from(d.weekday()
Minute => (2, time.map(|t| t.minute() as i64)), .number_from_monday()))),
Second => (2, time.map(|t| (t.second() + Ordinal => (3, date.map(|d| i64::from(d.ordinal()))),
t.nanosecond() / 1_000_000_000) as i64)), Hour => (2, time.map(|t| i64::from(t.hour()))),
Nanosecond => (9, time.map(|t| (t.nanosecond() % 1_000_000_000) as i64)), Hour12 => (2, time.map(|t| i64::from(t.hour12().1))),
Minute => (2, time.map(|t| i64::from(t.minute()))),
Second => (2, time.map(|t| i64::from(t.second() +
t.nanosecond() / 1_000_000_000))),
Nanosecond => (9, time.map(|t| i64::from(t.nanosecond() % 1_000_000_000))),
Timestamp => (1, match (date, time, off) { Timestamp => (1, match (date, time, off) {
(Some(d), Some(t), None) => (Some(d), Some(t), None) =>
Some(d.and_time(*t).timestamp()), Some(d.and_time(*t).timestamp()),
@ -395,7 +399,7 @@ pub fn format<'a, I>(w: &mut fmt::Formatter, date: Option<&NaiveDate>, time: Opt
}; };
if let Some(v) = v { if let Some(v) = v {
if (spec == Year || spec == IsoYear) && !(0 <= v && v < 10000) { if (spec == Year || spec == IsoYear) && !(0 <= v && v < 10_000) {
// non-four-digit years require an explicit sign as per ISO 8601 // non-four-digit years require an explicit sign as per ISO 8601
match pad { match pad {
Pad::None => try!(write!(w, "{:+}", v)), Pad::None => try!(write!(w, "{:+}", v)),

View File

@ -93,7 +93,7 @@ fn parse_rfc2822<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a st
s = s.trim_left(); s = s.trim_left();
try!(parsed.set_day(try_consume!(scan::number(s, 1, 2)))); try!(parsed.set_day(try_consume!(scan::number(s, 1, 2))));
s = try!(scan::space(s)); // mandatory s = try!(scan::space(s)); // mandatory
try!(parsed.set_month(1 + try_consume!(scan::short_month0(s)) as i64)); try!(parsed.set_month(1 + i64::from(try_consume!(scan::short_month0(s)))));
s = try!(scan::space(s)); // mandatory s = try!(scan::space(s)); // mandatory
// distinguish two- and three-digit years from four-digit years // distinguish two- and three-digit years from four-digit years
@ -119,7 +119,7 @@ fn parse_rfc2822<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a st
s = try!(scan::space(s)); // mandatory s = try!(scan::space(s)); // mandatory
if let Some(offset) = try_consume!(scan::timezone_offset_2822(s)) { if let Some(offset) = try_consume!(scan::timezone_offset_2822(s)) {
// only set the offset when it is definitely known (i.e. not `-0000`) // only set the offset when it is definitely known (i.e. not `-0000`)
try!(parsed.set_offset(offset as i64)); try!(parsed.set_offset(i64::from(offset)));
} }
Ok((s, ())) Ok((s, ()))
@ -180,8 +180,8 @@ fn parse_rfc3339<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a st
} }
let offset = try_consume!(scan::timezone_offset_zulu(s, |s| scan::char(s, b':'))); let offset = try_consume!(scan::timezone_offset_zulu(s, |s| scan::char(s, b':')));
if offset <= -86400 || offset >= 86400 { return Err(OUT_OF_RANGE); } if offset <= -86_400 || offset >= 86_400 { return Err(OUT_OF_RANGE); }
try!(parsed.set_offset(offset as i64)); try!(parsed.set_offset(i64::from(offset)));
Ok((s, ())) Ok((s, ()))
} }
@ -279,12 +279,12 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<(
match spec { match spec {
ShortMonthName => { ShortMonthName => {
let month0 = try_consume!(scan::short_month0(s)); let month0 = try_consume!(scan::short_month0(s));
try!(parsed.set_month(month0 as i64 + 1)); try!(parsed.set_month(i64::from(month0) + 1));
} }
LongMonthName => { LongMonthName => {
let month0 = try_consume!(scan::short_or_long_month0(s)); let month0 = try_consume!(scan::short_or_long_month0(s));
try!(parsed.set_month(month0 as i64 + 1)); try!(parsed.set_month(i64::from(month0) + 1));
} }
ShortWeekdayName => { ShortWeekdayName => {
@ -320,13 +320,13 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<(
TimezoneOffsetColon | TimezoneOffset => { TimezoneOffsetColon | TimezoneOffset => {
let offset = try_consume!(scan::timezone_offset(s.trim_left(), let offset = try_consume!(scan::timezone_offset(s.trim_left(),
scan::colon_or_space)); scan::colon_or_space));
try!(parsed.set_offset(offset as i64)); try!(parsed.set_offset(i64::from(offset)));
} }
TimezoneOffsetColonZ | TimezoneOffsetZ => { TimezoneOffsetColonZ | TimezoneOffsetZ => {
let offset = try_consume!(scan::timezone_offset_zulu(s.trim_left(), let offset = try_consume!(scan::timezone_offset_zulu(s.trim_left(),
scan::colon_or_space)); scan::colon_or_space));
try!(parsed.set_offset(offset as i64)); try!(parsed.set_offset(i64::from(offset)));
} }
RFC2822 => try_consume!(parse_rfc2822(parsed, s)), RFC2822 => try_consume!(parse_rfc2822(parsed, s)),

View File

@ -394,7 +394,7 @@ impl Parsed {
if week_from_sun > 53 { return Err(OUT_OF_RANGE); } // can it overflow? if week_from_sun > 53 { return Err(OUT_OF_RANGE); } // can it overflow?
let ndays = firstweek + (week_from_sun as i32 - 1) * 7 + let ndays = firstweek + (week_from_sun as i32 - 1) * 7 +
weekday.num_days_from_sunday() as i32; weekday.num_days_from_sunday() as i32;
let date = try!(newyear.checked_add_signed(OldDuration::days(ndays as i64)) let date = try!(newyear.checked_add_signed(OldDuration::days(i64::from(ndays)))
.ok_or(OUT_OF_RANGE)); .ok_or(OUT_OF_RANGE));
if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error
@ -419,7 +419,7 @@ impl Parsed {
if week_from_mon > 53 { return Err(OUT_OF_RANGE); } // can it overflow? if week_from_mon > 53 { return Err(OUT_OF_RANGE); } // can it overflow?
let ndays = firstweek + (week_from_mon as i32 - 1) * 7 + let ndays = firstweek + (week_from_mon as i32 - 1) * 7 +
weekday.num_days_from_monday() as i32; weekday.num_days_from_monday() as i32;
let date = try!(newyear.checked_add_signed(OldDuration::days(ndays as i64)) let date = try!(newyear.checked_add_signed(OldDuration::days(i64::from(ndays)))
.ok_or(OUT_OF_RANGE)); .ok_or(OUT_OF_RANGE));
if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error
@ -502,7 +502,7 @@ impl Parsed {
// verify the timestamp field if any // verify the timestamp field if any
// the following is safe, `timestamp` is very limited in range // the following is safe, `timestamp` is very limited in range
let timestamp = datetime.timestamp() - offset as i64; let timestamp = datetime.timestamp() - i64::from(offset);
if let Some(given_timestamp) = self.timestamp { if let Some(given_timestamp) = self.timestamp {
// if `datetime` represents a leap second, it might be off by one second. // if `datetime` represents a leap second, it might be off by one second.
if given_timestamp != timestamp && if given_timestamp != timestamp &&
@ -525,7 +525,7 @@ impl Parsed {
} }
// reconstruct date and time fields from timestamp // reconstruct date and time fields from timestamp
let ts = try!(timestamp.checked_add(offset as i64).ok_or(OUT_OF_RANGE)); let ts = try!(timestamp.checked_add(i64::from(offset)).ok_or(OUT_OF_RANGE));
let datetime = NaiveDateTime::from_timestamp_opt(ts, 0); let datetime = NaiveDateTime::from_timestamp_opt(ts, 0);
let mut datetime = try!(datetime.ok_or(OUT_OF_RANGE)); let mut datetime = try!(datetime.ok_or(OUT_OF_RANGE));
@ -544,12 +544,12 @@ impl Parsed {
} }
// ...and we have the correct candidates for other fields. // ...and we have the correct candidates for other fields.
} else { } else {
try!(parsed.set_second(datetime.second() as i64)); try!(parsed.set_second(i64::from(datetime.second())));
} }
try!(parsed.set_year (datetime.year() as i64)); try!(parsed.set_year (i64::from(datetime.year())));
try!(parsed.set_ordinal(datetime.ordinal() as i64)); // more efficient than ymd try!(parsed.set_ordinal(i64::from(datetime.ordinal()))); // more efficient than ymd
try!(parsed.set_hour (datetime.hour() as i64)); try!(parsed.set_hour (i64::from(datetime.hour())));
try!(parsed.set_minute (datetime.minute() as i64)); try!(parsed.set_minute (i64::from(datetime.minute())));
// validate other fields (e.g. week) and return // validate other fields (e.g. week) and return
let date = try!(parsed.to_naive_date()); let date = try!(parsed.to_naive_date());
@ -1041,7 +1041,7 @@ mod tests {
minute: 42, second: 4, nanosecond: 12_345_678, offset: -9876), minute: 42, second: 4, nanosecond: 12_345_678, offset: -9876),
ymdhmsn(2014,12,31, 1,42,4,12_345_678, -9876)); ymdhmsn(2014,12,31, 1,42,4,12_345_678, -9876));
assert_eq!(parse!(year: 2015, ordinal: 1, hour_div_12: 0, hour_mod_12: 4, assert_eq!(parse!(year: 2015, ordinal: 1, hour_div_12: 0, hour_mod_12: 4,
minute: 26, second: 40, nanosecond: 12_345_678, offset: 86400), minute: 26, second: 40, nanosecond: 12_345_678, offset: 86_400),
Err(OUT_OF_RANGE)); // `FixedOffset` does not support such huge offset Err(OUT_OF_RANGE)); // `FixedOffset` does not support such huge offset
} }

View File

@ -191,7 +191,7 @@ pub fn timezone_offset<F>(mut s: &str, mut colon: F) -> ParseResult<(&str, i32)>
// hours (00--99) // hours (00--99)
let hours = match try!(digits(s)) { let hours = match try!(digits(s)) {
(h1 @ b'0'...b'9', h2 @ b'0'...b'9') => ((h1 - b'0') * 10 + (h2 - b'0')) as i32, (h1 @ b'0'...b'9', h2 @ b'0'...b'9') => i32::from((h1 - b'0') * 10 + (h2 - b'0')),
_ => return Err(INVALID), _ => return Err(INVALID),
}; };
s = &s[2..]; s = &s[2..];
@ -201,7 +201,7 @@ pub fn timezone_offset<F>(mut s: &str, mut colon: F) -> ParseResult<(&str, i32)>
// minutes (00--59) // minutes (00--59)
let minutes = match try!(digits(s)) { let minutes = match try!(digits(s)) {
(m1 @ b'0'...b'5', m2 @ b'0'...b'9') => ((m1 - b'0') * 10 + (m2 - b'0')) as i32, (m1 @ b'0'...b'5', m2 @ b'0'...b'9') => i32::from((m1 - b'0') * 10 + (m2 - b'0')),
(b'6'...b'9', b'0'...b'9') => return Err(OUT_OF_RANGE), (b'6'...b'9', b'0'...b'9') => return Err(OUT_OF_RANGE),
_ => return Err(INVALID), _ => return Err(INVALID),
}; };

View File

@ -857,7 +857,7 @@ pub trait Datelike: Sized {
if year < 0 { if year < 0 {
let excess = 1 + (-year) / 400; let excess = 1 + (-year) / 400;
year += excess * 400; year += excess * 400;
ndays -= excess * 146097; ndays -= excess * 146_097;
} }
let div_100 = year / 100; let div_100 = year / 100;
ndays += ((year * 1461) >> 2) - div_100 + (div_100 >> 2); ndays += ((year * 1461) >> 2) - div_100 + (div_100 >> 2);

View File

@ -400,7 +400,7 @@ impl NaiveDate {
/// ~~~~ /// ~~~~
pub fn from_num_days_from_ce_opt(days: i32) -> Option<NaiveDate> { pub fn from_num_days_from_ce_opt(days: i32) -> Option<NaiveDate> {
let days = days + 365; // make December 31, 1 BCE equal to day 0 let days = days + 365; // make December 31, 1 BCE equal to day 0
let (year_div_400, cycle) = div_mod_floor(days, 146097); let (year_div_400, cycle) = div_mod_floor(days, 146_097);
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32); let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32); let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32,
@ -683,7 +683,7 @@ impl NaiveDate {
/// Returns the packed ordinal-flags. /// Returns the packed ordinal-flags.
#[inline] #[inline]
fn of(&self) -> Of { fn of(&self) -> Of {
Of((self.ymdf & 0b1111_11111_1111) as u32) Of((self.ymdf & 0b1_1111_1111_1111) as u32)
} }
/// Makes a new `NaiveDate` with the packed month-day-flags changed. /// Makes a new `NaiveDate` with the packed month-day-flags changed.
@ -701,7 +701,7 @@ impl NaiveDate {
fn with_of(&self, of: Of) -> Option<NaiveDate> { fn with_of(&self, of: Of) -> Option<NaiveDate> {
if of.valid() { if of.valid() {
let Of(of) = of; let Of(of) = of;
Some(NaiveDate { ymdf: (self.ymdf & !0b111111111_1111) | of as DateImpl }) Some(NaiveDate { ymdf: (self.ymdf & !0b1_1111_1111_1111) | of as DateImpl })
} else { } else {
None None
} }
@ -808,7 +808,7 @@ impl NaiveDate {
let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400); let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400);
let cycle = internals::yo_to_cycle(year_mod_400 as u32, self.of().ordinal()); let cycle = internals::yo_to_cycle(year_mod_400 as u32, self.of().ordinal());
let cycle = try_opt!((cycle as i32).checked_add(try_opt!(rhs.num_days().to_i32()))); let cycle = try_opt!((cycle as i32).checked_add(try_opt!(rhs.num_days().to_i32())));
let (cycle_div_400y, cycle) = div_mod_floor(cycle, 146097); let (cycle_div_400y, cycle) = div_mod_floor(cycle, 146_097);
year_div_400 += cycle_div_400y; year_div_400 += cycle_div_400y;
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32); let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
@ -844,7 +844,7 @@ impl NaiveDate {
let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400); let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400);
let cycle = internals::yo_to_cycle(year_mod_400 as u32, self.of().ordinal()); let cycle = internals::yo_to_cycle(year_mod_400 as u32, self.of().ordinal());
let cycle = try_opt!((cycle as i32).checked_sub(try_opt!(rhs.num_days().to_i32()))); let cycle = try_opt!((cycle as i32).checked_sub(try_opt!(rhs.num_days().to_i32())));
let (cycle_div_400y, cycle) = div_mod_floor(cycle, 146097); let (cycle_div_400y, cycle) = div_mod_floor(cycle, 146_097);
year_div_400 += cycle_div_400y; year_div_400 += cycle_div_400y;
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32); let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
@ -883,9 +883,9 @@ impl NaiveDate {
let year2 = rhs.year(); let year2 = rhs.year();
let (year1_div_400, year1_mod_400) = div_mod_floor(year1, 400); let (year1_div_400, year1_mod_400) = div_mod_floor(year1, 400);
let (year2_div_400, year2_mod_400) = div_mod_floor(year2, 400); let (year2_div_400, year2_mod_400) = div_mod_floor(year2, 400);
let cycle1 = internals::yo_to_cycle(year1_mod_400 as u32, self.of().ordinal()) as i64; let cycle1 = i64::from(internals::yo_to_cycle(year1_mod_400 as u32, self.of().ordinal()));
let cycle2 = internals::yo_to_cycle(year2_mod_400 as u32, rhs.of().ordinal()) as i64; let cycle2 = i64::from(internals::yo_to_cycle(year2_mod_400 as u32, rhs.of().ordinal()));
OldDuration::days((year1_div_400 as i64 - year2_div_400 as i64) * 146097 + OldDuration::days((i64::from(year1_div_400) - i64::from(year2_div_400)) * 146_097 +
(cycle1 - cycle2)) (cycle1 - cycle2))
} }

View File

@ -133,8 +133,8 @@ impl NaiveDateTime {
/// ~~~~ /// ~~~~
#[inline] #[inline]
pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> { pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
let (days, secs) = div_mod_floor(secs, 86400); let (days, secs) = div_mod_floor(secs, 86_400);
let date = days.to_i32().and_then(|days| days.checked_add(719163)) let date = days.to_i32().and_then(|days| days.checked_add(719_163))
.and_then(NaiveDate::from_num_days_from_ce_opt); .and_then(NaiveDate::from_num_days_from_ce_opt);
let time = NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs); let time = NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs);
match (date, time) { match (date, time) {
@ -258,9 +258,9 @@ impl NaiveDateTime {
/// ~~~~ /// ~~~~
#[inline] #[inline]
pub fn timestamp(&self) -> i64 { pub fn timestamp(&self) -> i64 {
let ndays = self.date.num_days_from_ce() as i64; let ndays = i64::from(self.date.num_days_from_ce());
let nseconds = self.time.num_seconds_from_midnight() as i64; let nseconds = i64::from(self.time.num_seconds_from_midnight());
(ndays - 719163) * 86400 + nseconds (ndays - 719_163) * 86_400 + nseconds
} }
/// Returns the number of non-leap *milliseconds* since midnight on January 1, 1970. /// Returns the number of non-leap *milliseconds* since midnight on January 1, 1970.
@ -287,7 +287,7 @@ impl NaiveDateTime {
#[inline] #[inline]
pub fn timestamp_millis(&self) -> i64 { pub fn timestamp_millis(&self) -> i64 {
let as_ms = self.timestamp() * 1000; let as_ms = self.timestamp() * 1000;
as_ms + self.timestamp_subsec_millis() as i64 as_ms + i64::from(self.timestamp_subsec_millis())
} }
/// Returns the number of milliseconds since the last whole non-leap second. /// Returns the number of milliseconds since the last whole non-leap second.
@ -381,7 +381,7 @@ impl NaiveDateTime {
/// Some(hms(3, 5, 6))); /// Some(hms(3, 5, 6)));
/// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::seconds(3600 + 60)), /// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::seconds(3600 + 60)),
/// Some(hms(4, 6, 7))); /// Some(hms(4, 6, 7)));
/// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::seconds(86400)), /// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::seconds(86_400)),
/// Some(from_ymd(2016, 7, 9).and_hms(3, 5, 7))); /// Some(from_ymd(2016, 7, 9).and_hms(3, 5, 7)));
/// ///
/// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli); /// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli);
@ -467,7 +467,7 @@ impl NaiveDateTime {
/// Some(hms(3, 5, 8))); /// Some(hms(3, 5, 8)));
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::seconds(3600 + 60)), /// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::seconds(3600 + 60)),
/// Some(hms(2, 4, 7))); /// Some(hms(2, 4, 7)));
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::seconds(86400)), /// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::seconds(86_400)),
/// Some(from_ymd(2016, 7, 7).and_hms(3, 5, 7))); /// Some(from_ymd(2016, 7, 7).and_hms(3, 5, 7)));
/// ///
/// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli); /// let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli);
@ -546,7 +546,7 @@ impl NaiveDateTime {
/// // July 8 is 190th day in the year 2016 /// // July 8 is 190th day in the year 2016
/// let d0 = from_ymd(2016, 1, 1); /// let d0 = from_ymd(2016, 1, 1);
/// assert_eq!(d.and_hms_milli(0, 7, 6, 500).signed_duration_since(d0.and_hms(0, 0, 0)), /// assert_eq!(d.and_hms_milli(0, 7, 6, 500).signed_duration_since(d0.and_hms(0, 0, 0)),
/// Duration::seconds(189 * 86400 + 7 * 60 + 6) + Duration::milliseconds(500)); /// Duration::seconds(189 * 86_400 + 7 * 60 + 6) + Duration::milliseconds(500));
/// # } /// # }
/// ~~~~ /// ~~~~
/// ///
@ -1163,7 +1163,7 @@ impl hash::Hash for NaiveDateTime {
/// assert_eq!(hms(3, 5, 7) + Duration::seconds(1), hms(3, 5, 8)); /// assert_eq!(hms(3, 5, 7) + Duration::seconds(1), hms(3, 5, 8));
/// assert_eq!(hms(3, 5, 7) + Duration::seconds(-1), hms(3, 5, 6)); /// assert_eq!(hms(3, 5, 7) + Duration::seconds(-1), hms(3, 5, 6));
/// assert_eq!(hms(3, 5, 7) + Duration::seconds(3600 + 60), hms(4, 6, 7)); /// assert_eq!(hms(3, 5, 7) + Duration::seconds(3600 + 60), hms(4, 6, 7));
/// assert_eq!(hms(3, 5, 7) + Duration::seconds(86400), /// assert_eq!(hms(3, 5, 7) + Duration::seconds(86_400),
/// from_ymd(2016, 7, 9).and_hms(3, 5, 7)); /// from_ymd(2016, 7, 9).and_hms(3, 5, 7));
/// assert_eq!(hms(3, 5, 7) + Duration::days(365), /// assert_eq!(hms(3, 5, 7) + Duration::days(365),
/// from_ymd(2017, 7, 8).and_hms(3, 5, 7)); /// from_ymd(2017, 7, 8).and_hms(3, 5, 7));
@ -1235,7 +1235,7 @@ impl AddAssign<OldDuration> for NaiveDateTime {
/// assert_eq!(hms(3, 5, 7) - Duration::seconds(1), hms(3, 5, 6)); /// assert_eq!(hms(3, 5, 7) - Duration::seconds(1), hms(3, 5, 6));
/// assert_eq!(hms(3, 5, 7) - Duration::seconds(-1), hms(3, 5, 8)); /// assert_eq!(hms(3, 5, 7) - Duration::seconds(-1), hms(3, 5, 8));
/// assert_eq!(hms(3, 5, 7) - Duration::seconds(3600 + 60), hms(2, 4, 7)); /// assert_eq!(hms(3, 5, 7) - Duration::seconds(3600 + 60), hms(2, 4, 7));
/// assert_eq!(hms(3, 5, 7) - Duration::seconds(86400), /// assert_eq!(hms(3, 5, 7) - Duration::seconds(86_400),
/// from_ymd(2016, 7, 7).and_hms(3, 5, 7)); /// from_ymd(2016, 7, 7).and_hms(3, 5, 7));
/// assert_eq!(hms(3, 5, 7) - Duration::days(365), /// assert_eq!(hms(3, 5, 7) - Duration::days(365),
/// from_ymd(2015, 7, 9).and_hms(3, 5, 7)); /// from_ymd(2015, 7, 9).and_hms(3, 5, 7));
@ -1807,9 +1807,9 @@ mod tests {
check((2014,5,6, 7,8,9), Duration::seconds(3600 + 60 + 1), Some((2014,5,6, 8,9,10))); check((2014,5,6, 7,8,9), Duration::seconds(3600 + 60 + 1), Some((2014,5,6, 8,9,10)));
check((2014,5,6, 7,8,9), Duration::seconds(-(3600 + 60 + 1)), Some((2014,5,6, 6,7,8))); check((2014,5,6, 7,8,9), Duration::seconds(-(3600 + 60 + 1)), Some((2014,5,6, 6,7,8)));
check((2014,5,6, 7,8,9), Duration::seconds(86399), Some((2014,5,7, 7,8,8))); check((2014,5,6, 7,8,9), Duration::seconds(86399), Some((2014,5,7, 7,8,8)));
check((2014,5,6, 7,8,9), Duration::seconds(86400 * 10), Some((2014,5,16, 7,8,9))); check((2014,5,6, 7,8,9), Duration::seconds(86_400 * 10), Some((2014,5,16, 7,8,9)));
check((2014,5,6, 7,8,9), Duration::seconds(-86400 * 10), Some((2014,4,26, 7,8,9))); check((2014,5,6, 7,8,9), Duration::seconds(-86_400 * 10), Some((2014,4,26, 7,8,9)));
check((2014,5,6, 7,8,9), Duration::seconds(86400 * 10), Some((2014,5,16, 7,8,9))); check((2014,5,6, 7,8,9), Duration::seconds(86_400 * 10), Some((2014,5,16, 7,8,9)));
// overflow check // overflow check
// assumes that we have correct values for MAX/MIN_DAYS_FROM_YEAR_0 from `naive::date`. // assumes that we have correct values for MAX/MIN_DAYS_FROM_YEAR_0 from `naive::date`.
@ -1818,7 +1818,7 @@ mod tests {
check((0,1,1, 0,0,0), max_days_from_year_0, Some((MAX_DATE.year(),12,31, 0,0,0))); check((0,1,1, 0,0,0), max_days_from_year_0, Some((MAX_DATE.year(),12,31, 0,0,0)));
check((0,1,1, 0,0,0), max_days_from_year_0 + Duration::seconds(86399), check((0,1,1, 0,0,0), max_days_from_year_0 + Duration::seconds(86399),
Some((MAX_DATE.year(),12,31, 23,59,59))); Some((MAX_DATE.year(),12,31, 23,59,59)));
check((0,1,1, 0,0,0), max_days_from_year_0 + Duration::seconds(86400), None); check((0,1,1, 0,0,0), max_days_from_year_0 + Duration::seconds(86_400), None);
check((0,1,1, 0,0,0), Duration::max_value(), None); check((0,1,1, 0,0,0), Duration::max_value(), None);
let min_days_from_year_0 = MIN_DATE.signed_duration_since(NaiveDate::from_ymd(0,1,1)); let min_days_from_year_0 = MIN_DATE.signed_duration_since(NaiveDate::from_ymd(0,1,1));

View File

@ -93,10 +93,10 @@ static YEAR_DELTAS: [u8; 401] = [
pub fn cycle_to_yo(cycle: u32) -> (u32, u32) { pub fn cycle_to_yo(cycle: u32) -> (u32, u32) {
let (mut year_mod_400, mut ordinal0) = div_rem(cycle, 365); let (mut year_mod_400, mut ordinal0) = div_rem(cycle, 365);
let delta = YEAR_DELTAS[year_mod_400 as usize] as u32; let delta = u32::from(YEAR_DELTAS[year_mod_400 as usize]);
if ordinal0 < delta { if ordinal0 < delta {
year_mod_400 -= 1; year_mod_400 -= 1;
ordinal0 += 365 - YEAR_DELTAS[year_mod_400 as usize] as u32; ordinal0 += 365 - u32::from(YEAR_DELTAS[year_mod_400 as usize]);
} else { } else {
ordinal0 -= delta; ordinal0 -= delta;
} }
@ -104,7 +104,7 @@ pub fn cycle_to_yo(cycle: u32) -> (u32, u32) {
} }
pub fn yo_to_cycle(year_mod_400: u32, ordinal: u32) -> u32 { pub fn yo_to_cycle(year_mod_400: u32, ordinal: u32) -> u32 {
year_mod_400 * 365 + YEAR_DELTAS[year_mod_400 as usize] as u32 + ordinal - 1 year_mod_400 * 365 + u32::from(YEAR_DELTAS[year_mod_400 as usize]) + ordinal - 1
} }
impl YearFlags { impl YearFlags {
@ -122,13 +122,13 @@ impl YearFlags {
#[inline] #[inline]
pub fn ndays(&self) -> u32 { pub fn ndays(&self) -> u32 {
let YearFlags(flags) = *self; let YearFlags(flags) = *self;
366 - (flags >> 3) as u32 366 - u32::from(flags >> 3)
} }
#[inline] #[inline]
pub fn isoweek_delta(&self) -> u32 { pub fn isoweek_delta(&self) -> u32 {
let YearFlags(flags) = *self; let YearFlags(flags) = *self;
let mut delta = flags as u32 & 0b111; let mut delta = u32::from(flags) & 0b0111;
if delta < 3 { delta += 7; } if delta < 3 { delta += 7; }
delta delta
} }
@ -136,7 +136,7 @@ impl YearFlags {
#[inline] #[inline]
pub fn nisoweeks(&self) -> u32 { pub fn nisoweeks(&self) -> u32 {
let YearFlags(flags) = *self; let YearFlags(flags) = *self;
52 + ((0b00000100_00000110 >> flags as usize) & 1) 52 + ((0b0000_0100_0000_0110 >> flags as usize) & 1)
} }
} }
@ -286,14 +286,14 @@ impl Of {
#[inline] #[inline]
pub fn new(ordinal: u32, YearFlags(flags): YearFlags) -> Of { pub fn new(ordinal: u32, YearFlags(flags): YearFlags) -> Of {
let ordinal = Of::clamp_ordinal(ordinal); let ordinal = Of::clamp_ordinal(ordinal);
Of((ordinal << 4) | (flags as u32)) Of((ordinal << 4) | u32::from(flags))
} }
#[inline] #[inline]
pub fn from_mdf(Mdf(mdf): Mdf) -> Of { pub fn from_mdf(Mdf(mdf): Mdf) -> Of {
let mdl = mdf >> 3; let mdl = mdf >> 3;
match MDL_TO_OL.get(mdl as usize) { match MDL_TO_OL.get(mdl as usize) {
Some(&v) => Of(mdf.wrapping_sub((v as i32 as u32 & 0x3ff) << 3)), Some(&v) => Of(mdf.wrapping_sub((i32::from(v) as u32 & 0x3ff) << 3)),
None => Of(0) None => Of(0)
} }
} }
@ -327,7 +327,7 @@ impl Of {
#[inline] #[inline]
pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Of { pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Of {
let Of(of) = *self; let Of(of) = *self;
Of((of & !0b1111) | (flags as u32)) Of((of & !0b1111) | u32::from(flags))
} }
#[inline] #[inline]
@ -393,14 +393,14 @@ impl Mdf {
pub fn new(month: u32, day: u32, YearFlags(flags): YearFlags) -> Mdf { pub fn new(month: u32, day: u32, YearFlags(flags): YearFlags) -> Mdf {
let month = Mdf::clamp_month(month); let month = Mdf::clamp_month(month);
let day = Mdf::clamp_day(day); let day = Mdf::clamp_day(day);
Mdf((month << 9) | (day << 4) | (flags as u32)) Mdf((month << 9) | (day << 4) | u32::from(flags))
} }
#[inline] #[inline]
pub fn from_of(Of(of): Of) -> Mdf { pub fn from_of(Of(of): Of) -> Mdf {
let ol = of >> 3; let ol = of >> 3;
match OL_TO_MDL.get(ol as usize) { match OL_TO_MDL.get(ol as usize) {
Some(&v) => Mdf(of + ((v as u32) << 3)), Some(&v) => Mdf(of + (u32::from(v) << 3)),
None => Mdf(0) None => Mdf(0)
} }
} }
@ -425,20 +425,20 @@ impl Mdf {
pub fn with_month(&self, month: u32) -> Mdf { pub fn with_month(&self, month: u32) -> Mdf {
let month = Mdf::clamp_month(month); let month = Mdf::clamp_month(month);
let Mdf(mdf) = *self; let Mdf(mdf) = *self;
Mdf((mdf & 0b11111_1111) | (month << 9)) Mdf((mdf & 0b1_1111_1111) | (month << 9))
} }
#[inline] #[inline]
pub fn day(&self) -> u32 { pub fn day(&self) -> u32 {
let Mdf(mdf) = *self; let Mdf(mdf) = *self;
(mdf >> 4) & 0b11111 (mdf >> 4) & 0b1_1111
} }
#[inline] #[inline]
pub fn with_day(&self, day: u32) -> Mdf { pub fn with_day(&self, day: u32) -> Mdf {
let day = Mdf::clamp_day(day); let day = Mdf::clamp_day(day);
let Mdf(mdf) = *self; let Mdf(mdf) = *self;
Mdf((mdf & !0b11111_0000) | (day << 4)) Mdf((mdf & !0b1_1111_0000) | (day << 4))
} }
#[inline] #[inline]
@ -450,7 +450,7 @@ impl Mdf {
#[inline] #[inline]
pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Mdf { pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Mdf {
let Mdf(mdf) = *self; let Mdf(mdf) = *self;
Mdf((mdf & !0b1111) | (flags as u32)) Mdf((mdf & !0b1111) | u32::from(flags))
} }
#[inline] #[inline]
@ -463,7 +463,7 @@ impl fmt::Debug for Mdf {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Mdf(mdf) = *self; let Mdf(mdf) = *self;
write!(f, "Mdf(({} << 9) | ({} << 4) | {:#04o} /*{:?}*/)", write!(f, "Mdf(({} << 9) | ({} << 4) | {:#04o} /*{:?}*/)",
mdf >> 9, (mdf >> 4) & 0b11111, mdf & 0b1111, YearFlags((mdf & 0b1111) as u8)) mdf >> 9, (mdf >> 4) & 0b1_1111, mdf & 0b1111, YearFlags((mdf & 0b1111) as u8))
} }
} }

View File

@ -40,7 +40,7 @@ pub fn iso_week_from_yof(year: i32, of: Of) -> IsoWeek {
(year, rawweek) (year, rawweek)
} }
}; };
IsoWeek { ywf: (year << 10) | (week << 4) as DateImpl | of.flags().0 as DateImpl } IsoWeek { ywf: (year << 10) | (week << 4) as DateImpl | DateImpl::from(of.flags().0) }
} }
impl IsoWeek { impl IsoWeek {

View File

@ -420,12 +420,12 @@ impl NaiveTime {
/// assert!(from_nsecs_opt(0, 0).is_some()); /// assert!(from_nsecs_opt(0, 0).is_some());
/// assert!(from_nsecs_opt(86399, 999_999_999).is_some()); /// assert!(from_nsecs_opt(86399, 999_999_999).is_some());
/// assert!(from_nsecs_opt(86399, 1_999_999_999).is_some()); // a leap second after 23:59:59 /// assert!(from_nsecs_opt(86399, 1_999_999_999).is_some()); // a leap second after 23:59:59
/// assert!(from_nsecs_opt(86400, 0).is_none()); /// assert!(from_nsecs_opt(86_400, 0).is_none());
/// assert!(from_nsecs_opt(86399, 2_000_000_000).is_none()); /// assert!(from_nsecs_opt(86399, 2_000_000_000).is_none());
/// ~~~~ /// ~~~~
#[inline] #[inline]
pub fn from_num_seconds_from_midnight_opt(secs: u32, nano: u32) -> Option<NaiveTime> { pub fn from_num_seconds_from_midnight_opt(secs: u32, nano: u32) -> Option<NaiveTime> {
if secs >= 86400 || nano >= 2_000_000_000 { return None; } if secs >= 86_400 || nano >= 2_000_000_000 { return None; }
Some(NaiveTime { secs: secs, frac: nano }) Some(NaiveTime { secs: secs, frac: nano })
} }
@ -513,9 +513,9 @@ impl NaiveTime {
/// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(11)), /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(11)),
/// (from_hms(14, 4, 5), 0)); /// (from_hms(14, 4, 5), 0));
/// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(23)), /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(23)),
/// (from_hms(2, 4, 5), 86400)); /// (from_hms(2, 4, 5), 86_400));
/// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(-7)), /// assert_eq!(from_hms(3, 4, 5).overflowing_add_signed(Duration::hours(-7)),
/// (from_hms(20, 4, 5), -86400)); /// (from_hms(20, 4, 5), -86_400));
/// # } /// # }
/// ~~~~ /// ~~~~
#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
@ -528,36 +528,36 @@ impl NaiveTime {
// otherwise the addition immediately finishes. // otherwise the addition immediately finishes.
if frac >= 1_000_000_000 { if frac >= 1_000_000_000 {
let rfrac = 2_000_000_000 - frac; let rfrac = 2_000_000_000 - frac;
if rhs >= OldDuration::nanoseconds(rfrac as i64) { if rhs >= OldDuration::nanoseconds(i64::from(rfrac)) {
rhs = rhs - OldDuration::nanoseconds(rfrac as i64); rhs = rhs - OldDuration::nanoseconds(i64::from(rfrac));
secs += 1; secs += 1;
frac = 0; frac = 0;
} else if rhs < OldDuration::nanoseconds(-(frac as i64)) { } else if rhs < OldDuration::nanoseconds(-i64::from(frac)) {
rhs = rhs + OldDuration::nanoseconds(frac as i64); rhs = rhs + OldDuration::nanoseconds(i64::from(frac));
frac = 0; frac = 0;
} else { } else {
frac = (frac as i64 + rhs.num_nanoseconds().unwrap()) as u32; frac = (i64::from(frac) + rhs.num_nanoseconds().unwrap()) as u32;
debug_assert!(frac < 2_000_000_000); debug_assert!(frac < 2_000_000_000);
return (NaiveTime { secs: secs, frac: frac }, 0); return (NaiveTime { secs: secs, frac: frac }, 0);
} }
} }
debug_assert!(secs <= 86400); debug_assert!(secs <= 86_400);
debug_assert!(frac < 1_000_000_000); debug_assert!(frac < 1_000_000_000);
let rhssecs = rhs.num_seconds(); let rhssecs = rhs.num_seconds();
let rhsfrac = (rhs - OldDuration::seconds(rhssecs)).num_nanoseconds().unwrap(); let rhsfrac = (rhs - OldDuration::seconds(rhssecs)).num_nanoseconds().unwrap();
debug_assert_eq!(OldDuration::seconds(rhssecs) + OldDuration::nanoseconds(rhsfrac), rhs); debug_assert_eq!(OldDuration::seconds(rhssecs) + OldDuration::nanoseconds(rhsfrac), rhs);
let rhssecsinday = rhssecs % 86400; let rhssecsinday = rhssecs % 86_400;
let mut morerhssecs = rhssecs - rhssecsinday; let mut morerhssecs = rhssecs - rhssecsinday;
let rhssecs = rhssecsinday as i32; let rhssecs = rhssecsinday as i32;
let rhsfrac = rhsfrac as i32; let rhsfrac = rhsfrac as i32;
debug_assert!(-86400 < rhssecs && rhssecs < 86400); debug_assert!(-86_400 < rhssecs && rhssecs < 86_400);
debug_assert_eq!(morerhssecs % 86400, 0); debug_assert_eq!(morerhssecs % 86_400, 0);
debug_assert!(-1_000_000_000 < rhsfrac && rhsfrac < 1_000_000_000); debug_assert!(-1_000_000_000 < rhsfrac && rhsfrac < 1_000_000_000);
let mut secs = secs as i32 + rhssecs; let mut secs = secs as i32 + rhssecs;
let mut frac = frac as i32 + rhsfrac; let mut frac = frac as i32 + rhsfrac;
debug_assert!(-86400 < secs && secs < 2 * 86400); debug_assert!(-86_400 < secs && secs < 2 * 86_400);
debug_assert!(-1_000_000_000 < frac && frac < 2_000_000_000); debug_assert!(-1_000_000_000 < frac && frac < 2_000_000_000);
if frac < 0 { if frac < 0 {
@ -567,17 +567,17 @@ impl NaiveTime {
frac -= 1_000_000_000; frac -= 1_000_000_000;
secs += 1; secs += 1;
} }
debug_assert!(-86400 <= secs && secs < 2 * 86400); debug_assert!(-86_400 <= secs && secs < 2 * 86_400);
debug_assert!(0 <= frac && frac < 1_000_000_000); debug_assert!(0 <= frac && frac < 1_000_000_000);
if secs < 0 { if secs < 0 {
secs += 86400; secs += 86_400;
morerhssecs -= 86400; morerhssecs -= 86_400;
} else if secs >= 86400 { } else if secs >= 86_400 {
secs -= 86400; secs -= 86_400;
morerhssecs += 86400; morerhssecs += 86_400;
} }
debug_assert!(0 <= secs && secs < 86400); debug_assert!(0 <= secs && secs < 86_400);
(NaiveTime { secs: secs as u32, frac: frac as u32 }, morerhssecs) (NaiveTime { secs: secs as u32, frac: frac as u32 }, morerhssecs)
} }
@ -599,9 +599,9 @@ impl NaiveTime {
/// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(2)), /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(2)),
/// (from_hms(1, 4, 5), 0)); /// (from_hms(1, 4, 5), 0));
/// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(17)), /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(17)),
/// (from_hms(10, 4, 5), 86400)); /// (from_hms(10, 4, 5), 86_400));
/// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(-22)), /// assert_eq!(from_hms(3, 4, 5).overflowing_sub_signed(Duration::hours(-22)),
/// (from_hms(1, 4, 5), -86400)); /// (from_hms(1, 4, 5), -86_400));
/// # } /// # }
/// ~~~~ /// ~~~~
#[inline] #[inline]
@ -683,8 +683,8 @@ impl NaiveTime {
use std::cmp::Ordering; use std::cmp::Ordering;
let secs = self.secs as i64 - rhs.secs as i64; let secs = i64::from(self.secs) - i64::from(rhs.secs);
let frac = self.frac as i64 - rhs.frac as i64; let frac = i64::from(self.frac) - i64::from(rhs.frac);
// `secs` may contain a leap second yet to be counted // `secs` may contain a leap second yet to be counted
let adjust = match self.secs.cmp(&rhs.secs) { let adjust = match self.secs.cmp(&rhs.secs) {
@ -1516,15 +1516,15 @@ mod tests {
assert_eq!(hmsm(3, 4, 5, 678).overflowing_add_signed(Duration::hours(11)), assert_eq!(hmsm(3, 4, 5, 678).overflowing_add_signed(Duration::hours(11)),
(hmsm(14, 4, 5, 678), 0)); (hmsm(14, 4, 5, 678), 0));
assert_eq!(hmsm(3, 4, 5, 678).overflowing_add_signed(Duration::hours(23)), assert_eq!(hmsm(3, 4, 5, 678).overflowing_add_signed(Duration::hours(23)),
(hmsm(2, 4, 5, 678), 86400)); (hmsm(2, 4, 5, 678), 86_400));
assert_eq!(hmsm(3, 4, 5, 678).overflowing_add_signed(Duration::hours(-7)), assert_eq!(hmsm(3, 4, 5, 678).overflowing_add_signed(Duration::hours(-7)),
(hmsm(20, 4, 5, 678), -86400)); (hmsm(20, 4, 5, 678), -86_400));
// overflowing_add_signed with leap seconds may be counter-intuitive // overflowing_add_signed with leap seconds may be counter-intuitive
assert_eq!(hmsm(3, 4, 5, 1_678).overflowing_add_signed(Duration::days(1)), assert_eq!(hmsm(3, 4, 5, 1_678).overflowing_add_signed(Duration::days(1)),
(hmsm(3, 4, 5, 678), 86400)); (hmsm(3, 4, 5, 678), 86_400));
assert_eq!(hmsm(3, 4, 5, 1_678).overflowing_add_signed(Duration::days(-1)), assert_eq!(hmsm(3, 4, 5, 1_678).overflowing_add_signed(Duration::days(-1)),
(hmsm(3, 4, 6, 678), -86400)); (hmsm(3, 4, 6, 678), -86_400));
} }
#[test] #[test]

View File

@ -48,7 +48,7 @@ impl FixedOffset {
/// ///
/// Returns `None` on the out-of-bound `secs`. /// Returns `None` on the out-of-bound `secs`.
pub fn east_opt(secs: i32) -> Option<FixedOffset> { pub fn east_opt(secs: i32) -> Option<FixedOffset> {
if -86400 < secs && secs < 86400 { if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: secs }) Some(FixedOffset { local_minus_utc: secs })
} else { } else {
None None
@ -78,7 +78,7 @@ impl FixedOffset {
/// ///
/// Returns `None` on the out-of-bound `secs`. /// Returns `None` on the out-of-bound `secs`.
pub fn west_opt(secs: i32) -> Option<FixedOffset> { pub fn west_opt(secs: i32) -> Option<FixedOffset> {
if -86400 < secs && secs < 86400 { if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: -secs }) Some(FixedOffset { local_minus_utc: -secs })
} else { } else {
None None
@ -145,7 +145,7 @@ fn add_with_leapsecond<T>(lhs: &T, rhs: i32) -> T
// extract and temporarily remove the fractional part and later recover it // extract and temporarily remove the fractional part and later recover it
let nanos = lhs.nanosecond(); let nanos = lhs.nanosecond();
let lhs = lhs.with_nanosecond(0).unwrap(); let lhs = lhs.with_nanosecond(0).unwrap();
(lhs + OldDuration::seconds(rhs as i64)).with_nanosecond(nanos).unwrap() (lhs + OldDuration::seconds(i64::from(rhs))).with_nanosecond(nanos).unwrap()
} }
impl Add<FixedOffset> for NaiveTime { impl Add<FixedOffset> for NaiveTime {