use php_std::datetime::{DateTime, Interval, TimeZone, TzInfo};
use crate::error::{Error, Kind, Result};
pub const FORMAT_DB: &str = "Y-m-d H:i:s.v";
pub const FORMAT_TZ: &str = "Y-m-d\\TH:i:s.vP";
pub fn now() -> String {
DateTime::new(b"now", None, &TzInfo::utc()).map(|date| format(&date)).unwrap_or_default()
}
fn malformed(message: &str) -> Error {
Error::database(message).with_previous(Error::new(Kind::DateMalformedString, message))
}
pub fn now_after(previous: Option<&str>) -> Result<String> {
let current = now();
let Some(previous) = previous else { return Ok(current) };
let date = DateTime::new(current.as_bytes(), None, &TzInfo::utc()).map_err(|e| malformed(&e.message()))?;
let mut minimum = DateTime::new(previous.as_bytes(), None, &TzInfo::utc()).map_err(|e| malformed(&e.message()))?;
if date <= minimum {
minimum.modify(b"+1 millisecond").map_err(|e| malformed(&e.message()))?;
return Ok(format(&minimum));
}
Ok(current)
}
pub fn format(date: &DateTime) -> String {
text(date.format(FORMAT_DB.as_bytes()))
}
pub fn add_seconds(date: &mut DateTime, seconds: i64) -> Result<String> {
let interval = Interval::from_date_string(format!("{seconds} seconds").as_bytes())
.map_err(|e| Error::new(Kind::DateMalformedIntervalString, e.message()))?;
date.add(&interval);
Ok(format(date))
}
pub fn set_timezone(datetime: impl AsRef<[u8]>) -> Result<String> {
let mut value = DateTime::new(datetime.as_ref(), None, &TzInfo::utc()).map_err(|e| malformed(&e.message()))?;
value.set_timezone(&TimeZone::utc());
Ok(format(&value))
}
pub fn format_tz(value: Option<&str>) -> Option<String> {
let value = value?;
if let Some(formatted) = format_tz_stored(value) {
return Some(formatted);
}
Some(match DateTime::new(value.as_bytes(), None, &TzInfo::utc()) {
Ok(date) => text(date.format(FORMAT_TZ.as_bytes())),
Err(_) => value.to_owned(),
})
}
fn format_tz_stored(value: &str) -> Option<String> {
let b = value.as_bytes();
if b.len() < 19 || b[4] != b'-' || b[7] != b'-' || b[10] != b' ' || b[13] != b':' || b[16] != b':' {
return None;
}
let number = |range: std::ops::Range<usize>| -> Option<u32> {
b[range].iter().try_fold(0u32, |n, c| c.is_ascii_digit().then(|| n * 10 + u32::from(c - b'0')))
};
let (year, month, day) = (number(0..4)?, number(5..7)?, number(8..10)?);
let (hour, minute, second) = (number(11..13)?, number(14..16)?, number(17..19)?);
let leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
let days = match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 if leap => 29,
2 => 28,
_ => return None,
};
if year < 1000 || day == 0 || day > days || hour > 23 || minute > 59 || second > 59 {
return None;
}
let fraction = match &b[19..] {
[] => &[][..],
[b'.', digits @ ..] if (1..=6).contains(&digits.len()) && digits.iter().all(u8::is_ascii_digit) => digits,
_ => return None,
};
let mut out = String::with_capacity(29);
out.push_str(&value[..10]);
out.push('T');
out.push_str(&value[11..19]);
out.push('.');
for i in 0..3 {
out.push(char::from(*fraction.get(i).unwrap_or(&b'0')));
}
out.push_str("+00:00");
Some(out)
}
fn text(bytes: Vec<u8>) -> String {
String::from_utf8(bytes).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
}
#[cfg(test)]
mod tests {
use super::*;
fn parsed(value: &str) -> String {
match DateTime::new(value.as_bytes(), None, &TzInfo::utc()) {
Ok(date) => text(date.format(FORMAT_TZ.as_bytes())),
Err(_) => value.to_owned(),
}
}
#[test]
fn stored_dates_format_as_the_parser_does() {
let mut values: Vec<String> = [
"2024-01-02 01:04:05.678",
"2024-01-02 01:04:05.67",
"2024-01-02 01:04:05.6",
"2024-01-02 01:04:05",
"2024-01-02 01:04:05.000",
"2024-01-02 01:04:05.123456",
"2024-02-29 23:59:59.999",
"2023-02-29 00:00:00.000",
"2100-02-29 00:00:00.000",
"2000-02-29 00:00:00.000",
"2024-04-31 00:00:00.000",
"2024-13-01 00:00:00.000",
"2024-00-10 00:00:00.000",
"2024-01-00 00:00:00.000",
"2024-01-01 24:00:00.000",
"2024-01-01 23:60:00.000",
"2024-01-01 23:59:60.000",
"0999-01-01 00:00:00.000",
"2024-01-01 00:00:00.1234567",
"2024-01-01 00:00:00.",
"2024-01-01T00:00:00.000",
"2024-01-01 00:00:00.000+02:00",
"2024-01-01 00:00:00.000 ",
"not a date",
]
.map(str::to_owned)
.to_vec();
for year in [1970, 1999, 2000, 2024, 2025, 9999] {
for month in 1..=12 {
for day in [1, 28, 29, 30, 31] {
values.push(format!("{year:04}-{month:02}-{day:02} 13:07:59.042"));
}
}
}
for value in &values {
assert_eq!(format_tz(Some(value)).unwrap(), parsed(value), "{value}");
}
assert_eq!(format_tz_stored("2024-01-02 01:04:05.67").as_deref(), Some("2024-01-02T01:04:05.670+00:00"));
assert_eq!(format_tz_stored("2024-01-02T01:04:05.678"), None);
}
}