1use crate::{
2 AsciiStr, Drift, Dt, Every, Meridiem, Offset, Scale, Spacetime, TimeParts, TimeRange, Weekday,
3 YmdHmsRich,
4};
5
6impl Dt {
7 pub const WIRE_VERSION: u8 = 1;
9
10 pub const WIRE_SIZE: usize = 17;
12
13 pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
24 let mut buf = [0u8; Self::WIRE_SIZE];
25 buf[0] = Self::WIRE_VERSION;
26 buf[1..9].copy_from_slice(&self.sec.to_le_bytes());
27 buf[9..17].copy_from_slice(&self.attos.to_le_bytes());
28 buf
29 }
30
31 pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
44 if bytes.len() != Self::WIRE_SIZE {
45 return None;
46 }
47
48 if bytes[0] != Self::WIRE_VERSION {
49 return None;
50 }
51
52 let sec = i64::from_le_bytes([
53 bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8],
54 ]);
55 let subsec = u64::from_le_bytes([
56 bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15], bytes[16],
57 ]);
58
59 Some(Self::new(sec, subsec))
60 }
61}
62
63impl Drift {
64 pub const WIRE_VERSION: u8 = 1;
66
67 pub const WIRE_SIZE: usize = 3 * Dt::WIRE_SIZE; pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
74 let mut buf = [0u8; Self::WIRE_SIZE];
75 let c = self.constant.to_wire_bytes();
76 let r = self.rate.to_wire_bytes();
77 let a = self.accel.to_wire_bytes();
78
79 buf[0..Dt::WIRE_SIZE].copy_from_slice(&c);
80 buf[Dt::WIRE_SIZE..2 * Dt::WIRE_SIZE].copy_from_slice(&r);
81 buf[2 * Dt::WIRE_SIZE..].copy_from_slice(&a);
82 buf
83 }
84
85 pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
97 if bytes.len() != Self::WIRE_SIZE {
98 return None;
99 }
100
101 if bytes[0] != Self::WIRE_VERSION {
102 return None;
103 }
104
105 let constant = Dt::from_wire_bytes(&bytes[0..Dt::WIRE_SIZE])?;
106 let rate = Dt::from_wire_bytes(&bytes[Dt::WIRE_SIZE..2 * Dt::WIRE_SIZE])?;
107 let accel = Dt::from_wire_bytes(&bytes[2 * Dt::WIRE_SIZE..])?;
108
109 Some(Self::new(constant, rate, accel))
110 }
111}
112
113impl Spacetime {
114 pub const WIRE_SIZE: usize = 24;
116
117 pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
121 let mut buf = [0u8; Self::WIRE_SIZE];
122 buf[0..8].copy_from_slice(&self.alpha.to_le_bytes());
123 buf[8..16].copy_from_slice(&self.beta.to_le_bytes());
124 buf[16..24].copy_from_slice(&self.kretschmann.to_le_bytes());
125 buf
126 }
127
128 pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
136 if bytes.len() != Self::WIRE_SIZE {
137 return None;
138 }
139 let alpha = f64::from_le_bytes([
140 bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
141 ]);
142 let beta = f64::from_le_bytes([
143 bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15],
144 ]);
145 let kretschmann = f64::from_le_bytes([
146 bytes[16], bytes[17], bytes[18], bytes[19], bytes[20], bytes[21], bytes[22], bytes[23],
147 ]);
148 Some(Self {
149 alpha,
150 beta,
151 kretschmann,
152 })
153 }
154}
155
156impl Every {
157 pub const WIRE_SIZE: usize = Dt::WIRE_SIZE + Dt::WIRE_SIZE;
159
160 pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
164 let mut buf = [0u8; Self::WIRE_SIZE];
165 let start = self.start.to_wire_bytes();
166 let step = self.step.to_wire_bytes();
167 buf[0..17].copy_from_slice(&start);
168 buf[17..33].copy_from_slice(&step);
169 buf
170 }
171
172 pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
179 if bytes.len() != Self::WIRE_SIZE {
180 return None;
181 }
182 let start = Dt::from_wire_bytes(&bytes[0..17])?;
183 let step = Dt::from_wire_bytes(&bytes[17..33])?;
184 Some(Self { start, step })
185 }
186}
187
188impl TimeRange {
189 pub const WIRE_VERSION: u8 = 1;
191
192 pub const WIRE_SIZE: usize = 1 + 2 * Dt::WIRE_SIZE + Dt::WIRE_SIZE + 1;
195
196 pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
203 let mut buf = [0u8; Self::WIRE_SIZE];
204 buf[0] = Self::WIRE_VERSION;
205
206 let start = self.start.to_wire_bytes();
207 let end = self.end.to_wire_bytes();
208 let step = self.step.to_wire_bytes();
209
210 let tp_size = Dt::WIRE_SIZE;
211 let span_size = Dt::WIRE_SIZE;
212
213 buf[1..1 + tp_size].copy_from_slice(&start);
214 buf[1 + tp_size..1 + 2 * tp_size].copy_from_slice(&end);
215 buf[1 + 2 * tp_size..1 + 2 * tp_size + span_size].copy_from_slice(&step);
216 buf[1 + 2 * tp_size + span_size] = if self.inclusive { 1 } else { 0 };
217
218 buf
219 }
220
221 pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
233 if bytes.len() != Self::WIRE_SIZE {
234 return None;
235 }
236
237 if bytes[0] != Self::WIRE_VERSION {
238 return None;
239 }
240
241 let tp_size = Dt::WIRE_SIZE;
242 let span_size = Dt::WIRE_SIZE;
243
244 let start = Dt::from_wire_bytes(&bytes[1..1 + tp_size])?;
245 let end = Dt::from_wire_bytes(&bytes[1 + tp_size..1 + 2 * tp_size])?;
246 let step = Dt::from_wire_bytes(&bytes[1 + 2 * tp_size..1 + 2 * tp_size + span_size])?;
247 let inclusive = bytes[1 + 2 * tp_size + span_size] != 0;
248
249 Some(Self::new(start, end, step, inclusive))
250 }
251}
252
253impl YmdHmsRich {
254 pub const WIRE_VERSION: u8 = 1;
256
257 pub const WIRE_SIZE: usize = 159;
259
260 pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
279 let mut buf = [0u8; Self::WIRE_SIZE];
280 buf[0] = Self::WIRE_VERSION;
281 let mut offset = 1usize;
282
283 buf[offset..offset + 16].copy_from_slice(&self.unix_attosec.to_le_bytes());
285 offset += 16;
286
287 buf[offset..offset + 8].copy_from_slice(&self.yr.to_le_bytes());
289 offset += 8;
290
291 buf[offset] = self.mo;
293 offset += 1;
294 buf[offset] = self.day;
295 offset += 1;
296 buf[offset] = self.hr;
297 offset += 1;
298 buf[offset] = self.min;
299 offset += 1;
300 buf[offset] = self.sec;
301 offset += 1;
302
303 buf[offset..offset + 8].copy_from_slice(&self.attos.to_le_bytes());
305 offset += 8;
306
307 buf[offset..offset + 8].copy_from_slice(&self.iso_yr.to_le_bytes());
309 offset += 8;
310
311 buf[offset] = self.iso_wk;
313 offset += 1;
314 buf[offset] = self.iso_wkday.to_wire_byte();
315 offset += 1;
316
317 buf[offset..offset + 2].copy_from_slice(&self.day_of_yr.to_le_bytes());
319 offset += 2;
320
321 buf[offset] = self.wkday;
323 offset += 1;
324
325 buf[offset] = self.wk_of_yr_sun;
327 offset += 1;
328 buf[offset] = self.wk_of_yr_mon;
329 offset += 1;
330
331 if let Some(val) = self.offset_sec {
333 buf[offset] = 1;
334 buf[offset + 1..offset + 5].copy_from_slice(&val.to_le_bytes());
335 } else {
336 buf[offset] = 0;
337 }
338 offset += 5;
339
340 if let Some(tz) = &self.tz {
342 buf[offset] = 1;
343 let tz_bytes = tz.to_wire_bytes();
344 buf[offset + 1..offset + 1 + AsciiStr::<49>::WIRE_SIZE].copy_from_slice(&tz_bytes);
345 } else {
346 buf[offset] = 0;
347 }
348 offset += 1 + AsciiStr::<49>::WIRE_SIZE;
349
350 if let Some(abbrev) = &self.tz_abbrev {
352 buf[offset] = 1;
353 let abbrev_bytes = abbrev.to_wire_bytes();
354 buf[offset + 1..offset + 1 + AsciiStr::<49>::WIRE_SIZE].copy_from_slice(&abbrev_bytes);
355 } else {
356 buf[offset] = 0;
357 }
358 offset += 1 + AsciiStr::<49>::WIRE_SIZE;
359
360 buf[offset] = self.scale.to_wire_byte();
362
363 buf
364 }
365
366 pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
375 if bytes.len() != Self::WIRE_SIZE {
376 return None;
377 }
378 if bytes[0] != Self::WIRE_VERSION {
379 return None;
380 }
381
382 let mut offset = 1usize;
383
384 let unix_attosec = i128::from_le_bytes(bytes[offset..offset + 16].try_into().ok()?);
386 offset += 16;
387
388 let yr = i64::from_le_bytes(bytes[offset..offset + 8].try_into().ok()?);
390 offset += 8;
391
392 let mo = bytes[offset];
394 offset += 1;
395 let day = bytes[offset];
396 offset += 1;
397 let hr = bytes[offset];
398 offset += 1;
399 let min = bytes[offset];
400 offset += 1;
401 let sec = bytes[offset];
402 offset += 1;
403
404 let attos = u64::from_le_bytes(bytes[offset..offset + 8].try_into().ok()?);
406 offset += 8;
407
408 let iso_yr = i64::from_le_bytes(bytes[offset..offset + 8].try_into().ok()?);
410 offset += 8;
411
412 let iso_wk = bytes[offset];
414 offset += 1;
415 let iso_wkday = Weekday::from_wire_byte(bytes[offset])?;
416 offset += 1;
417
418 let day_of_yr = u16::from_le_bytes(bytes[offset..offset + 2].try_into().ok()?);
420 offset += 2;
421
422 let wkday = bytes[offset];
424 offset += 1;
425
426 let wk_of_yr_sun = bytes[offset];
428 offset += 1;
429 let wk_of_yr_mon = bytes[offset];
430 offset += 1;
431
432 let offset_sec = if bytes[offset] == 1 {
434 Some(i32::from_le_bytes(
435 bytes[offset + 1..offset + 5].try_into().ok()?,
436 ))
437 } else {
438 None
439 };
440 offset += 5;
441
442 let tz = if bytes[offset] == 1 {
444 AsciiStr::<49>::from_wire_bytes(
445 &bytes[offset + 1..offset + 1 + AsciiStr::<49>::WIRE_SIZE],
446 )
447 } else {
448 None
449 };
450 offset += 1 + AsciiStr::<49>::WIRE_SIZE;
451
452 let tz_abbrev = if bytes[offset] == 1 {
454 AsciiStr::<49>::from_wire_bytes(
455 &bytes[offset + 1..offset + 1 + AsciiStr::<49>::WIRE_SIZE],
456 )
457 } else {
458 None
459 };
460 offset += 1 + AsciiStr::<49>::WIRE_SIZE;
461
462 let scale = Scale::from_u8(bytes[offset]);
464
465 Some(Self {
466 unix_attosec,
467 yr,
468 mo,
469 day,
470 hr,
471 min,
472 sec,
473 attos,
474 iso_yr,
475 iso_wk,
476 iso_wkday,
477 day_of_yr,
478 wkday,
479 wk_of_yr_sun,
480 wk_of_yr_mon,
481 offset_sec,
482 tz,
483 tz_abbrev,
484 scale,
485 })
486 }
487}
488
489impl Meridiem {
490 pub const WIRE_SIZE: usize = 1;
491
492 #[inline]
493 pub const fn to_wire_byte(self) -> u8 {
494 match self {
495 Meridiem::AM => 0,
496 Meridiem::PM => 1,
497 }
498 }
499
500 #[inline]
501 pub const fn from_wire_byte(b: u8) -> Option<Self> {
502 match b {
503 0 => Some(Meridiem::AM),
504 1 => Some(Meridiem::PM),
505 _ => None,
506 }
507 }
508}
509
510impl Offset {
511 pub const WIRE_SIZE: usize = 5; pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
514 let mut buf = [0u8; Self::WIRE_SIZE];
515 match self {
516 Offset::Utc => buf[0] = 0,
517 Offset::None => buf[0] = 1,
518 Offset::Fixed(offset) => {
519 buf[0] = 2;
520 buf[1..5].copy_from_slice(&offset.to_le_bytes());
521 }
522 }
523 buf
524 }
525
526 pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
527 if bytes.len() != Self::WIRE_SIZE {
528 return None;
529 }
530 match bytes[0] {
531 0 => Some(Offset::Utc),
532 1 => Some(Offset::None),
533 2 => {
534 let offset = i32::from_le_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]);
535 Some(Offset::Fixed(offset))
536 }
537 _ => None,
538 }
539 }
540}
541
542impl Weekday {
543 pub const WIRE_SIZE: usize = 1;
544
545 #[inline]
546 pub const fn to_wire_byte(self) -> u8 {
547 self.wk_sun()
548 }
549
550 #[inline]
551 pub const fn from_wire_byte(b: u8) -> Option<Self> {
552 Self::from_sunday_zero_offset(b)
553 }
554}
555
556impl TimeParts {
557 pub const WIRE_VERSION: u8 = 1;
559
560 pub const WIRE_SIZE: usize = 120;
562
563 pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
569 let mut buf = [0u8; Self::WIRE_SIZE];
570 buf[0] = Self::WIRE_VERSION;
571
572 let mut offset = 1usize;
573
574 let year = self.yr.unwrap_or(i64::MIN);
576 buf[offset..offset + 8].copy_from_slice(&year.to_le_bytes());
577 offset += 8;
578
579 buf[offset] = self.mo.unwrap_or(u8::MAX);
581 offset += 1;
582
583 buf[offset] = self.day.unwrap_or(u8::MAX);
585 offset += 1;
586
587 buf[offset] = self.hr.unwrap_or(u8::MAX);
589 offset += 1;
590
591 buf[offset] = self.min.unwrap_or(u8::MAX);
593 offset += 1;
594
595 buf[offset] = self.sec.unwrap_or(u8::MAX);
597 offset += 1;
598
599 let attos = self.attos.unwrap_or(u64::MAX);
601 buf[offset..offset + 8].copy_from_slice(&attos.to_le_bytes());
602 offset += 8;
603
604 let offset_bytes = self.offset.unwrap_or_default().to_wire_bytes();
606 buf[offset..offset + 5].copy_from_slice(&offset_bytes);
607 offset += 5;
608
609 if let Some(name) = &self.iana_name {
611 let name_bytes = name.to_wire_bytes();
612 buf[offset..offset + 49].copy_from_slice(&name_bytes);
613 }
614 offset += 49;
615
616 buf[offset] = if self.is_leap_sec { 1 } else { 0 };
618 offset += 1;
619
620 buf[offset] = self.scale as u8;
622 offset += 1;
623
624 buf[offset] = self.wkday.map_or(255, |w| w.to_wire_byte());
626 offset += 1;
627
628 let doy = self.day_of_yr.unwrap_or(u16::MAX);
630 buf[offset..offset + 2].copy_from_slice(&doy.to_le_bytes());
631 offset += 2;
632
633 let iso_y = self.iso_wk_yr.unwrap_or(i64::MIN);
635 buf[offset..offset + 8].copy_from_slice(&iso_y.to_le_bytes());
636 offset += 8;
637
638 buf[offset] = self.iso_wk.unwrap_or(u8::MAX);
640 offset += 1;
641
642 buf[offset] = self.wk_sun.unwrap_or(u8::MAX);
644 offset += 1;
645
646 buf[offset] = self.wk_mon.unwrap_or(u8::MAX);
648 offset += 1;
649
650 buf[offset] = self.meridiem.map_or(255, |m| m.to_wire_byte());
652 offset += 1;
653
654 let unix = self.unix_timestamp_seconds.unwrap_or(i64::MIN);
656 buf[offset..offset + 8].copy_from_slice(&unix.to_le_bytes());
657
658 buf
659 }
660
661 pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
665 if bytes.len() != Self::WIRE_SIZE {
666 return None;
667 }
668 if bytes[0] != Self::WIRE_VERSION {
669 return None;
670 }
671
672 let mut dc = TimeParts::default();
673 let mut offset = 1usize;
674
675 let year = i64::from_le_bytes(bytes[offset..offset + 8].try_into().ok()?);
677 if year != i64::MIN {
678 dc.yr = Some(year);
679 }
680 offset += 8;
681
682 let m = bytes[offset];
684 if m != u8::MAX {
685 dc.mo = Some(m);
686 }
687 offset += 1;
688
689 let d = bytes[offset];
691 if d != u8::MAX {
692 dc.day = Some(d);
693 }
694 offset += 1;
695
696 let h = bytes[offset];
698 if h != u8::MAX {
699 dc.hr = Some(h);
700 }
701 offset += 1;
702
703 let min = bytes[offset];
705 if min != u8::MAX {
706 dc.min = Some(min);
707 }
708 offset += 1;
709
710 let sec = bytes[offset];
712 if sec != u8::MAX {
713 dc.sec = Some(sec);
714 }
715 offset += 1;
716
717 let attos = u64::from_le_bytes(bytes[offset..offset + 8].try_into().ok()?);
719 if attos != u64::MAX {
720 dc.attos = Some(attos);
721 }
722 offset += 8;
723
724 if let Some(offset) = Offset::from_wire_bytes(&bytes[offset..offset + 5]) {
726 dc.offset = Some(offset);
727 }
728 offset += 5;
729
730 let iana_bytes = &bytes[offset..offset + 49];
732 if let Some(name) = AsciiStr::<49>::from_wire_bytes(iana_bytes)
733 && !name.is_empty()
734 {
735 dc.iana_name = Some(name);
736 }
737 offset += 49;
738
739 dc.is_leap_sec = bytes[offset] != 0;
741 offset += 1;
742
743 dc.scale = Scale::from_u8(bytes[offset]);
745 offset += 1;
746
747 let wd_byte = bytes[offset];
749 if wd_byte != 255
750 && let Some(wd) = Weekday::from_wire_byte(wd_byte)
751 {
752 dc.wkday = Some(wd);
753 }
754 offset += 1;
755
756 let doy = u16::from_le_bytes(bytes[offset..offset + 2].try_into().ok()?);
758 if doy != u16::MAX {
759 dc.day_of_yr = Some(doy);
760 }
761 offset += 2;
762
763 let iso_y = i64::from_le_bytes(bytes[offset..offset + 8].try_into().ok()?);
765 if iso_y != i64::MIN {
766 dc.iso_wk_yr = Some(iso_y);
767 }
768 offset += 8;
769
770 let iw = bytes[offset];
772 if iw != u8::MAX {
773 dc.iso_wk = Some(iw);
774 }
775 offset += 1;
776
777 let ws = bytes[offset];
779 if ws != u8::MAX {
780 dc.wk_sun = Some(ws);
781 }
782 offset += 1;
783
784 let wm = bytes[offset];
786 if wm != u8::MAX {
787 dc.wk_mon = Some(wm);
788 }
789 offset += 1;
790
791 let mer_byte = bytes[offset];
793 if mer_byte != 255
794 && let Some(m) = Meridiem::from_wire_byte(mer_byte)
795 {
796 dc.meridiem = Some(m);
797 }
798
799 offset += 1;
800
801 let unix = i64::from_le_bytes(bytes[offset..offset + 8].try_into().ok()?);
803 if unix != i64::MIN {
804 dc.unix_timestamp_seconds = Some(unix);
805 }
806
807 Some(dc)
808 }
809}