timex_datalink/protocol_4/eeprom/
appointment.rs

1//! Appointment implementation for Protocol 4 EEPROM
2//!
3//! This module handles appointments stored in the watch's EEPROM.
4
5use std::time::SystemTime;
6use chrono::{Datelike, Timelike};
7use crate::char_encoders::EepromString;
8
9/// Appointment structure for Protocol 4 EEPROM
10pub struct Appointment {
11    /// Time of appointment
12    pub time: SystemTime,
13    
14    /// Appointment message text (EEPROM encoded, max 31 characters)
15    pub message: EepromString,
16}
17
18impl Appointment {
19    /// Create the packet for an appointment, similar to Ruby's packet method
20    /// 
21    /// This returns the raw packet bytes without the length prefix
22    fn packet_content(&self) -> Vec<u8> {
23        // Extract time components from SystemTime
24        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        // Calculate time_15m (each 15-minute slot of the day)
39        let time_15m = hour * 4 + minute / 15;
40        
41        // Create the packet
42        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    /// Create the full packet including length prefix (LengthPacketWrapper in Ruby)
52    pub fn packet(&self) -> Vec<u8> {
53        let content = self.packet_content();
54        let mut result = Vec::with_capacity(content.len() + 1);
55        
56        // Add length byte (content length + 1 for the length byte itself)
57        result.push((content.len() + 1) as u8);
58        result.extend(content);
59        
60        result
61    }
62}