1use crate::error::{Error, Result};
7use crate::traits::Table;
8use dvb_common::{Parse, Serialize};
9
10pub const TABLE_ID: u8 = 0x70;
12pub const PID: u16 = 0x0014;
14
15const HEADER_LEN: usize = 3;
16const UTC_TIME_LEN: usize = 5;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize))]
21pub struct TdtSection {
22 pub utc_time_raw: [u8; 5],
25}
26
27#[cfg(feature = "chrono")]
28impl TdtSection {
29 #[must_use]
33 pub fn utc_time(&self) -> Option<chrono::DateTime<chrono::Utc>> {
34 dvb_common::time::decode_mjd_bcd_utc(self.utc_time_raw)
35 }
36
37 pub fn set_utc_time(&mut self, utc_time: chrono::DateTime<chrono::Utc>) -> Result<()> {
43 self.utc_time_raw =
44 dvb_common::time::encode_mjd_bcd_utc(utc_time).ok_or(Error::ValueOutOfRange {
45 field: "TdtSection::utc_time",
46 reason: "date not representable in 16-bit MJD",
47 })?;
48 Ok(())
49 }
50}
51
52impl<'a> Parse<'a> for TdtSection {
53 type Error = crate::error::Error;
54 fn parse(bytes: &'a [u8]) -> Result<Self> {
55 let min_len = HEADER_LEN + UTC_TIME_LEN;
56 if bytes.len() < min_len {
57 return Err(Error::BufferTooShort {
58 need: min_len,
59 have: bytes.len(),
60 what: "TdtSection",
61 });
62 }
63 if bytes[0] != TABLE_ID {
64 return Err(Error::UnexpectedTableId {
65 table_id: bytes[0],
66 what: "TdtSection",
67 expected: &[TABLE_ID],
68 });
69 }
70 let section_length = ((bytes[1] & 0x0F) as u16) << 8 | bytes[2] as u16;
71 if section_length as usize != UTC_TIME_LEN {
72 return Err(Error::SectionLengthOverflow {
73 declared: section_length as usize,
74 available: UTC_TIME_LEN,
75 });
76 }
77 let utc_time_raw = [bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]];
78 Ok(TdtSection { utc_time_raw })
79 }
80}
81
82impl Serialize for TdtSection {
83 type Error = crate::error::Error;
84 fn serialized_len(&self) -> usize {
85 HEADER_LEN + UTC_TIME_LEN
86 }
87 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
88 let len = self.serialized_len();
89 if buf.len() < len {
90 return Err(Error::OutputBufferTooSmall {
91 need: len,
92 have: buf.len(),
93 });
94 }
95 buf[0] = TABLE_ID;
96 buf[1] = 0x70 | ((UTC_TIME_LEN as u16 >> 8) as u8 & 0x0F);
97 buf[2] = UTC_TIME_LEN as u8;
98 buf[3..8].copy_from_slice(&self.utc_time_raw);
99 Ok(len)
100 }
101}
102
103impl<'a> Table<'a> for TdtSection {
104 const TABLE_ID: u8 = TABLE_ID;
105 const PID: u16 = PID;
106}
107
108impl<'a> crate::traits::TableDef<'a> for TdtSection {
109 const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
110 const NAME: &'static str = "TIME_AND_DATE";
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116
117 #[test]
118 fn parse_extracts_utc_time_raw() {
119 let bytes = [TABLE_ID, 0x70, 0x05, 0xE4, 0x09, 0x12, 0x34, 0x56];
120 let tdt = TdtSection::parse(&bytes).unwrap();
121 assert_eq!(tdt.utc_time_raw, [0xE4, 0x09, 0x12, 0x34, 0x56]);
122 }
123
124 #[test]
125 fn parse_rejects_wrong_tag() {
126 let bytes = [0x71, 0x70, 0x05, 0, 0, 0, 0, 0];
127 assert!(matches!(
128 TdtSection::parse(&bytes).unwrap_err(),
129 Error::UnexpectedTableId { table_id: 0x71, .. }
130 ));
131 }
132
133 #[test]
134 fn parse_rejects_wrong_section_length() {
135 let bytes = [TABLE_ID, 0x70, 0x04, 0, 0, 0, 0, 0];
136 assert!(matches!(
137 TdtSection::parse(&bytes).unwrap_err(),
138 Error::SectionLengthOverflow { .. }
139 ));
140 }
141
142 #[test]
143 fn serialize_round_trip() {
144 let tdt = TdtSection {
145 utc_time_raw: [0xE4, 0x09, 0x12, 0x34, 0x56],
146 };
147 let mut buf = vec![0u8; tdt.serialized_len()];
148 tdt.serialize_into(&mut buf).unwrap();
149 let re = TdtSection::parse(&buf).unwrap();
150 assert_eq!(tdt, re);
151 }
152
153 #[test]
154 fn utc_time_decodes_to_chrono() {
155 let tdt = TdtSection {
156 utc_time_raw: [0xEA, 0x19, 0x12, 0x34, 0x56],
157 };
158 let dt = tdt.utc_time();
159 assert!(dt.is_some());
160 }
161}