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

Merge 0.4.x #1467

Merged
merged 13 commits into from
Feb 28, 2024
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ js-sys = { version = "0.3", optional = true } # contains FFI bindings for
windows-targets = { version = "0.52", optional = true }

[target.'cfg(windows)'.dev-dependencies]
windows-bindgen = { version = "0.52" }
windows-bindgen = { version = "0.53" } # The MSRV of its windows-metatada 0.53 dependency is 1.70

[target.'cfg(unix)'.dependencies]
iana-time-zone = { version = "0.1.45", optional = true, features = ["fallback"] }
Expand Down
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,31 @@ See [docs.rs](https://docs.rs/chrono/latest/chrono/) for the API reference.

Default features:

* `alloc`: Enable features that depend on allocation (primarily string formatting)
* `alloc`: Enable features that depend on allocation (primarily string formatting).
* `std`: Enables functionality that depends on the standard library. This is a superset of `alloc`
and adds interoperation with standard library types and traits.
* `clock`: Enables reading the local timezone (`Local`). This is a superset of `now`.
* `now`: Enables reading the system time (`now`)
* `now`: Enables reading the system time (`now`).
* `wasmbind`: Interface with the JS Date API for the `wasm32` target.

Optional features:

* `serde`: Enable serialization/deserialization via serde.
* `rkyv`: Enable serialization/deserialization via rkyv.
* `serde`: Enable serialization/deserialization via [serde].
* `rkyv`: Deprecated, use the `rkyv-*` features.
* `rkyv-16`: Enable serialization/deserialization via [rkyv], using 16-bit integers for integral `*size` types.
* `rkyv-32`: Enable serialization/deserialization via [rkyv], using 32-bit integers for integral `*size` types.
* `rkyv-64`: Enable serialization/deserialization via [rkyv], using 64-bit integers for integral `*size` types.
* `rkyv-validation`: Enable rkyv validation support using `bytecheck`.
* `rustc-serialize`: Enable serialization/deserialization via rustc-serialize (deprecated).
* `arbitrary`: construct arbitrary instances of a type with the Arbitrary crate.
* `arbitrary`: Construct arbitrary instances of a type with the Arbitrary crate.
* `unstable-locales`: Enable localization. This adds various methods with a `_localized` suffix.
The implementation and API may change or even be removed in a patch release. Feedback welcome.
* `oldtime`: This feature no longer has any effect; it used to offer compatibility with the `time` 0.1 crate.

Note: The `rkyv{,-16,-32,-64}` features are mutually exclusive.

[serde]: https://github.com/serde-rs/serde
[rkyv]: https://github.com/rkyv/rkyv

## Rust version requirements

Expand Down
71 changes: 28 additions & 43 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use super::DateTime;
use crate::format::{write_rfc3339, SecondsFormat};
use crate::naive::datetime::serde::serde_from;
#[cfg(feature = "clock")]
use crate::offset::Local;
use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
Expand Down Expand Up @@ -132,11 +131,9 @@
use core::fmt;
use serde::{de, ser};

use crate::offset::TimeZone;
use crate::serde::invalid_ts;
use crate::{DateTime, Utc};

use super::serde_from;

/// Serialize a UTC datetime into an integer number of nanoseconds since the epoch
///
/// Intended for use with `serde`s `serialize_with` attribute.
Expand Down Expand Up @@ -226,24 +223,20 @@
where
E: de::Error,
{
serde_from(
Utc.timestamp(
value.div_euclid(1_000_000_000),
(value.rem_euclid(1_000_000_000)) as u32,
),
&value,
DateTime::from_timestamp(
value.div_euclid(1_000_000_000),
(value.rem_euclid(1_000_000_000)) as u32,
)
.ok_or_else(|| invalid_ts(value))
}

/// Deserialize a timestamp in nanoseconds since the epoch
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
serde_from(
Utc.timestamp((value / 1_000_000_000) as i64, (value % 1_000_000_000) as u32),
&value,
)
DateTime::from_timestamp((value / 1_000_000_000) as i64, (value % 1_000_000_000) as u32)
.ok_or_else(|| invalid_ts(value))
}
}
}
Expand Down Expand Up @@ -434,8 +427,7 @@
use core::fmt;
use serde::{de, ser};

use super::serde_from;
use crate::offset::TimeZone;
use crate::serde::invalid_ts;
use crate::{DateTime, Utc};

/// Serialize a UTC datetime into an integer number of microseconds since the epoch
Expand Down Expand Up @@ -517,24 +509,23 @@
where
E: de::Error,
{
serde_from(
Utc.timestamp(
value.div_euclid(1_000_000),
(value.rem_euclid(1_000_000) * 1_000) as u32,
),
&value,
DateTime::from_timestamp(
value.div_euclid(1_000_000),
(value.rem_euclid(1_000_000) * 1000) as u32,
)
.ok_or_else(|| invalid_ts(value))
}

/// Deserialize a timestamp in milliseconds since the epoch
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
serde_from(
Utc.timestamp((value / 1_000_000) as i64, ((value % 1_000_000) * 1_000) as u32),
&value,
DateTime::from_timestamp(
(value / 1_000_000) as i64,
((value % 1_000_000) * 1_000) as u32,
)
.ok_or_else(|| invalid_ts(value))
}
}
}
Expand Down Expand Up @@ -714,8 +705,7 @@
use core::fmt;
use serde::{de, ser};

use super::serde_from;
use crate::offset::TimeZone;
use crate::serde::invalid_ts;
use crate::{DateTime, Utc};

/// Serialize a UTC datetime into an integer number of milliseconds since the epoch
Expand Down Expand Up @@ -797,18 +787,16 @@
where
E: de::Error,
{
serde_from(Utc.timestamp_millis(value), &value)
DateTime::from_timestamp_millis(value).ok_or_else(|| invalid_ts(value))
}

/// Deserialize a timestamp in milliseconds since the epoch
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
serde_from(
Utc.timestamp((value / 1000) as i64, ((value % 1000) * 1_000_000) as u32),
&value,
)
DateTime::from_timestamp((value / 1000) as i64, ((value % 1000) * 1_000_000) as u32)
.ok_or_else(|| invalid_ts(value))
}
}
}
Expand Down Expand Up @@ -995,8 +983,8 @@
use core::fmt;
use serde::{de, ser};

use super::serde_from;
use crate::{DateTime, LocalResult, TimeZone, Utc};
use crate::serde::invalid_ts;
use crate::{DateTime, Utc};

/// Serialize a UTC datetime into an integer number of seconds since the epoch
///
Expand Down Expand Up @@ -1067,22 +1055,19 @@
where
E: de::Error,
{
serde_from(Utc.timestamp(value, 0), &value)
DateTime::from_timestamp(value, 0).ok_or_else(|| invalid_ts(value))

Check warning on line 1058 in src/datetime/serde.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/serde.rs#L1058

Added line #L1058 was not covered by tests
}

/// Deserialize a timestamp in seconds since the epoch
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
serde_from(
if value > i64::MAX as u64 {
LocalResult::None
} else {
Utc.timestamp(value as i64, 0)
},
&value,
)
if value > i64::MAX as u64 {
Err(invalid_ts(value))

Check warning on line 1067 in src/datetime/serde.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/serde.rs#L1067

Added line #L1067 was not covered by tests
} else {
DateTime::from_timestamp(value as i64, 0).ok_or_else(|| invalid_ts(value))
}
}
}
}
Expand Down
Loading
Loading