Struct bson::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 [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>,
}

Implementations

The latest possible date that can be represented in BSON.

The earliest possible date that can be represented in BSON.

Makes a new DateTime from the number of non-leap milliseconds since January 1, 1970 0:00:00 UTC (aka “UNIX timestamp”).

Returns a DateTime which corresponds to the current date and time.

Convert the given [std::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::SystemTime].

Returns the number of non-leap-milliseconds since January 1, 1970 UTC.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.