Merge branch 'chronotope:main' into localdate

This commit is contained in:
Artyom Liu 2021-10-26 10:49:28 +08:00 committed by GitHub
commit 3d88d823ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 172 additions and 14 deletions

View File

@ -12,7 +12,7 @@ jobs:
test:
strategy:
matrix:
os: [ubuntu-16.04, ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-18.04, ubuntu-latest, macos-latest, windows-latest]
rust_version: [stable]
include:
# check all tzs on most-recent OS's
@ -30,7 +30,7 @@ jobs:
rust_version: beta
- os: ubuntu-latest
rust_version: nightly
- os: ubuntu-16.04
- os: ubuntu-18.04
rust_version: 1.13.0
- os: macos-latest
rust_version: 1.13.0

View File

@ -14,7 +14,7 @@ jobs:
test:
strategy:
matrix:
os: [ubuntu-16.04, ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-18.04, ubuntu-latest, macos-latest, windows-latest]
rust_version: [stable]
exhaustive_tz: [onetz]
check_combinatoric: [no_combinatoric]
@ -38,7 +38,7 @@ jobs:
rust_version: beta
- os: ubuntu-latest
rust_version: nightly
- os: ubuntu-16.04
- os: ubuntu-18.04
rust_version: 1.13.0
- os: macos-latest
rust_version: 1.13.0

View File

@ -1222,6 +1222,10 @@ pub mod serde {
#[derive(Debug)]
pub struct NanoSecondsTimestampVisitor;
#[doc(hidden)]
#[derive(Debug)]
pub struct MicroSecondsTimestampVisitor;
#[doc(hidden)]
#[derive(Debug)]
pub struct MilliSecondsTimestampVisitor;
@ -1439,7 +1443,7 @@ pub mod serde {
use offset::TimeZone;
use {DateTime, Utc};
use super::serde_from;
use super::{serde_from, MicroSecondsTimestampVisitor};
/// Serialize a UTC datetime into an integer number of microseconds since the epoch
///
@ -1512,12 +1516,9 @@ pub mod serde {
where
D: de::Deserializer<'de>,
{
#[allow(deprecated)]
Ok(try!(d.deserialize_i64(MicroSecondsTimestampVisitor)))
Ok(d.deserialize_i64(MicroSecondsTimestampVisitor)?)
}
struct MicroSecondsTimestampVisitor;
impl<'de> de::Visitor<'de> for MicroSecondsTimestampVisitor {
type Value = DateTime<Utc>;
@ -1552,6 +1553,162 @@ pub mod serde {
}
}
/// Ser/de to/from optional timestamps in microseconds
///
/// Intended for use with `serde`'s `with` attribute.
///
/// # Example:
///
/// ```rust
/// # // We mark this ignored so that we can test on 1.13 (which does not
/// # // support custom derive), and run tests with --ignored on beta and
/// # // nightly to actually trigger these.
/// #
/// # #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate serde_json;
/// # extern crate chrono;
/// # use chrono::{TimeZone, DateTime, Utc};
/// use chrono::serde::ts_microseconds_option;
/// #[derive(Deserialize, Serialize)]
/// struct S {
/// #[serde(with = "ts_microseconds_option")]
/// time: Option<DateTime<Utc>>
/// }
///
/// # fn example() -> Result<S, serde_json::Error> {
/// let time = Some(Utc.ymd(2018, 5, 17).and_hms_micro(02, 04, 59, 918355));
/// let my_s = S {
/// time: time.clone(),
/// };
///
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
/// let my_s: S = serde_json::from_str(&as_string)?;
/// assert_eq!(my_s.time, time);
/// # Ok(my_s)
/// # }
/// # fn main() { example().unwrap(); }
/// ```
pub mod ts_microseconds_option {
use core::fmt;
use serdelib::{de, ser};
use {DateTime, Utc};
use super::MicroSecondsTimestampVisitor;
/// Serialize a UTC datetime into an integer number of microseconds since the epoch or none
///
/// Intended for use with `serde`s `serialize_with` attribute.
///
/// # Example:
///
/// ```rust
/// # // We mark this ignored so that we can test on 1.13 (which does not
/// # // support custom derive), and run tests with --ignored on beta and
/// # // nightly to actually trigger these.
/// #
/// # #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate serde_json;
/// # extern crate chrono;
/// # use chrono::{TimeZone, DateTime, Utc};
/// use chrono::serde::ts_microseconds_option::serialize as to_micro_tsopt;
/// #[derive(Serialize)]
/// struct S {
/// #[serde(serialize_with = "to_micro_tsopt")]
/// time: Option<DateTime<Utc>>
/// }
///
/// # fn example() -> Result<String, serde_json::Error> {
/// let my_s = S {
/// time: Some(Utc.ymd(2018, 5, 17).and_hms_micro(02, 04, 59, 918355)),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
/// # Ok(as_string)
/// # }
/// # fn main() { example().unwrap(); }
/// ```
pub fn serialize<S>(opt: &Option<DateTime<Utc>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
match *opt {
Some(ref dt) => serializer.serialize_some(&dt.timestamp_micros()),
None => serializer.serialize_none(),
}
}
/// Deserialize a `DateTime` from a microsecond timestamp or none
///
/// Intended for use with `serde`s `deserialize_with` attribute.
///
/// # Example:
///
/// ```rust
/// # // We mark this ignored so that we can test on 1.13 (which does not
/// # // support custom derive), and run tests with --ignored on beta and
/// # // nightly to actually trigger these.
/// #
/// # #[macro_use] extern crate serde_derive;
/// # #[macro_use] extern crate serde_json;
/// # extern crate chrono;
/// # use chrono::{DateTime, Utc};
/// use chrono::serde::ts_microseconds_option::deserialize as from_micro_tsopt;
/// #[derive(Deserialize)]
/// struct S {
/// #[serde(deserialize_with = "from_micro_tsopt")]
/// time: Option<DateTime<Utc>>
/// }
///
/// # fn example() -> Result<S, serde_json::Error> {
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
/// # Ok(my_s)
/// # }
/// # fn main() { example().unwrap(); }
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<Option<DateTime<Utc>>, D::Error>
where
D: de::Deserializer<'de>,
{
Ok(d.deserialize_option(OptionMicroSecondsTimestampVisitor)?)
}
struct OptionMicroSecondsTimestampVisitor;
impl<'de> de::Visitor<'de> for OptionMicroSecondsTimestampVisitor {
type Value = Option<DateTime<Utc>>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a unix timestamp in microseconds or none")
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_some<D>(self, d: D) -> Result<Option<DateTime<Utc>>, D::Error>
where
D: de::Deserializer<'de>,
{
d.deserialize_i64(MicroSecondsTimestampVisitor).map(Some)
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_none<E>(self) -> Result<Option<DateTime<Utc>>, E>
where
E: de::Error,
{
Ok(None)
}
/// Deserialize a timestamp in seconds since the epoch
fn visit_unit<E>(self) -> Result<Option<DateTime<Utc>>, E>
where
E: de::Error,
{
Ok(None)
}
}
}
/// Ser/de to/from timestamps in nanoseconds
///
/// Intended for use with `serde`'s `with` attribute.

View File

@ -744,6 +744,9 @@ pub struct DelayedFormat<I> {
/// An iterator returning formatting items.
items: I,
/// Locale used for text.
// TODO: Only used with the locale feature. We should make this property
// only present when the feature is enabled.
#[allow(dead_code)]
locale: Option<Locale>,
}

View File

@ -1534,8 +1534,6 @@ where
F: Fn(&NaiveDateTime) -> Result<String, E>,
E: ::std::fmt::Debug,
{
use naive::{MAX_DATE, MIN_DATE};
assert_eq!(
to_string(&NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90)).ok(),
Some(r#""2016-07-08T09:10:48.090""#.into())
@ -1568,8 +1566,6 @@ where
F: Fn(&str) -> Result<NaiveDateTime, E>,
E: ::std::fmt::Debug,
{
use naive::{MAX_DATE, MIN_DATE};
assert_eq!(
from_str(r#""2016-07-08T09:10:48.090""#).ok(),
Some(NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90))
@ -2307,7 +2303,7 @@ mod tests {
result.map(|(y, m, d, h, n, s)| NaiveDate::from_ymd(y, m, d).and_hms(h, n, s));
assert_eq!(lhs.checked_add_signed(rhs), sum);
assert_eq!(lhs.checked_sub_signed(-rhs), sum);
};
}
check(
(2014, 5, 6, 7, 8, 9),

View File

@ -338,6 +338,7 @@ impl Of {
(weekord / 7, Weekday::from_u32(weekord % 7).unwrap())
}
#[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))]
#[inline]
pub fn to_mdf(&self) -> Mdf {
Mdf::from_of(*self)
@ -460,6 +461,7 @@ impl Mdf {
Mdf((mdf & !0b1111) | u32::from(flags))
}
#[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))]
#[inline]
pub fn to_of(&self) -> Of {
Of::from_mdf(*self)