use super::data::{
self, DATE_B, DATE_NAMES, DATE_OVER, DATE_OVER_KEYS, DATE_PATS, DATE_TIMES, Loc,
};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Timestamp(Option<i64>);
const MAX_MS: i64 = 8_640_000_000_000_000;
impl Timestamp {
#[must_use]
pub fn millis(self) -> Option<i64> {
self.0
}
#[must_use]
pub fn is_valid(self) -> bool {
self.0.is_some()
}
}
impl From<i64> for Timestamp {
fn from(ms: i64) -> Self {
Timestamp((-MAX_MS..=MAX_MS).contains(&ms).then_some(ms))
}
}
impl From<f64> for Timestamp {
#[allow(
clippy::cast_possible_truncation,
reason = "the range is checked first"
)]
fn from(ms: f64) -> Self {
if ms.abs() <= MAX_MS as f64 {
Timestamp(Some(ms.trunc() as i64))
} else {
Timestamp(None)
}
}
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl From<std::time::SystemTime> for Timestamp {
fn from(t: std::time::SystemTime) -> Self {
let ms = match t.duration_since(std::time::UNIX_EPOCH) {
Ok(d) => i64::try_from(d.as_millis()).ok(),
Err(e) => i64::try_from(e.duration().as_millis()).ok().map(|m| -m),
};
ms.map_or(Timestamp(None), Timestamp::from)
}
}
#[cfg(feature = "time")]
fn from_nanos(ns: i128) -> Timestamp {
i64::try_from(ns / 1_000_000).map_or(Timestamp(None), Timestamp::from)
}
#[cfg(feature = "time")]
impl From<time::OffsetDateTime> for Timestamp {
fn from(t: time::OffsetDateTime) -> Self {
from_nanos(t.unix_timestamp_nanos())
}
}
#[cfg(feature = "time")]
impl From<time::UtcDateTime> for Timestamp {
fn from(t: time::UtcDateTime) -> Self {
from_nanos(t.unix_timestamp_nanos())
}
}
#[cfg(feature = "time")]
impl From<time::PrimitiveDateTime> for Timestamp {
fn from(t: time::PrimitiveDateTime) -> Self {
Timestamp::from(t.assume_utc())
}
}
#[cfg(feature = "tz")]
pub(crate) use crate::tz::Zone;
#[cfg(feature = "tz")]
use crate::tz::{Civil, civil_from_days, civil_from_unix, days_from_civil};
#[cfg(not(feature = "tz"))]
#[derive(Debug, Clone)]
pub(crate) struct Zone;
#[cfg(not(feature = "tz"))]
impl Zone {
pub(crate) fn utc() -> Zone {
Zone
}
pub(crate) fn system() -> Zone {
Zone
}
#[allow(clippy::unused_self, reason = "the `tz` zone's signature")]
pub(crate) fn offset_at(&self, _: i64) -> i32 {
0
}
pub(crate) fn named(_: &str) -> Option<Zone> {
None
}
}
#[cfg(not(feature = "tz"))]
#[path = "../tz/civil.rs"]
mod civil;
#[cfg(not(feature = "tz"))]
use civil::{Civil, civil_from_days, civil_from_unix, days_from_civil};
pub(crate) fn zone_named(name: &str) -> Option<Zone> {
const UTC: [&str; 6] = ["UTC", "Etc/UTC", "GMT", "Etc/GMT", "Etc/UCT", "UCT"];
if UTC.iter().any(|u| u.eq_ignore_ascii_case(name)) {
return Some(Zone::utc());
}
Zone::named(name)
}
#[inline(never)]
fn week_year(c: &Civil) -> i64 {
let days = days_from_civil(c.year, c.month, c.day);
let monday_based = (i64::from(c.weekday) + 6) % 7;
civil_from_days(days - monday_based + 3).0
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Cycle {
Locale,
H11,
H12,
H23,
H24,
Hour12(bool),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Shape {
Components { date: u8, time: u8 },
Styles { date: u8, time: u8 },
}
pub(crate) const NO_STYLE: u8 = u8::MAX;
#[derive(Debug, Clone)]
pub(crate) struct DateOptions {
pub(crate) shape: Shape,
pub(crate) cycle: Cycle,
pub(crate) zone: Option<Zone>,
}
impl DateOptions {
pub(crate) const DEFAULT: DateOptions = DateOptions {
shape: Shape::Components {
date: DATE_NUMERIC,
time: 0,
},
cycle: Cycle::Locale,
zone: None,
};
}
const DATE_NUMERIC: u8 = 88;
const DATE_STYLES: usize = 216;
const GLUE: usize = 220;
const STYLE_GLUE: usize = 224;
const HOUR_TABLES: usize = 9;
const TIME_STYLES: usize = 99;
fn glue_width(date: u8) -> usize {
match date / 12 % 6 {
3 if !date.is_multiple_of(4) => 0,
3 => 1,
4 => 2,
_ => 3,
}
}
fn table(cycle: Cycle, loc: &Loc) -> usize {
let letter = match cycle {
Cycle::Locale => return 0,
Cycle::H11 => b'K',
Cycle::H12 => b'h',
Cycle::H23 => b'H',
Cycle::H24 => b'k',
Cycle::Hour12(true) => loc.hc12,
Cycle::Hour12(false) => loc.hc24,
};
match letter {
b'K' => 1,
b'h' => 2,
b'H' => 3,
_ => 4,
}
}
fn override_of(loc: &Loc, kind: u32, a: u8, b: u8, mode: usize) -> Option<&'static str> {
let key = (kind << 20) | (u32::from(a) << 12) | (u32::from(b) << 4) | mode as u32;
let start = loc.dover as usize;
let keys = DATE_OVER_KEYS.get(start..start + usize::from(loc.dover_len))?;
let i = keys.binary_search(&key).ok()?;
DATE_OVER.get(start + i).map(|&p| data::dpat(p))
}
struct Picked {
pattern: &'static str,
date: &'static str,
time: &'static str,
}
fn pick(o: &DateOptions, loc: &Loc) -> Picked {
let row = DATE_PATS.get(usize::from(loc.dpats));
let times = DATE_TIMES.get(usize::from(loc.dtimes));
let p = |slot: usize| data::dpat(row.and_then(|r| r.get(slot)).copied().unwrap_or(0));
let t = |slot: usize| data::dpat(times.and_then(|r| r.get(slot)).copied().unwrap_or(0));
let mode = table(o.cycle, loc);
let one = |pattern| Picked {
pattern,
date: "",
time: "",
};
match o.shape {
Shape::Components { date, time } => {
let time_pattern = || match time {
1..9 => t(usize::from(time)),
_ => t(HOUR_TABLES + mode * 18 + usize::from(time) - 9),
};
match (date, time) {
(_, 0) => one(p(usize::from(date))),
(0, _) => one(time_pattern()),
_ => {
let m = if time < 9 { 0 } else { mode };
match override_of(loc, 0, date, time, m) {
Some(full) => one(full),
None => Picked {
pattern: p(GLUE + glue_width(date)),
date: p(usize::from(date)),
time: time_pattern(),
},
}
}
}
}
Shape::Styles { date, time } => {
let time_pattern = || t(TIME_STYLES + mode * 2 + usize::from(time.saturating_sub(2)));
match (date, time) {
(_, NO_STYLE) => one(p(DATE_STYLES + usize::from(date))),
(NO_STYLE, _) => one(time_pattern()),
_ => match override_of(loc, 1, date, time, mode) {
Some(full) => one(full),
None => Picked {
pattern: p(STYLE_GLUE + usize::from(date)),
date: p(DATE_STYLES + usize::from(date)),
time: time_pattern(),
},
},
}
}
}
}
struct Cx {
names: &'static [u16],
digits: [&'static str; 10],
latin: bool,
loc: &'static Loc,
}
pub(crate) fn write(
t: Timestamp,
o: &DateOptions,
loc: &'static Loc,
zone: &Zone,
out: &mut dyn fmt::Write,
) -> fmt::Result {
let Some(ms) = t.0 else {
return out.write_str("Invalid Date");
};
let zone = o.zone.as_ref().unwrap_or(zone);
let secs = ms.div_euclid(1000);
let c = civil_from_unix(secs + i64::from(zone.offset_at(secs)));
let cx = Cx {
names: DATE_NAMES
.get(usize::from(loc.dnames))
.map_or(&[][..], |r| &r[..]),
digits: super::number::digit_table(loc),
latin: data::s(loc.digits).is_empty(),
loc,
};
let picked = pick(o, loc);
render(picked.pattern, &c, &cx, (picked.date, picked.time), out)
}
fn render(
p: &str,
c: &Civil,
cx: &Cx,
parts: (&str, &str),
out: &mut dyn fmt::Write,
) -> fmt::Result {
let b = p.as_bytes();
let mut from = 0;
let mut i = 0;
while i < b.len() {
if b.get(i) != Some(&1) {
i += 1;
continue;
}
if from < i {
out.write_str(p.get(from..i).unwrap_or(""))?;
}
let letter = b.get(i + 1).copied().unwrap_or(0);
let width = b.get(i + 2).map_or(1, |w| w.wrapping_sub(b'0'));
i += 3;
from = i;
match (letter, width) {
(b'{', 1) => render(parts.0, c, cx, ("", ""), out)?,
(b'{', _) => render(parts.1, c, cx, ("", ""), out)?,
_ => field(letter, width, p, c, cx, out)?,
}
}
match p.get(from..) {
Some(rest) if !rest.is_empty() => out.write_str(rest),
_ => Ok(()),
}
}
static PAIRS: &str = match std::str::from_utf8(&PAIR_BYTES) {
Ok(s) => s,
Err(_) => "",
};
const PAIR_BYTES: [u8; 200] = {
let mut a = [0u8; 200];
let mut i = 0;
while i < 100 {
a[2 * i] = b'0' + (i / 10) as u8;
a[2 * i + 1] = b'0' + (i % 10) as u8;
i += 1;
}
a
};
fn number(v: u64, width: u8, cx: &Cx, out: &mut dyn fmt::Write) -> fmt::Result {
if cx.latin && v < 10_000 && width <= 4 {
let v = v as usize;
let len = match v {
0..10 => 1,
10..100 => 2,
100..1000 => 3,
_ => 4,
}
.max(usize::from(width));
let pair = |n: usize| PAIRS.get(2 * n..2 * n + 2).unwrap_or("00");
let (hi, lo) = (pair(v / 100), pair(v % 100));
if len > 2 {
out.write_str(if len == 3 {
hi.get(1..).unwrap_or("")
} else {
hi
})?;
}
return out.write_str(if len == 1 {
lo.get(1..).unwrap_or("")
} else {
lo
});
}
let mut buf = [0u8; 20];
let mut n = v;
let mut len = 0;
while (n > 0 || len < usize::from(width.max(1))) && len < buf.len() {
if let Some(slot) = buf.get_mut(len) {
*slot = (n % 10) as u8;
}
n /= 10;
len += 1;
}
for d in buf.get(..len).unwrap_or(&[]).iter().rev() {
out.write_str(cx.digits.get(usize::from(*d)).copied().unwrap_or("0"))?;
}
Ok(())
}
fn name(cx: &Cx, set: usize, i: usize) -> &'static str {
let at = match set {
0..6 => set * 12 + i,
6..14 => 72 + (set - 6) * 7 + i,
_ => 128 + (set - 14) * 2 + i,
};
data::dname(cx.names.get(at).copied().unwrap_or(0))
}
fn width_index(width: u8) -> usize {
match width {
4 => 1,
5 => 2,
6 => 3,
_ => 0,
}
}
fn field(
letter: u8,
width: u8,
p: &str,
c: &Civil,
cx: &Cx,
out: &mut dyn fmt::Write,
) -> fmt::Result {
match letter {
b'y' => {
let y = if c.year > 0 { c.year } else { 1 - c.year }.unsigned_abs();
if width == 2 {
number(y % 100, 2, cx, out)
} else {
number(y, width, cx, out)
}
}
b'Y' => {
let y = week_year(c);
if y < 0 {
out.write_str(data::s(
data::PATTERNS
.get(usize::from(cx.loc.decimal_pat))
.map_or(0, |pat| pat.np),
))?;
}
let a = y.unsigned_abs();
number(if width == 2 { a % 100 } else { a }, width, cx, out)
}
b'M' | b'L' if width <= 2 => number(u64::from(c.month), width, cx, out),
b'M' | b'L' => {
let set = usize::from(letter == b'L') * 3 + width_index(width).min(2);
out.write_str(name(cx, set, usize::from(c.month).saturating_sub(1)))
}
b'r' => {
const ROMAN: [&str; 12] = [
"i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii",
];
out.write_str(
ROMAN
.get(usize::from(c.month).saturating_sub(1))
.copied()
.unwrap_or(""),
)
}
b'd' => number(u64::from(c.day), width, cx, out),
b'E' | b'c' => {
let set = 6 + usize::from(letter == b'c') * 4 + width_index(width);
out.write_str(name(cx, set, usize::from(c.weekday)))
}
b'h' => number(
u64::from(match c.hour % 12 {
0 => 12,
h => h,
}),
width,
cx,
out,
),
b'H' => number(u64::from(c.hour), width, cx, out),
b'K' => number(u64::from(c.hour % 12), width, cx, out),
b'k' => number(
u64::from(if c.hour == 0 { 24 } else { c.hour }),
width,
cx,
out,
),
b'm' => number(u64::from(c.minute), width, cx, out),
b's' => number(u64::from(c.second), width, cx, out),
b'a' => out.write_str(name(
cx,
14 + width_index(width).min(2),
usize::from(c.hour >= 12),
)),
b'B' => out.write_str(flexible(p, c, cx, width)),
b'G' => out.write_str(name(
cx,
17 + width_index(width).min(2),
usize::from(c.year > 0),
)),
_ => Ok(()),
}
}
fn flexible(p: &str, c: &Civil, cx: &Cx, width: u8) -> &'static str {
let Some(row) = DATE_B.get(usize::from(cx.loc.db)) else {
return name(cx, 14, usize::from(c.hour >= 12));
};
let base = width_index(width).min(2) * 25;
let shows = |l: u8| p.as_bytes().windows(2).any(|w| w == [1, l]);
let noon = row.get(base + 24).copied().unwrap_or(0);
let exact = (!shows(b'm') || c.minute == 0) && (!shows(b's') || c.second == 0);
let i = if c.hour == 12 && noon != 0 && exact {
noon
} else {
row.get(base + usize::from(c.hour)).copied().unwrap_or(0)
};
data::dname(i)
}