Skip to content

Commit

Permalink
Merge pull request #11 from koushiro/apply-rustfmt-and-clippy
Browse files Browse the repository at this point in the history
Migrate to 2018 edition
  • Loading branch information
tailhook authored Jan 18, 2020
2 parents f569740 + f00dbba commit 4a40682
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 82 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ keywords = ["time", "human", "human-friendly", "parser", "duration"]
homepage = "https://github.com/tailhook/humantime"
documentation = "https://docs.rs/humantime"
version = "1.3.0"
edition = "2018"
authors = ["Paul Colomiets <paul@colomiets.name>"]
categories = ["date-and-time"]

Expand Down
10 changes: 4 additions & 6 deletions benches/datetime_format.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#![feature(test)]
extern crate chrono;
extern crate humantime;
extern crate test;

use std::io::Write;
use std::time::{Duration, UNIX_EPOCH};
use humantime::format_rfc3339;

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 @@ -28,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
3 changes: 0 additions & 3 deletions benches/datetime_parse.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#![feature(test)]
extern crate chrono;
extern crate humantime;
extern crate test;

use chrono::{DateTime};
use humantime::parse_rfc3339;


#[bench]
fn rfc3339_humantime_seconds(b: &mut test::Bencher) {
b.iter(|| {
Expand Down
79 changes: 38 additions & 41 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 @@ -358,18 +356,17 @@ impl fmt::Display for Rfc3339Timestamp {
};

// 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]) })
}
}

#[cfg(test)]
mod test {
extern crate time;
extern crate rand;

use std::str::from_utf8;
use self::rand::Rng;
use std::time::{UNIX_EPOCH, SystemTime, Duration};

use rand::Rng;

use super::{parse_rfc3339, parse_rfc3339_weak, format_rfc3339};
use super::{format_rfc3339_millis, format_rfc3339_micros};
use super::{format_rfc3339_nanos};
Expand All @@ -379,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 @@ -399,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 @@ -413,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 @@ -428,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 @@ -440,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 @@ -456,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 @@ -474,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 @@ -489,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 @@ -499,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 @@ -521,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 @@ -532,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 @@ -601,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 4a40682

Please sign in to comment.