timex_datalink/protocol_4/eeprom/
appointment.rs1use std::time::SystemTime;
6use chrono::{Datelike, Timelike};
7use crate::char_encoders::EepromString;
8
9pub struct Appointment {
11 pub time: SystemTime,
13
14 pub message: EepromString,
16}
17
18impl Appointment {
19 fn packet_content(&self) -> Vec<u8> {
23 let duration_since_epoch = self.time
25 .duration_since(std::time::UNIX_EPOCH)
26 .expect("Time went backwards");
27
28 let datetime = chrono::DateTime::<chrono::Utc>::from_timestamp(
29 duration_since_epoch.as_secs() as i64,
30 0
31 ).expect("Invalid timestamp");
32
33 let month = datetime.month() as u8;
34 let day = datetime.day() as u8;
35 let hour = datetime.hour() as u8;
36 let minute = datetime.minute() as u8;
37
38 let time_15m = hour * 4 + minute / 15;
40
41 let mut packet = Vec::new();
43 packet.push(month);
44 packet.push(day);
45 packet.push(time_15m);
46 packet.extend_from_slice(self.message.as_bytes());
47
48 packet
49 }
50
51 pub fn packet(&self) -> Vec<u8> {
53 let content = self.packet_content();
54 let mut result = Vec::with_capacity(content.len() + 1);
55
56 result.push((content.len() + 1) as u8);
58 result.extend(content);
59
60 result
61 }
62}