Skip to content

Commit

Permalink
Switch to 2024 style
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Feb 21, 2025
1 parent 11d227a commit 7f6cf5e
Show file tree
Hide file tree
Showing 33 changed files with 135 additions and 154 deletions.
8 changes: 4 additions & 4 deletions bench/benches/chrono.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Benchmarks for chrono that just depend on std
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main};

use chrono::format::StrftimeItems;
use chrono::prelude::*;
#[cfg(feature = "unstable-locales")]
use chrono::Locale;
use chrono::{DateTime, FixedOffset, Local, TimeDelta, Utc, __BenchYearFlags};
use chrono::format::StrftimeItems;
use chrono::prelude::*;
use chrono::{__BenchYearFlags, DateTime, FixedOffset, Local, TimeDelta, Utc};

fn bench_date_from_ymd(c: &mut Criterion) {
c.bench_function("bench_date_from_ymd", |b| {
Expand Down
2 changes: 1 addition & 1 deletion bench/benches/serde.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{Criterion, black_box, criterion_group, criterion_main};

use chrono::NaiveDateTime;

Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
style_edition = "2024"
use_small_heuristics = "Max"
20 changes: 8 additions & 12 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ use core::{fmt, hash, str};
#[cfg(feature = "std")]
use std::time::{SystemTime, UNIX_EPOCH};

#[allow(deprecated)]
use crate::Date;
#[cfg(all(feature = "unstable-locales", feature = "alloc"))]
use crate::format::Locale;
#[cfg(feature = "alloc")]
use crate::format::{DelayedFormat, SecondsFormat, write_rfc2822, write_rfc3339};
use crate::format::{
parse, parse_and_remainder, parse_rfc3339, Fixed, Item, ParseError, ParseResult, Parsed,
StrftimeItems, TOO_LONG,
Fixed, Item, ParseError, ParseResult, Parsed, StrftimeItems, TOO_LONG, parse,
parse_and_remainder, parse_rfc3339,
};
#[cfg(feature = "alloc")]
use crate::format::{write_rfc2822, write_rfc3339, DelayedFormat, SecondsFormat};
use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
#[cfg(feature = "clock")]
use crate::offset::Local;
use crate::offset::{FixedOffset, LocalResult, Offset, TimeZone, Utc};
#[allow(deprecated)]
use crate::Date;
use crate::{expect, try_opt};
use crate::{Datelike, Months, TimeDelta, Timelike, Weekday};
use crate::{expect, try_opt};

#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};
Expand Down Expand Up @@ -1849,11 +1849,7 @@ impl From<SystemTime> for DateTime<Utc> {
// unlikely but should be handled
let dur = e.duration();
let (sec, nsec) = (dur.as_secs() as i64, dur.subsec_nanos());
if nsec == 0 {
(-sec, 0)
} else {
(-sec - 1, 1_000_000_000 - nsec)
}
if nsec == 0 { (-sec, 0) } else { (-sec - 1, 1_000_000_000 - nsec) }
}
};
Utc.timestamp_opt(sec, nsec).unwrap()
Expand Down
6 changes: 4 additions & 2 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::fmt;
use serde::{de, ser};

use super::DateTime;
use crate::format::{write_rfc3339, SecondsFormat};
use crate::format::{SecondsFormat, write_rfc3339};
#[cfg(feature = "clock")]
use crate::offset::Local;
use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
Expand Down Expand Up @@ -1289,7 +1289,9 @@ mod tests {
}

assert!(serde_json::from_str::<DateTime<Utc>>(r#""2014-07-32T12:34:06Z""#).is_err());
assert!(serde_json::from_str::<DateTime<FixedOffset>>(r#""2014-07-32T12:34:06Z""#).is_err());
assert!(
serde_json::from_str::<DateTime<FixedOffset>>(r#""2014-07-32T12:34:06Z""#).is_err()
);
}

#[test]
Expand Down
11 changes: 5 additions & 6 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,10 +734,7 @@ fn test_datetime_rfc2822() {
"Thu,\n\t13\n Feb\n 1969\n 23:32\n -0330 (Newfoundland Time)"
),
Ok(
ymdhms(
&FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(),
1969, 2, 13, 23, 32, 0,
)
ymdhms(&FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(), 1969, 2, 13, 23, 32, 0,)
)
);
// example from RFC 2822 Appendix A.5. without trailing " (Newfoundland Time)"
Expand Down Expand Up @@ -1099,8 +1096,10 @@ fn test_parse_from_str() {
Ok(ymdhms(&edt, 2014, 5, 7, 12, 34, 56))
); // ignore offset
assert!(DateTime::parse_from_str("20140507000000", "%Y%m%d%H%M%S").is_err()); // no offset
assert!(DateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT")
.is_err());
assert!(
DateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT")
.is_err()
);
assert_eq!(
DateTime::parse_from_str("0", "%s").unwrap(),
DateTime::from_timestamp(0, 0).unwrap().fixed_offset()
Expand Down
2 changes: 1 addition & 1 deletion src/format/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ mod tests {
#[cfg(all(feature = "std", feature = "unstable-locales", feature = "alloc"))]
#[test]
fn test_with_locale_delayed_write_to() {
use crate::format::locales::Locale;
use crate::DateTime;
use crate::format::locales::Locale;

let dt = DateTime::from_timestamp(1643723400, 123456789).unwrap();
let df = dt.format_localized("%A, %B %d, %Y", Locale::ja_JP);
Expand Down
2 changes: 1 addition & 1 deletion src/format/locales.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "unstable-locales")]
mod localized {
use pure_rust_locales::{locale_match, Locale};
use pure_rust_locales::{Locale, locale_match};

pub(crate) const fn default_locale() -> Locale {
Locale::POSIX
Expand Down
4 changes: 2 additions & 2 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ pub mod strftime;
// not require `alloc`.
pub(crate) mod locales;

pub use formatting::SecondsFormat;
pub(crate) use formatting::write_hundreds;
#[cfg(feature = "alloc")]
pub(crate) use formatting::write_rfc2822;
#[cfg(any(feature = "alloc", feature = "serde"))]
pub(crate) use formatting::write_rfc3339;
pub use formatting::SecondsFormat;
#[cfg(feature = "alloc")]
#[allow(deprecated)]
pub use formatting::{format, format_item, DelayedFormat};
pub use formatting::{DelayedFormat, format, format_item};
#[cfg(feature = "unstable-locales")]
pub use locales::Locale;
pub(crate) use parse::parse_rfc3339;
Expand Down
16 changes: 10 additions & 6 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use core::borrow::Borrow;
use core::str;

use super::scan;
use super::{BAD_FORMAT, INVALID, OUT_OF_RANGE, TOO_LONG, TOO_SHORT};
use super::{Fixed, InternalFixed, InternalInternal, Item, Numeric, Pad, Parsed};
use super::{ParseError, ParseResult};
use super::{BAD_FORMAT, INVALID, OUT_OF_RANGE, TOO_LONG, TOO_SHORT};
use crate::{DateTime, FixedOffset, Weekday};

fn set_weekday_with_num_days_from_sunday(p: &mut Parsed, v: i64) -> ParseResult<()> {
Expand Down Expand Up @@ -640,15 +640,17 @@ mod tests {
// most unicode whitespace characters
parses(
"\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}",
&[Space("\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}")]
&[Space(
"\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}",
)],
);
// most unicode whitespace characters
parses(
"\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}",
&[
Space("\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}"),
Space("\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}")
]
Space("\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{3000}"),
],
);
check("a", &[Space("")], Err(TOO_LONG));
check("a", &[Space(" ")], Err(TOO_LONG));
Expand Down Expand Up @@ -1835,8 +1837,10 @@ mod tests {

#[test]
fn test_issue_1010() {
let dt = crate::NaiveDateTime::parse_from_str("\u{c}SUN\u{e}\u{3000}\0m@J\u{3000}\0\u{3000}\0m\u{c}!\u{c}\u{b}\u{c}\u{c}\u{c}\u{c}%A\u{c}\u{b}\0SU\u{c}\u{c}",
"\u{c}\u{c}%A\u{c}\u{b}\0SUN\u{c}\u{c}\u{c}SUNN\u{c}\u{c}\u{c}SUN\u{c}\u{c}!\u{c}\u{b}\u{c}\u{c}\u{c}\u{c}%A\u{c}\u{b}%a");
let dt = crate::NaiveDateTime::parse_from_str(
"\u{c}SUN\u{e}\u{3000}\0m@J\u{3000}\0\u{3000}\0m\u{c}!\u{c}\u{b}\u{c}\u{c}\u{c}\u{c}%A\u{c}\u{b}\0SU\u{c}\u{c}",
"\u{c}\u{c}%A\u{c}\u{b}\0SUN\u{c}\u{c}\u{c}SUNN\u{c}\u{c}\u{c}SUN\u{c}\u{c}!\u{c}\u{b}\u{c}\u{c}\u{c}\u{c}%A\u{c}\u{b}%a",
);
assert_eq!(dt, Err(ParseError(ParseErrorKind::Invalid)));
}
}
18 changes: 5 additions & 13 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! A collection of parsed date and time items.
//! They can be constructed incrementally while being checked for consistency.
use super::{ParseResult, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE};
use super::{IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE, ParseResult};
use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
use crate::offset::{FixedOffset, MappedLocalTime, Offset, TimeZone};
use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
Expand Down Expand Up @@ -176,11 +176,7 @@ pub struct Parsed {
#[inline]
fn set_if_consistent<T: PartialEq>(old: &mut Option<T>, new: T) -> ParseResult<()> {
if let Some(ref old) = *old {
if *old == new {
Ok(())
} else {
Err(IMPOSSIBLE)
}
if *old == new { Ok(()) } else { Err(IMPOSSIBLE) }
} else {
*old = Some(new);
Ok(())
Expand Down Expand Up @@ -701,11 +697,7 @@ impl Parsed {
(_, _, _) => return Err(NOT_ENOUGH),
};

if verified {
Ok(parsed_date)
} else {
Err(IMPOSSIBLE)
}
if verified { Ok(parsed_date) } else { Err(IMPOSSIBLE) }
}

/// Returns a parsed naive time out of given fields.
Expand Down Expand Up @@ -1166,10 +1158,10 @@ fn resolve_week_date(
mod tests {
use super::super::{IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE};
use super::Parsed;
use crate::naive::{NaiveDate, NaiveTime};
use crate::offset::{FixedOffset, TimeZone, Utc};
use crate::Datelike;
use crate::Weekday::*;
use crate::naive::{NaiveDate, NaiveTime};
use crate::offset::{FixedOffset, TimeZone, Utc};

#[test]
fn test_parsed_set_fields() {
Expand Down
10 changes: 3 additions & 7 deletions src/format/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Various scanning routines for the parser.
*/

use super::{ParseResult, INVALID, OUT_OF_RANGE, TOO_SHORT};
use super::{INVALID, OUT_OF_RANGE, ParseResult, TOO_SHORT};
use crate::Weekday;

/// Tries to parse the non-negative number from `min` to `max` digits.
Expand Down Expand Up @@ -217,11 +217,7 @@ where

const fn digits(s: &str) -> ParseResult<(u8, u8)> {
let b = s.as_bytes();
if b.len() < 2 {
Err(TOO_SHORT)
} else {
Ok((b[0], b[1]))
}
if b.len() < 2 { Err(TOO_SHORT) } else { Ok((b[0], b[1])) }
}
let negative = match s.chars().next() {
Some('+') => {
Expand Down Expand Up @@ -359,8 +355,8 @@ mod tests {
comment_2822, nanosecond, nanosecond_fixed, short_or_long_month0, short_or_long_weekday,
timezone_offset_2822,
};
use crate::format::{INVALID, TOO_SHORT};
use crate::Weekday;
use crate::format::{INVALID, TOO_SHORT};

#[test]
fn test_rfc2822_comments() {
Expand Down
12 changes: 6 additions & 6 deletions src/format/strftime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ Notes:
#[cfg(feature = "alloc")]
extern crate alloc;

use super::{fixed, internal_fixed, num, num0, nums};
#[cfg(feature = "unstable-locales")]
use super::{locales, Locale};
use super::{Fixed, InternalInternal, Item, Numeric, Pad};
#[cfg(any(feature = "alloc", feature = "std"))]
use super::{ParseError, BAD_FORMAT};
use super::{BAD_FORMAT, ParseError};
use super::{Fixed, InternalInternal, Item, Numeric, Pad};
#[cfg(feature = "unstable-locales")]
use super::{Locale, locales};
use super::{fixed, internal_fixed, num, num0, nums};
#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
use alloc::vec::Vec;

Expand Down Expand Up @@ -686,8 +686,8 @@ mod tests {
use crate::format::Item::{self, Literal, Space};
#[cfg(feature = "unstable-locales")]
use crate::format::Locale;
use crate::format::{fixed, internal_fixed, num, num0, nums};
use crate::format::{Fixed, InternalInternal, Numeric::*};
use crate::format::{fixed, internal_fixed, num, num0, nums};
#[cfg(feature = "alloc")]
use crate::{DateTime, FixedOffset, NaiveDate, TimeZone, Timelike, Utc};

Expand Down
6 changes: 3 additions & 3 deletions src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use core::fmt;
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

use crate::naive::NaiveDate;
use crate::OutOfRange;
use crate::naive::NaiveDate;

/// The month of the year.
///
Expand Down Expand Up @@ -395,8 +395,8 @@ mod tests {
#[test]
#[cfg(feature = "serde")]
fn test_serde_serialize() {
use serde_json::to_string;
use Month::*;
use serde_json::to_string;

let cases: Vec<(Month, &str)> = vec![
(January, "\"January\""),
Expand All @@ -422,8 +422,8 @@ mod tests {
#[test]
#[cfg(feature = "serde")]
fn test_serde_deserialize() {
use serde_json::from_str;
use Month::*;
use serde_json::from_str;

let cases: Vec<(&str, Month)> = vec![
("\"january\"", January),
Expand Down
6 changes: 3 additions & 3 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ use pure_rust_locales::Locale;
#[cfg(feature = "alloc")]
use crate::format::DelayedFormat;
use crate::format::{
parse, parse_and_remainder, write_hundreds, Item, Numeric, Pad, ParseError, ParseResult,
Parsed, StrftimeItems,
Item, Numeric, Pad, ParseError, ParseResult, Parsed, StrftimeItems, parse, parse_and_remainder,
write_hundreds,
};
use crate::month::Months;
use crate::naive::{Days, IsoWeek, NaiveDateTime, NaiveTime, NaiveWeek};
use crate::{expect, try_opt};
use crate::{Datelike, TimeDelta, Weekday};
use crate::{expect, try_opt};

use super::internals::{Mdf, YearFlags};

Expand Down
4 changes: 2 additions & 2 deletions src/naive/date/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Days, Months, NaiveDate, MAX_YEAR, MIN_YEAR};
use crate::naive::internals::{YearFlags, A, AG, B, BA, C, CB, D, DC, E, ED, F, FE, G, GF};
use super::{Days, MAX_YEAR, MIN_YEAR, Months, NaiveDate};
use crate::naive::internals::{A, AG, B, BA, C, CB, D, DC, E, ED, F, FE, G, GF, YearFlags};
use crate::{Datelike, TimeDelta, Weekday};

// as it is hard to verify year flags in `NaiveDate::MIN` and `NaiveDate::MAX`,
Expand Down
6 changes: 3 additions & 3 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use rkyv::{Archive, Deserialize, Serialize};

#[cfg(feature = "alloc")]
use crate::format::DelayedFormat;
use crate::format::{parse, parse_and_remainder, ParseError, ParseResult, Parsed, StrftimeItems};
use crate::format::{Fixed, Item, Numeric, Pad};
use crate::format::{ParseError, ParseResult, Parsed, StrftimeItems, parse, parse_and_remainder};
use crate::naive::{Days, IsoWeek, NaiveDate, NaiveTime};
use crate::offset::Utc;
use crate::time_delta::NANOS_PER_SEC;
use crate::{
expect, try_opt, DateTime, Datelike, FixedOffset, MappedLocalTime, Months, TimeDelta, TimeZone,
Timelike, Weekday,
DateTime, Datelike, FixedOffset, MappedLocalTime, Months, TimeDelta, TimeZone, Timelike,
Weekday, expect, try_opt,
};

/// Tools to help serializing/deserializing `NaiveDateTime`s
Expand Down
9 changes: 4 additions & 5 deletions src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,10 @@ fn test_datetime_parse_from_str() {
NaiveDateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT"),
Ok(ymdhms(2013, 8, 9, 23, 54, 35))
);
assert!(NaiveDateTime::parse_from_str(
"Sat, 09 Aug 2013 23:54:35 GMT",
"%a, %d %b %Y %H:%M:%S GMT"
)
.is_err());
assert!(
NaiveDateTime::parse_from_str("Sat, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT")
.is_err()
);
assert!(NaiveDateTime::parse_from_str("2014-5-7 12:3456", "%Y-%m-%d %H:%M:%S").is_err());
assert!(NaiveDateTime::parse_from_str("12:34:56", "%H:%M:%S").is_err()); // insufficient
assert_eq!(
Expand Down
Loading

0 comments on commit 7f6cf5e

Please sign in to comment.