/rust/registry/src/index.crates.io-1949cf8c6b5b557f/calendrical_calculations-0.1.2/src/coptic.rs
Line | Count | Source |
1 | | // This file is part of ICU4X. |
2 | | // |
3 | | // The contents of this file implement algorithms from Calendrical Calculations |
4 | | // by Reingold & Dershowitz, Cambridge University Press, 4th edition (2018), |
5 | | // which have been released as Lisp code at <https://github.com/EdReingold/calendar-code2/> |
6 | | // under the Apache-2.0 license. Accordingly, this file is released under |
7 | | // the Apache License, Version 2.0 which can be found at the calendrical_calculations |
8 | | // package root or at http://www.apache.org/licenses/LICENSE-2.0. |
9 | | |
10 | | use crate::helpers::{i64_to_i32, I32CastError}; |
11 | | use crate::rata_die::RataDie; |
12 | | |
13 | | pub(crate) const COPTIC_EPOCH: RataDie = crate::julian::fixed_from_julian(284, 8, 29); |
14 | | |
15 | | /// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1978> |
16 | 0 | pub fn fixed_from_coptic(year: i32, month: u8, day: u8) -> RataDie { |
17 | 0 | COPTIC_EPOCH - 1 |
18 | 0 | + 365 * (year as i64 - 1) |
19 | 0 | + year.div_euclid(4) as i64 |
20 | 0 | + 30 * (month as i64 - 1) |
21 | 0 | + day as i64 |
22 | 0 | } |
23 | | |
24 | | /// Lisp code reference: <https://github.com/EdReingold/calendar-code2/blob/1ee51ecfaae6f856b0d7de3e36e9042100b4f424/calendar.l#L1990> |
25 | 0 | pub fn coptic_from_fixed(date: RataDie) -> Result<(i32, u8, u8), I32CastError> { |
26 | 0 | let year = (4 * (date - COPTIC_EPOCH) + 1463).div_euclid(1461); |
27 | 0 | let year = i64_to_i32(year)?; |
28 | 0 | let month = ((date - fixed_from_coptic(year, 1, 1)).div_euclid(30) + 1) as u8; // <= 12 < u8::MAX |
29 | 0 | let day = (date + 1 - fixed_from_coptic(year, month, 1)) as u8; // <= days_in_month < u8::MAX |
30 | | |
31 | 0 | Ok((year, month, day)) |
32 | 0 | } |