#![forbid(unsafe_code)]
pub mod format;
mod parse;
use std::fmt;
use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
use std::str::FromStr;
pub use jiff;
use jiff::civil;
use jiff::tz::TimeZone;
use jiff::{SignedDuration, Timestamp};
pub use parse::ParseError;
pub use jiff::civil::Date;
pub use jiff::civil::DateTime as CivilDateTime;
pub use jiff::civil::Time;
pub fn parse_date(s: &str, fmt: &str) -> Result<Date, ParseError> {
if s.starts_with(['+', '-']) {
return Err(ParseError::invalid());
}
Date::strptime(fmt, s).map_err(|_| ParseError::invalid())
}
pub fn parse_civil_datetime(s: &str, fmt: &str) -> Result<CivilDateTime, ParseError> {
if s.starts_with(['+', '-']) {
return Err(ParseError::invalid());
}
CivilDateTime::strptime(fmt, s).map_err(|_| ParseError::invalid())
}
#[must_use]
pub fn utc_from_civil(dt: CivilDateTime) -> Option<UtcDateTime> {
dt.to_zoned(TimeZone::UTC)
.ok()
.map(|z| UtcDateTime(z.timestamp()))
}
#[must_use]
pub fn utc_from_local_civil(dt: CivilDateTime) -> Option<UtcDateTime> {
use jiff::tz::AmbiguousOffset;
let offset = match TimeZone::system().to_ambiguous_timestamp(dt).offset() {
AmbiguousOffset::Unambiguous { offset } | AmbiguousOffset::Fold { before: offset, .. } => {
offset
}
AmbiguousOffset::Gap { .. } => return utc_from_civil(dt),
};
offset.to_timestamp(dt).ok().map(UtcDateTime)
}
#[must_use]
pub fn days_between(earlier: Date, later: Date) -> i64 {
i64::from((later - earlier).get_days())
}
macro_rules! civil_ops {
($t:ty) => {
impl Add<Duration> for $t {
type Output = $t;
fn add(self, d: Duration) -> $t {
self.checked_add(d.0).expect(concat!(
"`",
stringify!($t),
" + Duration` overflowed"
))
}
}
impl Sub<Duration> for $t {
type Output = $t;
fn sub(self, d: Duration) -> $t {
self.checked_sub(d.0).expect(concat!(
"`",
stringify!($t),
" - Duration` overflowed"
))
}
}
};
}
civil_ops!(Date);
civil_ops!(CivilDateTime);
const NANOS_PER_SEC: i128 = 1_000_000_000;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SecondsFormat {
Secs,
Millis,
Micros,
Nanos,
AutoSi,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OutOfRangeError;
impl fmt::Display for OutOfRangeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("out of range")
}
}
impl std::error::Error for OutOfRangeError {}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UtcDateTime(Timestamp);
impl Default for UtcDateTime {
fn default() -> Self {
Self::UNIX_EPOCH
}
}
impl UtcDateTime {
pub const UNIX_EPOCH: Self = Self(Timestamp::UNIX_EPOCH);
#[must_use]
pub fn now() -> Self {
Self(Timestamp::now())
}
#[must_use]
pub fn with_ymd_and_hms(
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
) -> Option<Self> {
let date = civil::Date::new(
i16::try_from(year).ok()?,
i8::try_from(month).ok()?,
i8::try_from(day).ok()?,
)
.ok()?;
let time = civil::Time::new(
i8::try_from(hour).ok()?,
i8::try_from(minute).ok()?,
i8::try_from(second).ok()?,
0,
)
.ok()?;
date.to_datetime(time)
.to_zoned(TimeZone::UTC)
.ok()
.map(|z| Self(z.timestamp()))
}
#[must_use]
pub const fn from_jiff(ts: Timestamp) -> Self {
Self(ts)
}
#[must_use]
pub const fn as_jiff(self) -> Timestamp {
self.0
}
#[must_use]
pub fn from_timestamp(secs: i64, nanos: u32) -> Option<Self> {
if nanos >= 1_000_000_000 {
return None;
}
Self::from_nanos_i128(i128::from(secs) * NANOS_PER_SEC + i128::from(nanos))
}
#[must_use]
pub fn from_timestamp_millis(millis: i64) -> Option<Self> {
Self::from_nanos_i128(i128::from(millis) * 1_000_000)
}
#[must_use]
pub fn from_timestamp_micros(micros: i64) -> Option<Self> {
Self::from_nanos_i128(i128::from(micros) * 1_000)
}
#[must_use]
pub fn from_timestamp_nanos(nanos: i64) -> Self {
Self::from_nanos_i128(i128::from(nanos)).expect("every i64 nanosecond count is in range")
}
fn from_nanos_i128(nanos: i128) -> Option<Self> {
Timestamp::from_nanosecond(nanos).ok().map(Self)
}
fn nanos_i128(self) -> i128 {
self.0.as_nanosecond()
}
#[must_use]
pub fn timestamp(self) -> i64 {
to_i64(self.nanos_i128().div_euclid(NANOS_PER_SEC))
}
#[must_use]
pub fn timestamp_millis(self) -> i64 {
to_i64(self.nanos_i128().div_euclid(1_000_000))
}
#[must_use]
pub fn timestamp_micros(self) -> i64 {
to_i64(self.nanos_i128().div_euclid(1_000))
}
#[must_use]
pub fn timestamp_nanos_opt(self) -> Option<i64> {
i64::try_from(self.nanos_i128()).ok()
}
#[must_use]
pub fn timestamp_subsec_nanos(self) -> u32 {
u32::try_from(self.nanos_i128().rem_euclid(NANOS_PER_SEC)).expect("below one second")
}
#[must_use]
pub fn timestamp_subsec_millis(self) -> u32 {
self.timestamp_subsec_nanos() / 1_000_000
}
#[must_use]
pub fn timestamp_subsec_micros(self) -> u32 {
self.timestamp_subsec_nanos() / 1_000
}
pub fn parse_from_rfc3339(s: &str) -> Result<Self, ParseError> {
parse::rfc3339(s, parse::Mode::Strict).map(|p| p.utc)
}
pub fn parse_from_str(s: &str, fmt: &str) -> Result<Self, ParseError> {
Timestamp::strptime(fmt, s)
.map(Self)
.map_err(|_| ParseError::invalid())
}
#[must_use]
pub fn to_rfc3339(self) -> String {
self.to_rfc3339_opts(SecondsFormat::AutoSi, false)
}
#[must_use]
pub fn to_rfc3339_opts(self, secform: SecondsFormat, use_z: bool) -> String {
let mut out = String::with_capacity(32);
let parts = format::Parts::utc(self);
format::write_rfc3339(
&mut out,
&parts,
secform,
if use_z { "Z" } else { "+00:00" },
);
out
}
#[must_use]
pub fn format(self, fmt: &str) -> format::Formatted<'_> {
format::Formatted::new(format::Parts::utc(self), fmt)
}
#[must_use]
pub fn date_naive(self) -> civil::Date {
self.civil().date()
}
#[must_use]
pub fn naive_utc(self) -> civil::DateTime {
self.civil()
}
fn civil(self) -> civil::DateTime {
TimeZone::UTC.to_datetime(self.0)
}
#[must_use]
pub fn year(self) -> i32 {
i32::from(self.civil().year())
}
#[must_use]
pub fn month(self) -> u32 {
u32::try_from(self.civil().month()).expect("month is positive")
}
#[must_use]
pub fn day(self) -> u32 {
u32::try_from(self.civil().day()).expect("day is positive")
}
#[must_use]
pub fn hour(self) -> u32 {
u32::try_from(self.civil().hour()).expect("hour is not negative")
}
#[must_use]
pub fn minute(self) -> u32 {
u32::try_from(self.civil().minute()).expect("minute is not negative")
}
#[must_use]
pub fn second(self) -> u32 {
u32::try_from(self.civil().second()).expect("second is not negative")
}
#[must_use]
pub fn ordinal(self) -> u32 {
u32::try_from(self.civil().day_of_year()).expect("day of year is positive")
}
#[must_use]
pub fn weekday_from_monday(self) -> u32 {
u32::try_from(self.civil().weekday().to_monday_zero_offset()).expect("0..=6")
}
#[must_use]
pub fn to_local(self) -> LocalDateTime {
LocalDateTime(self.0.to_zoned(TimeZone::system()))
}
#[must_use]
pub const fn fixed_offset(self) -> OffsetDateTime {
OffsetDateTime {
utc: self,
offset_secs: 0,
}
}
#[must_use]
pub fn with_offset_seconds(self, secs: i32) -> Option<OffsetDateTime> {
(-86_399..=86_399)
.contains(&secs)
.then_some(OffsetDateTime {
utc: self,
offset_secs: secs,
})
}
pub fn duration_trunc(self, span: Duration) -> Result<Self, OutOfRangeError> {
let span = span.0.as_nanos();
let stamp = self.nanos_i128();
if span <= 0 || span > stamp.abs() {
return Err(OutOfRangeError);
}
Self::from_nanos_i128(stamp - stamp.rem_euclid(span)).ok_or(OutOfRangeError)
}
#[must_use]
pub fn with_nanosecond(self, nano: u32) -> Option<Self> {
if i128::from(nano) >= NANOS_PER_SEC {
return None;
}
let secs = i128::from(self.timestamp());
Self::from_nanos_i128(secs * NANOS_PER_SEC + i128::from(nano))
}
#[must_use]
pub const fn to_utc(self) -> Self {
self
}
#[must_use]
pub fn signed_duration_since(self, other: impl Into<UtcDateTime>) -> Duration {
Duration::from_nanos_i128(self.nanos_i128() - other.into().nanos_i128())
.expect("the difference of two instants fits a duration")
}
#[must_use]
pub fn checked_add_signed(self, d: Duration) -> Option<Self> {
self.0.checked_add(d.0).ok().map(Self)
}
#[must_use]
pub fn checked_sub_signed(self, d: Duration) -> Option<Self> {
self.0.checked_sub(d.0).ok().map(Self)
}
#[must_use]
pub fn trunc_subsecs_to_secs(self) -> Self {
Self::from_nanos_i128(self.nanos_i128() - self.nanos_i128().rem_euclid(NANOS_PER_SEC))
.expect("truncation stays in range")
}
#[must_use]
pub fn to_system_time(self) -> std::time::SystemTime {
std::time::SystemTime::from(self.0)
}
}
fn to_i64(v: i128) -> i64 {
i64::try_from(v).expect("jiff's range fits i64 at this resolution")
}
impl From<std::time::SystemTime> for UtcDateTime {
fn from(t: std::time::SystemTime) -> Self {
Self(Timestamp::try_from(t).expect("system time within years -9999..=9999"))
}
}
impl From<UtcDateTime> for std::time::SystemTime {
fn from(t: UtcDateTime) -> Self {
t.to_system_time()
}
}
impl From<Timestamp> for UtcDateTime {
fn from(ts: Timestamp) -> Self {
Self(ts)
}
}
impl From<UtcDateTime> for Timestamp {
fn from(t: UtcDateTime) -> Self {
t.0
}
}
impl fmt::Debug for UtcDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.to_rfc3339_opts(SecondsFormat::AutoSi, true))
}
}
impl fmt::Display for UtcDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut out = String::with_capacity(32);
format::write_display(&mut out, &format::Parts::utc(*self));
f.pad(&out)
}
}
impl FromStr for UtcDateTime {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, ParseError> {
parse::rfc3339(s, parse::Mode::Relaxed).map(|p| p.utc)
}
}
impl serde::Serialize for UtcDateTime {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(&format_args!("{self:?}"))
}
}
impl<'de> serde::Deserialize<'de> for UtcDateTime {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl serde::de::Visitor<'_> for Visitor {
type Value = UtcDateTime;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("an RFC 3339 date and time string")
}
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<UtcDateTime, E> {
v.parse().map_err(E::custom)
}
}
deserializer.deserialize_str(Visitor)
}
}
#[cfg(feature = "schemars")]
impl schemars::JsonSchema for UtcDateTime {
fn inline_schema() -> bool {
true
}
fn schema_name() -> std::borrow::Cow<'static, str> {
"DateTime".into()
}
fn schema_id() -> std::borrow::Cow<'static, str> {
"chrono::DateTime".into()
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({
"type": "string",
"format": "date-time"
})
}
}
impl Add<Duration> for UtcDateTime {
type Output = Self;
fn add(self, d: Duration) -> Self {
self.checked_add_signed(d)
.expect("`UtcDateTime + Duration` overflowed")
}
}
impl Sub<Duration> for UtcDateTime {
type Output = Self;
fn sub(self, d: Duration) -> Self {
self.checked_sub_signed(d)
.expect("`UtcDateTime - Duration` overflowed")
}
}
impl AddAssign<Duration> for UtcDateTime {
fn add_assign(&mut self, d: Duration) {
*self = *self + d;
}
}
impl SubAssign<Duration> for UtcDateTime {
fn sub_assign(&mut self, d: Duration) {
*self = *self - d;
}
}
impl Add<std::time::Duration> for UtcDateTime {
type Output = Self;
fn add(self, d: std::time::Duration) -> Self {
self + Duration::from_std(d).expect("std duration within range")
}
}
impl Sub<std::time::Duration> for UtcDateTime {
type Output = Self;
fn sub(self, d: std::time::Duration) -> Self {
self - Duration::from_std(d).expect("std duration within range")
}
}
impl Sub for UtcDateTime {
type Output = Duration;
fn sub(self, other: Self) -> Duration {
self.signed_duration_since(other)
}
}
#[derive(Clone, Copy)]
pub struct OffsetDateTime {
utc: UtcDateTime,
offset_secs: i32,
}
impl OffsetDateTime {
pub fn parse_from_rfc3339(s: &str) -> Result<Self, ParseError> {
parse::rfc3339(s, parse::Mode::Strict).map(|p| Self {
utc: p.utc,
offset_secs: p.offset_secs,
})
}
#[must_use]
pub const fn to_utc(self) -> UtcDateTime {
self.utc
}
#[must_use]
pub const fn offset_seconds(self) -> i32 {
self.offset_secs
}
#[must_use]
pub fn timestamp(self) -> i64 {
self.utc.timestamp()
}
#[must_use]
pub fn timestamp_millis(self) -> i64 {
self.utc.timestamp_millis()
}
#[must_use]
pub fn signed_duration_since(self, other: impl Into<UtcDateTime>) -> Duration {
self.utc.signed_duration_since(other)
}
#[must_use]
pub fn checked_add_signed(self, d: Duration) -> Option<Self> {
self.utc
.checked_add_signed(d)
.map(|utc| Self { utc, ..self })
}
#[must_use]
pub fn checked_sub_signed(self, d: Duration) -> Option<Self> {
self.utc
.checked_sub_signed(d)
.map(|utc| Self { utc, ..self })
}
#[must_use]
pub fn to_rfc3339(self) -> String {
self.to_rfc3339_opts(SecondsFormat::AutoSi, false)
}
#[must_use]
pub fn to_rfc3339_opts(self, secform: SecondsFormat, use_z: bool) -> String {
let mut out = String::with_capacity(32);
let parts = format::Parts::with_offset(self.utc, self.offset_secs, None);
let offset = if use_z && self.offset_secs == 0 {
"Z".to_owned()
} else {
format::offset_colon(self.offset_secs)
};
format::write_rfc3339(&mut out, &parts, secform, &offset);
out
}
#[must_use]
pub fn timestamp_subsec_nanos(self) -> u32 {
self.utc.timestamp_subsec_nanos()
}
#[must_use]
pub fn to_local(self) -> LocalDateTime {
self.utc.to_local()
}
#[must_use]
pub fn format(self, fmt: &str) -> format::Formatted<'_> {
format::Formatted::new(
format::Parts::with_offset(self.utc, self.offset_secs, None),
fmt,
)
}
}
impl From<OffsetDateTime> for UtcDateTime {
fn from(t: OffsetDateTime) -> Self {
t.utc
}
}
impl PartialEq for OffsetDateTime {
fn eq(&self, other: &Self) -> bool {
self.utc == other.utc
}
}
impl Eq for OffsetDateTime {}
impl std::hash::Hash for OffsetDateTime {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.utc.hash(state);
}
}
impl PartialEq<UtcDateTime> for OffsetDateTime {
fn eq(&self, other: &UtcDateTime) -> bool {
self.utc == *other
}
}
impl PartialEq<OffsetDateTime> for UtcDateTime {
fn eq(&self, other: &OffsetDateTime) -> bool {
*self == other.utc
}
}
impl PartialOrd<UtcDateTime> for OffsetDateTime {
fn partial_cmp(&self, other: &UtcDateTime) -> Option<std::cmp::Ordering> {
Some(self.utc.cmp(other))
}
}
impl PartialOrd<OffsetDateTime> for UtcDateTime {
fn partial_cmp(&self, other: &OffsetDateTime) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other.utc))
}
}
impl Sub for OffsetDateTime {
type Output = Duration;
fn sub(self, other: Self) -> Duration {
self.utc.signed_duration_since(other)
}
}
impl Sub<UtcDateTime> for OffsetDateTime {
type Output = Duration;
fn sub(self, other: UtcDateTime) -> Duration {
self.utc.signed_duration_since(other)
}
}
impl Sub<OffsetDateTime> for UtcDateTime {
type Output = Duration;
fn sub(self, other: OffsetDateTime) -> Duration {
self.signed_duration_since(other)
}
}
impl Add<Duration> for OffsetDateTime {
type Output = Self;
fn add(self, d: Duration) -> Self {
self.checked_add_signed(d)
.expect("`OffsetDateTime + Duration` overflowed")
}
}
impl Sub<Duration> for OffsetDateTime {
type Output = Self;
fn sub(self, d: Duration) -> Self {
self.checked_sub_signed(d)
.expect("`OffsetDateTime - Duration` overflowed")
}
}
impl Ord for OffsetDateTime {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.utc.cmp(&other.utc)
}
}
impl PartialOrd for OffsetDateTime {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl fmt::Debug for OffsetDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.to_rfc3339())
}
}
impl fmt::Display for OffsetDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = self.format("%Y-%m-%d %H:%M:%S%.f %:z").to_string();
f.pad(&s)
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct LocalDateTime(jiff::Zoned);
impl LocalDateTime {
#[must_use]
pub fn now() -> Self {
Self(jiff::Zoned::now())
}
#[must_use]
pub fn to_utc(&self) -> UtcDateTime {
UtcDateTime(self.0.timestamp())
}
#[must_use]
pub fn offset_seconds(&self) -> i32 {
self.0.offset().seconds()
}
#[must_use]
pub fn date_naive(&self) -> civil::Date {
self.0.date()
}
#[must_use]
pub fn naive_local(&self) -> civil::DateTime {
self.0.datetime()
}
#[must_use]
pub fn hour(&self) -> u32 {
u32::try_from(self.0.hour()).expect("hour is not negative")
}
#[must_use]
pub fn to_rfc3339(&self) -> String {
let mut out = String::with_capacity(32);
let parts = self.parts();
let offset = format::offset_colon(self.offset_seconds());
format::write_rfc3339(&mut out, &parts, SecondsFormat::AutoSi, &offset);
out
}
#[must_use]
pub fn format<'a>(&self, fmt: &'a str) -> format::Formatted<'a> {
format::Formatted::new(self.parts(), fmt)
}
fn parts(&self) -> format::Parts {
format::Parts::with_offset(self.to_utc(), self.offset_seconds(), None)
}
#[must_use]
pub fn as_jiff(&self) -> &jiff::Zoned {
&self.0
}
}
impl fmt::Debug for LocalDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.to_rfc3339())
}
}
impl fmt::Display for LocalDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = self.format("%Y-%m-%d %H:%M:%S%.f %:z").to_string();
f.pad(&s)
}
}
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Duration(SignedDuration);
macro_rules! ctor {
($(#[$doc:meta])* $name:ident, $try_name:ident, $nanos:expr) => {
$(#[$doc])*
#[must_use]
pub const fn $name(n: i64) -> Self {
match Self::$try_name(n) {
Some(d) => d,
None => panic!(concat!("Duration::", stringify!($name), " out of bounds")),
}
}
$(#[$doc])*
#[must_use]
#[allow(clippy::cast_lossless)] pub const fn $try_name(n: i64) -> Option<Self> {
Self::from_nanos_i128(n as i128 * $nanos)
}
};
}
impl Duration {
pub const ZERO: Self = Self(SignedDuration::ZERO);
#[must_use]
pub const fn zero() -> Self {
Self::ZERO
}
ctor!(
weeks,
try_weeks,
7 * 86_400 * NANOS_PER_SEC
);
ctor!(
days,
try_days,
86_400 * NANOS_PER_SEC
);
ctor!(
hours,
try_hours,
3_600 * NANOS_PER_SEC
);
ctor!(
minutes,
try_minutes,
60 * NANOS_PER_SEC
);
ctor!(
seconds,
try_seconds,
NANOS_PER_SEC
);
ctor!(
milliseconds,
try_milliseconds,
1_000_000
);
ctor!(
microseconds,
try_microseconds,
1_000
);
#[must_use]
pub fn nanoseconds(n: i64) -> Self {
Self(SignedDuration::from_nanos(n))
}
pub const MAX: Self = Self(SignedDuration::new(
i64::MAX / 1_000,
(i64::MAX % 1_000) as i32 * 1_000_000,
));
pub const MIN: Self = Self(SignedDuration::new(
-(i64::MAX / 1_000),
-((i64::MAX % 1_000) as i32 * 1_000_000),
));
#[allow(clippy::cast_possible_truncation)]
const fn from_nanos_i128(nanos: i128) -> Option<Self> {
const LIMIT: i128 = i64::MAX as i128 * 1_000_000;
if nanos < -LIMIT || nanos > LIMIT {
return None;
}
let secs = (nanos / NANOS_PER_SEC) as i64;
let sub = (nanos % NANOS_PER_SEC) as i32;
Some(Self(SignedDuration::new(secs, sub)))
}
fn nanos_i128(self) -> i128 {
self.0.as_nanos()
}
#[must_use]
pub fn num_weeks(self) -> i64 {
self.num_days() / 7
}
#[must_use]
pub fn num_days(self) -> i64 {
self.num_seconds() / 86_400
}
#[must_use]
pub fn num_hours(self) -> i64 {
self.num_seconds() / 3_600
}
#[must_use]
pub fn num_minutes(self) -> i64 {
self.num_seconds() / 60
}
#[must_use]
pub fn num_seconds(self) -> i64 {
self.0.as_secs()
}
#[must_use]
pub fn num_milliseconds(self) -> i64 {
i64::try_from(self.nanos_i128() / 1_000_000)
.expect("milliseconds of an i64-second span fit i64")
}
#[must_use]
pub fn num_microseconds(self) -> Option<i64> {
i64::try_from(self.nanos_i128() / 1_000).ok()
}
#[must_use]
pub fn num_nanoseconds(self) -> Option<i64> {
i64::try_from(self.nanos_i128()).ok()
}
#[must_use]
pub fn subsec_nanos(self) -> i32 {
self.0.subsec_nanos()
}
#[must_use]
pub fn as_seconds_f64(self) -> f64 {
self.0.as_secs_f64()
}
#[must_use]
pub fn is_zero(self) -> bool {
self.0.is_zero()
}
#[must_use]
pub fn is_negative(self) -> bool {
self.0.is_negative()
}
#[must_use]
pub fn abs(self) -> Self {
Self(self.0.abs())
}
#[must_use]
pub fn checked_add(&self, rhs: &Self) -> Option<Self> {
Self::from_nanos_i128(self.nanos_i128() + rhs.nanos_i128())
}
#[must_use]
pub fn checked_sub(&self, rhs: &Self) -> Option<Self> {
Self::from_nanos_i128(self.nanos_i128() - rhs.nanos_i128())
}
pub fn from_std(d: std::time::Duration) -> Result<Self, OutOfRangeError> {
Self::from_nanos_i128(i128::try_from(d.as_nanos()).map_err(|_| OutOfRangeError)?)
.ok_or(OutOfRangeError)
}
pub fn to_std(self) -> Result<std::time::Duration, OutOfRangeError> {
std::time::Duration::try_from(self.0).map_err(|_| OutOfRangeError)
}
#[must_use]
pub const fn from_jiff(d: SignedDuration) -> Self {
Self(d)
}
#[must_use]
pub const fn as_jiff(self) -> SignedDuration {
self.0
}
}
impl Add for Duration {
type Output = Self;
fn add(self, rhs: Self) -> Self {
self.checked_add(&rhs)
.expect("`Duration + Duration` overflowed")
}
}
impl Sub for Duration {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
self.checked_sub(&rhs)
.expect("`Duration - Duration` overflowed")
}
}
impl AddAssign for Duration {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl SubAssign for Duration {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl Neg for Duration {
type Output = Self;
fn neg(self) -> Self {
Self(-self.0)
}
}
impl Mul<i32> for Duration {
type Output = Self;
fn mul(self, rhs: i32) -> Self {
Self::from_nanos_i128(self.nanos_i128() * i128::from(rhs))
.expect("`Duration * i32` overflowed")
}
}
impl Div<i32> for Duration {
type Output = Self;
fn div(self, rhs: i32) -> Self {
assert!(rhs != 0, "`Duration / i32` divided by zero");
let total = self.nanos_i128();
let (secs, nanos) = (
total.div_euclid(NANOS_PER_SEC),
total.rem_euclid(NANOS_PER_SEC),
);
let rhs = i128::from(rhs);
let mut q_secs = secs / rhs;
let carry = secs % rhs;
let mut q_nanos = nanos / rhs + carry * NANOS_PER_SEC / rhs;
if q_nanos < 0 {
q_secs -= 1;
q_nanos += NANOS_PER_SEC;
} else if q_nanos >= NANOS_PER_SEC {
q_secs += 1;
q_nanos -= NANOS_PER_SEC;
}
Self::from_nanos_i128(q_secs * NANOS_PER_SEC + q_nanos).expect("a quotient stays in range")
}
}
impl std::iter::Sum for Duration {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self::ZERO, Add::add)
}
}
impl<'a> std::iter::Sum<&'a Duration> for Duration {
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
iter.copied().sum()
}
}
impl fmt::Debug for Duration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl fmt::Display for Duration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let nanos = self.nanos_i128();
let sign = if nanos < 0 { "-" } else { "" };
let abs = nanos.unsigned_abs();
if abs == 0 {
return f.write_str("P0D");
}
let secs = abs / 1_000_000_000;
let mut frac = abs % 1_000_000_000;
write!(f, "{sign}PT{secs}")?;
if frac > 0 {
let mut width = 9usize;
while frac.is_multiple_of(10) {
frac /= 10;
width -= 1;
}
write!(f, ".{frac:0width$}")?;
}
f.write_str("S")
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests;