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;
let (width, v) = match spec {
Year => (4, date.map(|d| d.year() as i64)),
YearDiv100 => (2, date.map(|d| div_floor(d.year() as i64, 100))),
YearMod100 => (2, date.map(|d| mod_floor(d.year() as i64, 100))),
IsoYear => (4, date.map(|d| d.iso_week().year() as i64)),
IsoYearDiv100 => (2, date.map(|d| div_floor(d.iso_week().year() as i64, 100))),
IsoYearMod100 => (2, date.map(|d| mod_floor(d.iso_week().year() as i64, 100))),
Month => (2, date.map(|d| d.month() as i64)),
Day => (2, date.map(|d| d.day() as i64)),
WeekFromSun => (2, date.map(|d| week_from_sun(d) as i64)),
WeekFromMon => (2, date.map(|d| week_from_mon(d) as i64)),
IsoWeek => (2, date.map(|d| d.iso_week().week() as i64)),
NumDaysFromSun => (1, date.map(|d| d.weekday().num_days_from_sunday() as i64)),
WeekdayFromMon => (1, date.map(|d| d.weekday().number_from_monday() as i64)),
Ordinal => (3, date.map(|d| d.ordinal() as i64)),
Hour => (2, time.map(|t| t.hour() as i64)),
Hour12 => (2, time.map(|t| t.hour12().1 as i64)),
Minute => (2, time.map(|t| t.minute() as i64)),
Second => (2, time.map(|t| (t.second() +
t.nanosecond() / 1_000_000_000) as i64)),
Nanosecond => (9, time.map(|t| (t.nanosecond() % 1_000_000_000) as i64)),
Year => (4, date.map(|d| i64::from(d.year()))),
YearDiv100 => (2, date.map(|d| div_floor(i64::from(d.year()), 100))),
YearMod100 => (2, date.map(|d| mod_floor(i64::from(d.year()), 100))),
IsoYear => (4, date.map(|d| i64::from(d.iso_week().year()))),
IsoYearDiv100 => (2, date.map(|d| div_floor(
i64::from(d.iso_week().year()), 100))),
IsoYearMod100 => (2, date.map(|d| mod_floor(
i64::from(d.iso_week().year()), 100))),
Month => (2, date.map(|d| i64::from(d.month()))),
Day => (2, date.map(|d| i64::from(d.day()))),
WeekFromSun => (2, date.map(|d| i64::from(week_from_sun(d)))),
WeekFromMon => (2, date.map(|d| i64::from(week_from_mon(d)))),
IsoWeek => (2, date.map(|d| i64::from(d.iso_week().week()))),
NumDaysFromSun => (1, date.map(|d| i64::from(d.weekday()
.num_days_from_sunday()))),
WeekdayFromMon => (1, date.map(|d| i64::from(d.weekday()
.number_from_monday()))),
Ordinal => (3, date.map(|d| i64::from(d.ordinal()))),
Hour => (2, time.map(|t| i64::from(t.hour()))),
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) {
(Some(d), Some(t), None) =>
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 (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
match pad {
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();
try!(parsed.set_day(try_consume!(scan::number(s, 1, 2))));
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
// 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
if let Some(offset) = try_consume!(scan::timezone_offset_2822(s)) {
// 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, ()))
@ -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':')));
if offset <= -86400 || offset >= 86400 { return Err(OUT_OF_RANGE); }
try!(parsed.set_offset(offset as i64));
if offset <= -86_400 || offset >= 86_400 { return Err(OUT_OF_RANGE); }
try!(parsed.set_offset(i64::from(offset)));
Ok((s, ()))
}
@ -279,12 +279,12 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<(
match spec {
ShortMonthName => {
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 => {
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 => {
@ -320,13 +320,13 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<(
TimezoneOffsetColon | TimezoneOffset => {
let offset = try_consume!(scan::timezone_offset(s.trim_left(),
scan::colon_or_space));
try!(parsed.set_offset(offset as i64));
try!(parsed.set_offset(i64::from(offset)));
}
TimezoneOffsetColonZ | TimezoneOffsetZ => {
let offset = try_consume!(scan::timezone_offset_zulu(s.trim_left(),
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)),

View File

@ -394,7 +394,7 @@ impl Parsed {
if week_from_sun > 53 { return Err(OUT_OF_RANGE); } // can it overflow?
let ndays = firstweek + (week_from_sun as i32 - 1) * 7 +
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));
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?
let ndays = firstweek + (week_from_mon as i32 - 1) * 7 +
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));
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
// 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 `datetime` represents a leap second, it might be off by one second.
if given_timestamp != timestamp &&
@ -525,7 +525,7 @@ impl Parsed {
}
// 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 mut datetime = try!(datetime.ok_or(OUT_OF_RANGE));
@ -544,12 +544,12 @@ impl Parsed {
}
// ...and we have the correct candidates for other fields.
} 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_ordinal(datetime.ordinal() as i64)); // more efficient than ymd
try!(parsed.set_hour (datetime.hour() as i64));
try!(parsed.set_minute (datetime.minute() as i64));
try!(parsed.set_year (i64::from(datetime.year())));
try!(parsed.set_ordinal(i64::from(datetime.ordinal()))); // more efficient than ymd
try!(parsed.set_hour (i64::from(datetime.hour())));
try!(parsed.set_minute (i64::from(datetime.minute())));
// validate other fields (e.g. week) and return
let date = try!(parsed.to_naive_date());
@ -1041,7 +1041,7 @@ mod tests {
minute: 42, second: 4, nanosecond: 12_345_678, offset: -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,
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
}

View File

@ -191,7 +191,7 @@ pub fn timezone_offset<F>(mut s: &str, mut colon: F) -> ParseResult<(&str, i32)>
// hours (00--99)
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),
};
s = &s[2..];
@ -201,7 +201,7 @@ pub fn timezone_offset<F>(mut s: &str, mut colon: F) -> ParseResult<(&str, i32)>
// minutes (00--59)
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),
_ => return Err(INVALID),
};

View File

@ -857,7 +857,7 @@ pub trait Datelike: Sized {
if year < 0 {
let excess = 1 + (-year) / 400;
year += excess * 400;
ndays -= excess * 146097;
ndays -= excess * 146_097;
}
let div_100 = year / 100;
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> {
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 flags = YearFlags::from_year_mod_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.
#[inline]
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.
@ -701,7 +701,7 @@ impl NaiveDate {
fn with_of(&self, of: Of) -> Option<NaiveDate> {
if of.valid() {
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 {
None
}
@ -808,7 +808,7 @@ impl NaiveDate {
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 = 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;
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 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_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;
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
@ -883,9 +883,9 @@ impl NaiveDate {
let year2 = rhs.year();
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 cycle1 = internals::yo_to_cycle(year1_mod_400 as u32, self.of().ordinal()) as i64;
let cycle2 = internals::yo_to_cycle(year2_mod_400 as u32, rhs.of().ordinal()) as i64;
OldDuration::days((year1_div_400 as i64 - year2_div_400 as i64) * 146097 +
let cycle1 = i64::from(internals::yo_to_cycle(year1_mod_400 as u32, self.of().ordinal()));
let cycle2 = i64::from(internals::yo_to_cycle(year2_mod_400 as u32, rhs.of().ordinal()));
OldDuration::days((i64::from(year1_div_400) - i64::from(year2_div_400)) * 146_097 +
(cycle1 - cycle2))
}

View File

@ -133,8 +133,8 @@ impl NaiveDateTime {
/// ~~~~
#[inline]
pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
let (days, secs) = div_mod_floor(secs, 86400);
let date = days.to_i32().and_then(|days| days.checked_add(719163))
let (days, secs) = div_mod_floor(secs, 86_400);
let date = days.to_i32().and_then(|days| days.checked_add(719_163))
.and_then(NaiveDate::from_num_days_from_ce_opt);
let time = NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs);
match (date, time) {
@ -258,9 +258,9 @@ impl NaiveDateTime {
/// ~~~~
#[inline]
pub fn timestamp(&self) -> i64 {
let ndays = self.date.num_days_from_ce() as i64;
let nseconds = self.time.num_seconds_from_midnight() as i64;
(ndays - 719163) * 86400 + nseconds
let ndays = i64::from(self.date.num_days_from_ce());
let nseconds = i64::from(self.time.num_seconds_from_midnight());
(ndays - 719_163) * 86_400 + nseconds
}
/// Returns the number of non-leap *milliseconds* since midnight on January 1, 1970.
@ -287,7 +287,7 @@ impl NaiveDateTime {
#[inline]
pub fn timestamp_millis(&self) -> i64 {
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.
@ -381,7 +381,7 @@ impl NaiveDateTime {
/// Some(hms(3, 5, 6)));
/// assert_eq!(hms(3, 5, 7).checked_add_signed(Duration::seconds(3600 + 60)),
/// 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)));
///
/// 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)));
/// assert_eq!(hms(3, 5, 7).checked_sub_signed(Duration::seconds(3600 + 60)),
/// 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)));
///
/// 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
/// 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)),
/// 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, 6));
/// 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));
/// assert_eq!(hms(3, 5, 7) + Duration::days(365),
/// 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, 8));
/// 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));
/// assert_eq!(hms(3, 5, 7) - Duration::days(365),
/// 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, 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(86400 * 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(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(-86_400 * 10), Some((2014,4,26, 7,8,9)));
check((2014,5,6, 7,8,9), Duration::seconds(86_400 * 10), Some((2014,5,16, 7,8,9)));
// overflow check
// 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 + Duration::seconds(86399),
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);
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) {
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 {
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 {
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 {
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 {
@ -122,13 +122,13 @@ impl YearFlags {
#[inline]
pub fn ndays(&self) -> u32 {
let YearFlags(flags) = *self;
366 - (flags >> 3) as u32
366 - u32::from(flags >> 3)
}
#[inline]
pub fn isoweek_delta(&self) -> u32 {
let YearFlags(flags) = *self;
let mut delta = flags as u32 & 0b111;
let mut delta = u32::from(flags) & 0b0111;
if delta < 3 { delta += 7; }
delta
}
@ -136,7 +136,7 @@ impl YearFlags {
#[inline]
pub fn nisoweeks(&self) -> u32 {
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]
pub fn new(ordinal: u32, YearFlags(flags): YearFlags) -> Of {
let ordinal = Of::clamp_ordinal(ordinal);
Of((ordinal << 4) | (flags as u32))
Of((ordinal << 4) | u32::from(flags))
}
#[inline]
pub fn from_mdf(Mdf(mdf): Mdf) -> Of {
let mdl = mdf >> 3;
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)
}
}
@ -327,7 +327,7 @@ impl Of {
#[inline]
pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Of {
let Of(of) = *self;
Of((of & !0b1111) | (flags as u32))
Of((of & !0b1111) | u32::from(flags))
}
#[inline]
@ -393,14 +393,14 @@ impl Mdf {
pub fn new(month: u32, day: u32, YearFlags(flags): YearFlags) -> Mdf {
let month = Mdf::clamp_month(month);
let day = Mdf::clamp_day(day);
Mdf((month << 9) | (day << 4) | (flags as u32))
Mdf((month << 9) | (day << 4) | u32::from(flags))
}
#[inline]
pub fn from_of(Of(of): Of) -> Mdf {
let ol = of >> 3;
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)
}
}
@ -425,20 +425,20 @@ impl Mdf {
pub fn with_month(&self, month: u32) -> Mdf {
let month = Mdf::clamp_month(month);
let Mdf(mdf) = *self;
Mdf((mdf & 0b11111_1111) | (month << 9))
Mdf((mdf & 0b1_1111_1111) | (month << 9))
}
#[inline]
pub fn day(&self) -> u32 {
let Mdf(mdf) = *self;
(mdf >> 4) & 0b11111
(mdf >> 4) & 0b1_1111
}
#[inline]
pub fn with_day(&self, day: u32) -> Mdf {
let day = Mdf::clamp_day(day);
let Mdf(mdf) = *self;
Mdf((mdf & !0b11111_0000) | (day << 4))
Mdf((mdf & !0b1_1111_0000) | (day << 4))
}
#[inline]
@ -450,7 +450,7 @@ impl Mdf {
#[inline]
pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Mdf {
let Mdf(mdf) = *self;
Mdf((mdf & !0b1111) | (flags as u32))
Mdf((mdf & !0b1111) | u32::from(flags))
}
#[inline]
@ -463,7 +463,7 @@ impl fmt::Debug for Mdf {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Mdf(mdf) = *self;
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)
}
};
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 {

View File

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

View File

@ -48,7 +48,7 @@ impl FixedOffset {
///
/// Returns `None` on the out-of-bound `secs`.
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 })
} else {
None
@ -78,7 +78,7 @@ impl FixedOffset {
///
/// Returns `None` on the out-of-bound `secs`.
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 })
} else {
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
let nanos = lhs.nanosecond();
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 {