kalendarium 0.1.0

A library for converting Arabic numerals into Roman numerals and modern dates into the Kalendarium Romanum
Documentation
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! # kalendarium Roman date implementation
//!
//! A library for converting a date into the Kalendarium Romanum
//!
//! While testing dates in late antiquity and classical times, the day of the week kept coming
//! up as wrong, which led me down a rabbit hole, whereby I discovered that the Gregorian calendar
//! dropped 12 days in Sept 1752 when it switched from the Julian calendar. The "cal" function
//! built into the macbook does not implement proleptic Gregorian (whereby dates are extraporated
//! backward even though people who lived in the time periods would not have used the calendar), so
//! "cal" 'correctly' reported the Battle of Hastings took place on a Saturday. But rust chrono
//! implement this hybid calendar, instead using proleptic Gregorian, so it reports that October
//! 14, 1066 is a Monday. On the proleptic Calendar, the Battle took place on October 2nd (a
//! Monday).
//!
//! After coming up with a quick solution for just the day-of-the-week calculations, I then
//! learned that there is a leapyear in 1000 CE on the Julian Calendar (there isn't one in
//! proleptic Gregorian), which broke weekdays before 1000, too, as well as eliminating February
//! 29, 1000 from a valid date.  Fortunately, there is a rust crate Julian, that implements a
//! REFORMED1582 calendar, which is exactly what I wanted! I switched over my crate to use that
//! library, and everything is working!
//!
//! ## License
//!
//! GNU GPL 3

use crate::{OutOfRangeError, Result, RomanNumeral};
use core::fmt;
use julian::{Calendar, Date};

/// A Kalendarium Romanum object
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Kalendarium(Date);

impl Kalendarium {
    /// Creates a ``Kalendarium`` for any date. Year, Month, and day are required.
    ///
    /// Example
    /// -------
    ///
    /// .. code-block:: rust
    ///
    ///    let kal: Kalendarium = Kalendarium::new("2025, "5", "23").unwrap();
    ///    assert_eq!("ante diem X Kal. Iun. MMDCCLXXVIII a.u.c. diēs Veneris", answer.to_str());
    pub fn new(year: &str, month: &str, day: &str) -> Result<Self, OutOfRangeError> {
        if year.parse::<i32>().unwrap() > -755_i32 {
            let mut date_str = String::new();

            if year.parse::<i32>().unwrap() < 0 {
                // Year 0 in proleptic julian doesn't exist on historical calendars.
                // year -1 (i.e. 1 BCE) is following by year 1 CE
                date_str.push_str(&(year.parse::<i32>().unwrap() + 1_i32).to_string());
            } else {
                date_str.push_str(year);
            }
            date_str.push_str("-");
            date_str.push_str(month);
            date_str.push_str("-");
            date_str.push_str(day);
            let cal = Calendar::REFORM1582;
            let date = cal.parse_date(&date_str).unwrap();
            Ok(Kalendarium(date))
        } else {
            Err(OutOfRangeError)
        }
    }

    /// Displays a ``Kalendarium`` as a ancient Roman date string.
    ///
    /// Example
    /// -------
    ///
    /// .. code-block:: rust
    ///
    ///    let kal: Kalendarium = Kalendarium::new("800", "12", "25").unwrap();
    ///    assert_eq!( "ante diem bis VI Kal. Mart. MDLIII a.u.c. Dīēs Nātālis Sōlis Invictī diēs Veneris", kal.to_str());
    ///
    #[must_use]
    #[cfg(feature = "std")]
    pub fn to_str(&self) -> String {
        let mut out = String::new();

        out.push_str(&Self::roman_day(self));
        out.push_str(" ");
        out.push_str(&Self::roman_year(self));
        out.push_str(" ");
        out.push_str(&Self::roman_festival_day(self));
        out.push_str(&Self::roman_day_of_week(self));
        out
    }

    /// helper function to determine whether the date's year is a leap year
    fn is_leap_year(&self) -> bool {
        let year: i16 = self.0.year() as i16;
        if year == 1000 {
            // Even though it "shouldn't" be, 1000 was a leap year on the Julian Calendar
            true
        } else if year % 4 == 0 {
            if (year % 100) == 0 {
                if year % 400 == 0 {
                    true
                } else {
                    false
                }
            } else {
                true
            }
        } else {
            false
        }
    }

    /// Display the Gregorian calendar year as a Roman year, i.e. years since found of Rome
    pub fn roman_year(&self) -> String {
        let mut out = String::new();
        let year: i16 = self.0.year() as i16;
        // No longer have to offset for year zero because I do that in the constructor now
        out.push_str(
            &RomanNumeral::new((753_i16 + year) as u32)
                .unwrap()
                .to_uppercase(),
        );
        out.push_str(" a.u.c.");
        out
    }

    /// helper function that returns a festival if there is on on the data
    pub fn roman_festival_day(&self) -> String {
        let month: u8 = self.0.month() as u8;
        let day: u8 = self.0.day() as u8;
        let mut out = String::new();
        for (_index, festival_day) in FERIAE.iter().enumerate() {
            if festival_day.month == month && festival_day.day == day {
                out.push_str(festival_day.festival);
                out.push_str(" ");
                break;
            }
        }
        out
    }

    /// The day of the week in Latin
    pub fn roman_day_of_week(&self) -> String {
        let mut out = String::new();
        let day_of_week = self.0.weekday() as usize;
        out.push_str(DIES[day_of_week].day_of_week);
        out
    }

    /// The day of the month on the Kalendarium Romanum
    pub fn roman_day(&self) -> String {
        let mut out = String::new();

        let day: usize = self.0.day() as usize;
        let month: usize = self.0.month() as usize;

        let leap_day: usize = if Self::is_leap_year(self) && day > 25 && month == 2 {
            1
        } else {
            0
        };

        if day == 1 {
            // The First of the Month, the Kalends
            out.push_str("Kalendae ");
            out.push_str(MENSES[month - 1].nominis);
        } else if day < MENSES[month - 1].nones {
            // Days leading up to the Nones
            if MENSES[month - 1].nones - day < 2 {
                out.push_str("prīdiē Nōnās ");
                out.push_str(MENSES[month - 1].nomines);
            } else {
                out.push_str("ante diem ");
                out.push_str(
                    &RomanNumeral::new((1 + MENSES[month - 1].nones - day) as u32)
                        .unwrap()
                        .to_uppercase(),
                );
                out.push_str(" Nōn. ");
                out.push_str(MENSES[month - 1].nomunculus);
            }
        } else if day == MENSES[month - 1].nones {
            // The Nones herself
            out.push_str("Nōnae ");
            out.push_str(MENSES[month - 1].nominis);
        } else if day < MENSES[month - 1].ides {
            // Days leading up to the Ides
            if MENSES[month - 1].ides - day < 2 {
                out.push_str("prīdiē Īdūs ");
                out.push_str(MENSES[month - 1].nomines);
            } else {
                out.push_str("ante diem ");
                out.push_str(
                    &RomanNumeral::new((1 + MENSES[month - 1].ides - day) as u32)
                        .unwrap()
                        .to_uppercase(),
                );
                out.push_str(" Īd. ");
                out.push_str(MENSES[month - 1].nomunculus);
            }
        } else if day == MENSES[month - 1].ides {
            // The Ides herself
            out.push_str("Īdūs ");
            out.push_str(MENSES[month - 1].nominis);
        } else {
            if Self::is_leap_year(self) && day == 25 {
                out.push_str("ante diem bis VI Kal. Mart.");
            } else if (MENSES[month - 1].finis + leap_day) - day < 2 {
                out.push_str("prīdiē Kalendae ");
                out.push_str(MENSES[month].nomines);
            } else {
                out.push_str("ante diem ");
                out.push_str(
                    &RomanNumeral::new((2 + (MENSES[month - 1].finis + leap_day) - day) as u32)
                        .unwrap()
                        .to_uppercase(),
                );
                out.push_str(" Kal. ");
                out.push_str(MENSES[month].nomunculus);
            }
        }
        out
    }

    /// debugging function
    pub fn debug(&self) -> String {
        let mut out = String::new();
        out.push_str("day: ");
        out.push_str(&self.0.day().to_string());
        out.push_str(", month: ");
        out.push_str(&self.0.month().to_string());
        out.push_str(", year: ");
        out.push_str(&self.0.year().to_string());
        out.push_str(", weekday: ");
        out.push_str(&self.0.weekday().to_string());
        out
    }
}

#[cfg(feature = "std")]
impl fmt::Display for Kalendarium {
    /// Displays a Date into the Kalendarium Romanum
    ///
    /// Example
    /// -------
    ///
    /// .. code-block:: rust
    ///
    ///    let kal: Kalendarium = Kalendarium::new("-43", "03", "15").unwrap();
    ///    assert_eq!("prīdiē Kalendae Iānuāriās DCCLIII a.u.c. diēs Veneris", kal.to_string());
    ///
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.to_str())
    }
}

/// Struct for defining the days of week in Latin
struct DiesStruct<'m> {
    day_of_week: &'m str,
}

static DIES: [DiesStruct; 8] = [
    DiesStruct {
        day_of_week: "diēs Sōlis",
    },
    DiesStruct {
        day_of_week: "diēs Lūnae",
    },
    DiesStruct {
        day_of_week: "diēs Mārtis",
    },
    DiesStruct {
        day_of_week: "diēs Mercuriī",
    },
    DiesStruct {
        day_of_week: "diēs Iovis",
    },
    DiesStruct {
        day_of_week: "diēs Veneris",
    },
    DiesStruct {
        day_of_week: "diēs Sāturnī",
    },
    DiesStruct {
        day_of_week: "diēs Sōlis",
    },
];

/// Struct for storing festival days
struct FeriaStruct<'m> {
    month: u8,
    day: u8,
    festival: &'m str,
}

static FERIAE: [FeriaStruct; 14] = [
    FeriaStruct {
        month: 12,
        day: 17,
        festival: "prīmus diēs Saturnālium",
    },
    FeriaStruct {
        month: 12,
        day: 18,
        festival: "secundus diēs Saturnālium",
    },
    FeriaStruct {
        month: 12,
        day: 19,
        festival: "tertius diēs Saturnālium",
    },
    FeriaStruct {
        month: 12,
        day: 20,
        festival: "quārtus diēs Saturnālium",
    },
    FeriaStruct {
        month: 12,
        day: 21,
        festival: "quīntus diēs Saturnālium",
    },
    FeriaStruct {
        month: 12,
        day: 22,
        festival: "sextus diēs Saturnālium",
    },
    FeriaStruct {
        month: 12,
        day: 23,
        festival: "septimus diēs Saturnālium",
    },
    FeriaStruct {
        month: 12,
        day: 25,
        festival: "Dīēs Nātālis Sōlis Invictī",
    },
    FeriaStruct {
        month: 2,
        day: 15,
        festival: "Lupercālia",
    },
    FeriaStruct {
        month: 3,
        day: 17,
        festival: "Līberālia",
    },
    FeriaStruct {
        month: 4,
        day: 1,
        festival: "Venerālia",
    },
    FeriaStruct {
        month: 4,
        day: 27,
        festival: "Flōrālia",
    },
    FeriaStruct {
        month: 6,
        day: 24,
        festival: "Fors Fortūna",
    },
    FeriaStruct {
        month: 4,
        day: 21,
        festival: "Parilia",
    },
];

/// struct for defining what a Roman month looks like
struct MensStruct<'m> {
    nominis: &'m str,
    nomines: &'m str,
    nomunculus: &'m str,
    nones: usize,
    ides: usize,
    finis: usize,
}

static MENSES: [MensStruct; 13] = [
    MensStruct {
        nominis: "Iānuāriae",
        nomines: "Iānuāriās",
        nomunculus: "Iān.",
        nones: 7,
        ides: 15,
        finis: 31,
    },
    MensStruct {
        nominis: "Februāriae",
        nomines: "Februāriās",
        nomunculus: "Feb.",
        nones: 5,
        ides: 13,
        finis: 28,
    },
    MensStruct {
        nominis: "Mārtiae",
        nomines: "Mārtiās",
        nomunculus: "Mārt.",
        nones: 7,
        ides: 15,
        finis: 31,
    },
    MensStruct {
        nominis: "Aprīlis",
        nomines: "Aprīlēs",
        nomunculus: "Apr.",
        nones: 5,
        ides: 13,
        finis: 30,
    },
    MensStruct {
        nominis: "Māiae",
        nomines: "Māiās",
        nomunculus: "Māi.",
        nones: 7,
        ides: 15,
        finis: 31,
    },
    MensStruct {
        nominis: "Iūniae",
        nomines: "Iūniās",
        nomunculus: "Iun.",
        nones: 5,
        ides: 13,
        finis: 30,
    },
    MensStruct {
        nominis: "Iūliae",
        nomines: "Iūliās",
        nomunculus: "Iul.",
        nones: 7,
        ides: 15,
        finis: 31,
    },
    MensStruct {
        nominis: "Augustae",
        nomines: "Augustās",
        nomunculus: "Aug.",
        nones: 7,
        ides: 15,
        finis: 31,
    },
    MensStruct {
        nominis: "Septembrae",
        nomines: "Septembrās",
        nomunculus: "Sept.",
        nones: 5,
        ides: 13,
        finis: 30,
    },
    MensStruct {
        nominis: "Octobrae",
        nomines: "Octobrās",
        nomunculus: "Oct.",
        nones: 7,
        ides: 15,
        finis: 31,
    },
    MensStruct {
        nominis: "Novembrae",
        nomines: "Novembrās",
        nomunculus: "Nov.",
        nones: 5,
        ides: 13,
        finis: 30,
    },
    MensStruct {
        nominis: "Decembrae",
        nomines: "Decembrās",
        nomunculus: "Dec.",
        nones: 7,
        ides: 15,
        finis: 31,
    },
    MensStruct {
        nominis: "Iānuāriae",
        nomines: "Iānuāriās",
        nomunculus: "Iān.",
        nones: 7,
        ides: 15,
        finis: 31,
    },
];