use crate::CmsError;
use crate::writer::write_u16_be;
#[cfg(not(target_arch = "wasm32"))]
use std::time::{SystemTime, UNIX_EPOCH};
#[repr(C)]
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub struct ColorDateTime {
pub year: u16,
pub month: u16,
pub day_of_the_month: u16,
pub hours: u16,
pub minutes: u16,
pub seconds: u16,
}
fn is_leap(year: i32) -> bool {
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}
fn days_in_month(year: i32, month: i32) -> i32 {
match month {
1 => 31,
2 => {
if is_leap(year) {
29
} else {
28
}
}
3 => 31,
4 => 30,
5 => 31,
6 => 30,
7 => 31,
8 => 31,
9 => 30,
10 => 31,
11 => 30,
12 => 31,
_ => unreachable!("Unknown month"),
}
}
impl Default for ColorDateTime {
fn default() -> Self {
Self::now()
}
}
impl ColorDateTime {
pub fn new_from_slice(slice: &[u8]) -> Result<ColorDateTime, CmsError> {
if slice.len() != 12 {
return Err(CmsError::InvalidProfile);
}
let year = u16::from_be_bytes([slice[0], slice[1]]);
let month = u16::from_be_bytes([slice[2], slice[3]]);
let day_of_the_month = u16::from_be_bytes([slice[4], slice[5]]);
let hours = u16::from_be_bytes([slice[6], slice[7]]);
let minutes = u16::from_be_bytes([slice[8], slice[9]]);
let seconds = u16::from_be_bytes([slice[10], slice[11]]);
Ok(ColorDateTime {
year,
month,
day_of_the_month,
hours,
minutes,
seconds,
})
}
pub fn now() -> Self {
#[cfg(not(target_arch = "wasm32"))]
let now = match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(v) => v,
Err(_) => return Self::default(),
};
#[cfg(target_arch = "wasm32")]
use std::time::Duration;
#[cfg(target_arch = "wasm32")]
let now = Duration::new(365 * 60 * 60 * 24 * 30, 0);
let mut days = (now.as_secs() / 86_400) as i64;
let secs_of_day = (now.as_secs() % 86_400) as i64;
let mut year = 1970;
loop {
let year_days = if is_leap(year) { 366 } else { 365 };
if days >= year_days {
days -= year_days;
year += 1;
} else {
break;
}
}
let mut month = 1;
loop {
let mdays = days_in_month(year, month);
if days >= mdays as i64 {
days -= mdays as i64;
month += 1;
} else {
break;
}
}
let day = days + 1;
let hour = secs_of_day / 3600;
let min = (secs_of_day % 3600) / 60;
let sec = secs_of_day % 60;
Self {
year: year as u16,
month: month as u16,
day_of_the_month: day as u16,
hours: hour as u16,
minutes: min as u16,
seconds: sec as u16,
}
}
#[inline]
pub(crate) fn encode(&self, into: &mut Vec<u8>) {
let year = self.year;
let month = self.month;
let day_of_the_month = self.day_of_the_month;
let hours = self.hours;
let minutes = self.minutes;
let seconds = self.seconds;
write_u16_be(into, year);
write_u16_be(into, month);
write_u16_be(into, day_of_the_month);
write_u16_be(into, hours);
write_u16_be(into, minutes);
write_u16_be(into, seconds);
}
}