1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
use diesel::{
backend::Backend,
deserialize::{FromSql, FromSqlRow},
expression::AsExpression,
sql_types::Integer,
sqlite::Sqlite,
};
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug, AsExpression, FromSqlRow)]
#[diesel(sql_type=Integer)]
pub struct ActualbudgetDate(i64);
#[derive(Clone, PartialOrd, PartialEq, Eq)]
enum DatePart {
MonthOfYear,
DayOfMonth,
}
// actualbudget represents dates as numbers which "look like" the corresponding
// ISO string when written in decimal. E.g. 20240901 represents 1 Sep 2024. It
// also uses month specifiers, e.g. 202409 to represent Sep 2024. In this case,
// we interpret this as the first day of the month, e.g. 2024-09-01.
impl ActualbudgetDate {
pub fn to_iso(&self) -> String {
format!(
"{0}-{1}-{2}T00:00:00.000Z",
self.year(),
self.month(),
self.day()
)
}
pub fn to_iso_date(&self) -> String {
format!("{0}-{1}-{2}", self.year(), self.month(), self.day())
}
pub fn year(&self) -> String {
self.0.to_string()[0..4].to_string()
}
pub fn month(&self) -> String {
self.0.to_string()[4..6].to_string()
}
pub fn day(&self) -> String {
match self.date_part() {
DatePart::DayOfMonth => self.0.to_string()[6..8].to_string(),
DatePart::MonthOfYear => String::from("01"),
}
}
pub fn from_i32(i: i32) -> Self {
ActualbudgetDate(i.into())
}
pub fn from_ymd<T, S, U>(year: T, month: S, day: U) -> Self
where
T: Into<i64>,
S: Into<i64>,
U: Into<i64>,
{
Self(year.into() * 10_000 + month.into() * 100 + day.into())
}
pub fn oldest_valid_date() -> Self {
Self(10000101)
}
fn date_part(&self) -> DatePart {
if self.0 < 10000000 {
DatePart::MonthOfYear
} else {
DatePart::DayOfMonth
}
}
fn dayified_value(&self) -> i64 {
match self.date_part() {
DatePart::DayOfMonth => self.0,
DatePart::MonthOfYear => self.0 * 100 + 1,
}
}
}
impl Ord for ActualbudgetDate {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.dayified_value().cmp(&other.dayified_value())
}
}
impl PartialOrd for ActualbudgetDate {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl FromSql<Integer, Sqlite> for ActualbudgetDate {
fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> diesel::deserialize::Result<Self> {
let int: i32 = FromSql::<Integer, Sqlite>::from_sql(bytes)?;
Ok(ActualbudgetDate(int.into()))
}
}
#[cfg(test)]
mod test {
use crate::ActualbudgetDate;
#[test]
fn when_from_ymd_2026_01_16_then_constructs_2026_01_16() {
let actualbudget_date = ActualbudgetDate::from_ymd(2026, 1, 16);
assert_eq!(actualbudget_date.year(), "2026");
assert_eq!(actualbudget_date.month(), "01");
assert_eq!(actualbudget_date.day(), "16");
}
}
|