ocpp-types 0.3.0

Strongly typed OCPP 1.6J, 2.0.1, and 2.1 message types for Rust. no_std, no alloc, embedded-friendly.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
//! A fixed-size timestamp for OCPP's `dateTime` fields.
//!
//! Every OCPP version types its timestamps identically -- `{"type":
//! "string", "format": "date-time"}` -- and not one of the 142 such fields
//! across 1.6J, 2.0.1 and 2.1 declares a `maxLength`. Treated as an
//! unbounded string, each reserved the generator's 1024-byte default, which
//! is how `v16::HeartbeatResponse` came to be 1,032 bytes to carry a
//! 24-character instant.
//!
//! [`OcppTimestamp`] stores the instant decomposed instead: 16 bytes, no
//! allocator, and comparable, which a string is not.
//!
//! # Representation
//!
//! The instant is Unix seconds plus nanoseconds, with the UTC offset kept
//! alongside so a local-time timestamp survives a round trip in the form the
//! peer sent it. The offset is *presentation*, not identity: two values
//! naming the same instant in different offsets are equal and compare equal,
//! because they are the same moment. Use [`OcppTimestamp::utc_offset_minutes`]
//! when the written form matters.
//!
//! # Format
//!
//! Parsing accepts RFC 3339 as the specification requires, with `T`/`t`/` `
//! as the date-time separator and `Z`/`z` or `±HH:MM` as the offset.
//! Fractional seconds of any length are accepted and kept to nanosecond
//! precision.
//!
//! Writing emits a canonical RFC 3339 form: `Z` for UTC, `±HH:MM` otherwise,
//! and fractional seconds only when non-zero (trimmed to milliseconds when
//! they divide evenly, which is what OCPP deployments use in practice).

use core::fmt;

/// The widest string [`OcppTimestamp::to_rfc3339`] can produce:
/// `-262143-01-01T00:00:00.123456789+01:00`. Callers sizing their own buffer
/// should use this.
pub const MAX_RFC3339_LEN: usize = 40;

/// A `dateTime` value from any OCPP version.
///
/// See this module's documentation for the representation and its
/// equality semantics.
#[derive(Debug, Clone, Copy)]
pub struct OcppTimestamp {
    /// Seconds since the Unix epoch, UTC.
    secs: i64,
    /// Nanoseconds within the second, `0..1_000_000_000`.
    nanos: u32,
    /// Offset from UTC in minutes, as written by the peer.
    offset_minutes: i16,
}

/// Why a string is not an OCPP `dateTime`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TimestampError {
    /// Not RFC 3339 shaped -- wrong length, wrong separators, or a
    /// non-digit where a digit belongs.
    Malformed,
    /// Shaped correctly but not a real instant, e.g. `2024-02-30` or an
    /// hour of 24.
    OutOfRange,
}

impl fmt::Display for TimestampError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Malformed => "not an RFC 3339 date-time",
            Self::OutOfRange => "RFC 3339 date-time with an out-of-range field",
        })
    }
}

impl core::error::Error for TimestampError {}

/// Days from the Unix epoch to the given civil date, by Howard Hinnant's
/// `days_from_civil`. Valid for any proleptic Gregorian date; the era
/// arithmetic relies on truncating division, which is what Rust's `/` does.
const fn days_from_civil(year: i64, month: u32, day: u32) -> i64 {
    let year = year - if month <= 2 { 1 } else { 0 };
    let era = if year >= 0 { year } else { year - 399 } / 400;
    let year_of_era = year - era * 400;
    let month = month as i64;
    let day_of_year = (153 * (month + if month > 2 { -3 } else { 9 }) + 2) / 5 + day as i64 - 1;
    let day_of_era =
        year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;

    era * 146097 + day_of_era - 719468
}

/// Inverse of [`days_from_civil`].
const fn civil_from_days(days: i64) -> (i64, u32, u32) {
    let days = days + 719468;
    let era = if days >= 0 { days } else { days - 146096 } / 146097;
    let day_of_era = days - era * 146097;
    let year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36524
        - day_of_era / 146096)
        / 365;
    let year = year_of_era + era * 400;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let mp = (5 * day_of_year + 2) / 153;
    let day = (day_of_year - (153 * mp + 2) / 5 + 1) as u32;
    let month = (mp + if mp < 10 { 3 } else { -9 }) as u32;

    (year + if month <= 2 { 1 } else { 0 }, month, day)
}

const fn is_leap(year: i64) -> bool {
    (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}

const fn days_in_month(year: i64, month: u32) -> u32 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 if is_leap(year) => 29,
        2 => 28,
        _ => 0,
    }
}

impl OcppTimestamp {
    /// The Unix epoch, `1970-01-01T00:00:00Z`.
    pub const UNIX_EPOCH: Self = Self {
        secs: 0,
        nanos: 0,
        offset_minutes: 0,
    };

    /// Builds a UTC timestamp from Unix seconds and nanoseconds.
    ///
    /// Returns [`TimestampError::OutOfRange`] if `nanos` is not a valid
    /// subsecond value.
    pub const fn from_unix(secs: i64, nanos: u32) -> Result<Self, TimestampError> {
        if nanos >= 1_000_000_000 {
            return Err(TimestampError::OutOfRange);
        }

        Ok(Self {
            secs,
            nanos,
            offset_minutes: 0,
        })
    }

    /// Seconds since the Unix epoch, UTC, regardless of the written offset.
    pub const fn unix_seconds(&self) -> i64 {
        self.secs
    }

    /// Nanoseconds within the second.
    pub const fn subsec_nanos(&self) -> u32 {
        self.nanos
    }

    /// The UTC offset this timestamp was written with, in minutes. Zero for
    /// a `Z` timestamp, and for anything built from Unix time.
    pub const fn utc_offset_minutes(&self) -> i16 {
        self.offset_minutes
    }

    /// Returns the same instant, to be written with `offset_minutes` instead.
    ///
    /// Returns [`TimestampError::OutOfRange`] for an offset beyond ±24 hours.
    pub const fn with_utc_offset_minutes(self, offset_minutes: i16) -> Result<Self, TimestampError> {
        if offset_minutes <= -1440 || offset_minutes >= 1440 {
            return Err(TimestampError::OutOfRange);
        }

        Ok(Self {
            offset_minutes,
            ..self
        })
    }

    /// Parses an RFC 3339 date-time, as every OCPP version's `dateTime`
    /// fields carry.
    pub fn parse_rfc3339(text: &str) -> Result<Self, TimestampError> {
        let bytes = text.as_bytes();

        // `YYYY-MM-DDTHH:MM:SS` is the shortest legal prefix, and an offset
        // (`Z` at minimum) is required by RFC 3339.
        if bytes.len() < 20 {
            return Err(TimestampError::Malformed);
        }

        let year = parse_number(&bytes[0..4])? as i64;
        expect(bytes[4], b'-')?;
        let month = parse_number(&bytes[5..7])?;
        expect(bytes[7], b'-')?;
        let day = parse_number(&bytes[8..10])?;

        match bytes[10] {
            b'T' | b't' | b' ' => {}
            _ => return Err(TimestampError::Malformed),
        }

        let hour = parse_number(&bytes[11..13])?;
        expect(bytes[13], b':')?;
        let minute = parse_number(&bytes[14..16])?;
        expect(bytes[16], b':')?;
        let second = parse_number(&bytes[17..19])?;

        let mut cursor = 19;
        let mut nanos = 0u32;

        if bytes[cursor] == b'.' || bytes[cursor] == b',' {
            cursor += 1;
            let start = cursor;

            while cursor < bytes.len() && bytes[cursor].is_ascii_digit() {
                // Only the first nine digits are representable; the rest are
                // still consumed so the offset parses from the right place.
                if cursor - start < 9 {
                    nanos = nanos * 10 + u32::from(bytes[cursor] - b'0');
                }
                cursor += 1;
            }

            let digits = cursor - start;

            if digits == 0 {
                return Err(TimestampError::Malformed);
            }

            // Scale a short fraction up to nanoseconds: `.5` is 500_000_000.
            for _ in digits.min(9)..9 {
                nanos *= 10;
            }
        }

        if cursor >= bytes.len() {
            return Err(TimestampError::Malformed);
        }

        let offset_minutes = match bytes[cursor] {
            b'Z' | b'z' if cursor + 1 == bytes.len() => 0i32,
            b'+' | b'-' => {
                if cursor + 6 != bytes.len() {
                    return Err(TimestampError::Malformed);
                }

                let sign = if bytes[cursor] == b'-' { -1i32 } else { 1 };
                let offset_hour = parse_number(&bytes[cursor + 1..cursor + 3])? as i32;
                expect(bytes[cursor + 3], b':')?;
                let offset_minute = parse_number(&bytes[cursor + 4..cursor + 6])? as i32;

                if offset_hour > 23 || offset_minute > 59 {
                    return Err(TimestampError::OutOfRange);
                }

                sign * (offset_hour * 60 + offset_minute)
            }
            _ => return Err(TimestampError::Malformed),
        };

        if !(1..=12).contains(&month) || day < 1 || day > days_in_month(year, month) {
            return Err(TimestampError::OutOfRange);
        }

        // A leap second is reported as `:60`. There is no representation for
        // it, so it clamps to the last representable instant of the minute
        // rather than being rejected -- a peer may legitimately send one.
        if hour > 23 || minute > 59 || second > 60 {
            return Err(TimestampError::OutOfRange);
        }

        let second = second.min(59);
        let days = days_from_civil(year, month, day);
        let secs = days * 86_400
            + i64::from(hour) * 3600
            + i64::from(minute) * 60
            + i64::from(second)
            - i64::from(offset_minutes) * 60;

        Ok(Self {
            secs,
            nanos,
            offset_minutes: offset_minutes as i16,
        })
    }

    /// Writes this timestamp as RFC 3339 into `buf`, returning the written
    /// slice.
    ///
    /// `buf` must be at least [`MAX_RFC3339_LEN`] bytes; a shorter buffer
    /// yields `None` rather than a truncated timestamp.
    pub fn to_rfc3339<'buf>(&self, buf: &'buf mut [u8]) -> Option<&'buf str> {
        if buf.len() < MAX_RFC3339_LEN {
            return None;
        }

        // Render in the written offset, not UTC.
        let local = self.secs + i64::from(self.offset_minutes) * 60;
        let days = local.div_euclid(86_400);
        let time_of_day = local.rem_euclid(86_400);
        let (year, month, day) = civil_from_days(days);

        let mut at = 0;

        write_year(buf, &mut at, year);
        buf[at] = b'-';
        at += 1;
        write_two(buf, &mut at, month as u64);
        buf[at] = b'-';
        at += 1;
        write_two(buf, &mut at, day as u64);
        buf[at] = b'T';
        at += 1;
        write_two(buf, &mut at, (time_of_day / 3600) as u64);
        buf[at] = b':';
        at += 1;
        write_two(buf, &mut at, (time_of_day % 3600 / 60) as u64);
        buf[at] = b':';
        at += 1;
        write_two(buf, &mut at, (time_of_day % 60) as u64);

        if self.nanos != 0 {
            buf[at] = b'.';
            at += 1;

            // Milliseconds when they divide evenly, which is what OCPP
            // deployments emit; full nanosecond precision otherwise.
            if self.nanos.is_multiple_of(1_000_000) {
                write_padded(buf, &mut at, u64::from(self.nanos / 1_000_000), 3);
            } else {
                write_padded(buf, &mut at, u64::from(self.nanos), 9);
            }
        }

        if self.offset_minutes == 0 {
            buf[at] = b'Z';
            at += 1;
        } else {
            let (sign, magnitude) = if self.offset_minutes < 0 {
                (b'-', -i32::from(self.offset_minutes))
            } else {
                (b'+', i32::from(self.offset_minutes))
            };

            buf[at] = sign;
            at += 1;
            write_two(buf, &mut at, (magnitude / 60) as u64);
            buf[at] = b':';
            at += 1;
            write_two(buf, &mut at, (magnitude % 60) as u64);
        }

        core::str::from_utf8(&buf[..at]).ok()
    }
}

fn expect(actual: u8, expected: u8) -> Result<(), TimestampError> {
    if actual == expected {
        Ok(())
    } else {
        Err(TimestampError::Malformed)
    }
}

fn parse_number(bytes: &[u8]) -> Result<u32, TimestampError> {
    let mut value = 0u32;

    for &byte in bytes {
        if !byte.is_ascii_digit() {
            return Err(TimestampError::Malformed);
        }

        value = value * 10 + u32::from(byte - b'0');
    }

    Ok(value)
}

fn write_two(buf: &mut [u8], at: &mut usize, value: u64) {
    write_padded(buf, at, value, 2);
}

fn write_padded(buf: &mut [u8], at: &mut usize, value: u64, width: usize) {
    let mut digits = [0u8; 20];
    let mut count = 0;
    let mut value = value;

    while value > 0 {
        digits[count] = b'0' + (value % 10) as u8;
        value /= 10;
        count += 1;
    }

    for _ in count..width {
        buf[*at] = b'0';
        *at += 1;
    }

    for index in (0..count).rev() {
        buf[*at] = digits[index];
        *at += 1;
    }
}

fn write_year(buf: &mut [u8], at: &mut usize, year: i64) {
    if year < 0 {
        buf[*at] = b'-';
        *at += 1;
        write_padded(buf, at, year.unsigned_abs(), 4);
    } else {
        write_padded(buf, at, year as u64, 4);
    }
}

impl fmt::Display for OcppTimestamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut buf = [0u8; MAX_RFC3339_LEN];

        match self.to_rfc3339(&mut buf) {
            Some(text) => f.write_str(text),
            None => Err(fmt::Error),
        }
    }
}

impl core::str::FromStr for OcppTimestamp {
    type Err = TimestampError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        Self::parse_rfc3339(text)
    }
}

impl TryFrom<&str> for OcppTimestamp {
    type Error = TimestampError;

    fn try_from(text: &str) -> Result<Self, Self::Error> {
        Self::parse_rfc3339(text)
    }
}

// Identity is the instant, not the way it was written: `2024-01-01T00:00:00Z`
// and `2024-01-01T01:00:00+01:00` are the same moment. Comparing the written
// offset too would make `Ord` disagree with what a reader means by "earlier".
impl PartialEq for OcppTimestamp {
    fn eq(&self, other: &Self) -> bool {
        self.secs == other.secs && self.nanos == other.nanos
    }
}

impl Eq for OcppTimestamp {}

impl core::hash::Hash for OcppTimestamp {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.secs.hash(state);
        self.nanos.hash(state);
    }
}

impl PartialOrd for OcppTimestamp {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for OcppTimestamp {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.secs
            .cmp(&other.secs)
            .then(self.nanos.cmp(&other.nanos))
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for OcppTimestamp {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut buf = [0u8; MAX_RFC3339_LEN];
        let text = self
            .to_rfc3339(&mut buf)
            .ok_or_else(|| serde::ser::Error::custom("timestamp is not representable"))?;

        serializer.serialize_str(text)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for OcppTimestamp {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct Visitor;

        impl<'v> serde::de::Visitor<'v> for Visitor {
            type Value = OcppTimestamp;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("an RFC 3339 date-time")
            }

            fn visit_str<E: serde::de::Error>(self, text: &str) -> Result<Self::Value, E> {
                OcppTimestamp::parse_rfc3339(text).map_err(serde::de::Error::custom)
            }
        }

        deserializer.deserialize_str(Visitor)
    }
}

/// Conversions to and from [`chrono`], for callers that already use it.
///
/// Deliberately an optional feature rather than the crate's representation:
/// chrono's own serde support formats through an allocating `to_rfc3339`,
/// which the no-`alloc` build -- the one these sizes matter most for -- cannot
/// use. Keeping chrono at the boundary gives its ergonomics to callers who
/// want them without putting an allocator on the path of everyone else.
#[cfg(feature = "chrono")]
mod chrono_interop {
    use super::OcppTimestamp;
    use chrono::{DateTime, FixedOffset, TimeZone, Utc};

    impl From<OcppTimestamp> for DateTime<Utc> {
        fn from(value: OcppTimestamp) -> Self {
            Utc.timestamp_opt(value.unix_seconds(), value.subsec_nanos())
                .single()
                .expect("an OcppTimestamp always names exactly one UTC instant")
        }
    }

    impl From<DateTime<Utc>> for OcppTimestamp {
        fn from(value: DateTime<Utc>) -> Self {
            OcppTimestamp::from_unix(value.timestamp(), value.timestamp_subsec_nanos())
                .expect("chrono keeps subsec nanos below one second")
        }
    }

    impl From<DateTime<FixedOffset>> for OcppTimestamp {
        fn from(value: DateTime<FixedOffset>) -> Self {
            let offset_minutes = (value.offset().local_minus_utc() / 60) as i16;

            OcppTimestamp::from_unix(value.timestamp(), value.timestamp_subsec_nanos())
                .expect("chrono keeps subsec nanos below one second")
                .with_utc_offset_minutes(offset_minutes)
                .expect("chrono offsets are within +/- 24 hours")
        }
    }

    impl From<OcppTimestamp> for DateTime<FixedOffset> {
        fn from(value: OcppTimestamp) -> Self {
            let offset = FixedOffset::east_opt(i32::from(value.utc_offset_minutes()) * 60)
                .expect("an OcppTimestamp offset is within +/- 24 hours");

            DateTime::<Utc>::from(value).with_timezone(&offset)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn rendered(text: &str) -> heapless::String<MAX_RFC3339_LEN> {
        let parsed = OcppTimestamp::parse_rfc3339(text).expect(text);
        let mut buf = [0u8; MAX_RFC3339_LEN];

        heapless::String::try_from(parsed.to_rfc3339(&mut buf).unwrap()).unwrap()
    }

    /// Anchors against instants whose Unix time is independently known, so a
    /// wrong civil-date algorithm can't pass by being self-consistent.
    #[test]
    fn parses_known_instants_to_their_known_unix_time() {
        for (text, secs) in [
            ("1970-01-01T00:00:00Z", 0),
            ("2000-03-01T00:00:00Z", 951_868_800),
            ("2024-02-29T12:00:00Z", 1_709_208_000),
            ("2038-01-19T03:14:07Z", 2_147_483_647),
            ("1969-12-31T23:59:59Z", -1),
            ("1900-01-01T00:00:00Z", -2_208_988_800),
        ] {
            let parsed = OcppTimestamp::parse_rfc3339(text).expect(text);
            assert_eq!(parsed.unix_seconds(), secs, "for {text}");
        }
    }

    #[test]
    fn round_trips_every_shape_the_spec_allows() {
        for text in [
            "2024-01-01T00:00:00Z",
            "2024-06-15T12:34:56Z",
            "2024-06-15T12:34:56.789Z",
            "1970-01-01T00:00:00Z",
            "2038-01-19T03:14:07Z",
        ] {
            assert_eq!(rendered(text).as_str(), text);
        }
    }

    #[test]
    fn accepts_the_separators_and_cases_rfc3339_permits() {
        let canonical = OcppTimestamp::parse_rfc3339("2024-01-01T00:00:00Z").unwrap();

        for text in [
            "2024-01-01t00:00:00Z",
            "2024-01-01 00:00:00Z",
            "2024-01-01T00:00:00z",
        ] {
            assert_eq!(OcppTimestamp::parse_rfc3339(text).unwrap(), canonical, "for {text}");
        }
    }

    #[test]
    fn an_offset_names_the_same_instant_as_its_utc_form() {
        let with_offset = OcppTimestamp::parse_rfc3339("2024-01-01T01:00:00+01:00").unwrap();
        let utc = OcppTimestamp::parse_rfc3339("2024-01-01T00:00:00Z").unwrap();

        assert_eq!(with_offset, utc);
        assert_eq!(with_offset.unix_seconds(), utc.unix_seconds());
        // ...but the written form is preserved, not normalized to UTC.
        assert_eq!(with_offset.utc_offset_minutes(), 60);
        assert_eq!(rendered("2024-01-01T01:00:00+01:00").as_str(), "2024-01-01T01:00:00+01:00");
        assert_eq!(rendered("2023-12-31T19:00:00-05:00").as_str(), "2023-12-31T19:00:00-05:00");
        assert_eq!(rendered("2024-01-01T00:30:00+05:30").as_str(), "2024-01-01T00:30:00+05:30");
    }

    /// A fraction is scaled by its digit count, not read as an integer:
    /// `.5` is 500ms, not 5ns.
    #[test]
    fn scales_fractional_seconds_by_their_length() {
        for (text, nanos) in [
            ("2024-01-01T00:00:00.5Z", 500_000_000),
            ("2024-01-01T00:00:00.05Z", 50_000_000),
            ("2024-01-01T00:00:00.123Z", 123_000_000),
            ("2024-01-01T00:00:00.000001Z", 1_000),
            ("2024-01-01T00:00:00.123456789Z", 123_456_789),
        ] {
            assert_eq!(
                OcppTimestamp::parse_rfc3339(text).unwrap().subsec_nanos(),
                nanos,
                "for {text}"
            );
        }

        // More than nanosecond precision is truncated, not rejected, and the
        // offset after it still parses.
        let over = OcppTimestamp::parse_rfc3339("2024-01-01T00:00:00.1234567891Z").unwrap();
        assert_eq!(over.subsec_nanos(), 123_456_789);
    }

    #[test]
    fn writes_milliseconds_when_they_divide_evenly_and_nanoseconds_otherwise() {
        assert_eq!(rendered("2024-01-01T00:00:00.250Z").as_str(), "2024-01-01T00:00:00.250Z");
        assert_eq!(
            rendered("2024-01-01T00:00:00.123456789Z").as_str(),
            "2024-01-01T00:00:00.123456789Z"
        );
        // A zero fraction is omitted entirely.
        assert_eq!(rendered("2024-01-01T00:00:00.000Z").as_str(), "2024-01-01T00:00:00Z");
    }

    #[test]
    fn rejects_dates_that_do_not_exist() {
        for text in [
            "2023-02-29T00:00:00Z", // 2023 is not a leap year
            "1900-02-29T00:00:00Z", // divisible by 100, not by 400
            "2024-13-01T00:00:00Z",
            "2024-04-31T00:00:00Z",
            "2024-01-01T24:00:00Z",
            "2024-01-01T00:60:00Z",
            "2024-01-01T00:00:00+24:00",
        ] {
            assert_eq!(
                OcppTimestamp::parse_rfc3339(text),
                Err(TimestampError::OutOfRange),
                "for {text}"
            );
        }

        // ...but 2000 and 2024 *are* leap years.
        assert!(OcppTimestamp::parse_rfc3339("2000-02-29T00:00:00Z").is_ok());
        assert!(OcppTimestamp::parse_rfc3339("2024-02-29T00:00:00Z").is_ok());
    }

    #[test]
    fn rejects_strings_that_are_not_rfc3339() {
        for text in [
            "",
            "2024-01-01",
            "2024-01-01T00:00:00",       // no offset
            "2024/01/01T00:00:00Z",
            "2024-01-01T00:00:00.Z",     // empty fraction
            "2024-01-01T00:00:00+0100",  // offset needs a colon
            "2024-01-01T00:00:00Zextra",
            "not-a-date-at-all!!!",
        ] {
            assert_eq!(
                OcppTimestamp::parse_rfc3339(text),
                Err(TimestampError::Malformed),
                "for {text}"
            );
        }
    }

    /// A leap second is a real thing a peer may send; there is no instant to
    /// map it to, so it clamps rather than failing the whole message.
    #[test]
    fn clamps_a_leap_second_rather_than_rejecting_it() {
        let parsed = OcppTimestamp::parse_rfc3339("2016-12-31T23:59:60Z").unwrap();

        assert_eq!(
            parsed,
            OcppTimestamp::parse_rfc3339("2016-12-31T23:59:59Z").unwrap()
        );
    }

    #[test]
    fn orders_by_instant_regardless_of_written_offset() {
        let earlier = OcppTimestamp::parse_rfc3339("2024-01-01T00:00:00Z").unwrap();
        let later = OcppTimestamp::parse_rfc3339("2024-01-01T00:00:01Z").unwrap();
        let same_instant_other_offset =
            OcppTimestamp::parse_rfc3339("2024-01-01T01:00:00+01:00").unwrap();

        assert!(earlier < later);
        assert_eq!(earlier.cmp(&same_instant_other_offset), core::cmp::Ordering::Equal);
    }

    #[test]
    fn round_trips_across_a_wide_span_of_days() {
        // Every 37th day for ~200 years, to exercise leap years, century
        // rules and the pre-epoch branch of the civil-date algorithm.
        let mut day = -36_500i64;

        while day < 36_500 {
            let stamp = OcppTimestamp::from_unix(day * 86_400 + 3661, 0).unwrap();
            let mut buf = [0u8; MAX_RFC3339_LEN];
            let text = stamp.to_rfc3339(&mut buf).unwrap();

            assert_eq!(
                OcppTimestamp::parse_rfc3339(text).unwrap(),
                stamp,
                "failed to round-trip {text}"
            );

            day += 37;
        }
    }

    #[test]
    fn to_rfc3339_refuses_a_buffer_it_could_overflow() {
        let stamp = OcppTimestamp::UNIX_EPOCH;
        let mut small = [0u8; MAX_RFC3339_LEN - 1];

        assert_eq!(stamp.to_rfc3339(&mut small), None);
    }

    #[test]
    fn is_sixteen_bytes() {
        // The whole point: a `dateTime` field used to reserve 1024.
        assert_eq!(core::mem::size_of::<OcppTimestamp>(), 16);
    }

    // --- OcppTimeOfDay / OcppDate ---------------------------------------

    /// Rendering needs no buffer, but the tests need something to compare.
    fn rendered_civil(value: &impl fmt::Display) -> heapless::String<16> {
        let mut out = heapless::String::new();
        core::fmt::Write::write_fmt(&mut out, format_args!("{value}")).unwrap();
        out
    }

    #[test]
    fn the_civil_types_are_as_small_as_their_fields_allow() {
        // `HH:MM` has 1,440 distinct values, so a minute resolution needs
        // more than a byte; two is the floor.
        assert_eq!(core::mem::size_of::<OcppTimeOfDay>(), 2);
        assert_eq!(core::mem::size_of::<OcppDate>(), 4);
    }

    #[test]
    fn time_of_day_round_trips_the_specs_format() {
        for text in ["00:00", "09:05", "13:45", "23:59"] {
            let parsed = OcppTimeOfDay::parse(text).expect(text);
            assert_eq!(rendered_civil(&parsed).as_str(), text);
        }
    }

    #[test]
    fn time_of_day_rejects_impossible_and_malformed_values() {
        for text in ["24:00", "23:60", "99:99"] {
            assert_eq!(OcppTimeOfDay::parse(text), Err(TimestampError::OutOfRange), "{text}");
        }

        // The spec's regex requires leading zeros and exactly `HH:MM`.
        for text in ["", "9:05", "09:5", "09:05:00", "0905", "ab:cd", "09-05"] {
            assert_eq!(OcppTimeOfDay::parse(text), Err(TimestampError::Malformed), "{text}");
        }
    }

    #[test]
    fn time_of_day_orders_chronologically() {
        let early = OcppTimeOfDay::parse("09:05").unwrap();
        let late = OcppTimeOfDay::parse("09:30").unwrap();

        assert!(early < late);
        assert!(OcppTimeOfDay::MIDNIGHT < early);
        assert_eq!(late.minutes_since_midnight(), 570);
    }

    #[test]
    fn date_round_trips_the_specs_format() {
        for text in ["1970-01-01", "2015-12-24", "2024-02-29", "9999-12-31"] {
            let parsed = OcppDate::parse(text).expect(text);
            assert_eq!(rendered_civil(&parsed).as_str(), text);
        }
    }

    #[test]
    fn date_rejects_days_the_month_does_not_have() {
        for text in ["2023-02-29", "1900-02-29", "2024-04-31", "2024-13-01", "2024-00-10"] {
            assert_eq!(OcppDate::parse(text), Err(TimestampError::OutOfRange), "{text}");
        }

        assert!(OcppDate::parse("2000-02-29").is_ok());
        assert!(OcppDate::parse("2024-02-29").is_ok());
    }

    #[test]
    fn date_orders_chronologically_and_converts_to_epoch_days() {
        let earlier = OcppDate::parse("2024-01-31").unwrap();
        let later = OcppDate::parse("2024-02-01").unwrap();

        assert!(earlier < later);
        assert_eq!(OcppDate::parse("1970-01-01").unwrap().days_from_epoch(), 0);
        assert_eq!(later.days_from_epoch() - earlier.days_from_epoch(), 1);
    }

}

#[cfg(all(test, feature = "chrono"))]
mod chrono_tests {
    use super::*;
    use chrono::{DateTime, FixedOffset, Utc};

    #[test]
    fn round_trips_through_chrono_utc() {
        let ours = OcppTimestamp::parse_rfc3339("2024-06-15T12:34:56.789Z").unwrap();
        let theirs: DateTime<Utc> = ours.into();

        assert_eq!(theirs.timestamp(), ours.unix_seconds());
        assert_eq!(theirs.timestamp_subsec_nanos(), ours.subsec_nanos());
        assert_eq!(OcppTimestamp::from(theirs), ours);
    }

    /// The written offset is what a fixed-offset conversion must preserve --
    /// converting through UTC and back would silently rewrite the peer's
    /// local time as `Z`.
    #[test]
    fn a_fixed_offset_survives_the_round_trip() {
        let ours = OcppTimestamp::parse_rfc3339("2024-01-01T01:00:00+01:00").unwrap();
        let theirs: DateTime<FixedOffset> = ours.into();

        assert_eq!(theirs.offset().local_minus_utc(), 3600);

        let back = OcppTimestamp::from(theirs);
        assert_eq!(back, ours);
        assert_eq!(back.utc_offset_minutes(), 60);
    }

    #[test]
    fn chrono_agrees_with_our_parser_on_a_span_of_instants() {
        let mut day = -20_000i64;

        while day < 20_000 {
            let ours = OcppTimestamp::from_unix(day * 86_400 + 7261, 0).unwrap();
            let mut buf = [0u8; MAX_RFC3339_LEN];
            let text = ours.to_rfc3339(&mut buf).unwrap();

            let theirs = DateTime::parse_from_rfc3339(text)
                .unwrap_or_else(|e| panic!("chrono rejected our output {text}: {e}"));

            assert_eq!(theirs.timestamp(), ours.unix_seconds(), "for {text}");
            day += 61;
        }
    }
}

/// A local time of day, `HH:MM`.
///
/// 2.1's tariff conditions carry `startTimeOfDay`/`endTimeOfDay`, whose
/// property descriptions state the format (and its regex) while the schema
/// itself declares no `maxLength` -- so as strings they took the generator's
/// unbounded default.
///
/// Two bytes: an hour and a minute. One would not do, since a minute
/// resolution has 1,440 distinct values and a byte holds 256.
///
/// Ordering is chronological within the day, which is what the derive gives:
/// the fields are declared most-significant first.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct OcppTimeOfDay {
    hour: u8,
    minute: u8,
}

impl OcppTimeOfDay {
    /// Midnight, `00:00`.
    pub const MIDNIGHT: Self = Self { hour: 0, minute: 0 };

    /// Builds a time of day, rejecting an hour past 23 or a minute past 59.
    pub const fn new(hour: u8, minute: u8) -> Result<Self, TimestampError> {
        if hour > 23 || minute > 59 {
            return Err(TimestampError::OutOfRange);
        }

        Ok(Self { hour, minute })
    }

    pub const fn hour(&self) -> u8 {
        self.hour
    }

    pub const fn minute(&self) -> u8 {
        self.minute
    }

    /// Minutes since midnight, for arithmetic against a window.
    pub const fn minutes_since_midnight(&self) -> u16 {
        self.hour as u16 * 60 + self.minute as u16
    }

    /// Parses `HH:MM`, as the specification's own regex describes. Leading
    /// zeros are required, matching that regex.
    pub fn parse(text: &str) -> Result<Self, TimestampError> {
        let bytes = text.as_bytes();

        if bytes.len() != 5 {
            return Err(TimestampError::Malformed);
        }

        let hour = parse_number(&bytes[0..2])?;
        expect(bytes[2], b':')?;
        let minute = parse_number(&bytes[3..5])?;

        Self::new(hour as u8, minute as u8)
    }
}

impl fmt::Display for OcppTimeOfDay {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:02}:{:02}", self.hour, self.minute)
    }
}

impl core::str::FromStr for OcppTimeOfDay {
    type Err = TimestampError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        Self::parse(text)
    }
}

impl TryFrom<&str> for OcppTimeOfDay {
    type Error = TimestampError;

    fn try_from(text: &str) -> Result<Self, Self::Error> {
        Self::parse(text)
    }
}

/// A local calendar date, `YYYY-MM-DD`.
///
/// The companion to [`OcppTimeOfDay`]: 2.1's `validFromDate`/`validToDate`
/// state this format in prose and leave the property unbounded.
///
/// Four bytes, and chronologically ordered by the derive since the fields
/// run most-significant first.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct OcppDate {
    year: u16,
    month: u8,
    day: u8,
}

impl OcppDate {
    /// Builds a date, rejecting a month outside 1..=12 or a day the month
    /// doesn't have -- including 29 February in a common year.
    pub const fn new(year: u16, month: u8, day: u8) -> Result<Self, TimestampError> {
        if month < 1 || month > 12 || day < 1 || day > days_in_month(year as i64, month as u32) as u8
        {
            return Err(TimestampError::OutOfRange);
        }

        Ok(Self { year, month, day })
    }

    pub const fn year(&self) -> u16 {
        self.year
    }

    pub const fn month(&self) -> u8 {
        self.month
    }

    pub const fn day(&self) -> u8 {
        self.day
    }

    /// Days since the Unix epoch, for arithmetic against other dates.
    pub const fn days_from_epoch(&self) -> i64 {
        days_from_civil(self.year as i64, self.month as u32, self.day as u32)
    }

    /// Parses `YYYY-MM-DD`, as the specification's own regex describes.
    pub fn parse(text: &str) -> Result<Self, TimestampError> {
        let bytes = text.as_bytes();

        if bytes.len() != 10 {
            return Err(TimestampError::Malformed);
        }

        let year = parse_number(&bytes[0..4])?;
        expect(bytes[4], b'-')?;
        let month = parse_number(&bytes[5..7])?;
        expect(bytes[7], b'-')?;
        let day = parse_number(&bytes[8..10])?;

        Self::new(year as u16, month as u8, day as u8)
    }
}

impl fmt::Display for OcppDate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:04}-{:02}-{:02}", self.year, self.month, self.day)
    }
}

impl core::str::FromStr for OcppDate {
    type Err = TimestampError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        Self::parse(text)
    }
}

impl TryFrom<&str> for OcppDate {
    type Error = TimestampError;

    fn try_from(text: &str) -> Result<Self, Self::Error> {
        Self::parse(text)
    }
}

/// Shared by [`OcppTimeOfDay`] and [`OcppDate`]: both render short, fixed
/// forms, so `Display` is enough and no buffer needs threading through.
#[cfg(feature = "serde")]
macro_rules! serde_via_display {
    ($ty:ty, $expecting:literal) => {
        #[cfg(feature = "serde")]
        impl serde::Serialize for $ty {
            fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                serializer.collect_str(self)
            }
        }

        #[cfg(feature = "serde")]
        impl<'de> serde::Deserialize<'de> for $ty {
            fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                struct Visitor;

                impl<'v> serde::de::Visitor<'v> for Visitor {
                    type Value = $ty;

                    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                        f.write_str($expecting)
                    }

                    fn visit_str<E: serde::de::Error>(self, text: &str) -> Result<Self::Value, E> {
                        <$ty>::parse(text).map_err(serde::de::Error::custom)
                    }
                }

                deserializer.deserialize_str(Visitor)
            }
        }
    };
}

#[cfg(feature = "serde")]
serde_via_display!(OcppTimeOfDay, "a time of day as HH:MM");
#[cfg(feature = "serde")]
serde_via_display!(OcppDate, "a date as YYYY-MM-DD");