use super::{Date, Timestamp, TimestampWithTimeZone};
use crate::datum::IntoDatum;
use crate::{direct_function_call, pg_sys};
pub fn now() -> TimestampWithTimeZone {
unsafe { pg_sys::GetCurrentTransactionStartTimestamp().try_into().unwrap() }
}
pub fn transaction_timestamp() -> TimestampWithTimeZone {
now()
}
pub fn statement_timestamp() -> TimestampWithTimeZone {
unsafe { pg_sys::GetCurrentStatementStartTimestamp().try_into().unwrap() }
}
pub fn clock_timestamp() -> TimestampWithTimeZone {
unsafe { pg_sys::GetCurrentTimestamp().try_into().unwrap() }
}
pub enum TimestampPrecision {
Full,
Rounded(i32),
}
impl From<TimestampPrecision> for i32 {
fn from(value: TimestampPrecision) -> Self {
match value {
TimestampPrecision::Full => -1,
TimestampPrecision::Rounded(p) => p,
}
}
}
pub fn current_date() -> Date {
current_timestamp(TimestampPrecision::Full).into()
}
pub fn current_time() -> Date {
current_timestamp(TimestampPrecision::Full).into()
}
pub fn current_timestamp(precision: TimestampPrecision) -> TimestampWithTimeZone {
unsafe { pg_sys::GetSQLCurrentTimestamp(precision.into()).try_into().unwrap() }
}
pub fn local_timestamp(precision: TimestampPrecision) -> Timestamp {
unsafe { pg_sys::GetSQLLocalTimestamp(precision.into()).try_into().unwrap() }
}
pub fn time_of_day() -> String {
unsafe { direct_function_call(pg_sys::timeofday, &[]).unwrap() }
}
pub fn to_timestamp(epoch_seconds: f64) -> TimestampWithTimeZone {
unsafe {
direct_function_call(pg_sys::float8_timestamptz, &[epoch_seconds.into_datum()]).unwrap()
}
}