Skip to content

Commit

Permalink
rustfmt everything
Browse files Browse the repository at this point in the history
  • Loading branch information
quodlibetor committed Jun 27, 2020
1 parent e0412b7 commit febb8dc
Show file tree
Hide file tree
Showing 22 changed files with 2,857 additions and 1,871 deletions.
42 changes: 18 additions & 24 deletions benches/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
extern crate chrono;
extern crate criterion;

use criterion::{black_box, criterion_group, criterion_main, Criterion, BenchmarkId};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};

use chrono::prelude::*;
use chrono::{Utc, FixedOffset, DateTime, __BenchYearFlags};
use chrono::{DateTime, FixedOffset, Utc, __BenchYearFlags};

fn bench_datetime_parse_from_rfc2822(c: &mut Criterion) {
c.bench_function("bench_datetime_parse_from_rfc2822", |b| {
Expand Down Expand Up @@ -39,26 +39,24 @@ fn bench_datetime_from_str(c: &mut Criterion) {
fn bench_datetime_to_rfc2822(c: &mut Criterion) {
let pst = FixedOffset::east(8 * 60 * 60);
let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_000);
c.bench_function("bench_datetime_to_rfc2822", |b| {
b.iter(|| black_box(dt).to_rfc2822())
});
c.bench_function("bench_datetime_to_rfc2822", |b| b.iter(|| black_box(dt).to_rfc2822()));
}

fn bench_datetime_to_rfc3339(c: &mut Criterion) {
let pst = FixedOffset::east(8 * 60 * 60);
let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_000);
c.bench_function("bench_datetime_to_rfc3339", |b| {
b.iter(|| black_box(dt).to_rfc3339())
});
c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339()));
}

fn bench_year_flags_from_year(c: &mut Criterion) {
c.bench_function("bench_year_flags_from_year", |b|
b.iter(|| {
for year in -999i32..1000 {
__BenchYearFlags::from_year(year);
}
}));
c.bench_function("bench_year_flags_from_year", |b| {
b.iter(|| {
for year in -999i32..1000 {
__BenchYearFlags::from_year(year);
}
})
});
}

/// Returns the number of multiples of `div` in the range `start..end`.
///
Expand Down Expand Up @@ -95,16 +93,12 @@ fn bench_num_days_from_ce(c: &mut Criterion) {
let mut group = c.benchmark_group("num_days_from_ce");
for year in &[1, 500, 2000, 2019] {
let d = NaiveDate::from_ymd(*year, 1, 1);
group.bench_with_input(
BenchmarkId::new("new", year),
&d,
|b, y| b.iter(|| num_days_from_ce_alt(y)),
);
group.bench_with_input(
BenchmarkId::new("classic", year),
&d,
|b, y| b.iter(|| y.num_days_from_ce()),
);
group.bench_with_input(BenchmarkId::new("new", year), &d, |b, y| {
b.iter(|| num_days_from_ce_alt(y))
});
group.bench_with_input(BenchmarkId::new("classic", year), &d, |b, y| {
b.iter(|| y.num_days_from_ce())
});
}
}

Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use_small_heuristics = "Max"
117 changes: 87 additions & 30 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
#[cfg(any(feature = "alloc", feature = "std", test))]
use core::borrow::Borrow;
use core::{fmt, hash};
use core::cmp::Ordering;
use core::ops::{Add, Sub};
use core::{fmt, hash};
use oldtime::Duration as OldDuration;

use {Weekday, Datelike};
use offset::{TimeZone, Utc};
use naive::{self, NaiveDate, NaiveTime, IsoWeek};
use DateTime;
#[cfg(any(feature = "alloc", feature = "std", test))]
use format::{DelayedFormat, Item, StrftimeItems};
use naive::{self, IsoWeek, NaiveDate, NaiveTime};
use offset::{TimeZone, Utc};
use DateTime;
use {Datelike, Weekday};

/// ISO 8601 calendar date with time zone.
///
Expand Down Expand Up @@ -106,8 +106,13 @@ impl<Tz: TimeZone> Date<Tz> {
///
/// Returns `None` on invalid hour, minute, second and/or millisecond.
#[inline]
pub fn and_hms_milli_opt(&self, hour: u32, min: u32, sec: u32,
milli: u32) -> Option<DateTime<Tz>> {
pub fn and_hms_milli_opt(
&self,
hour: u32,
min: u32,
sec: u32,
milli: u32,
) -> Option<DateTime<Tz>> {
NaiveTime::from_hms_milli_opt(hour, min, sec, milli).and_then(|time| self.and_time(time))
}

Expand All @@ -127,8 +132,13 @@ impl<Tz: TimeZone> Date<Tz> {
///
/// Returns `None` on invalid hour, minute, second and/or microsecond.
#[inline]
pub fn and_hms_micro_opt(&self, hour: u32, min: u32, sec: u32,
micro: u32) -> Option<DateTime<Tz>> {
pub fn and_hms_micro_opt(
&self,
hour: u32,
min: u32,
sec: u32,
micro: u32,
) -> Option<DateTime<Tz>> {
NaiveTime::from_hms_micro_opt(hour, min, sec, micro).and_then(|time| self.and_time(time))
}

Expand All @@ -148,8 +158,13 @@ impl<Tz: TimeZone> Date<Tz> {
///
/// Returns `None` on invalid hour, minute, second and/or nanosecond.
#[inline]
pub fn and_hms_nano_opt(&self, hour: u32, min: u32, sec: u32,
nano: u32) -> Option<DateTime<Tz>> {
pub fn and_hms_nano_opt(
&self,
hour: u32,
min: u32,
sec: u32,
nano: u32,
) -> Option<DateTime<Tz>> {
NaiveTime::from_hms_nano_opt(hour, min, sec, nano).and_then(|time| self.and_time(time))
}

Expand Down Expand Up @@ -251,16 +266,24 @@ impl<Tz: TimeZone> Date<Tz> {

/// Maps the local date to other date with given conversion function.
fn map_local<Tz: TimeZone, F>(d: &Date<Tz>, mut f: F) -> Option<Date<Tz>>
where F: FnMut(NaiveDate) -> Option<NaiveDate> {
where
F: FnMut(NaiveDate) -> Option<NaiveDate>,
{
f(d.naive_local()).and_then(|date| d.timezone().from_local_date(&date).single())
}

impl<Tz: TimeZone> Date<Tz> where Tz::Offset: fmt::Display {
impl<Tz: TimeZone> Date<Tz>
where
Tz::Offset: fmt::Display,
{
/// Formats the date with the specified formatting items.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[inline]
pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
where I: Iterator<Item=B> + Clone, B: Borrow<Item<'a>> {
where
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
{
DelayedFormat::new_with_offset(Some(self.naive_local()), None, &self.offset, items)
}

Expand All @@ -275,15 +298,42 @@ impl<Tz: TimeZone> Date<Tz> where Tz::Offset: fmt::Display {
}

impl<Tz: TimeZone> Datelike for Date<Tz> {
#[inline] fn year(&self) -> i32 { self.naive_local().year() }
#[inline] fn month(&self) -> u32 { self.naive_local().month() }
#[inline] fn month0(&self) -> u32 { self.naive_local().month0() }
#[inline] fn day(&self) -> u32 { self.naive_local().day() }
#[inline] fn day0(&self) -> u32 { self.naive_local().day0() }
#[inline] fn ordinal(&self) -> u32 { self.naive_local().ordinal() }
#[inline] fn ordinal0(&self) -> u32 { self.naive_local().ordinal0() }
#[inline] fn weekday(&self) -> Weekday { self.naive_local().weekday() }
#[inline] fn iso_week(&self) -> IsoWeek { self.naive_local().iso_week() }
#[inline]
fn year(&self) -> i32 {
self.naive_local().year()
}
#[inline]
fn month(&self) -> u32 {
self.naive_local().month()
}
#[inline]
fn month0(&self) -> u32 {
self.naive_local().month0()
}
#[inline]
fn day(&self) -> u32 {
self.naive_local().day()
}
#[inline]
fn day0(&self) -> u32 {
self.naive_local().day0()
}
#[inline]
fn ordinal(&self) -> u32 {
self.naive_local().ordinal()
}
#[inline]
fn ordinal0(&self) -> u32 {
self.naive_local().ordinal0()
}
#[inline]
fn weekday(&self) -> Weekday {
self.naive_local().weekday()
}
#[inline]
fn iso_week(&self) -> IsoWeek {
self.naive_local().iso_week()
}

#[inline]
fn with_year(&self, year: i32) -> Option<Date<Tz>> {
Expand Down Expand Up @@ -326,11 +376,12 @@ impl<Tz: TimeZone> Copy for Date<Tz> where <Tz as TimeZone>::Offset: Copy {}
unsafe impl<Tz: TimeZone> Send for Date<Tz> where <Tz as TimeZone>::Offset: Send {}

impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<Date<Tz2>> for Date<Tz> {
fn eq(&self, other: &Date<Tz2>) -> bool { self.date == other.date }
fn eq(&self, other: &Date<Tz2>) -> bool {
self.date == other.date
}
}

impl<Tz: TimeZone> Eq for Date<Tz> {
}
impl<Tz: TimeZone> Eq for Date<Tz> {}

impl<Tz: TimeZone> PartialOrd for Date<Tz> {
fn partial_cmp(&self, other: &Date<Tz>) -> Option<Ordering> {
Expand All @@ -339,11 +390,15 @@ impl<Tz: TimeZone> PartialOrd for Date<Tz> {
}

impl<Tz: TimeZone> Ord for Date<Tz> {
fn cmp(&self, other: &Date<Tz>) -> Ordering { self.date.cmp(&other.date) }
fn cmp(&self, other: &Date<Tz>) -> Ordering {
self.date.cmp(&other.date)
}
}

impl<Tz: TimeZone> hash::Hash for Date<Tz> {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.date.hash(state) }
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.date.hash(state)
}
}

impl<Tz: TimeZone> Add<OldDuration> for Date<Tz> {
Expand Down Expand Up @@ -379,9 +434,11 @@ impl<Tz: TimeZone> fmt::Debug for Date<Tz> {
}
}

impl<Tz: TimeZone> fmt::Display for Date<Tz> where Tz::Offset: fmt::Display {
impl<Tz: TimeZone> fmt::Display for Date<Tz>
where
Tz::Offset: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.naive_local(), self.offset)
}
}

Loading

0 comments on commit febb8dc

Please sign in to comment.