1use std::fmt;
11
12use num_bigint::BigInt;
13use num_integer::Integer;
14use num_traits::ToPrimitive;
15
16use crate::decimal::Decimal;
17use crate::error::Fault;
18use crate::int::Int;
19
20#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
22pub struct Duration(Decimal);
23
24impl Duration {
25 pub fn from_seconds(seconds: Decimal) -> Self {
27 Duration(seconds)
28 }
29
30 pub fn from_millis(count: &Int) -> Self {
37 Duration(
38 Decimal::new(count.as_bigint().clone(), -3)
39 .expect("normalizing a count of milliseconds only raises its exponent towards 0"),
40 )
41 }
42
43 pub fn seconds(&self) -> &Decimal {
45 &self.0
46 }
47}
48
49impl fmt::Display for Duration {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 write!(f, "PT{}S", self.0)
54 }
55}
56
57#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
59pub struct Instant(Decimal);
60
61impl Instant {
62 pub fn from_unix_seconds(seconds: Decimal) -> Self {
64 Instant(seconds)
65 }
66
67 pub fn unix_seconds(&self) -> &Decimal {
69 &self.0
70 }
71
72 pub fn add(&self, span: &Duration) -> Result<Instant, Fault> {
78 Ok(Instant(self.0.add(&span.0)?))
79 }
80
81 pub fn since(&self, earlier: &Instant) -> Result<Duration, Fault> {
89 Ok(Duration(self.0.sub(&earlier.0)?))
90 }
91}
92
93const SECONDS_PER_DAY: i64 = 86_400;
94
95impl fmt::Display for Instant {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 let whole = self.0.floor();
102 let fraction = self
103 .0
104 .sub(&Decimal::from(&whole))
105 .expect("an exact difference of numbers that share their low digits");
106 let (days, second_of_day) = whole
107 .as_bigint()
108 .div_mod_floor(&BigInt::from(SECONDS_PER_DAY));
109 let second_of_day = second_of_day.to_i64().expect("a remainder below 86400");
110 let (year, month, day) = civil_from_days(&days);
111
112 let year = if year < BigInt::from(0) {
113 format!("-{:04}", -year)
114 } else if year > BigInt::from(9999) {
115 format!("+{year}")
116 } else {
117 format!("{year:04}")
118 };
119 let fraction = fraction.to_string();
122 write!(
123 f,
124 "{year}-{month:02}-{day:02}T{:02}:{:02}:{:02}{}Z",
125 second_of_day / 3600,
126 second_of_day / 60 % 60,
127 second_of_day % 60,
128 fraction.strip_prefix('0').unwrap_or(&fraction),
129 )
130 }
131}
132
133fn civil_from_days(days: &BigInt) -> (BigInt, i64, i64) {
139 let shifted: BigInt = days + 719_468;
140 let (era, day_of_era) = shifted.div_mod_floor(&BigInt::from(146_097));
141 let day_of_era = day_of_era.to_i64().expect("a remainder below 146097");
142 let year_of_era =
143 (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146_096) / 365;
144 let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
145 let shifted_month = (5 * day_of_year + 2) / 153;
146 let day = day_of_year - (153 * shifted_month + 2) / 5 + 1;
147 let month = if shifted_month < 10 {
148 shifted_month + 3
149 } else {
150 shifted_month - 9
151 };
152 let year = era * 400 + year_of_era + i64::from(month <= 2);
153 (year, month, day)
154}