use crate::helpers::{i64_to_i32, I32CastError};
use crate::rata_die::RataDie;
pub(crate) const COPTIC_EPOCH: RataDie = crate::julian::fixed_from_julian(284, 8, 29);
pub fn fixed_from_coptic(year: i32, month: u8, day: u8) -> RataDie {
COPTIC_EPOCH - 1
+ 365 * (year as i64 - 1)
+ year.div_euclid(4) as i64
+ 30 * (month as i64 - 1)
+ day as i64
}
pub fn coptic_from_fixed(date: RataDie) -> Result<(i32, u8, u8), I32CastError> {
let year = (4 * (date - COPTIC_EPOCH) + 1463).div_euclid(1461);
let year = i64_to_i32(year)?;
let month = ((date - fixed_from_coptic(year, 1, 1)).div_euclid(30) + 1) as u8; let day = (date + 1 - fixed_from_coptic(year, month, 1)) as u8;
Ok((year, month, day))
}