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
//! Defines the [`DateTimeUtc`] struct. A newtype to help when working with datetimes.
use std::ops::{Deref, DerefMut};
use std::time::UNIX_EPOCH;
use chrono::{DateTime, NaiveDateTime, Utc};
use serde::Serialize;
/// A Newtype wrapper around [`chrono`]'s [`DateTime<Utc>`] to allow for a
/// Default implementation.
///
/// Why do we need a Default implementation?
///
/// When a new template is added to the [`Templates`][templates] struct needs
/// to be validated both for its syntax and for the fields that its variables
/// reference. In order to achieve the latter, a dummy [`Entry`][entry] struct
/// ---its Default implementation---is passed to validate the template's
/// variables. Seeing as `DateTime` does not have a Default implementation, it
/// was either we implement a hand written Default of [`Entry`][entry] which
/// would include multiple nested structs or wrap [`DateTime<Utc>`] and provide
/// a Default implementation.
///
/// See [`Templates::validate_template()`][validate-template] for more
/// information.
///
/// [entry]: super::entry::Entry
/// [templates]: crate::templates::manager::Templates
/// [validate-template]: crate::templates::manager::Templates::validate_template()
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct DateTimeUtc(DateTime<Utc>);
impl Default for DateTimeUtc {
fn default() -> Self {
Self(DateTime::<Utc>::from(UNIX_EPOCH))
}
}
impl Deref for DateTimeUtc {
type Target = DateTime<Utc>;
fn deref(&self) -> &DateTime<Utc> {
&self.0
}
}
impl DerefMut for DateTimeUtc {
fn deref_mut(&mut self) -> &mut DateTime<Utc> {
&mut self.0
}
}
/// Converts a `Core Data` timestamp (f64) to `DateTime`.
///
/// A `Core Data` timestamp is the number of seconds (or nanoseconds) since
/// midnight, January 1, 2001, GMT. The difference between a `Core Data`
/// timestamp and a Unix timestamp (seconds since 1/1/1970) is 978307200
/// seconds.
///
/// <https://www.epochconverter.com/coredata>
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
impl From<f64> for DateTimeUtc {
fn from(f: f64) -> Self {
// Add the `Core Data` timestamp offset
let timestamp = f + 978_307_200_f64;
let seconds = timestamp.trunc() as i64;
let nanoseconds = timestamp.fract() * 1_000_000_000.0;
// Unwrap should be safe here as the timestamps are coming from the OS.
let datetime = NaiveDateTime::from_timestamp_opt(seconds, nanoseconds as u32).unwrap();
DateTimeUtc(DateTime::from_utc(datetime, Utc))
}
}