use std::fmt;
use std::str::FromStr;
use jiff::Timestamp;
use jiff::civil::{Date, DateTime, Time};
use jiff::tz::TimeZone;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueryTime {
At(Timestamp),
Local(DateTime),
Today(Time),
}
impl QueryTime {
pub fn in_zone(&self, zone: &TimeZone, now: Timestamp) -> Result<Timestamp, String> {
let datetime = match *self {
Self::At(timestamp) => return Ok(timestamp),
Self::Local(datetime) => datetime,
Self::Today(time) => today(zone, now).to_datetime(time),
};
datetime
.to_zoned_datetime(zone.clone())
.map(|zoned| zoned.timestamp())
.map_err(|error| format!("invalid time {datetime}: {error}"))
}
pub fn is_local(&self) -> bool {
!matches!(self, Self::At(_))
}
}
fn today(zone: &TimeZone, now: Timestamp) -> Date {
now.to_zoned_datetime(zone.clone()).date()
}
impl From<Timestamp> for QueryTime {
fn from(timestamp: Timestamp) -> Self {
Self::At(timestamp)
}
}
impl FromStr for QueryTime {
type Err = String;
fn from_str(input: &str) -> Result<Self, String> {
let input = input.trim();
if let Ok(timestamp) = input.parse::<Timestamp>() {
return Ok(Self::At(timestamp));
}
let has_date = input.contains(['-', 'T', 't', ' ']);
if has_date && let Ok(datetime) = input.parse::<DateTime>() {
return Ok(Self::Local(datetime));
}
if !has_date && let Ok(time) = input.parse::<Time>() {
return Ok(Self::Today(time));
}
Err(format!(
"invalid time {input:?}: use HH:MM for today, a local time like \
2026-11-27T07:50, or RFC 3339 like 2026-11-27T07:50:00+01:00"
))
}
}
impl fmt::Display for QueryTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::At(timestamp) => timestamp.fmt(f),
Self::Local(datetime) => datetime.fmt(f),
Self::Today(time) => time.fmt(f),
}
}
}
pub(crate) fn zone(name: Option<&str>) -> Option<TimeZone> {
TimeZone::get(name?).ok()
}
#[cfg(test)]
mod tests {
use super::*;
fn now() -> Timestamp {
"2026-11-27T08:15:00Z".parse().unwrap()
}
fn berlin() -> TimeZone {
TimeZone::get("Europe/Berlin").unwrap()
}
fn resolved(input: &str, zone: &TimeZone) -> String {
let time: QueryTime = input.parse().unwrap();
time.in_zone(zone, now()).unwrap().to_string()
}
#[test]
fn reads_local_times_in_the_zone() {
let berlin = berlin();
assert_eq!(resolved("07:50", &berlin), "2026-11-27T06:50:00Z");
assert_eq!(resolved("00:10", &berlin), "2026-11-26T23:10:00Z");
assert_eq!(resolved(" 23:59 ", &berlin), "2026-11-27T22:59:00Z");
assert_eq!(
resolved("2026-11-28T07:50", &berlin),
"2026-11-28T06:50:00Z"
);
assert_eq!(
resolved("2026-07-01T07:50", &berlin),
"2026-07-01T05:50:00Z"
);
assert_eq!(resolved("07:50", &TimeZone::UTC), "2026-11-27T07:50:00Z");
let tokyo = TimeZone::get("Asia/Tokyo").unwrap();
let late: Timestamp = "2026-11-27T20:00:00Z".parse().unwrap();
let time: QueryTime = "07:50".parse().unwrap();
assert_eq!(
time.in_zone(&tokyo, late).unwrap().to_string(),
"2026-11-27T22:50:00Z"
);
}
#[test]
fn keeps_instants() {
let berlin = berlin();
for input in ["2026-11-28T07:50:00+01:00", "2026-11-28T06:50:00Z"] {
let time: QueryTime = input.parse().unwrap();
assert!(!time.is_local());
assert_eq!(resolved(input, &berlin), "2026-11-28T06:50:00Z");
assert_eq!(resolved(input, &TimeZone::UTC), "2026-11-28T06:50:00Z");
}
let at: Timestamp = "2026-11-28T06:50:00Z".parse().unwrap();
assert_eq!(QueryTime::from(at), QueryTime::At(at));
assert_eq!(QueryTime::from(at).to_string(), "2026-11-28T06:50:00Z");
assert!("07:50".parse::<QueryTime>().unwrap().is_local());
assert_eq!(
"07:50".parse::<QueryTime>().unwrap().to_string(),
"07:50:00"
);
let local: QueryTime = "2026-11-28T07:50".parse().unwrap();
assert!(local.is_local());
assert_eq!(local.to_string(), "2026-11-28T07:50:00");
}
#[test]
fn rejects_other_input() {
for input in ["", "7.50", "25:00", "tomorrow", "2026-02-30T07:50"] {
let error = input.parse::<QueryTime>().unwrap_err();
assert!(error.starts_with("invalid time"), "{input}: {error}");
}
let time: QueryTime = "9999-12-31T23:59".parse().unwrap();
let error = time.in_zone(&berlin(), now()).unwrap_err();
assert!(
error.starts_with("invalid time 9999-12-31T23:59"),
"{error}"
);
}
#[test]
fn finds_zones_by_name() {
assert!(zone(Some("Europe/Berlin")).is_some());
assert!(zone(Some("Nowhere/Else")).is_none());
assert!(zone(None).is_none());
}
}