Skip to content

Commit

Permalink
Fallable constructors for less panics (chronotope#815)
Browse files Browse the repository at this point in the history
- Creates a global Error enum
- Breaks backwards compatiblility mainly because of promoting fallable functions (chronotope#263)
- Some tests still fall
- Not all doctests are fixed
- to_naive_datetime_with_offset function is broken and needs fixing
- serde related stuff is not checked properly

This is a rebase of chronotope#817 onto the 0.5 main branch. Main differences:

- Unify three different error structs
- Removed ErrorKind
- Adapted a lot of unit tests
- Removed some commits from presumably unrelated branches (chronotope#829) or
  mainlined commits (chronotope#271)

Co-authored-by: John-John Tedro <udoprog@tedro.se>
  • Loading branch information
Zomtir and udoprog committed Mar 22, 2023
1 parent 2fcdd9e commit 8b3732f
Show file tree
Hide file tree
Showing 40 changed files with 4,478 additions and 5,899 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ Consider using `NaiveDateTime` with the implicit TAI (International Atomic Time)
if you want.

Chrono inherently does not support an inaccurate or partial date and time representation.
Any operation that can be ambiguous will return `None` in such cases.
Any operation that can be ambiguous will return `Err(chrono::Error)` in such cases.
For example, "a month later" of 2014-01-30 is not well-defined
and consequently `Utc.ymd_opt(2014, 1, 30).unwrap().with_month(2)` returns `None`.
and consequently `Utc.ymd(2014, 1, 30).with_month(2)` returns `Err(chrono::Error)`.

Non ISO week handling is not yet supported.
For now you can use the [chrono_ext](https://crates.io/crates/chrono_ext)
Expand Down
28 changes: 9 additions & 19 deletions benches/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,18 @@ fn bench_datetime_from_str(c: &mut Criterion) {
}

fn bench_datetime_to_rfc2822(c: &mut Criterion) {
let pst = FixedOffset::east_opt(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(),
)
.unwrap();
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
let dt = pst.from_local_datetime(
&NaiveDate::from_ymd(2018, 1, 11)?.and_hms_nano(10, 5, 13, 84_660_000)?
);
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_opt(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(),
)
.unwrap();
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
let dt = pst.from_local_datetime(
&NaiveDate::from_ymd(2018, 1, 11)?.and_hms_nano(10, 5, 13, 84_660_000)?
);
c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339()));
}

Expand Down Expand Up @@ -112,7 +102,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
2 changes: 1 addition & 1 deletion ci/core-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
use chrono::{TimeZone, Utc};

pub fn create_time() {
let _ = Utc.with_ymd_and_hms(2019, 1, 1, 0, 0, 0).unwrap();
let _ = Utc.ymd(2019, 1, 1).and_hms(0, 0, 0);
}
Loading

0 comments on commit 8b3732f

Please sign in to comment.