|
23 | 23 |
|
24 | 24 | use std::fmt::{self, Debug, Display}; |
25 | 25 |
|
26 | | -use chrono::{Datelike, NaiveDateTime, SecondsFormat, TimeZone, Utc}; |
| 26 | +use chrono::Datelike; |
27 | 27 | use serde_json::{json, Value}; |
28 | 28 |
|
29 | 29 | pub use crate::document::Document; |
@@ -285,9 +285,16 @@ impl From<oid::ObjectId> for Bson { |
285 | 285 | } |
286 | 286 | } |
287 | 287 |
|
288 | | -impl From<chrono::DateTime<Utc>> for Bson { |
289 | | - fn from(a: chrono::DateTime<Utc>) -> Bson { |
290 | | - Bson::DateTime(DateTime::from(a)) |
| 288 | +#[cfg(feature = "chrono-0_4")] |
| 289 | +impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for Bson { |
| 290 | + fn from(a: chrono::DateTime<T>) -> Bson { |
| 291 | + Bson::DateTime(crate::DateTime::from(a)) |
| 292 | + } |
| 293 | +} |
| 294 | + |
| 295 | +impl From<crate::DateTime> for Bson { |
| 296 | + fn from(dt: crate::DateTime) -> Self { |
| 297 | + Bson::DateTime(dt) |
291 | 298 | } |
292 | 299 | } |
293 | 300 |
|
@@ -370,15 +377,9 @@ impl Bson { |
370 | 377 | }) |
371 | 378 | } |
372 | 379 | Bson::ObjectId(v) => json!({"$oid": v.to_hex()}), |
373 | | - Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.0.year() <= 99999 => { |
374 | | - let seconds_format = if v.0.timestamp_subsec_millis() == 0 { |
375 | | - SecondsFormat::Secs |
376 | | - } else { |
377 | | - SecondsFormat::Millis |
378 | | - }; |
379 | | - |
| 380 | + Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_chrono().year() <= 99999 => { |
380 | 381 | json!({ |
381 | | - "$date": v.0.to_rfc3339_opts(seconds_format, true), |
| 382 | + "$date": v.to_rfc3339(), |
382 | 383 | }) |
383 | 384 | } |
384 | 385 | Bson::DateTime(v) => json!({ |
@@ -536,15 +537,9 @@ impl Bson { |
536 | 537 | "$oid": v.to_string(), |
537 | 538 | } |
538 | 539 | } |
539 | | - Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.0.year() <= 99999 => { |
540 | | - let seconds_format = if v.0.timestamp_subsec_millis() == 0 { |
541 | | - SecondsFormat::Secs |
542 | | - } else { |
543 | | - SecondsFormat::Millis |
544 | | - }; |
545 | | - |
| 540 | + Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_chrono().year() <= 99999 => { |
546 | 541 | doc! { |
547 | | - "$date": v.0.to_rfc3339_opts(seconds_format, true), |
| 542 | + "$date": v.to_rfc3339(), |
548 | 543 | } |
549 | 544 | } |
550 | 545 | Bson::DateTime(v) => doc! { |
@@ -735,12 +730,12 @@ impl Bson { |
735 | 730 |
|
736 | 731 | ["$date"] => { |
737 | 732 | if let Ok(date) = doc.get_i64("$date") { |
738 | | - return Bson::DateTime(DateTime::from_millis(date)); |
| 733 | + return Bson::DateTime(crate::DateTime::from_millis(date)); |
739 | 734 | } |
740 | 735 |
|
741 | 736 | if let Ok(date) = doc.get_str("$date") { |
742 | 737 | if let Ok(date) = chrono::DateTime::parse_from_rfc3339(date) { |
743 | | - return Bson::DateTime(date.with_timezone(&Utc).into()); |
| 738 | + return Bson::DateTime(crate::DateTime::from_chrono(date)); |
744 | 739 | } |
745 | 740 | } |
746 | 741 | } |
@@ -970,80 +965,6 @@ impl Timestamp { |
970 | 965 | } |
971 | 966 | } |
972 | 967 |
|
973 | | -/// Struct representing a BSON datetime. |
974 | | -/// |
975 | | -/// Is is recommended to use a [`chrono::DateTime`] for date operations |
976 | | -/// and to convert it to/from a [`crate::DateTime`] via the `From`/`Into` implementations. |
977 | | -/// |
978 | | -/// ``` |
979 | | -/// use chrono::prelude::*; |
980 | | -/// # fn main() -> std::result::Result<(), Box<dyn std::error::Error>> { |
981 | | -/// let chrono_dt: chrono::DateTime<Utc> = "2014-11-28T12:00:09Z".parse()?; |
982 | | -/// let bson_dt: bson::DateTime = chrono_dt.into(); |
983 | | -/// let back_to_chrono: chrono::DateTime<Utc> = bson_dt.into(); |
984 | | -/// # Ok(()) |
985 | | -/// # } |
986 | | -/// ``` |
987 | | -/// |
988 | | -/// This type differs from [`chrono::DateTime`] in that it serializes to and deserializes from a |
989 | | -/// BSON datetime rather than an ISO-8601 formatted string. This means that in non-BSON formats, it |
990 | | -/// will serialize to and deserialize from that format's equivalent of the [extended JSON representation](https://docs.mongodb.com/manual/reference/mongodb-extended-json/) of a datetime. To serialize a |
991 | | -/// [`chrono::DateTime`] as a BSON datetime, you can use |
992 | | -/// [`serde_helpers::chrono_0_4_datetime_as_bson_datetime`]. |
993 | | -/// |
994 | | -/// ```rust |
995 | | -/// use serde::{Serialize, Deserialize}; |
996 | | -/// |
997 | | -/// #[derive(Serialize, Deserialize)] |
998 | | -/// struct Foo { |
999 | | -/// // serializes as a BSON datetime. |
1000 | | -/// date_time: bson::DateTime, |
1001 | | -/// |
1002 | | -/// // serializes as an ISO-8601 string. |
1003 | | -/// chrono_datetime: chrono::DateTime<chrono::Utc>, |
1004 | | -/// |
1005 | | -/// // serializes as a BSON datetime. |
1006 | | -/// #[serde(with = "bson::serde_helpers::chrono_0_4_datetime_as_bson_datetime")] |
1007 | | -/// chrono_as_bson: chrono::DateTime<chrono::Utc>, |
1008 | | -/// } |
1009 | | -/// ``` |
1010 | | -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone)] |
1011 | | -pub struct DateTime(chrono::DateTime<Utc>); |
1012 | | - |
1013 | | -impl crate::DateTime { |
1014 | | - pub(crate) fn from_millis(date: i64) -> Self { |
1015 | | - Utc.timestamp_millis(date).into() |
1016 | | - } |
1017 | | - |
1018 | | - /// Returns the number of non-leap-milliseconds since January 1, 1970 UTC. |
1019 | | - pub fn timestamp_millis(&self) -> i64 { |
1020 | | - self.0.timestamp_millis() |
1021 | | - } |
1022 | | -} |
1023 | | - |
1024 | | -impl Display for crate::DateTime { |
1025 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
1026 | | - Display::fmt(&self.0, f) |
1027 | | - } |
1028 | | -} |
1029 | | - |
1030 | | -impl From<crate::DateTime> for chrono::DateTime<Utc> { |
1031 | | - fn from(utc: DateTime) -> Self { |
1032 | | - utc.0 |
1033 | | - } |
1034 | | -} |
1035 | | - |
1036 | | -impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for crate::DateTime { |
1037 | | - fn from(x: chrono::DateTime<T>) -> Self { |
1038 | | - let dt = x.with_timezone(&Utc); |
1039 | | - |
1040 | | - DateTime(chrono::DateTime::<Utc>::from_utc( |
1041 | | - NaiveDateTime::from_timestamp(dt.timestamp(), dt.timestamp_subsec_millis() * 1_000_000), |
1042 | | - Utc, |
1043 | | - )) |
1044 | | - } |
1045 | | -} |
1046 | | - |
1047 | 968 | /// Represents a BSON regular expression value. |
1048 | 969 | #[derive(Debug, Clone, PartialEq)] |
1049 | 970 | pub struct Regex { |
|
0 commit comments