Struct bson::datetime::DateTime [−][src]
pub struct DateTime(_);
Expand description
Struct representing a BSON datetime. Note: BSON datetimes have millisecond precision.
To enable conversions between this type and chrono::DateTime
, enable the "chrono-0_4"
feature flag in your Cargo.toml
.
use chrono::prelude::*;
let chrono_dt: chrono::DateTime<Utc> = "2014-11-28T12:00:09Z".parse()?;
let bson_dt: bson::DateTime = chrono_dt.into();
let bson_dt = bson::DateTime::from_chrono(chrono_dt);
let back_to_chrono: chrono::DateTime<Utc> = bson_dt.into();
let back_to_chrono = bson_dt.to_chrono();
This type differs from chrono::DateTime
in that it serializes to and deserializes from a
BSON datetime rather than an RFC 3339 formatted string. Additionally, in non-BSON formats, it
will serialize to and deserialize from that format’s equivalent of the
extended JSON representation of a datetime.
To serialize a chrono::DateTime
as a BSON datetime, you can use
crate::serde_helpers::chrono_datetime_as_bson_datetime
.
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct Foo {
// serializes as a BSON datetime.
date_time: bson::DateTime,
// serializes as an RFC 3339 / ISO-8601 string.
chrono_datetime: chrono::DateTime<chrono::Utc>,
// serializes as a BSON datetime.
// this requires the "chrono-0_4" feature flag
#[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
chrono_as_bson: chrono::DateTime<chrono::Utc>,
}
The serde_with
feature flag
The serde_with
feature can be enabled to support more ergonomic serde attributes for
(de)serializing chrono::DateTime
from/to BSON via the serde_with
crate. The main benefit of this compared to the regular serde_helpers
is that serde_with
can
handle nested chrono::DateTime
values (e.g. in Option
), whereas the former only works on
fields that are exactly chrono::DateTime
.
use serde::{Deserialize, Serialize};
use bson::doc;
#[serde_with::serde_as]
#[derive(Deserialize, Serialize, PartialEq, Debug)]
struct Foo {
/// Serializes as a BSON datetime rather than using `chrono::DateTime`'s serialization
#[serde_as(as = "Option<bson::DateTime>")]
as_bson: Option<chrono::DateTime<chrono::Utc>>,
}
let dt = chrono::Utc::now();
let foo = Foo {
as_bson: Some(dt),
};
let expected = doc! {
"as_bson": bson::DateTime::from_chrono(dt),
};
assert_eq!(bson::to_document(&foo)?, expected);
Implementations
Makes a new DateTime
from the number of non-leap milliseconds since
January 1, 1970 0:00:00 UTC (aka “UNIX timestamp”).
This is supported on crate feature chrono-0_4
only.
chrono-0_4
only.Convert the given chrono::DateTime
into a bson::DateTime
, truncating it to millisecond
precision.
This is supported on crate feature chrono-0_4
only.
chrono-0_4
only.Convert this DateTime
to a chrono::DateTime<Utc>
.
Note: Not every BSON datetime can be represented as a chrono::DateTime
. For such dates,
chrono::MIN_DATETIME
or chrono::MAX_DATETIME
will be returned, whichever is closer.
let bson_dt = bson::DateTime::now();
let chrono_dt = bson_dt.to_chrono();
assert_eq!(bson_dt.timestamp_millis(), chrono_dt.timestamp_millis());
let big = bson::DateTime::from_millis(i64::MAX);
let chrono_big = big.to_chrono();
assert_eq!(chrono_big, chrono::MAX_DATETIME)
Convert the given std::time::SystemTime
to a DateTime
.
If the provided time is too far in the future or too far in the past to be represented
by a BSON datetime, either DateTime::MAX
or DateTime::MIN
will be
returned, whichever is closer.
Convert this DateTime
to a std::time::SystemTime
.
Returns the number of non-leap-milliseconds since January 1, 1970 UTC.
Convert this DateTime
to an RFC 3339 formatted string.
Trait Implementations
Deserialize this value from the given Serde deserializer. Read more
impl<'de> DeserializeAs<'de, DateTime<Utc>> for DateTime
This is supported on crate features chrono-0_4
and serde_with
only.
impl<'de> DeserializeAs<'de, DateTime<Utc>> for DateTime
chrono-0_4
and serde_with
only.fn deserialize_as<D>(deserializer: D) -> Result<DateTime<Utc>, D::Error> where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<DateTime<Utc>, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
Performs the conversion.
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn serialize_as<S>(
source: &DateTime<Utc>,
serializer: S
) -> Result<S::Ok, S::Error> where
S: Serializer,
fn serialize_as<S>(
source: &DateTime<Utc>,
serializer: S
) -> Result<S::Ok, S::Error> where
S: Serializer,
Serialize this value into the given Serde serializer.
Auto Trait Implementations
impl RefUnwindSafe for DateTime
impl UnwindSafe for DateTime
Blanket Implementations
Mutably borrows from an owned value. Read more
Compare self to key
and return true
if they are equal.