use metering::DayBoundary;
use metering::jiff;
use serde::Serialize;
use time::{Date, Duration, Month, OffsetDateTime, UtcOffset};
fn to_jiff_date(date: Date) -> Option<jiff::civil::Date> {
jiff::civil::Date::new(
i16::try_from(date.year()).ok()?,
i8::try_from(u8::from(date.month())).ok()?,
i8::try_from(date.day()).ok()?,
)
.ok()
}
fn from_jiff_date(date: jiff::civil::Date) -> Option<Date> {
let month = Month::try_from(u8::try_from(date.month()).ok()?).ok()?;
Date::from_calendar_date(
i32::from(date.year()),
month,
u8::try_from(date.day()).ok()?,
)
.ok()
}
fn to_timestamp(instant: OffsetDateTime) -> Option<jiff::Timestamp> {
jiff::Timestamp::from_nanosecond(instant.unix_timestamp_nanos()).ok()
}
fn from_timestamp(instant: jiff::Timestamp) -> Option<OffsetDateTime> {
OffsetDateTime::from_unix_timestamp_nanos(instant.as_nanosecond()).ok()
}
#[must_use]
pub fn to_berlin(instant: OffsetDateTime) -> OffsetDateTime {
to_timestamp(instant)
.map(metering::time::calendar::to_berlin)
.and_then(|zoned| {
let offset = UtcOffset::from_whole_seconds(zoned.offset().seconds()).ok()?;
Some(instant.to_offset(offset))
})
.unwrap_or_else(|| instant.to_offset(UtcOffset::UTC))
}
pub const QUARTER: Duration = Duration::minutes(15);
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum CalendarError {
#[error("{0} is outside the supported calendar years")]
Unsupported(Date),
#[error("{0} is not aligned to a quarter-hour")]
Misaligned(OffsetDateTime),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BusinessDay {
date: Date,
start: OffsetDateTime,
end: OffsetDateTime,
}
impl BusinessDay {
pub fn new(date: Date) -> Result<Self, CalendarError> {
let period = to_jiff_date(date)
.and_then(|d| DayBoundary::Strom.day(d))
.ok_or(CalendarError::Unsupported(date))?;
match (from_timestamp(period.start()), from_timestamp(period.end())) {
(Some(start), Some(end)) => Ok(Self { date, start, end }),
_ => Err(CalendarError::Unsupported(date)),
}
}
pub fn containing(instant: OffsetDateTime) -> Result<Self, CalendarError> {
let date = to_timestamp(instant)
.and_then(|t| DayBoundary::Strom.day_of(t))
.and_then(from_jiff_date)
.ok_or(CalendarError::Unsupported(instant.date()))?;
Self::new(date)
}
#[must_use]
pub const fn date(&self) -> Date {
self.date
}
#[must_use]
pub const fn start(&self) -> OffsetDateTime {
self.start
}
#[must_use]
pub const fn end(&self) -> OffsetDateTime {
self.end
}
#[must_use]
pub fn len(&self) -> u16 {
let quarters = (self.end() - self.start()).whole_minutes() / 15;
u16::try_from(quarters).unwrap_or(u16::MAX)
}
#[must_use]
pub fn is_empty(&self) -> bool {
false
}
pub fn quarters(&self) -> impl Iterator<Item = Quarter> + '_ {
(0..self.len()).map(move |index| Quarter {
start: self.start() + QUARTER * i32::from(index),
date: self.date,
index,
})
}
pub fn next(&self) -> Result<Self, CalendarError> {
Self::containing(self.end())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
pub struct Quarter {
#[serde(with = "time::serde::rfc3339")]
start: OffsetDateTime,
date: Date,
index: u16,
}
impl Quarter {
pub fn containing(instant: OffsetDateTime) -> Result<Self, CalendarError> {
let day = BusinessDay::containing(instant)?;
let offset = (instant - day.start()).whole_minutes() / 15;
let index = u16::try_from(offset).unwrap_or(0);
Ok(Self {
start: day.start() + QUARTER * i32::from(index),
date: day.date(),
index,
})
}
pub fn starting_at(start: OffsetDateTime) -> Result<Self, CalendarError> {
let quarter = Self::containing(start)?;
if quarter.start == start.to_offset(time::UtcOffset::UTC) {
Ok(quarter)
} else {
Err(CalendarError::Misaligned(start))
}
}
#[must_use]
pub const fn start(&self) -> OffsetDateTime {
self.start
}
#[must_use]
pub fn end(&self) -> OffsetDateTime {
self.start + QUARTER
}
#[must_use]
pub const fn date(&self) -> Date {
self.date
}
#[must_use]
pub const fn index(&self) -> u16 {
self.index
}
pub fn next(&self) -> Result<Self, CalendarError> {
Self::containing(self.end())
}
}