surrealdb_core/sql/
datetime.rs

1use crate::err::Error;
2use crate::sql::duration::Duration;
3use crate::sql::strand::Strand;
4use crate::syn;
5use chrono::{offset::LocalResult, DateTime, SecondsFormat, TimeZone, Utc};
6use revision::revisioned;
7use serde::{Deserialize, Serialize};
8use std::fmt::{self, Display, Formatter};
9use std::ops;
10use std::ops::Deref;
11use std::str;
12use std::str::FromStr;
13
14use super::escape::quote_str;
15use super::value::TrySub;
16
17pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Datetime";
18
19#[revisioned(revision = 1)]
20#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
21#[serde(rename = "$surrealdb::private::sql::Datetime")]
22#[non_exhaustive]
23pub struct Datetime(pub DateTime<Utc>);
24
25impl Datetime {
26	pub const MIN_UTC: Self = Datetime(DateTime::<Utc>::MIN_UTC);
27	pub const MAX_UTC: Self = Datetime(DateTime::<Utc>::MAX_UTC);
28}
29
30impl Default for Datetime {
31	fn default() -> Self {
32		Self(Utc::now())
33	}
34}
35
36impl From<DateTime<Utc>> for Datetime {
37	fn from(v: DateTime<Utc>) -> Self {
38		Self(v)
39	}
40}
41
42impl From<Datetime> for DateTime<Utc> {
43	fn from(x: Datetime) -> Self {
44		x.0
45	}
46}
47
48impl FromStr for Datetime {
49	type Err = ();
50	fn from_str(s: &str) -> Result<Self, Self::Err> {
51		Self::try_from(s)
52	}
53}
54
55impl TryFrom<String> for Datetime {
56	type Error = ();
57	fn try_from(v: String) -> Result<Self, Self::Error> {
58		Self::try_from(v.as_str())
59	}
60}
61
62impl TryFrom<Strand> for Datetime {
63	type Error = ();
64	fn try_from(v: Strand) -> Result<Self, Self::Error> {
65		Self::try_from(v.as_str())
66	}
67}
68
69impl TryFrom<&str> for Datetime {
70	type Error = ();
71	fn try_from(v: &str) -> Result<Self, Self::Error> {
72		match syn::datetime(v) {
73			Ok(v) => Ok(v),
74			_ => Err(()),
75		}
76	}
77}
78
79impl TryFrom<(i64, u32)> for Datetime {
80	type Error = ();
81	fn try_from(v: (i64, u32)) -> Result<Self, Self::Error> {
82		match Utc.timestamp_opt(v.0, v.1) {
83			LocalResult::Single(v) => Ok(Self(v)),
84			_ => Err(()),
85		}
86	}
87}
88
89impl Deref for Datetime {
90	type Target = DateTime<Utc>;
91	fn deref(&self) -> &Self::Target {
92		&self.0
93	}
94}
95
96impl Datetime {
97	/// Convert the Datetime to a raw String
98	pub fn to_raw(&self) -> String {
99		self.0.to_rfc3339_opts(SecondsFormat::AutoSi, true)
100	}
101
102	/// Convert to nanosecond timestamp.
103	pub fn to_u64(&self) -> Option<u64> {
104		self.0.timestamp_nanos_opt().map(|v| v as u64)
105	}
106
107	/// Convert to nanosecond timestamp.
108	pub fn to_i64(&self) -> Option<i64> {
109		self.0.timestamp_nanos_opt()
110	}
111}
112
113impl Display for Datetime {
114	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
115		write!(f, "d{}", &quote_str(&self.to_raw()))
116	}
117}
118
119impl ops::Sub<Self> for Datetime {
120	type Output = Duration;
121	fn sub(self, other: Self) -> Duration {
122		match (self.0 - other.0).to_std() {
123			Ok(d) => Duration::from(d),
124			Err(_) => Duration::default(),
125		}
126	}
127}
128
129impl TrySub for Datetime {
130	type Output = Duration;
131	fn try_sub(self, other: Self) -> Result<Duration, Error> {
132		(self.0 - other.0)
133			.to_std()
134			.map_err(|_| Error::ArithmeticNegativeOverflow(format!("{self} - {other}")))
135			.map(Duration::from)
136	}
137}