use elicitation::elicit_newtype;
use elicitation_derive::reflect_methods;
use tracing::instrument;
elicit_newtype!(jiff::Timestamp, as Timestamp, serde);
#[reflect_methods]
impl Timestamp {
#[instrument(skip(self))]
pub fn as_second(&self) -> i64 {
self.0.as_second()
}
#[instrument(skip(self))]
pub fn as_millisecond(&self) -> i64 {
self.0.as_millisecond()
}
#[instrument(skip(self))]
pub fn as_microsecond(&self) -> i64 {
self.0.as_microsecond()
}
#[instrument(skip(self))]
pub fn subsec_nanosecond(&self) -> i32 {
self.0.subsec_nanosecond()
}
#[instrument(skip(self))]
pub fn is_zero(&self) -> bool {
self.0.is_zero()
}
#[instrument(skip(self))]
pub fn signum(&self) -> i8 {
self.0.signum()
}
#[instrument(skip(self))]
pub fn in_tz(&self, name: String) -> Option<String> {
self.0.in_tz(&name).ok().map(|z| z.to_string())
}
}
impl Timestamp {
pub fn now() -> Self {
jiff::Timestamp::now().into()
}
pub fn from_second(second: i64) -> Option<Self> {
jiff::Timestamp::from_second(second)
.ok()
.map(|t| std::sync::Arc::new(t).into())
}
pub fn parse(s: &str) -> Option<Self> {
s.parse::<jiff::Timestamp>()
.ok()
.map(|t| std::sync::Arc::new(t).into())
}
}
mod emit_impls {
use super::Timestamp;
use elicitation::emit_code::ToCodeLiteral;
use proc_macro2::TokenStream;
impl ToCodeLiteral for Timestamp {
fn to_code_literal(&self) -> TokenStream {
let s = self.0.to_string();
quote::quote! {
::elicit_jiff::Timestamp::from(#s.parse::<::jiff::Timestamp>().expect("valid Timestamp"))
}
}
}
}
impl elicitation::ElicitComplete for Timestamp {}