The original would first check that there is right amount of numeric
characters and then parsed them using the std::str::parse, which
internally checks the characters again and also checks for -/+ prefix,
which is not necessary in this case.
Since we are already going over the characters, we may as well do the
parsing ourselves. The length of the function is roughly the same and
it is faster:
name simplify-from-str ns/iter reimplement-number ns/iter diff ns/iter diff % speedup
datetime::tests::bench_datetime_from_str 448 365 -83 -18.53% x 1.23
datetime::tests::bench_datetime_parse_from_rfc2822 242 195 -47 -19.42% x 1.24
datetime::tests::bench_datetime_parse_from_rfc3339 234 166 -68 -29.06% x 1.41
The Item::Space calls str::trim_left and Item::Numeric also calls
str::trim_left before doing anything else, so there is no need to
have Item::Space before Item::Numeric.
Speeds up parsing:
name remove-cloned ns/iter simplify-from-str ns/iter diff ns/iter diff % speedup
datetime::tests::bench_datetime_from_str 582 448 -134 -23.02% x 1.30
datetime::tests::bench_datetime_parse_from_rfc2822 244 242 -2 -0.82% x 1.01
datetime::tests::bench_datetime_parse_from_rfc3339 239 234 -5 -2.09% x 1.02
The parse::parse and format::format functions accepted Iterator of owned
Items. While it is sometimes convenient to pass in the owned values,
neither of the functions really need to own them, so references would
be enough. The Borrow trait allows us to pass in Iterator over values,
references, boxes, etc.
According to RFC 1105 this is a minor change, because it shouldn't break
any existing code. And chrono is in pre-1.0 version anyway.
This allows us to remove multiple cloned() calls which speeds up parsing
and formating:
name control ns/iter remove-cloned ns/iter diff ns/iter diff % speedup
datetime::tests::bench_datetime_from_str 712 582 -130 -18.26% x 1.22
datetime::tests::bench_datetime_parse_from_rfc2822 252 244 -8 -3.17% x 1.03
datetime::tests::bench_datetime_parse_from_rfc3339 242 239 -3 -1.24% x 1.01
The #[cfg(bench)] attribute does not exist and is always false. Lets
define a feature "bench" which can be used to enable benchmarks when
building with nightly.
The new wording tries to make clearer that those methods use a 1-based
numbering scheme.
This commit also includes a couple of drive-by cosmetic changes.
This adds a new `std` feature to chrono that is enabled by default. By
deactivating this feature via `default-features = false` you can now use
chrono in applications that don't use the standard library. The `serde`
feature is supported as well.
Resolves#336
The internal, tz-independent timestamp was valid, but since the timezone offset
could change as you add or subtract a second or two you would get
nonsense/nonexistent times when you observed them.
Fixes#318
This patch fixes the case where a negative millisecond offset is passed
to Timezone::timestamp_millis and timestamp_millis_opt and adds a test
case for it. Without this patch, calling timestamp_offset with a
negative value will panic with an overflow like this:
```
---- tests::test_parse_samples stdout ----
thread 'tests::test_parse_samples' panicked at 'attempt to multiply with
overflow',
/home/c/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-0.4.6/src/offset/mod.rs:349:34
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a
verbose backtrace.
stack backtrace:
0: std::sys::unix::backtrace::tracing:👿:unwind_backtrace
at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
1: std::sys_common::backtrace::print
at libstd/sys_common/backtrace.rs:71
at libstd/sys_common/backtrace.rs:59
2: std::panicking::default_hook::{{closure}}
at libstd/panicking.rs:211
3: std::panicking::default_hook
at libstd/panicking.rs:221
4: std::panicking::rust_panic_with_hook
at libstd/panicking.rs:477
5: std::panicking::continue_panic_fmt
at libstd/panicking.rs:391
6: rust_begin_unwind
at libstd/panicking.rs:326
7: core::panicking::panic_fmt
at libcore/panicking.rs:77
8: core::panicking::panic
at libcore/panicking.rs:52
9: chrono::offset::TimeZone::timestamp_millis_opt
at
/home/c/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-0.4.6/src/offset/mod.rs:349
10: chrono::offset::TimeZone::timestamp_millis
at
/home/c/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-0.4.6/src/offset/mod.rs:327
```