use chrono::{DateTime, Datelike, FixedOffset, NaiveDate, NaiveDateTime, Timelike, Utc};
use crate::{HawDBError, Result, Value};
const MICROS_PER_SECOND: i64 = 1_000_000;
const POSTGRES_EPOCH_MICROS: i64 = 946_684_800 * MICROS_PER_SECOND;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TimestampPrecision(u8);
impl TimestampPrecision {
pub const MAX: u8 = 6;
pub const DEFAULT: Self = Self(Self::MAX);
pub const fn new(precision: u8) -> Option<Self> {
if precision <= Self::MAX {
Some(Self(precision))
} else {
None
}
}
pub const fn get(self) -> u8 {
self.0
}
const fn scale(self) -> i64 {
let mut scale = 1;
let mut digits = Self::MAX - self.0;
while digits > 0 {
scale *= 10;
digits -= 1;
}
scale
}
pub fn round(self, micros: i64) -> Result<i64> {
let scale = self.scale();
if scale == 1 {
return Ok(micros);
}
let relative = i128::from(micros) - i128::from(POSTGRES_EPOCH_MICROS);
let (scale, offset) = (i128::from(scale), i128::from(scale / 2));
let rounded = if relative >= 0 {
(relative + offset) / scale * scale
} else {
-((-relative + offset) / scale * scale)
};
i64::try_from(rounded + i128::from(POSTGRES_EPOCH_MICROS))
.ok()
.filter(|value| in_range(*value))
.ok_or_else(out_of_range)
}
pub fn accepts(self, micros: i64) -> bool {
in_range(micros) && self.round(micros).is_ok_and(|rounded| rounded == micros)
}
}
impl Default for TimestampPrecision {
fn default() -> Self {
Self::DEFAULT
}
}
pub fn min_micros() -> i64 {
NaiveDateTime::MIN.and_utc().timestamp_micros()
}
pub fn max_micros() -> i64 {
NaiveDateTime::MAX.and_utc().timestamp_micros()
}
pub fn in_range(micros: i64) -> bool {
(min_micros()..=max_micros()).contains(µs)
}
fn out_of_range() -> HawDBError {
HawDBError::Semantic("timestamp out of range".into())
}
pub fn checked(micros: i64) -> Result<i64> {
in_range(micros).then_some(micros).ok_or_else(out_of_range)
}
pub fn from_naive(value: NaiveDateTime) -> Result<i64> {
let utc = value.and_utc();
let micros = i128::from(utc.timestamp()) * i128::from(MICROS_PER_SECOND)
+ i128::from((utc.timestamp_subsec_nanos() + 500) / 1_000);
i64::try_from(micros)
.ok()
.filter(|value| in_range(*value))
.ok_or_else(out_of_range)
}
pub fn from_datetime(value: DateTime<FixedOffset>) -> Result<i64> {
from_naive(value.naive_utc())
}
pub fn to_naive(micros: i64) -> Result<NaiveDateTime> {
DateTime::<Utc>::from_timestamp_micros(checked(micros)?)
.map(|value| value.naive_utc())
.ok_or_else(out_of_range)
}
pub fn to_datetime(micros: i64) -> Result<DateTime<FixedOffset>> {
Ok(to_naive(micros)?.and_utc().fixed_offset())
}
pub fn local_value(micros: i64) -> Result<Value> {
to_naive(micros).map(Value::LocalDateTime)
}
pub fn zoned_value(micros: i64) -> Result<Value> {
to_datetime(micros).map(Value::DateTime)
}
pub fn parse_timestamp(input: &str) -> Result<i64> {
let (local, offset) = parse(input)?;
if offset.is_some() {
return Err(invalid(
input,
"TIMESTAMP values must not carry a time zone offset; use TIMESTAMPTZ",
));
}
from_naive(local)
}
pub fn parse_timestamptz(input: &str) -> Result<i64> {
let (local, offset) = parse(input)?;
let offset = offset.unwrap_or(0);
let utc = local
.checked_sub_signed(chrono::TimeDelta::seconds(i64::from(offset)))
.ok_or_else(out_of_range)?;
from_naive(utc)
}
pub fn format_timestamp(micros: i64) -> Result<String> {
to_naive(micros).map(|value| format_naive(&value))
}
pub fn format_timestamptz(micros: i64) -> Result<String> {
format_timestamp(micros).map(|text| text + "+00")
}
fn format_naive(value: &NaiveDateTime) -> String {
let year = value.year();
let mut text = if (0..=9999).contains(&year) {
format!("{year:04}")
} else {
format!("{year:+05}")
};
text.push_str(&format!(
"-{:02}-{:02} {:02}:{:02}:{:02}",
value.month(),
value.day(),
value.hour(),
value.minute(),
value.second()
));
let micros = value.nanosecond() / 1_000;
if micros != 0 {
let fraction = format!("{micros:06}");
text.push('.');
text.push_str(fraction.trim_end_matches('0'));
}
text
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TimestampType {
pub zoned: bool,
pub precision: TimestampPrecision,
}
impl TimestampType {
pub const fn name(self) -> &'static str {
if self.zoned {
"TIMESTAMPTZ"
} else {
"TIMESTAMP"
}
}
pub fn convert(self, value: &Value) -> Result<Option<i64>> {
let micros = match value {
Value::Null => return Ok(None),
Value::String(text) if self.zoned => parse_timestamptz(text)?,
Value::String(text) => parse_timestamp(text)?,
Value::LocalDateTime(value) => from_naive(*value)?,
Value::DateTime(value) => from_datetime(*value)?,
other => {
return Err(HawDBError::Semantic(format!(
"cannot convert {} to {}",
describe(other),
self.name()
)))
}
};
self.precision.round(micros).map(Some)
}
pub fn value(self, micros: i64) -> Result<Value> {
if self.zoned {
zoned_value(micros)
} else {
local_value(micros)
}
}
}
pub fn current_timestamp(kind: TimestampType) -> Result<Value> {
let micros = kind.precision.round(from_datetime(
crate::temporal::statement_now().fixed_offset(),
)?)?;
kind.value(micros)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TimestampCast {
Text,
Timestamp(TimestampType),
}
impl TimestampCast {
pub fn apply(self, value: &Value) -> Result<Value> {
match self {
Self::Timestamp(kind) => match kind.convert(value)? {
None => Ok(Value::Null),
Some(micros) => kind.value(micros),
},
Self::Text => match value {
Value::Null => Ok(Value::Null),
Value::String(text) => Ok(Value::String(text.clone())),
Value::LocalDateTime(value) => {
format_timestamp(from_naive(*value)?).map(Value::String)
}
Value::DateTime(value) => {
format_timestamptz(from_datetime(*value)?).map(Value::String)
}
other => Err(HawDBError::Semantic(format!(
"CAST to TEXT supports TEXT, TIMESTAMP and TIMESTAMPTZ operands, not {}",
describe(other)
))),
},
}
}
}
impl std::fmt::Display for TimestampCast {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Text => formatter.write_str("TEXT"),
Self::Timestamp(kind) => write!(formatter, "{}({})", kind.name(), kind.precision.get()),
}
}
}
fn describe(value: &Value) -> String {
value
.logical_type()
.map_or_else(|| "NULL".to_string(), |kind| format!("{kind:?}"))
}
fn invalid(input: &str, reason: &str) -> HawDBError {
HawDBError::Semantic(format!("invalid timestamp {input:?}: {reason}"))
}
struct Cursor<'a> {
input: &'a str,
text: &'a str,
bytes: &'a [u8],
position: usize,
}
impl Cursor<'_> {
fn peek(&self) -> Option<u8> {
self.bytes.get(self.position).copied()
}
fn eat(&mut self, byte: u8) -> bool {
if self.peek() == Some(byte) {
self.position += 1;
true
} else {
false
}
}
fn digits(&mut self, min: usize, max: usize) -> Result<(u64, usize)> {
let start = self.position;
while self.position - start < max && self.peek().is_some_and(|b| b.is_ascii_digit()) {
self.position += 1;
}
let count = self.position - start;
if count < min {
return Err(invalid(self.input, "expected digits"));
}
let value = self.text[start..self.position]
.parse::<u64>()
.map_err(|_| invalid(self.input, "field value out of range"))?;
Ok((value, count))
}
fn fixed(&mut self, width: usize) -> Result<u32> {
let (value, _) = self.digits(width, width)?;
u32::try_from(value).map_err(|_| invalid(self.input, "field value out of range"))
}
}
fn parse(input: &str) -> Result<(NaiveDateTime, Option<i32>)> {
let trimmed = input.trim_matches(|c: char| c.is_ascii_whitespace());
let mut cursor = Cursor {
input,
text: trimmed,
bytes: trimmed.as_bytes(),
position: 0,
};
let negative = if cursor.eat(b'-') {
true
} else {
cursor.eat(b'+');
false
};
let (year, _) = cursor.digits(4, 6)?;
let year = i32::try_from(year).map_err(|_| out_of_range())?;
let year = if negative { -year } else { year };
if !cursor.eat(b'-') {
return Err(invalid(input, "expected YYYY-MM-DD"));
}
let month = cursor.fixed(2)?;
if !cursor.eat(b'-') {
return Err(invalid(input, "expected YYYY-MM-DD"));
}
let day = cursor.fixed(2)?;
let date = NaiveDate::from_ymd_opt(year, month, day)
.ok_or_else(|| invalid(input, "date field value out of range"))?;
let (mut hour, mut minute, mut second, mut nanos) = (0, 0, 0, 0u32);
let mut round_up = false;
if cursor.eat(b' ') || cursor.eat(b'T') || cursor.eat(b't') {
hour = cursor.fixed(2)?;
if !cursor.eat(b':') {
return Err(invalid(input, "expected HH:MM"));
}
minute = cursor.fixed(2)?;
if cursor.eat(b':') {
second = cursor.fixed(2)?;
if cursor.eat(b'.') {
let start = cursor.position;
while cursor.peek().is_some_and(|b| b.is_ascii_digit()) {
cursor.position += 1;
}
let fraction = &trimmed[start..cursor.position];
if fraction.is_empty() {
return Err(invalid(input, "expected fractional seconds"));
}
let mut micros = 0u32;
for (index, digit) in fraction.bytes().enumerate() {
let digit = u32::from(digit - b'0');
match index {
0..=5 => micros = micros * 10 + digit,
6 => round_up = digit >= 5,
_ => {}
}
}
micros *= 10u32.pow(6u32.saturating_sub(fraction.len() as u32));
nanos = micros * 1_000;
}
}
}
let time = chrono::NaiveTime::from_hms_nano_opt(hour, minute, second, nanos)
.ok_or_else(|| invalid(input, "time field value out of range"))?;
let mut local = NaiveDateTime::new(date, time);
if round_up {
local = local
.checked_add_signed(chrono::TimeDelta::microseconds(1))
.ok_or_else(out_of_range)?;
}
let _ = cursor.eat(b' ');
let offset = match cursor.peek() {
None => None,
Some(b'Z' | b'z') => {
cursor.position += 1;
Some(0)
}
Some(sign @ (b'+' | b'-')) => {
cursor.position += 1;
let hours = cursor.fixed(2)?;
let colon = cursor.eat(b':');
let mut minutes = 0;
let mut seconds = 0;
if colon || cursor.peek().is_some_and(|b| b.is_ascii_digit()) {
minutes = cursor.fixed(2)?;
if colon && cursor.eat(b':') {
seconds = cursor.fixed(2)?;
}
}
if hours > 15 || minutes > 59 || seconds > 59 {
return Err(invalid(input, "time zone offset out of range"));
}
let total =
i32::try_from(hours * 3600 + minutes * 60 + seconds).map_err(|_| out_of_range())?;
Some(if sign == b'-' { -total } else { total })
}
Some(_) => return Err(invalid(input, "unexpected trailing text")),
};
if cursor.position != trimmed.len() {
return Err(invalid(input, "unexpected trailing text"));
}
Ok((local, offset))
}
#[cfg(test)]
mod tests;