|
| 1 | +#![allow(deprecated)] |
| 2 | + |
1 | 3 | use std::fmt;
|
2 | 4 |
|
3 | 5 | use serde::{Deserialize, Serialize};
|
4 | 6 | use time::format_description::well_known::Rfc3339;
|
5 | 7 | use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset};
|
6 | 8 |
|
7 |
| -/// DateTime Precision |
| 9 | +/// Precision with which datetimes are truncated when stored in fast fields. This setting is only |
| 10 | +/// relevant for fast fields. In the docstore, datetimes are always saved with nanosecond precision. |
8 | 11 | #[derive(
|
9 | 12 | Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default,
|
10 | 13 | )]
|
11 | 14 | #[serde(rename_all = "lowercase")]
|
12 |
| -pub enum DatePrecision { |
13 |
| - /// Seconds precision |
| 15 | +pub enum DateTimePrecision { |
| 16 | + /// Second precision. |
14 | 17 | #[default]
|
| 18 | + Second, |
| 19 | + /// Millisecond precision. |
| 20 | + Millisecond, |
| 21 | + /// Microsecond precision. |
| 22 | + Microsecond, |
| 23 | + /// Nanosecond precision. |
| 24 | + Nanosecond, |
| 25 | + // TODO: Remove deprecated variants after 2 releases. |
| 26 | + #[deprecated(since = "0.20.0", note = "Use `Second` instead")] |
15 | 27 | Seconds,
|
16 |
| - /// Milli-seconds precision. |
| 28 | + #[deprecated(since = "0.20.0", note = "Use `Millisecond` instead")] |
17 | 29 | Milliseconds,
|
18 |
| - /// Micro-seconds precision. |
| 30 | + #[deprecated(since = "0.20.0", note = "Use `Microsecond` instead")] |
19 | 31 | Microseconds,
|
20 |
| - /// Nano-seconds precision. |
| 32 | + #[deprecated(since = "0.20.0", note = "Use `Nanosecond` instead")] |
21 | 33 | Nanoseconds,
|
22 | 34 | }
|
23 | 35 |
|
| 36 | +#[deprecated(since = "0.20.0", note = "Use `DateTimePrecision` instead")] |
| 37 | +pub type DatePrecision = DateTimePrecision; |
| 38 | + |
24 | 39 | /// A date/time value with nanoseconds precision.
|
25 | 40 | ///
|
26 | 41 | /// This timestamp does not carry any explicit time zone information.
|
@@ -139,12 +154,18 @@ impl DateTime {
|
139 | 154 | }
|
140 | 155 |
|
141 | 156 | /// Truncates the microseconds value to the corresponding precision.
|
142 |
| - pub fn truncate(self, precision: DatePrecision) -> Self { |
| 157 | + pub fn truncate(self, precision: DateTimePrecision) -> Self { |
143 | 158 | let truncated_timestamp_micros = match precision {
|
144 |
| - DatePrecision::Seconds => (self.timestamp_nanos / 1_000_000_000) * 1_000_000_000, |
145 |
| - DatePrecision::Milliseconds => (self.timestamp_nanos / 1_000_000) * 1_000_000, |
146 |
| - DatePrecision::Microseconds => (self.timestamp_nanos / 1_000) * 1_000, |
147 |
| - DatePrecision::Nanoseconds => self.timestamp_nanos, |
| 159 | + DateTimePrecision::Second | DateTimePrecision::Seconds => { |
| 160 | + (self.timestamp_nanos / 1_000_000_000) * 1_000_000_000 |
| 161 | + } |
| 162 | + DateTimePrecision::Millisecond | DateTimePrecision::Milliseconds => { |
| 163 | + (self.timestamp_nanos / 1_000_000) * 1_000_000 |
| 164 | + } |
| 165 | + DateTimePrecision::Microsecond | DateTimePrecision::Microseconds => { |
| 166 | + (self.timestamp_nanos / 1_000) * 1_000 |
| 167 | + } |
| 168 | + DateTimePrecision::Nanosecond | DateTimePrecision::Nanoseconds => self.timestamp_nanos, |
148 | 169 | };
|
149 | 170 | Self {
|
150 | 171 | timestamp_nanos: truncated_timestamp_micros,
|
|
0 commit comments