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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright Amazon.com, Inc. or its affiliates.

//! Provides integration between `ION_TIMESTAMP` and `chrono::DateTime`.
//!
//! Specifically, models the Ion notion of a Timestamp, with [`IonDateTime`](./struct.IonDateTime.html)
//! Which combines a `DateTime` with the concept of [*precision*](./enum.TSPrecision.html) and
//! [**known** versus **unknown** *offsets*](./enum.TSOffsetKind.html).

use crate::result::*;
use crate::*;

use self::Mantissa::*;
use self::TSOffsetKind::*;
use self::TSPrecision::*;

use bigdecimal::{BigDecimal, ToPrimitive};
use chrono::{DateTime, FixedOffset, Timelike};

pub(crate) const TS_MAX_MANTISSA_DIGITS: i64 = 9;

/// The fractional part of a `Fractional` [`TSPrecision`](./enum.TSPrecision.html).
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd)]
pub enum Mantissa {
    /// A kind of precision that uses digits from the nanoseconds field of the associated
    /// `DateTime` to represent the amount of mantissa.
    ///
    /// This is required for precision of nanoseconds or lower.
    Digits(u32),
    /// Specifies the mantissa precisely as a `BigDecimal` in the range `>= 0` and `< 1`.
    /// This should correspond to the nanoseconds field insofar as it is not truncated.
    ///
    /// This is only used for precision of greater than nanoseconds.
    Fraction(BigDecimal),
}

/// Precision of an [`IonDateTime`](./struct.IonDateTime.html).
///
/// All Ion timestamps are complete points in time, but they have explicit precision
/// that is either at the date components, the minute, or second (including sub-second)
/// granularity.
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd)]
pub enum TSPrecision {
    /// Year-level precision (e.g. `2020T`)
    Year,
    /// Month-level precision (e.g. `2020-08T`)
    Month,
    /// Day-level precision (e.g. `2020-08-01T`)
    Day,
    /// Minute-level precision (e.g. `2020-08-01T12:34Z`)
    Minute,
    /// Second-level precision. (e.g. `2020-08-01T12:34:56Z`)
    Second,
    /// Sub-second precision (e.g. `2020-08-01T12:34:56.123456789Z`)
    Fractional(Mantissa),
}

/// The kind of offset associated with a [`IonDateTime`](./struct.IonDateTime.html).
///
/// This is generally some specific `FixedOffset` associated with the `DateTime`,
/// but in the case of a timestamp with an *unknown UTC offset*, this will be `Unknown`,
/// and the effective `FixedOffset` will be UTC+00:00--this allows an application to
/// preserve the difference between UTC+00:00 (zulu time) and UTC-00:00 which is the unknown offset.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum TSOffsetKind {
    KnownOffset,
    UnknownOffset,
}

/// Higher-level wrapper over `DateTime` preserving `ION_TIMESTAMP` properties
/// that `DateTime` does not preserve on its own.
///
/// Specifically, this adds the [*precision*](./enum.TSPrecision.html) of the timestamp and
/// its associated [*kind of offset*](./enum.TSOffsetKind.html).
///
/// ## Usage
/// Generally, users will create their own `DateTime<FixedOffset>`
/// and construct an `IonDateTime` indicating the precision as follows:
/// ```
/// # use ion_c_sys::timestamp::*;
/// # use ion_c_sys::timestamp::TSPrecision::*;
/// # use ion_c_sys::timestamp::TSOffsetKind::*;
/// # use ion_c_sys::timestamp::Mantissa::*;
/// # use ion_c_sys::result::*;
/// # use chrono::*;
/// # fn main() -> IonCResult<()> {
/// // construct a DateTime with milliseconds of fractional seconds
/// use ion_c_sys::timestamp::Mantissa::Digits;
/// let dt = DateTime::parse_from_rfc3339("2020-02-27T04:15:00.123Z").unwrap();
/// // move that into an IonDateTime with the explicit milliseconds of precision
/// let ion_dt = IonDateTime::try_new(dt, Fractional(Digits(3)), KnownOffset)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct IonDateTime {
    date_time: DateTime<FixedOffset>,
    precision: TSPrecision,
    offset_kind: TSOffsetKind,
}

impl IonDateTime {
    /// Constructs a new `IonDateTime` directly without validating the `Fractional` precision.
    #[inline]
    pub(crate) fn new(
        date_time: DateTime<FixedOffset>,
        precision: TSPrecision,
        offset_kind: TSOffsetKind,
    ) -> Self {
        Self {
            date_time,
            precision,
            offset_kind,
        }
    }

    /// Constructs a new `IonDateTime` from its constituent components.
    ///
    /// Note that the `Fractional` precision must match the nanoseconds field of this
    /// will fail.  Also note that the `TSOffsetKind` must be `Unknown` for precision less than
    /// a `Minute` and must correspond to UTC+00:00 in the `DateTime`.
    #[inline]
    pub fn try_new(
        date_time: DateTime<FixedOffset>,
        precision: TSPrecision,
        offset_kind: TSOffsetKind,
    ) -> IonCResult<Self> {
        match offset_kind {
            KnownOffset => {
                if precision <= Day {
                    return Err(IonCError::with_additional(
                        ion_error_code_IERR_INVALID_TIMESTAMP,
                        "Day precision or less must not have KnownOffset",
                    ));
                }
            }
            UnknownOffset => {
                if date_time.offset().utc_minus_local() != 0 {
                    return Err(IonCError::with_additional(
                        ion_error_code_IERR_INVALID_TIMESTAMP,
                        "Mismatched offset with UnknownOffset",
                    ));
                }
            }
        };
        match &precision {
            Fractional(mantissa) => match mantissa {
                Digits(digits) => {
                    if (*digits as i64) > TS_MAX_MANTISSA_DIGITS {
                        return Err(IonCError::with_additional(
                            ion_error_code_IERR_INVALID_TIMESTAMP,
                            "Invalid digits in precision",
                        ));
                    }
                }
                Fraction(frac) => {
                    if frac < &BigDecimal::zero() || frac >= &BigDecimal::from(1) {
                        return Err(IonCError::with_additional(
                            ion_error_code_IERR_INVALID_TIMESTAMP,
                            "Mantissa outside of range",
                        ));
                    }
                    let (_, scale) = frac.as_bigint_and_exponent();
                    if scale <= TS_MAX_MANTISSA_DIGITS {
                        return Err(IonCError::with_additional(
                            ion_error_code_IERR_INVALID_TIMESTAMP,
                            "Fractional mantissa not allowed for sub-nanosecond precision"
                        ));
                    }
                    let ns = date_time.nanosecond();
                    let frac_ns = (frac * BigDecimal::from(NS_IN_SEC)).abs().to_u32().ok_or(
                        IonCError::with_additional(
                            ion_error_code_IERR_INVALID_TIMESTAMP,
                            "Invalid mantissa in precision",
                        ),
                    )?;
                    if ns != frac_ns {
                        return Err(IonCError::with_additional(
                            ion_error_code_IERR_INVALID_TIMESTAMP,
                            "Fractional mantissa inconsistent in precision",
                        ));
                    }
                }
            },
            _ => {}
        };

        Ok(Self::new(date_time, precision, offset_kind))
    }

    /// Returns a reference to the underlying `DateTime`.
    #[inline]
    pub fn as_datetime(&self) -> &DateTime<FixedOffset> {
        &(self.date_time)
    }

    /// Returns the precision of this `IonDateTime`.
    #[inline]
    pub fn precision(&self) -> &TSPrecision {
        &(self.precision)
    }

    /// Returns the offset of this `IonDateTime`.
    #[inline]
    pub fn offset_kind(&self) -> TSOffsetKind {
        self.offset_kind
    }

    /// Consumes the underlying components of this `IonDateTime` into a `DateTime`.
    #[inline]
    pub fn into_datetime(self) -> DateTime<FixedOffset> {
        self.date_time
    }
}

#[cfg(test)]
mod test_iondt {
    use super::*;

    use rstest::rstest;

    fn frac(lit: &str) -> Mantissa {
        Fraction(BigDecimal::parse_bytes(lit.as_bytes(), 10).unwrap())
    }

    #[rstest(
        dt_lit,
        precision,
        offset_kind,
        error,
        case::year("2020-01-01T00:01:00.1234567Z", Year, UnknownOffset, None),
        case::month("2020-01-01T00:01:00.1234567Z", Month, UnknownOffset, None),
        case::day("2020-01-01T00:01:00.1234567Z", Day, UnknownOffset, None),
        case::year_bad_known_offset(
            "2020-01-01T00:01:00.1234567Z",
            Year,
            KnownOffset,
            Some(ion_error_code_IERR_INVALID_TIMESTAMP),
        ),
        case::month_bad_known_offset(
            "2020-01-01T00:01:00.1234567Z",
            Month,
            KnownOffset,
            Some(ion_error_code_IERR_INVALID_TIMESTAMP),
        ),
        case::day_bad_known_offset(
            "2020-01-01T00:01:00.1234567Z",
            Day,
            KnownOffset,
            Some(ion_error_code_IERR_INVALID_TIMESTAMP),
        ),
        case::minute("2020-01-01T00:01:00.1234567Z", Minute, KnownOffset, None),
        case::second("2020-01-01T00:01:00.1234567Z", Second, KnownOffset, None),
        case::second_unknown_offset("2020-01-01T00:01:00.1234567Z", Second, UnknownOffset, None),
        case::second_bad_unknown_offset(
            "2020-01-01T00:01:00.1234567-00:15",
            Second,
            UnknownOffset,
            Some(ion_error_code_IERR_INVALID_TIMESTAMP),
        ),
        case::fractional_digits(
            "2020-01-01T00:01:00.1234567Z",
            Fractional(Digits(3)),
            KnownOffset,
            None,
        ),
        case::fractional_digits_too_big(
            "2020-01-01T00:01:00.1234567Z",
            Fractional(Digits(10)),
            KnownOffset,
            Some(ion_error_code_IERR_INVALID_TIMESTAMP),
        ),
        case::fractional_mantissa_neg(
            "2020-01-01T00:01:00.1234567Z",
            Fractional(frac("-0.1234567")),
            KnownOffset,
            Some(ion_error_code_IERR_INVALID_TIMESTAMP),
        ),
        case::fractional_mantissa_not_fractional(
            "2020-01-01T00:01:00.1234567Z",
            Fractional(frac("1.234567")),
            KnownOffset,
            Some(ion_error_code_IERR_INVALID_TIMESTAMP),
        ),
        case::fractional_mantissa_too_small(
            "2020-01-01T00:01:00.1234567Z",
            Fractional(frac("0.1234567")),
            KnownOffset,
            Some(ion_error_code_IERR_INVALID_TIMESTAMP),
        ),
        case::fractional_mantissa_more_precision(
            "2020-01-01T00:01:00.1234567Z",
            Fractional(frac("0.1234567001234567")),
            KnownOffset,
            None,
        ),
        case::fractional_mantissa_mismatch_digits(
            "2020-01-01T00:01:00.1234567Z",
            Fractional(frac("0.123456789")),
            KnownOffset,
            Some(ion_error_code_IERR_INVALID_TIMESTAMP),
        )
    )]
    fn try_new_precision(
        dt_lit: &str,
        precision: TSPrecision,
        offset_kind: TSOffsetKind,
        error: Option<i32>,
    ) -> IonCResult<()> {
        let dt = DateTime::parse_from_rfc3339(dt_lit).unwrap();
        let res = IonDateTime::try_new(dt, precision, offset_kind);
        match res {
            Ok(_) => {
                assert_eq!(None, error);
            }
            Err(actual) => {
                if let Some(expected_code) = error {
                    assert_eq!(expected_code, actual.code, "Testing expected error codes");
                } else {
                    assert!(false, "Expected no error, but got: {:?}", actual);
                }
            }
        }
        Ok(())
    }
}