oxedyne/fe2o3/fe2o3_datime/src/calendar/duration.rs
3.2 KiB, 32 runs
created by r1870400018:6295, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 2 | //! Anthropic Claude |
| 3 | |
| 4 | use crate::core::Duration; |
| 5 | |
| 6 | use oxedyne_fe2o3_core::prelude::*; |
| 7 | |
| 8 | use std::fmt::{self, Display}; |
| 9 | |
| 10 | /// Calendar units, whose length in days depends on where they are applied. |
| 11 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 12 | pub struct CalendarDuration { |
| 13 | years: i32, |
| 14 | months: i32, |
| 15 | days: i32, |
| 16 | } |
| 17 | |
| 18 | impl CalendarDuration { |
| 19 | pub fn new(years: i32, months: i32, days: i32) -> Self { |
| 20 | Self { |
| 21 | years, |
| 22 | months, |
| 23 | days, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | pub fn from_days(days: i32) -> Self { |
| 28 | Self { |
| 29 | years: 0, |
| 30 | months: 0, |
| 31 | days, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub fn from_months(months: i32) -> Self { |
| 36 | Self { |
| 37 | years: 0, |
| 38 | months, |
| 39 | days: 0, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn from_years(years: i32) -> Self { |
| 44 | Self { |
| 45 | years, |
| 46 | months: 0, |
| 47 | days: 0, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn years(&self) -> i32 { |
| 52 | self.years |
| 53 | } |
| 54 | |
| 55 | pub fn months(&self) -> i32 { |
| 56 | self.months |
| 57 | } |
| 58 | |
| 59 | pub fn days(&self) -> i32 { |
| 60 | self.days |
| 61 | } |
| 62 | |
| 63 | pub fn in_days(&self) -> i32 { |
| 64 | // Approximate: 365.25 days per year, 30.44 days per month |
| 65 | (self.years as f64 * 365.25 + self.months as f64 * 30.44 + self.days as f64) as i32 |
| 66 | } |
| 67 | |
| 68 | pub fn is_negative(&self) -> bool { |
| 69 | self.years < 0 || self.months < 0 || self.days < 0 |
| 70 | } |
| 71 | |
| 72 | pub fn negate(&self) -> Self { |
| 73 | Self { |
| 74 | years: -self.years, |
| 75 | months: -self.months, |
| 76 | days: -self.days, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | pub fn plus(&self, other: &Self) -> Self { |
| 81 | Self { |
| 82 | years: self.years + other.years, |
| 83 | months: self.months + other.months, |
| 84 | days: self.days + other.days, |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub fn minus(&self, other: &Self) -> Self { |
| 89 | Self { |
| 90 | years: self.years - other.years, |
| 91 | months: self.months - other.months, |
| 92 | days: self.days - other.days, |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl Duration for CalendarDuration { |
| 98 | fn to_nanos(&self) -> Outcome<i64> { |
| 99 | // Approximate conversion |
| 100 | let total_days = self.in_days() as i64; |
| 101 | Ok(total_days * 24 * 60 * 60 * 1_000_000_000) |
| 102 | } |
| 103 | |
| 104 | fn to_seconds(&self) -> Outcome<i64> { |
| 105 | // Approximate conversion |
| 106 | let total_days = self.in_days() as i64; |
| 107 | Ok(total_days * 24 * 60 * 60) |
| 108 | } |
| 109 | |
| 110 | fn to_days(&self) -> Outcome<i32> { |
| 111 | Ok(self.in_days()) |
| 112 | } |
| 113 | |
| 114 | fn is_negative(&self) -> bool { |
| 115 | self.is_negative() |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl Display for CalendarDuration { |
| 120 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 121 | if self.years != 0 { |
| 122 | ok!(write!(f, "{}Y", self.years)); |
| 123 | } |
| 124 | if self.months != 0 { |
| 125 | if self.years != 0 { |
| 126 | ok!(write!(f, " ")); |
| 127 | } |
| 128 | ok!(write!(f, "{}M", self.months)); |
| 129 | } |
| 130 | if self.days != 0 || (self.years == 0 && self.months == 0) { |
| 131 | if self.years != 0 || self.months != 0 { |
| 132 | ok!(write!(f, " ")); |
| 133 | } |
| 134 | ok!(write!(f, "{}D", self.days)); |
| 135 | } |
| 136 | Ok(()) |
| 137 | } |
| 138 | } |