1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate*;
use crateFirestoreResult;
use *;
/// Converts a Google `prost_types::Timestamp` to a `chrono::DateTime<Utc>`.
///
/// Firestore uses Google's `Timestamp` protobuf message to represent timestamps.
/// This function facilitates conversion to the more commonly used `chrono::DateTime<Utc>`
/// in Rust applications.
///
/// # Arguments
/// * `ts`: The Google `Timestamp` to convert.
///
/// # Returns
/// A `FirestoreResult` containing the `DateTime<Utc>` on success, or a
/// `FirestoreError::DeserializeError` if the timestamp is invalid or out of range.
///
/// # Examples
/// ```rust
/// use firestore::timestamp_utils::from_timestamp;
/// use chrono::{Utc, TimeZone};
///
/// let prost_timestamp = gcloud_sdk::prost_types::Timestamp { seconds: 1670000000, nanos: 0 };
/// let chrono_datetime = from_timestamp(prost_timestamp).unwrap();
///
/// assert_eq!(chrono_datetime, Utc.with_ymd_and_hms(2022, 12, 2, 16, 53, 20).unwrap());
/// ```
/// Converts a `chrono::DateTime<Utc>` to a Google `prost_types::Timestamp`.
///
/// This is the reverse of [`from_timestamp`], used when sending timestamp data
/// to Firestore.
///
/// # Arguments
/// * `dt`: The `chrono::DateTime<Utc>` to convert.
///
/// # Returns
/// The corresponding Google `Timestamp`.
///
/// # Examples
/// ```rust
/// use firestore::timestamp_utils::to_timestamp;
/// use chrono::{Utc, TimeZone};
///
/// let chrono_datetime = Utc.with_ymd_and_hms(2022, 12, 2, 16, 53, 20).unwrap();
/// let prost_timestamp = to_timestamp(chrono_datetime);
///
/// assert_eq!(prost_timestamp.seconds, 1670000000);
/// assert_eq!(prost_timestamp.nanos, 0);
/// ```
/// Converts a Google `prost_types::Duration` to a `chrono::Duration`.
///
/// Google's `Duration` protobuf message is used in some Firestore contexts,
/// for example, in query execution statistics.
///
/// # Arguments
/// * `duration`: The Google `Duration` to convert.
///
/// # Returns
/// The corresponding `chrono::Duration`.
///
/// # Examples
/// ```rust
/// use firestore::timestamp_utils::from_duration;
///
/// let prost_duration = gcloud_sdk::prost_types::Duration { seconds: 5, nanos: 500_000_000 };
/// let chrono_duration = from_duration(prost_duration);
///
/// assert_eq!(chrono_duration, chrono::Duration::milliseconds(5500));
/// ```