use core::fmt;
use jiff::{civil, tz};
use crate::{CompositeFilterMarker, CompositeMarker, DefaultMarker, ScalarFilter, ToGraphqlInput};
use crate::graphql::filter::opt_accessor;
#[cfg(feature = "sqlx")]
mod comparison;
#[cfg(feature = "async-graphql")]
mod graphql;
mod storage;
pub use storage::{LocalTimestampLeaf, LocalTimestampPartsError};
#[cfg(test)]
mod tests;
#[cfg(feature = "sqlx")]
pub use comparison::SqlLocalTimestampComparison;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LocalTimestamp {
local: civil::DateTime,
offset: Option<tz::Offset>,
}
impl LocalTimestamp {
#[inline]
#[must_use]
pub const fn wall(local: civil::DateTime) -> Self {
Self {
local,
offset: None,
}
}
#[inline]
#[must_use]
pub const fn at(local: civil::DateTime, offset: tz::Offset) -> Self {
Self {
local,
offset: Some(offset),
}
}
#[inline]
#[must_use]
pub const fn new(local: civil::DateTime, offset: Option<tz::Offset>) -> Self {
Self { local, offset }
}
#[inline]
#[must_use]
pub const fn local(&self) -> civil::DateTime {
self.local
}
#[inline]
#[must_use]
pub const fn offset(&self) -> Option<tz::Offset> {
self.offset
}
#[inline]
#[must_use]
pub const fn is_zoned(&self) -> bool {
self.offset.is_some()
}
pub fn to_utc(&self) -> Result<jiff::Timestamp, NoUtcPosition> {
let offset = self.offset.ok_or(NoUtcPosition::Unzoned)?;
offset
.to_timestamp(self.local)
.map_err(|_| NoUtcPosition::OutOfRange)
}
pub fn to_utc_nanos(&self) -> Result<i64, NoUtcPosition> {
let nanos = self.to_utc()?.as_nanosecond();
i64::try_from(nanos).map_err(|_| NoUtcPosition::NanosecondOverflow { nanos })
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NoUtcPosition {
Unzoned,
OutOfRange,
NanosecondOverflow {
nanos: i128,
},
}
impl fmt::Display for NoUtcPosition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unzoned => f.write_str(
"this wall clock states no zone offset, so it names no instant — and one assumed from the \
reading machine's own zone would be a claim about where the value came from",
),
Self::OutOfRange => f.write_str(
"the wall clock and its offset compose into an instant outside the -9999..=9999 year range",
),
Self::NanosecondOverflow { nanos } => {
write!(f, "{nanos} nanoseconds does not fit an i64")
}
}
}
}
impl core::error::Error for NoUtcPosition {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct LocalTimestampOperand(LocalTimestamp);
impl LocalTimestampOperand {
#[inline]
#[must_use]
pub const fn new(timestamp: LocalTimestamp) -> Self {
Self(timestamp)
}
#[inline]
#[must_use]
pub const fn get(&self) -> LocalTimestamp {
self.0
}
}
impl From<LocalTimestamp> for LocalTimestampOperand {
#[inline]
fn from(timestamp: LocalTimestamp) -> Self {
Self(timestamp)
}
}
impl From<LocalTimestampOperand> for LocalTimestamp {
#[inline]
fn from(operand: LocalTimestampOperand) -> Self {
operand.0
}
}
impl ToGraphqlInput for LocalTimestamp {
type Input = LocalTimestampOperand;
type Error = core::convert::Infallible;
#[inline]
fn cross(input: Self::Input) -> Result<Self, Self::Error> {
Ok(input.into())
}
#[inline]
fn carry(operand: &Self) -> Self::Input {
LocalTimestampOperand::new(*operand)
}
}
impl CompositeFilterMarker for LocalTimestamp {
type Filter = LocalTimestampFilter;
type ColumnFilter = LocalTimestampFilter;
}
impl DefaultMarker for LocalTimestamp {
type Marker = CompositeMarker<LocalTimestamp>;
}
impl crate::DefaultVecMarker for LocalTimestamp {
type Marker = crate::QuantifiedMarker<Vec<LocalTimestamp>>;
}
#[derive(Debug, Clone, PartialEq, ingraph::Filter)]
#[ingraph(composed)]
pub struct LocalTimestampFilter {
exact: Option<LocalTimestamp>,
eq: Option<crate::Timestamp>,
ne: Option<crate::Timestamp>,
in_: Option<Box<[crate::Timestamp]>>,
not_in: Option<Box<[crate::Timestamp]>>,
gt: Option<crate::Timestamp>,
gte: Option<crate::Timestamp>,
lt: Option<crate::Timestamp>,
lte: Option<crate::Timestamp>,
local: Option<ScalarFilter<civil::DateTime>>,
offset: Option<ScalarFilter<tz::Offset>>,
zoned: Option<bool>,
}
impl LocalTimestampFilter {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
exact: None,
eq: None,
ne: None,
in_: None,
not_in: None,
gt: None,
gte: None,
lt: None,
lte: None,
local: None,
offset: None,
zoned: None,
}
}
#[inline]
#[must_use]
pub const fn is_empty(&self) -> bool {
self.exact.is_none()
&& self.eq.is_none()
&& self.ne.is_none()
&& self.in_.is_none()
&& self.not_in.is_none()
&& self.gt.is_none()
&& self.gte.is_none()
&& self.lt.is_none()
&& self.lte.is_none()
&& self.zoned.is_none()
&& match &self.local {
Some(local) => local.is_empty(),
None => true,
}
&& match &self.offset {
Some(offset) => offset.is_empty(),
None => true,
}
}
#[inline]
#[must_use]
pub const fn local(&self) -> Option<&ScalarFilter<civil::DateTime>> {
self.local.as_ref()
}
#[inline]
#[must_use]
pub const fn offset(&self) -> Option<&ScalarFilter<tz::Offset>> {
self.offset.as_ref()
}
opt_accessor!(exact: LocalTimestamp => with_exact, set_exact, maybe_exact, update_exact);
opt_accessor!(eq: crate::Timestamp => with_eq, set_eq, maybe_eq, update_eq);
opt_accessor!(ne: crate::Timestamp => with_ne, set_ne, maybe_ne, update_ne);
opt_accessor!(in_: [crate::Timestamp] => with_in_, set_in_, maybe_in_, update_in_);
opt_accessor!(not_in: [crate::Timestamp] => with_not_in, set_not_in, maybe_not_in, update_not_in);
opt_accessor!(gt: crate::Timestamp => with_gt, set_gt, maybe_gt, update_gt);
opt_accessor!(gte: crate::Timestamp => with_gte, set_gte, maybe_gte, update_gte);
opt_accessor!(lt: crate::Timestamp => with_lt, set_lt, maybe_lt, update_lt);
opt_accessor!(lte: crate::Timestamp => with_lte, set_lte, maybe_lte, update_lte);
opt_accessor!(zoned: bool => with_zoned, set_zoned, maybe_zoned, update_zoned);
#[must_use]
#[inline]
pub fn with_local(mut self, local: ScalarFilter<civil::DateTime>) -> Self {
self.set_local(local);
self
}
#[inline]
pub fn set_local(&mut self, local: ScalarFilter<civil::DateTime>) -> &mut Self {
self.local = Some(local);
self
}
#[must_use]
#[inline]
pub fn with_offset(mut self, offset: ScalarFilter<tz::Offset>) -> Self {
self.set_offset(offset);
self
}
#[inline]
pub fn set_offset(&mut self, offset: ScalarFilter<tz::Offset>) -> &mut Self {
self.offset = Some(offset);
self
}
}
impl Default for LocalTimestampFilter {
#[inline]
fn default() -> Self {
Self::new()
}
}