Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro committed Jan 17, 2020
1 parent 897b7f6 commit 2ec4fba
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 75 deletions.
6 changes: 3 additions & 3 deletions benches/datetime_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use humantime::format_rfc3339;

#[bench]
fn rfc3339_humantime_seconds(b: &mut test::Bencher) {
let time = UNIX_EPOCH + Duration::new(1483228799, 0);
let time = UNIX_EPOCH + Duration::new(1_483_228_799, 0);
let mut buf = Vec::with_capacity(100);
b.iter(|| {
buf.truncate(0);
Expand All @@ -26,11 +26,11 @@ fn rfc3339_chrono(b: &mut test::Bencher) {
use chrono::format::Pad::*;

let time = DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(1483228799, 0), Utc);
NaiveDateTime::from_timestamp(1_483_228_799, 0), Utc);
let mut buf = Vec::with_capacity(100);

// formatting code from env_logger
const ITEMS: &'static [Item<'static>] = {
const ITEMS: &[Item<'static>] = {
&[
Numeric(Year, Zero),
Literal("-"),
Expand Down
74 changes: 36 additions & 38 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ mod max {
all(target_arch="wasm32", not(target_os="emscripten")),
))]
mod max {
pub const SECONDS: u64 = 253402300800-1; // last second of year 9999
pub const SECONDS: u64 = 253_402_300_800-1; // last second of year 9999
#[allow(unused)]
pub const TIMESTAMP: &'static str = "9999-12-31T23:59:59Z";
pub const TIMESTAMP: &str = "9999-12-31T23:59:59Z";
}

/// Error parsing datetime (timestamp)
Expand Down Expand Up @@ -90,7 +90,7 @@ pub fn parse_rfc3339(s: &str) -> Result<SystemTime, Error> {
if b[10] != b'T' || b[b.len()-1] != b'Z' {
return Err(Error::InvalidFormat);
}
return parse_rfc3339_weak(s);
parse_rfc3339_weak(s)
}

/// Parse RFC3339-like timestamp `2018-02-14 00:28:07`
Expand Down Expand Up @@ -177,22 +177,20 @@ pub fn parse_rfc3339_weak(s: &str) -> Result<SystemTime, Error> {
nanos += mult * (b[idx] - b'0') as u32;
mult /= 10;
}
} else {
if b.len() != 19 && (b.len() > 20 || b[19] != b'Z') {
return Err(Error::InvalidFormat);
}
} else if b.len() != 19 && (b.len() > 20 || b[19] != b'Z') {
return Err(Error::InvalidFormat);
}

let total_seconds = time + days * 86400;
if total_seconds > max::SECONDS {
return Err(Error::OutOfRange);
}

return Ok(UNIX_EPOCH + Duration::new(total_seconds, nanos));
Ok(UNIX_EPOCH + Duration::new(total_seconds, nanos))
}

fn is_leap_year(y: u64) -> bool {
y % 4 == 0 && (!(y % 100 == 0) || y % 400 == 0)
y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)
}

/// Format an RFC3339 timestamp `2018-02-14T00:28:07Z`
Expand All @@ -203,7 +201,7 @@ fn is_leap_year(y: u64) -> bool {
///
/// The value is always UTC and ignores system timezone.
pub fn format_rfc3339(system_time: SystemTime) -> Rfc3339Timestamp {
return Rfc3339Timestamp(system_time, Precision::Smart);
Rfc3339Timestamp(system_time, Precision::Smart)
}

/// Format an RFC3339 timestamp `2018-02-14T00:28:07Z`
Expand All @@ -212,7 +210,7 @@ pub fn format_rfc3339(system_time: SystemTime) -> Rfc3339Timestamp {
///
/// The value is always UTC and ignores system timezone.
pub fn format_rfc3339_seconds(system_time: SystemTime) -> Rfc3339Timestamp {
return Rfc3339Timestamp(system_time, Precision::Seconds);
Rfc3339Timestamp(system_time, Precision::Seconds)
}

/// Format an RFC3339 timestamp `2018-02-14T00:28:07.000Z`
Expand All @@ -221,7 +219,7 @@ pub fn format_rfc3339_seconds(system_time: SystemTime) -> Rfc3339Timestamp {
///
/// The value is always UTC and ignores system timezone.
pub fn format_rfc3339_millis(system_time: SystemTime) -> Rfc3339Timestamp {
return Rfc3339Timestamp(system_time, Precision::Millis);
Rfc3339Timestamp(system_time, Precision::Millis)
}

/// Format an RFC3339 timestamp `2018-02-14T00:28:07.000000Z`
Expand All @@ -230,7 +228,7 @@ pub fn format_rfc3339_millis(system_time: SystemTime) -> Rfc3339Timestamp {
///
/// The value is always UTC and ignores system timezone.
pub fn format_rfc3339_micros(system_time: SystemTime) -> Rfc3339Timestamp {
return Rfc3339Timestamp(system_time, Precision::Micros);
Rfc3339Timestamp(system_time, Precision::Micros)
}

/// Format an RFC3339 timestamp `2018-02-14T00:28:07.000000000Z`
Expand All @@ -239,7 +237,7 @@ pub fn format_rfc3339_micros(system_time: SystemTime) -> Rfc3339Timestamp {
///
/// The value is always UTC and ignores system timezone.
pub fn format_rfc3339_nanos(system_time: SystemTime) -> Rfc3339Timestamp {
return Rfc3339Timestamp(system_time, Precision::Nanos);
Rfc3339Timestamp(system_time, Precision::Nanos)
}

impl fmt::Display for Rfc3339Timestamp {
Expand All @@ -251,7 +249,7 @@ impl fmt::Display for Rfc3339Timestamp {
let secs_since_epoch = dur.as_secs();
let nanos = dur.subsec_nanos();

if secs_since_epoch >= 253402300800 { // year 9999
if secs_since_epoch >= 253_402_300_800 { // year 9999
return Err(fmt::Error);
}

Expand Down Expand Up @@ -352,13 +350,13 @@ impl fmt::Display for Rfc3339Timestamp {
buf[25] = b'0' + (nanos / 1_000 % 10) as u8;
buf[26] = b'0' + (nanos / 100 % 10) as u8;
buf[27] = b'0' + (nanos / 10 % 10) as u8;
buf[28] = b'0' + (nanos / 1 % 10) as u8;
buf[28] = b'0' + (nanos % 10) as u8;
// 29th is 'Z'
29
};

// we know our chars are all ascii
f.write_str(unsafe { str::from_utf8_unchecked(&buf[..offset+1]) })
f.write_str(unsafe { str::from_utf8_unchecked(&buf[..=offset]) })
}
}

Expand All @@ -378,7 +376,7 @@ mod test {
let s = time::at_utc(time::Timespec { sec: sec as i64, nsec: 0 })
.rfc3339().to_string();
let time = UNIX_EPOCH + Duration::new(sec, 0);
return (s, time)
(s, time)
}

#[test]
Expand All @@ -398,9 +396,9 @@ mod test {
assert_eq!(parse_rfc3339("1970-01-01T00:00:01Z").unwrap(),
UNIX_EPOCH + Duration::new(1, 0));
assert_eq!(parse_rfc3339("2018-02-13T23:08:32Z").unwrap(),
UNIX_EPOCH + Duration::new(1518563312, 0));
UNIX_EPOCH + Duration::new(1_518_563_312, 0));
assert_eq!(parse_rfc3339("2012-01-01T00:00:00Z").unwrap(),
UNIX_EPOCH + Duration::new(1325376000, 0));
UNIX_EPOCH + Duration::new(1_325_376_000, 0));
}

#[test]
Expand All @@ -412,10 +410,10 @@ mod test {
format_rfc3339(UNIX_EPOCH + Duration::new(1, 0)).to_string(),
"1970-01-01T00:00:01Z");
assert_eq!(
format_rfc3339(UNIX_EPOCH + Duration::new(1518563312, 0)).to_string(),
format_rfc3339(UNIX_EPOCH + Duration::new(1_518_563_312, 0)).to_string(),
"2018-02-13T23:08:32Z");
assert_eq!(
format_rfc3339(UNIX_EPOCH + Duration::new(1325376000, 0)).to_string(),
format_rfc3339(UNIX_EPOCH + Duration::new(1_325_376_000, 0)).to_string(),
"2012-01-01T00:00:00Z");
}

Expand All @@ -427,7 +425,7 @@ mod test {
"1970-01-01T00:00:00.000Z");
assert_eq!(
format_rfc3339_millis(UNIX_EPOCH +
Duration::new(1518563312, 123_000_000)).to_string(),
Duration::new(1_518_563_312, 123_000_000)).to_string(),
"2018-02-13T23:08:32.123Z");
}

Expand All @@ -439,11 +437,11 @@ mod test {
"1970-01-01T00:00:00.000000Z");
assert_eq!(
format_rfc3339_micros(UNIX_EPOCH +
Duration::new(1518563312, 123_000_000)).to_string(),
Duration::new(1_518_563_312, 123_000_000)).to_string(),
"2018-02-13T23:08:32.123000Z");
assert_eq!(
format_rfc3339_micros(UNIX_EPOCH +
Duration::new(1518563312, 456_123_000)).to_string(),
Duration::new(1_518_563_312, 456_123_000)).to_string(),
"2018-02-13T23:08:32.456123Z");
}

Expand All @@ -455,11 +453,11 @@ mod test {
"1970-01-01T00:00:00.000000000Z");
assert_eq!(
format_rfc3339_nanos(UNIX_EPOCH +
Duration::new(1518563312, 123_000_000)).to_string(),
Duration::new(1_518_563_312, 123_000_000)).to_string(),
"2018-02-13T23:08:32.123000000Z");
assert_eq!(
format_rfc3339_nanos(UNIX_EPOCH +
Duration::new(1518563312, 789_456_123)).to_string(),
Duration::new(1_518_563_312, 789_456_123)).to_string(),
"2018-02-13T23:08:32.789456123Z");
}

Expand All @@ -473,13 +471,13 @@ mod test {
#[test]
fn leap_second() {
assert_eq!(parse_rfc3339("2016-12-31T23:59:60Z").unwrap(),
UNIX_EPOCH + Duration::new(1483228799, 0));
UNIX_EPOCH + Duration::new(1_483_228_799, 0));
}

#[test]
fn first_731_days() {
let year_start = 0; // 1970
for day in 0.. (365 * 2 + 1) { // scan leap year and non-leap year
for day in 0..= 365 * 2 { // scan leap year and non-leap year
let (s, time) = from_sec(year_start + day * 86400);
assert_eq!(parse_rfc3339(&s).unwrap(), time);
assert_eq!(format_rfc3339(time).to_string(), s);
Expand All @@ -488,8 +486,8 @@ mod test {

#[test]
fn the_731_consecutive_days() {
let year_start = 1325376000; // 2012
for day in 0.. (365 * 2 + 1) { // scan leap year and non-leap year
let year_start = 1_325_376_000; // 2012
for day in 0..= 365 * 2 { // scan leap year and non-leap year
let (s, time) = from_sec(year_start + day * 86400);
assert_eq!(parse_rfc3339(&s).unwrap(), time);
assert_eq!(format_rfc3339(time).to_string(), s);
Expand All @@ -498,7 +496,7 @@ mod test {

#[test]
fn all_86400_seconds() {
let day_start = 1325376000;
let day_start = 1_325_376_000;
for second in 0..86400 { // scan leap year and non-leap year
let (s, time) = from_sec(day_start + second);
assert_eq!(parse_rfc3339(&s).unwrap(), time);
Expand All @@ -520,7 +518,7 @@ mod test {

#[test]
fn random_wide_range() {
for _ in 0..100000 {
for _ in 0..100_000 {
let sec = rand::thread_rng().gen_range(0, max::SECONDS);
let (s, time) = from_sec(sec);
assert_eq!(parse_rfc3339(&s).unwrap(), time);
Expand All @@ -531,8 +529,8 @@ mod test {
#[test]
fn milliseconds() {
assert_eq!(parse_rfc3339("1970-01-01T00:00:00.123Z").unwrap(),
UNIX_EPOCH + Duration::new(0, 123000000));
assert_eq!(format_rfc3339(UNIX_EPOCH + Duration::new(0, 123000000))
UNIX_EPOCH + Duration::new(0, 123_000_000));
assert_eq!(format_rfc3339(UNIX_EPOCH + Duration::new(0, 123_000_000))
.to_string(), "1970-01-01T00:00:00.123000000Z");
}

Expand Down Expand Up @@ -600,15 +598,15 @@ mod test {
parse_rfc3339("1970-01-01 00:00:00").unwrap_err();

assert_eq!(parse_rfc3339_weak("1970-01-01 00:00:00.000123").unwrap(),
UNIX_EPOCH + Duration::new(0, 123000));
UNIX_EPOCH + Duration::new(0, 123_000));
parse_rfc3339("1970-01-01 00:00:00.000123").unwrap_err();

assert_eq!(parse_rfc3339_weak("1970-01-01T00:00:00.000123").unwrap(),
UNIX_EPOCH + Duration::new(0, 123000));
UNIX_EPOCH + Duration::new(0, 123_000));
parse_rfc3339("1970-01-01T00:00:00.000123").unwrap_err();

assert_eq!(parse_rfc3339_weak("1970-01-01 00:00:00.000123Z").unwrap(),
UNIX_EPOCH + Duration::new(0, 123000));
UNIX_EPOCH + Duration::new(0, 123_000));
parse_rfc3339("1970-01-01 00:00:00.000123Z").unwrap_err();

assert_eq!(parse_rfc3339_weak("1970-01-01 00:00:00Z").unwrap(),
Expand Down
Loading

0 comments on commit 2ec4fba

Please sign in to comment.