use alloc::string::String;
use core::fmt::Write;
use super::{
MONTHS_PER_YEAR, PgDate, PgTime, PgTimeZone, PgTimestamp, PgTimestampTz, USECS_PER_SEC, date2j,
local_time_type, split_clock, timestamp2tm,
};
use crate::postgres::PgInterval;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
pub enum TemporalFormatError {
#[error("invalid format specification for an interval value")]
InvalidForInterval,
#[error("time zone format value too long")]
TimeZoneFormatValueTooLong,
#[error("date out of range for timestamp")]
DateOutOfRangeForTimestamp,
}
const MONTHS: [&str; 12] = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const DAYS: [&str; 7] = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const ROMAN_MONTHS: [&str; 12] = [
"XII", "XI", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I",
];
struct Fields<'a> {
year: i32,
month: i32,
day: i32,
hour: i64,
minute: i64,
second: i64,
microsecond: i64,
weekday: i32,
year_day: i32,
offset: i32,
zone: Option<&'a str>,
interval: bool,
}
impl Fields<'_> {
fn civil(micros: i64) -> Self {
let (year, month, day, clock) = timestamp2tm(micros);
let (hour, minute, second, microsecond) = split_clock(clock);
let julian = date2j(year, month, day);
Self {
year,
month,
day,
hour,
minute,
second,
microsecond,
weekday: (julian + 1) % 7,
year_day: julian - date2j(year, 1, 1) + 1,
offset: 0,
zone: None,
interval: false,
}
}
fn interval(interval: PgInterval) -> Self {
let (hour, minute, second, microsecond) = split_clock(interval.microseconds);
Self {
year: interval.months / MONTHS_PER_YEAR,
month: interval.months % MONTHS_PER_YEAR,
day: interval.days,
hour,
minute,
second,
microsecond,
weekday: 0,
year_day: interval.months.wrapping_mul(30).wrapping_add(interval.days),
offset: 0,
zone: None,
interval: true,
}
}
const fn era_year(&self, year: i32) -> i32 {
if self.interval || year > 0 {
year
} else {
1 - year
}
}
}
#[derive(Debug, Clone, Copy)]
enum Case {
Upper,
Capitalized,
Lower,
}
#[derive(Debug, Clone, Copy)]
enum Keyword {
Meridiem([&'static str; 2]),
Era([&'static str; 2]),
Hour12,
Hour24,
Minute,
Second,
Fraction(u8),
SecondsOfDay,
ZoneAbbreviation {
lower: bool,
},
ZoneHours,
ZoneMinutes,
UtcOffset,
MonthName(Case),
MonthAbbreviation(Case),
Month,
DayName(Case),
DayAbbreviation(Case),
DayOfYear,
IsoDayOfYear,
DayOfMonth,
DayOfWeek,
IsoDayOfWeek,
WeekOfYear,
IsoWeek,
Quarter,
Century,
YearWithComma,
Year {
digits: usize,
},
IsoYear {
digits: usize,
},
RomanMonth {
upper: bool,
},
WeekOfMonth,
Julian,
FixedFormat,
}
const KEYWORDS: &[(&str, Keyword)] = {
use Case::{Capitalized, Lower, Upper};
use Keyword::*;
&[
("A.D.", Era(["A.D.", "B.C."])),
("A.M.", Meridiem(["A.M.", "P.M."])),
("AD", Era(["AD", "BC"])),
("AM", Meridiem(["AM", "PM"])),
("B.C.", Era(["A.D.", "B.C."])),
("BC", Era(["AD", "BC"])),
("CC", Century),
("DAY", DayName(Upper)),
("DDD", DayOfYear),
("DD", DayOfMonth),
("DY", DayAbbreviation(Upper)),
("Day", DayName(Capitalized)),
("Dy", DayAbbreviation(Capitalized)),
("D", DayOfWeek),
("FF1", Fraction(1)),
("FF2", Fraction(2)),
("FF3", Fraction(3)),
("FF4", Fraction(4)),
("FF5", Fraction(5)),
("FF6", Fraction(6)),
("FX", FixedFormat),
("HH24", Hour24),
("HH12", Hour12),
("HH", Hour12),
("IDDD", IsoDayOfYear),
("ID", IsoDayOfWeek),
("IW", IsoWeek),
("IYYY", IsoYear { digits: 4 }),
("IYY", IsoYear { digits: 3 }),
("IY", IsoYear { digits: 2 }),
("I", IsoYear { digits: 1 }),
("J", Julian),
("MI", Minute),
("MM", Month),
("MONTH", MonthName(Upper)),
("MON", MonthAbbreviation(Upper)),
("MS", Fraction(3)),
("Month", MonthName(Capitalized)),
("Mon", MonthAbbreviation(Capitalized)),
("OF", UtcOffset),
("P.M.", Meridiem(["A.M.", "P.M."])),
("PM", Meridiem(["AM", "PM"])),
("Q", Quarter),
("RM", RomanMonth { upper: true }),
("SSSSS", SecondsOfDay),
("SSSS", SecondsOfDay),
("SS", Second),
("TZH", ZoneHours),
("TZM", ZoneMinutes),
("TZ", ZoneAbbreviation { lower: false }),
("US", Fraction(6)),
("WW", WeekOfYear),
("W", WeekOfMonth),
("Y,YYY", YearWithComma),
("YYYY", Year { digits: 4 }),
("YYY", Year { digits: 3 }),
("YY", Year { digits: 2 }),
("Y", Year { digits: 1 }),
("a.d.", Era(["a.d.", "b.c."])),
("a.m.", Meridiem(["a.m.", "p.m."])),
("ad", Era(["ad", "bc"])),
("am", Meridiem(["am", "pm"])),
("b.c.", Era(["a.d.", "b.c."])),
("bc", Era(["ad", "bc"])),
("cc", Century),
("day", DayName(Lower)),
("ddd", DayOfYear),
("dd", DayOfMonth),
("dy", DayAbbreviation(Lower)),
("d", DayOfWeek),
("ff1", Fraction(1)),
("ff2", Fraction(2)),
("ff3", Fraction(3)),
("ff4", Fraction(4)),
("ff5", Fraction(5)),
("ff6", Fraction(6)),
("fx", FixedFormat),
("hh24", Hour24),
("hh12", Hour12),
("hh", Hour12),
("iddd", IsoDayOfYear),
("id", IsoDayOfWeek),
("iw", IsoWeek),
("iyyy", IsoYear { digits: 4 }),
("iyy", IsoYear { digits: 3 }),
("iy", IsoYear { digits: 2 }),
("i", IsoYear { digits: 1 }),
("j", Julian),
("mi", Minute),
("mm", Month),
("month", MonthName(Lower)),
("mon", MonthAbbreviation(Lower)),
("ms", Fraction(3)),
("of", UtcOffset),
("p.m.", Meridiem(["a.m.", "p.m."])),
("pm", Meridiem(["am", "pm"])),
("q", Quarter),
("rm", RomanMonth { upper: false }),
("sssss", SecondsOfDay),
("ssss", SecondsOfDay),
("ss", Second),
("tzh", ZoneHours),
("tzm", ZoneMinutes),
("tz", ZoneAbbreviation { lower: true }),
("us", Fraction(6)),
("ww", WeekOfYear),
("w", WeekOfMonth),
("y,yyy", YearWithComma),
("yyyy", Year { digits: 4 }),
("yyy", Year { digits: 3 }),
("yy", Year { digits: 2 }),
("y", Year { digits: 1 }),
]
};
#[derive(Debug, Clone, Copy)]
enum Ordinal {
Upper,
Lower,
}
#[derive(Debug, Clone, Copy)]
struct Field {
keyword: Keyword,
fill: bool,
translate: bool,
ordinal: Option<Ordinal>,
}
#[derive(Debug, Clone, Copy)]
enum Node<'a> {
Text(&'a str),
Field(Field),
}
struct Nodes<'a> {
rest: &'a str,
quoted: bool,
}
impl<'a> Nodes<'a> {
const fn new(template: &'a str) -> Self {
Self {
rest: template,
quoted: false,
}
}
fn eat(&mut self, prefix: &str) -> bool {
match self.rest.strip_prefix(prefix) {
Some(rest) => {
self.rest = rest;
true
}
None => false,
}
}
fn character(&mut self, escaped: bool) -> &'a str {
if escaped {
self.rest = &self.rest[1..];
}
let width = self.rest.chars().next().map_or(0, char::len_utf8);
let (character, rest) = self.rest.split_at(width);
self.rest = rest;
character
}
}
impl<'a> Iterator for Nodes<'a> {
type Item = Node<'a>;
fn next(&mut self) -> Option<Node<'a>> {
loop {
if self.rest.is_empty() {
return None;
}
if self.quoted {
if self.eat("\"") {
self.quoted = false;
continue;
}
let escaped = self.rest.starts_with('\\') && self.rest.len() > 1;
return Some(Node::Text(self.character(escaped)));
}
let fill = self.eat("FM") || self.eat("fm");
let translate = !fill && (self.eat("TM") || self.eat("tm"));
if let Some(&(name, keyword)) = KEYWORDS
.iter()
.find(|(name, _)| self.rest.starts_with(name))
{
self.rest = &self.rest[name.len()..];
let ordinal = if self.eat("TH") {
Some(Ordinal::Upper)
} else if self.eat("th") {
Some(Ordinal::Lower)
} else {
self.eat("SP");
None
};
return Some(Node::Field(Field {
keyword,
fill,
translate,
ordinal,
}));
}
if self.rest.is_empty() {
return None;
}
if self.eat("\"") {
self.quoted = true;
continue;
}
let escaped = self.rest.starts_with("\\\"");
return Some(Node::Text(self.character(escaped)));
}
}
}
fn format(template: &str, fields: &Fields<'_>) -> Result<String, TemporalFormatError> {
let mut out = String::with_capacity(template.len());
for node in Nodes::new(template) {
match node {
Node::Text(text) => out.push_str(text),
Node::Field(field) => field.write(fields, &mut out)?,
}
}
Ok(out)
}
const fn j2day(julian: i32) -> i32 {
julian.wrapping_add(1).rem_euclid(7)
}
const fn iso_week_start(year: i32) -> i32 {
let day4 = date2j(year, 1, 4);
day4.wrapping_sub(j2day(day4.wrapping_sub(1)))
}
const fn date2isoweek(year: i32, month: i32, day: i32) -> i32 {
let julian = date2j(year, month, day);
let mut start = iso_week_start(year);
if julian < start {
start = iso_week_start(year.wrapping_sub(1));
}
let mut week = julian.wrapping_sub(start) / 7 + 1;
if week >= 52 {
let next = iso_week_start(year.wrapping_add(1));
if julian >= next {
week = julian.wrapping_sub(next) / 7 + 1;
}
}
week
}
const fn date2isoyear(year: i32, month: i32, day: i32) -> i32 {
let julian = date2j(year, month, day);
let mut year = year;
let mut start = iso_week_start(year);
if julian < start {
year = year.wrapping_sub(1);
start = iso_week_start(year);
}
if julian.wrapping_sub(start) / 7 + 1 >= 52 && julian >= iso_week_start(year.wrapping_add(1)) {
year = year.wrapping_add(1);
}
year
}
const fn signed_width(fill: bool, negative: bool, width: usize) -> usize {
if fill {
0
} else if negative {
width + 1
} else {
width
}
}
impl Field {
fn write(self, fields: &Fields<'_>, out: &mut String) -> Result<(), TemporalFormatError> {
let start = out.len();
let fill = self.fill;
let width = |width| if fill { 0 } else { width };
if fields.interval && self.keyword.needs_calendar_date() {
return Err(TemporalFormatError::InvalidForInterval);
}
match self.keyword {
Keyword::Meridiem([morning, afternoon]) => {
out.push_str(if fields.hour % 24 >= 12 {
afternoon
} else {
morning
});
}
Keyword::Era([common, before]) => {
out.push_str(if fields.year <= 0 { before } else { common });
}
Keyword::Hour12 => {
let hour = match fields.hour % 12 {
0 => 12,
hour => hour,
};
number(out, hour, signed_width(fill, fields.hour < 0, 2));
}
Keyword::Hour24 => number(out, fields.hour, signed_width(fill, fields.hour < 0, 2)),
Keyword::Minute => {
number(out, fields.minute, signed_width(fill, fields.minute < 0, 2));
}
Keyword::Second => {
number(out, fields.second, signed_width(fill, fields.second < 0, 2));
}
Keyword::Fraction(digits) => {
let divisor = 10_i64.pow(u32::from(6 - digits));
number(out, fields.microsecond / divisor, usize::from(digits));
}
Keyword::SecondsOfDay => {
number(
out,
fields.hour * 3_600 + fields.minute * 60 + fields.second,
0,
);
}
Keyword::ZoneAbbreviation { lower } => {
if let Some(zone) = fields.zone {
if zone.len() > 24 {
return Err(TemporalFormatError::TimeZoneFormatValueTooLong);
}
if lower {
out.extend(zone.chars().map(|c| c.to_ascii_lowercase()));
} else {
out.push_str(zone);
}
}
}
Keyword::ZoneHours => {
out.push(offset_sign(fields.offset));
number(out, (fields.offset.unsigned_abs() / 3_600).into(), 2);
}
Keyword::ZoneMinutes => {
number(out, (fields.offset.unsigned_abs() % 3_600 / 60).into(), 2);
}
Keyword::UtcOffset => {
let magnitude = fields.offset.unsigned_abs();
out.push(offset_sign(fields.offset));
number(out, (magnitude / 3_600).into(), width(2));
if !magnitude.is_multiple_of(3_600) {
out.push(':');
number(out, (magnitude % 3_600 / 60).into(), 2);
}
}
Keyword::MonthName(case) => {
self.name(out, MONTHS[month_index(fields.month)], case, 9);
}
Keyword::MonthAbbreviation(case) => {
self.name(out, &MONTHS[month_index(fields.month)][..3], case, 0);
}
Keyword::Month => {
let month = fields.month.into();
number(out, month, signed_width(fill, month < 0, 2));
}
Keyword::DayName(case) => self.name(out, DAYS[day_index(fields.weekday)], case, 9),
Keyword::DayAbbreviation(case) => {
self.name(out, &DAYS[day_index(fields.weekday)][..3], case, 0);
}
Keyword::DayOfYear => number(out, fields.year_day.into(), width(3)),
Keyword::IsoDayOfYear => {
let year = date2isoyear(fields.year, fields.month, fields.day);
let julian = date2j(fields.year, fields.month, fields.day);
let day = julian.wrapping_sub(iso_week_start(year)).wrapping_add(1);
number(out, day.into(), width(3));
}
Keyword::DayOfMonth => number(out, fields.day.into(), width(2)),
Keyword::DayOfWeek => number(out, (fields.weekday + 1).into(), 0),
Keyword::IsoDayOfWeek => {
let day = if fields.weekday == 0 {
7
} else {
fields.weekday
};
number(out, day.into(), 0);
}
Keyword::WeekOfYear => {
let week = fields.year_day.wrapping_sub(1) / 7 + 1;
number(out, week.into(), width(2));
}
Keyword::IsoWeek => {
let week = date2isoweek(fields.year, fields.month, fields.day);
number(out, week.into(), width(2));
}
Keyword::Quarter => {
if fields.month == 0 {
return Ok(());
}
number(out, ((fields.month - 1) / 3 + 1).into(), 0);
}
Keyword::Century => {
let century = if fields.interval {
fields.year / 100
} else if fields.year > 0 {
(fields.year - 1) / 100 + 1
} else {
fields.year / 100 - 1
};
let width = if (-99..=99).contains(¢ury) {
signed_width(fill, century < 0, 2)
} else {
0
};
number(out, century.into(), width);
}
Keyword::YearWithComma => {
let year = fields.era_year(fields.year);
let thousands = year / 1_000;
number(out, thousands.into(), 0);
out.push(',');
number(out, (year - thousands * 1_000).into(), 3);
}
Keyword::Year { digits } => {
self.year(out, fields, fields.era_year(fields.year), digits);
}
Keyword::IsoYear { digits } => {
let year = date2isoyear(fields.year, fields.month, fields.day);
self.year(out, fields, fields.era_year(year), digits);
}
Keyword::RomanMonth { upper } => {
if fields.month == 0 && fields.year == 0 {
return Ok(());
}
let index = match fields.month {
0 if fields.year >= 0 => 0,
0 => 11,
month if month < 0 => -(month + 1),
month => 12 - month,
};
let numeral = ROMAN_MONTHS[usize::try_from(index).expect("a month within a year")];
let padding = if fill { 0 } else { 4 };
let numeral_start = out.len();
if upper {
out.push_str(numeral);
} else {
out.extend(numeral.chars().map(|c| c.to_ascii_lowercase()));
}
pad(out, numeral_start, padding);
return Ok(());
}
Keyword::WeekOfMonth => {
number(out, (fields.day.wrapping_sub(1) / 7 + 1).into(), 0);
}
Keyword::Julian => {
number(out, date2j(fields.year, fields.month, fields.day).into(), 0);
}
Keyword::FixedFormat => return Ok(()),
}
if let Some(ordinal) = self.ordinal
&& self.keyword.is_number()
{
append_ordinal(out, start, ordinal);
}
Ok(())
}
fn name(self, out: &mut String, name: &str, case: Case, padding: usize) {
let start = out.len();
match case {
Case::Upper => out.extend(name.chars().map(|c| c.to_ascii_uppercase())),
Case::Capitalized => out.push_str(name),
Case::Lower => out.extend(name.chars().map(|c| c.to_ascii_lowercase())),
}
if !self.fill && !self.translate {
pad(out, start, padding);
}
}
fn year(self, out: &mut String, fields: &Fields<'_>, year: i32, digits: usize) {
let (value, width) = match digits {
4 => (year, 4),
3 => (year % 1_000, 3),
2 => (year % 100, 2),
_ => {
number(out, (year % 10).into(), 1);
return;
}
};
let negative = fields.era_year(fields.year) < 0;
number(out, value.into(), signed_width(self.fill, negative, width));
}
}
impl Keyword {
const fn needs_calendar_date(self) -> bool {
matches!(
self,
Self::Era(_)
| Self::ZoneAbbreviation { .. }
| Self::ZoneHours
| Self::ZoneMinutes
| Self::UtcOffset
| Self::MonthName(_)
| Self::MonthAbbreviation(_)
| Self::DayName(_)
| Self::DayAbbreviation(_)
| Self::DayOfWeek
| Self::IsoDayOfWeek
)
}
const fn is_number(self) -> bool {
!matches!(
self,
Self::Meridiem { .. }
| Self::Era { .. }
| Self::ZoneAbbreviation { .. }
| Self::ZoneHours
| Self::ZoneMinutes
| Self::UtcOffset
| Self::MonthName(_)
| Self::MonthAbbreviation(_)
| Self::DayName(_)
| Self::DayAbbreviation(_)
| Self::RomanMonth { .. }
| Self::FixedFormat
)
}
}
fn month_index(month: i32) -> usize {
usize::try_from(month - 1).expect("a calendar month from 1 to 12")
}
fn day_index(weekday: i32) -> usize {
usize::try_from(weekday.rem_euclid(7)).expect("a remainder of 7 fits usize")
}
const fn offset_sign(offset: i32) -> char {
if offset >= 0 { '+' } else { '-' }
}
fn number(out: &mut String, value: i64, width: usize) {
write!(out, "{value:0width$}").expect("a String takes any text");
}
fn pad(out: &mut String, start: usize, width: usize) {
let written = out[start..].chars().count();
out.extend(core::iter::repeat_n(' ', width.saturating_sub(written)));
}
fn append_ordinal(out: &mut String, start: usize, ordinal: Ordinal) {
let digits = &out.as_bytes()[start..];
let last = digits.last().copied();
let teen = digits.len() > 1 && digits[digits.len() - 2] == b'1';
let ending = match (last, teen) {
(Some(b'1'), false) => "st",
(Some(b'2'), false) => "nd",
(Some(b'3'), false) => "rd",
_ => "th",
};
match ordinal {
Ordinal::Upper => out.extend(ending.chars().map(|c| c.to_ascii_uppercase())),
Ordinal::Lower => out.push_str(ending),
}
}
impl PgTimestamp {
#[must_use]
pub fn to_char(self, template: &str) -> Option<String> {
if template.is_empty() || !self.is_finite() {
return None;
}
let fields = Fields::civil(self.pg_micros());
let text = format(template, &fields);
Some(text.expect("only an interval's fields or a zone abbreviation refuse a template"))
}
}
impl PgTimestampTz {
pub fn to_char(
self,
template: &str,
zone: &PgTimeZone,
) -> Result<Option<String>, TemporalFormatError> {
if template.is_empty() || !self.is_finite() {
return Ok(None);
}
let utc = self.pg_micros();
let (offset, abbreviation) = local_time_type(zone, utc);
let fields = Fields {
offset,
zone: Some(abbreviation.as_str()),
..Fields::civil(utc + i64::from(offset) * USECS_PER_SEC)
};
format(template, &fields).map(Some)
}
}
impl PgDate {
pub fn to_char(
self,
template: &str,
zone: &PgTimeZone,
) -> Result<Option<String>, TemporalFormatError> {
let out_of_range = TemporalFormatError::DateOutOfRangeForTimestamp;
let midnight = self.to_timestamp().map_err(|_| out_of_range)?;
let instant = midnight.to_timestamptz(zone).map_err(|_| out_of_range)?;
instant.to_char(template, zone)
}
}
impl PgInterval {
pub fn to_char(self, template: &str) -> Result<Option<String>, TemporalFormatError> {
if template.is_empty() || !self.is_finite() {
return Ok(None);
}
format(template, &Fields::interval(self)).map(Some)
}
}
impl PgTime {
pub fn to_char(self, template: &str) -> Result<Option<String>, TemporalFormatError> {
let interval = PgInterval {
months: 0,
days: 0,
microseconds: self.micros(),
};
interval.to_char(template)
}
}