time/serde/
iso8601.rs

1//! Use the well-known [ISO 8601 format] when serializing and deserializing an [`OffsetDateTime`].
2//!
3//! Use this module in combination with serde's [`#[with]`][with] attribute.
4//!
5//! [ISO 8601 format]: https://www.iso.org/iso-8601-date-and-time-format.html
6//! [with]: https://serde.rs/field-attrs.html#with
7
8#[cfg(feature = "parsing")]
9use core::marker::PhantomData;
10
11#[cfg(feature = "formatting")]
12use serde::ser::Error as _;
13#[cfg(feature = "parsing")]
14use serde::Deserializer;
15#[cfg(feature = "formatting")]
16use serde::{Serialize, Serializer};
17
18#[cfg(feature = "parsing")]
19use super::Visitor;
20use crate::format_description::well_known::iso8601::{Config, EncodedConfig};
21use crate::format_description::well_known::Iso8601;
22use crate::OffsetDateTime;
23
24/// The configuration of ISO 8601 used for serde implementations.
25pub(crate) const SERDE_CONFIG: EncodedConfig =
26    Config::DEFAULT.set_year_is_six_digits(true).encode();
27
28/// Serialize an [`OffsetDateTime`] using the well-known ISO 8601 format.
29#[cfg(feature = "formatting")]
30#[inline]
31pub fn serialize<S: Serializer>(
32    datetime: &OffsetDateTime,
33    serializer: S,
34) -> Result<S::Ok, S::Error> {
35    datetime
36        .format(&Iso8601::<SERDE_CONFIG>)
37        .map_err(S::Error::custom)?
38        .serialize(serializer)
39}
40
41/// Deserialize an [`OffsetDateTime`] from its ISO 8601 representation.
42#[cfg(feature = "parsing")]
43#[inline]
44pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> {
45    deserializer.deserialize_str(Visitor::<Iso8601<SERDE_CONFIG>>(PhantomData))
46}
47
48/// Use the well-known ISO 8601 format when serializing and deserializing an
49/// [`Option<OffsetDateTime>`].
50///
51/// Use this module in combination with serde's [`#[with]`][with] attribute.
52///
53/// [ISO 8601 format]: https://www.iso.org/iso-8601-date-and-time-format.html
54/// [with]: https://serde.rs/field-attrs.html#with
55pub mod option {
56    use super::*;
57
58    /// Serialize an [`Option<OffsetDateTime>`] using the well-known ISO 8601 format.
59    #[cfg(feature = "formatting")]
60    #[inline]
61    pub fn serialize<S: Serializer>(
62        option: &Option<OffsetDateTime>,
63        serializer: S,
64    ) -> Result<S::Ok, S::Error> {
65        option
66            .map(|odt| odt.format(&Iso8601::<SERDE_CONFIG>))
67            .transpose()
68            .map_err(S::Error::custom)?
69            .serialize(serializer)
70    }
71
72    /// Deserialize an [`Option<OffsetDateTime>`] from its ISO 8601 representation.
73    #[cfg(feature = "parsing")]
74    #[inline]
75    pub fn deserialize<'a, D: Deserializer<'a>>(
76        deserializer: D,
77    ) -> Result<Option<OffsetDateTime>, D::Error> {
78        deserializer.deserialize_option(Visitor::<Option<Iso8601<SERDE_CONFIG>>>(PhantomData))
79    }
80}