Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.5.x: Rename NaiveDate::*_opt methods #1436

Merged
merged 16 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions bench/benches/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use chrono::{DateTime, FixedOffset, Local, TimeDelta, Utc, __BenchYearFlags};

fn bench_date_from_ymd(c: &mut Criterion) {
c.bench_function("bench_date_from_ymd", |b| {
let expected = NaiveDate::from_ymd_opt(2024, 2, 12);
let expected = NaiveDate::from_ymd(2024, 2, 12);
b.iter(|| {
let (y, m, d) = black_box((2024, 2, 12));
assert_eq!(NaiveDate::from_ymd_opt(y, m, d), expected)
assert_eq!(NaiveDate::from_ymd(y, m, d), expected)
})
});
}
Expand Down Expand Up @@ -50,10 +50,7 @@ fn bench_datetime_to_rfc2822(c: &mut Criterion) {
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_000)
.unwrap(),
&NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc2822", |b| b.iter(|| black_box(dt).to_rfc2822()));
Expand All @@ -63,10 +60,7 @@ fn bench_datetime_to_rfc3339(c: &mut Criterion) {
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_000)
.unwrap(),
&NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339()));
Expand All @@ -76,10 +70,7 @@ fn bench_datetime_to_rfc3339_opts(c: &mut Criterion) {
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_000)
.unwrap(),
&NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc3339_opts", |b| {
Expand Down Expand Up @@ -139,7 +130,7 @@ fn num_days_from_ce_alt<Date: Datelike>(date: &Date) -> i32 {
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_opt(*year, 1, 1).unwrap();
let d = NaiveDate::from_ymd(*year, 1, 1).unwrap();
group.bench_with_input(BenchmarkId::new("new", year), &d, |b, y| {
b.iter(|| num_days_from_ce_alt(y))
});
Expand Down Expand Up @@ -207,7 +198,7 @@ fn bench_format_manual(c: &mut Criterion) {
}

fn bench_naivedate_add_signed(c: &mut Criterion) {
let date = NaiveDate::from_ymd_opt(2023, 7, 29).unwrap();
let date = NaiveDate::from_ymd(2023, 7, 29).unwrap();
let extra = TimeDelta::days(25);
c.bench_function("bench_naivedate_add_signed", |b| {
b.iter(|| black_box(date).checked_add_signed(extra).unwrap())
Expand Down
28 changes: 14 additions & 14 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
#[must_use]
pub fn date_naive(&self) -> NaiveDate {
let local = self.naive_local();
NaiveDate::from_ymd_opt(local.year(), local.month(), local.day()).unwrap()
NaiveDate::from_ymd(local.year(), local.month(), local.day()).unwrap()
}

/// Retrieves the time component.
Expand Down Expand Up @@ -148,10 +148,10 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
/// use chrono::{Utc, NaiveDate};
///
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_milli_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(1970, 1, 1).unwrap().and_hms_milli(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_millis(), 1_444);
///
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_milli_opt(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(2001, 9, 9).unwrap().and_hms_milli(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_millis(), 1_000_000_000_555);
/// ```
#[inline]
Expand All @@ -167,10 +167,10 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
/// use chrono::{Utc, NaiveDate};
///
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_micro_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(1970, 1, 1).unwrap().and_hms_micro(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_micros(), 1_000_444);
///
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_micro_opt(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(2001, 9, 9).unwrap().and_hms_micro(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
/// ```
#[inline]
Expand All @@ -194,22 +194,22 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
/// use chrono::{Utc, NaiveDate};
///
/// let dt = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_nano_opt(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(1970, 1, 1).unwrap().and_hms_nano(0, 0, 1, 444).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_nanos(), Some(1_000_000_444));
///
/// let dt = NaiveDate::from_ymd_opt(2001, 9, 9).unwrap().and_hms_nano_opt(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(2001, 9, 9).unwrap().and_hms_nano(1, 46, 40, 555).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_nanos(), Some(1_000_000_000_000_000_555));
///
/// let dt = NaiveDate::from_ymd_opt(1677, 9, 21).unwrap().and_hms_nano_opt(0, 12, 43, 145_224_192).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(1677, 9, 21).unwrap().and_hms_nano(0, 12, 43, 145_224_192).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_nanos(), Some(-9_223_372_036_854_775_808));
///
/// let dt = NaiveDate::from_ymd_opt(2262, 4, 11).unwrap().and_hms_nano_opt(23, 47, 16, 854_775_807).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(2262, 4, 11).unwrap().and_hms_nano(23, 47, 16, 854_775_807).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_nanos(), Some(9_223_372_036_854_775_807));
///
/// let dt = NaiveDate::from_ymd_opt(1677, 9, 21).unwrap().and_hms_nano_opt(0, 12, 43, 145_224_191).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(1677, 9, 21).unwrap().and_hms_nano(0, 12, 43, 145_224_191).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_nanos(), None);
///
/// let dt = NaiveDate::from_ymd_opt(2262, 4, 11).unwrap().and_hms_nano_opt(23, 47, 16, 854_775_808).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(2262, 4, 11).unwrap().and_hms_nano(23, 47, 16, 854_775_808).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timestamp_nanos(), None);
/// ```
#[inline]
Expand Down Expand Up @@ -487,7 +487,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
///
/// ```rust
/// # use chrono::{FixedOffset, SecondsFormat, TimeZone, Utc, NaiveDate};
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(18, 30, 9, 453_829).unwrap().and_local_timezone(Utc).unwrap();
/// let dt = NaiveDate::from_ymd(2018, 1, 26).unwrap().and_hms_micro(18, 30, 9, 453_829).unwrap().and_local_timezone(Utc).unwrap();
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, false),
/// "2018-01-26T18:30:09.453+00:00");
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, true),
Expand All @@ -496,7 +496,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// "2018-01-26T18:30:09Z");
///
/// let pst = FixedOffset::east(8 * 60 * 60).unwrap();
/// let dt = pst.from_local_datetime(&NaiveDate::from_ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(10, 30, 9, 453_829).unwrap()).unwrap();
/// let dt = pst.from_local_datetime(&NaiveDate::from_ymd(2018, 1, 26).unwrap().and_hms_micro(10, 30, 9, 453_829).unwrap()).unwrap();
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true),
/// "2018-01-26T10:30:09+08:00");
/// ```
Expand Down Expand Up @@ -757,7 +757,7 @@ impl DateTime<FixedOffset> {
///
/// let dt = DateTime::parse_from_str(
/// "1983 Apr 13 12:09:14.274 +0000", "%Y %b %d %H:%M:%S%.3f %z");
/// assert_eq!(dt, Ok(FixedOffset::east(0).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(1983, 4, 13).unwrap().and_hms_milli_opt(12, 9, 14, 274).unwrap()).unwrap()));
/// assert_eq!(dt, Ok(FixedOffset::east(0).unwrap().from_local_datetime(&NaiveDate::from_ymd(1983, 4, 13).unwrap().and_hms_milli(12, 9, 14, 274).unwrap()).unwrap()));
/// ```
pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<DateTime<FixedOffset>> {
let mut parsed = Parsed::new();
Expand Down
24 changes: 12 additions & 12 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'de> de::Deserialize<'de> for DateTime<Local> {
/// time: DateTime<Utc>
/// }
///
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap();
/// let time = NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_nano(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap();
/// let my_s = S {
/// time: time.clone(),
/// };
Expand Down Expand Up @@ -159,7 +159,7 @@ pub mod ts_nanoseconds {
/// }
///
/// let my_s = S {
/// time: NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap(),
/// time: NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_nano(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
Expand Down Expand Up @@ -256,7 +256,7 @@ pub mod ts_nanoseconds {
/// time: Option<DateTime<Utc>>
/// }
///
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap());
/// let time = Some(NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_nano(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap());
/// let my_s = S {
/// time: time.clone(),
/// };
Expand Down Expand Up @@ -300,7 +300,7 @@ pub mod ts_nanoseconds_option {
/// }
///
/// let my_s = S {
/// time: Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap()),
/// time: Some(NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_nano(02, 04, 59, 918355733).unwrap().and_local_timezone(Utc).unwrap()),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
Expand Down Expand Up @@ -396,7 +396,7 @@ pub mod ts_nanoseconds_option {
/// time: DateTime<Utc>
/// }
///
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap();
/// let time = NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_micro(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap();
/// let my_s = S {
/// time: time.clone(),
/// };
Expand Down Expand Up @@ -432,7 +432,7 @@ pub mod ts_microseconds {
/// }
///
/// let my_s = S {
/// time: NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap(),
/// time: NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_micro(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
Expand Down Expand Up @@ -527,7 +527,7 @@ pub mod ts_microseconds {
/// time: Option<DateTime<Utc>>
/// }
///
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap());
/// let time = Some(NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_micro(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap());
/// let my_s = S {
/// time: time.clone(),
/// };
Expand Down Expand Up @@ -562,7 +562,7 @@ pub mod ts_microseconds_option {
/// }
///
/// let my_s = S {
/// time: Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_micro_opt(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap()),
/// time: Some(NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_micro(02, 04, 59, 918355).unwrap().and_local_timezone(Utc).unwrap()),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
Expand Down Expand Up @@ -656,7 +656,7 @@ pub mod ts_microseconds_option {
/// time: DateTime<Utc>
/// }
///
/// let time = NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap();
/// let time = NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_milli(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap();
/// let my_s = S {
/// time: time.clone(),
/// };
Expand Down Expand Up @@ -692,7 +692,7 @@ pub mod ts_milliseconds {
/// }
///
/// let my_s = S {
/// time: NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap(),
/// time: NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_milli(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
Expand Down Expand Up @@ -781,7 +781,7 @@ pub mod ts_milliseconds {
/// time: Option<DateTime<Utc>>
/// }
///
/// let time = Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap());
/// let time = Some(NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_milli(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap());
/// let my_s = S {
/// time: time.clone(),
/// };
Expand Down Expand Up @@ -816,7 +816,7 @@ pub mod ts_milliseconds_option {
/// }
///
/// let my_s = S {
/// time: Some(NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap()),
/// time: Some(NaiveDate::from_ymd(2018, 5, 17).unwrap().and_hms_milli(02, 04, 59, 918).unwrap().and_local_timezone(Utc).unwrap()),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
Expand Down
Loading
Loading