use ::time::format_description::well_known::Rfc3339;
use ::time::format_description::FormatItem;
use ::time::macros::format_description;
use ::time::{OffsetDateTime, PrimitiveDateTime, UtcOffset};
use super::common::Sealed;
use super::DateBackend;
const DATE_FMT: &[FormatItem<'static>] = format_description!("[year]-[month]-[day]");
const TIME_FMT: &[FormatItem<'static>] =
format_description!("[hour]:[minute]:[second][optional [.[subsecond]]]");
const TIME_OUT_FMT: &[FormatItem<'static>] =
format_description!("[hour]:[minute]:[second].[subsecond]");
const NAIVE_FMT: &[FormatItem<'static>] =
format_description!("[year]-[month]-[day]T[hour]:[minute]:[second][optional [.[subsecond]]]");
#[derive(Debug, Clone, Copy)]
pub struct Time;
impl Sealed for Time {}
impl DateBackend for Time {
type Date = ::time::Date;
type DateTime = OffsetDateTime;
type Time = ::time::Time;
fn parse_date(s: &str) -> Option<Self::Date> {
::time::Date::parse(s, DATE_FMT).ok()
}
fn parse_time(s: &str) -> Option<Self::Time> {
::time::Time::parse(s, TIME_FMT).ok()
}
fn naive_to_utc(naive: &str, offset_seconds: i32) -> Option<Self::DateTime> {
let offset = UtcOffset::from_whole_seconds(offset_seconds).ok()?;
PrimitiveDateTime::parse(naive, NAIVE_FMT)
.ok()
.and_then(|dt| dt.assume_offset(offset).checked_to_offset(UtcOffset::UTC))
}
fn today() -> Self::Date {
OffsetDateTime::now_utc().date()
}
fn now() -> Self::DateTime {
OffsetDateTime::now_utc()
}
fn format_time(t: &Self::Time) -> String {
t.format(TIME_OUT_FMT).unwrap_or_else(|_| t.to_string())
}
fn format_datetime(dt: &Self::DateTime) -> String {
dt.format(&Rfc3339).unwrap_or_else(|_| dt.to_string())
}
}
pub type ZDate = super::ZDate<Time>;
pub type ZDateTime = super::ZDateTime<Time>;
pub fn date() -> ZDate {
ZDate::new()
}
pub fn datetime() -> ZDateTime {
ZDateTime::new()
}
pub type ZTime = super::ZTime<Time>;
pub fn time() -> ZTime {
ZTime::new()
}