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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! ASN.1 primitives related to time types.

use {
    bcder::{
        decode::{Constructed, DecodeError, Primitive, SliceSource, Source},
        encode::{PrimitiveContent, Values},
        Mode, Tag,
    },
    chrono::{Datelike, TimeZone, Timelike},
    std::{
        fmt::{Display, Formatter},
        io::Write,
        ops::{Add, Deref},
        str::FromStr,
    },
};

/// Timezone formats of [GeneralizedTime] to allow.
///
/// X.690 allows [GeneralizedTime] to have multiple timezone formats. However,
/// various RFCs restrict which formats are allowed. This enumeration exists to
/// express which formats should be allowed by a parser.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GeneralizedTimeAllowedTimezone {
    /// Allow either `Z` or timezone offset formats.
    Any,

    /// `Z` timezone identifier only.
    Z,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Time {
    UtcTime(UtcTime),
    GeneralTime(GeneralizedTime),
}

impl Time {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        cons.take_primitive(|tag, prim| match tag {
            Tag::UTC_TIME => Ok(Self::UtcTime(UtcTime::from_primitive(prim)?)),
            Tag::GENERALIZED_TIME => Ok(Self::GeneralTime(
                GeneralizedTime::from_primitive_no_fractional_or_timezone_offsets(prim)?,
            )),
            _ => Err(prim.content_err(format!("expected UTCTime or GeneralizedTime; got {}", tag))),
        })
    }

    pub fn take_opt_from<S: Source>(
        cons: &mut Constructed<S>,
    ) -> Result<Option<Self>, DecodeError<S::Error>> {
        cons.take_opt_primitive(|tag, prim| match tag {
            Tag::UTC_TIME => Ok(Self::UtcTime(UtcTime::from_primitive(prim)?)),
            Tag::GENERALIZED_TIME => Ok(Self::GeneralTime(
                GeneralizedTime::from_primitive_no_fractional_or_timezone_offsets(prim)?,
            )),
            _ => Err(prim.content_err(format!("expected UTCTime or GeneralizedTime; got {}", tag))),
        })
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        match self {
            Self::UtcTime(utc) => (Some(utc.encode()), None),
            Self::GeneralTime(gt) => (None, Some(gt.encode())),
        }
    }
}

impl From<chrono::DateTime<chrono::Utc>> for Time {
    fn from(t: chrono::DateTime<chrono::Utc>) -> Self {
        Self::UtcTime(UtcTime(t))
    }
}

impl From<Time> for chrono::DateTime<chrono::Utc> {
    fn from(value: Time) -> Self {
        match value {
            Time::UtcTime(v) => v.into(),
            Time::GeneralTime(v) => v.into(),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum Zone {
    Utc,
    Offset(chrono::FixedOffset),
}

impl Display for Zone {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Utc => f.write_str("Z"),
            Self::Offset(offset) => f.write_str(format!("{}", offset).replace(':', "").as_str()),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GeneralizedTime {
    time: chrono::NaiveDateTime,
    fractional_seconds: bool,
    timezone: Zone,
}

impl GeneralizedTime {
    /// Take a value, not allowing fractional seconds and requiring the `Z` timezone identifier.
    pub fn take_from_no_fractional_z<S: Source>(
        cons: &mut Constructed<S>,
    ) -> Result<Self, DecodeError<S::Error>> {
        cons.take_primitive_if(Tag::GENERALIZED_TIME, |prim| {
            Self::from_primitive_no_fractional_or_timezone_offsets(prim)
        })
    }

    /// Take a value, allowing fractional seconds and requiring the `Z` timezone identifier.
    pub fn take_from_allow_fractional_z<S: Source>(
        cons: &mut Constructed<S>,
    ) -> Result<Self, DecodeError<S::Error>> {
        cons.take_primitive_if(Tag::GENERALIZED_TIME, |prim| {
            let data = prim.take_all()?;

            Self::parse(
                SliceSource::new(data.as_ref()),
                true,
                GeneralizedTimeAllowedTimezone::Z,
            )
            .map_err(|e| e.convert())
        })
    }

    /// Parse a [GeneralizedTime] from a primitive string and don't allow fractional seconds or timezone offsets.
    pub fn from_primitive_no_fractional_or_timezone_offsets<S: Source>(
        prim: &mut Primitive<S>,
    ) -> Result<Self, DecodeError<S::Error>> {
        let data = prim.take_all()?;

        Self::parse(
            SliceSource::new(data.as_ref()),
            false,
            GeneralizedTimeAllowedTimezone::Z,
        )
        .map_err(|e| e.convert())
    }

    /// Parse GeneralizedTime string data.
    pub fn parse(
        source: SliceSource,
        allow_fractional_seconds: bool,
        tz: GeneralizedTimeAllowedTimezone,
    ) -> Result<Self, DecodeError<<SliceSource as Source>::Error>> {
        let data = source.slice();

        // This form is common and is most strict. So check for it quickly.
        if !allow_fractional_seconds
            && matches!(tz, GeneralizedTimeAllowedTimezone::Z)
            && data.len() != "YYYYMMDDHHMMSSZ".len()
        {
            return Err(source.content_err(format!(
                "{} is not of format YYYYMMDDHHMMSSZ",
                String::from_utf8_lossy(data)
            )));
        }

        if data.len() < 15 {
            return Err(source.content_err("GeneralizedTime value too short"));
        }

        let year = i32::from_str(
            std::str::from_utf8(&data[0..4]).map_err(|s| source.content_err(s.to_string()))?,
        )
        .map_err(|s| source.content_err(s.to_string()))?;
        let month = u32::from_str(
            std::str::from_utf8(&data[4..6]).map_err(|s| source.content_err(s.to_string()))?,
        )
        .map_err(|s| source.content_err(s.to_string()))?;
        let day = u32::from_str(
            std::str::from_utf8(&data[6..8]).map_err(|s| source.content_err(s.to_string()))?,
        )
        .map_err(|s| source.content_err(s.to_string()))?;
        let hour = u32::from_str(
            std::str::from_utf8(&data[8..10]).map_err(|s| source.content_err(s.to_string()))?,
        )
        .map_err(|s| source.content_err(s.to_string()))?;
        let minute = u32::from_str(
            std::str::from_utf8(&data[10..12]).map_err(|s| source.content_err(s.to_string()))?,
        )
        .map_err(|s| source.content_err(s.to_string()))?;
        let second = u32::from_str(
            std::str::from_utf8(&data[12..14]).map_err(|s| source.content_err(s.to_string()))?,
        )
        .map_err(|s| source.content_err(s.to_string()))?;

        let remaining = &data[14..];

        let (nano, remaining) = match allow_fractional_seconds {
            true => {
                if remaining.starts_with(b".") {
                    if let Some((nondigit_offset, _)) = remaining
                        .iter()
                        .enumerate()
                        .skip(1)
                        .find(|(_, c)| !c.is_ascii_digit())
                    {
                        let digits_count = nondigit_offset - 1;

                        let (digits, remaining) = remaining.split_at(nondigit_offset);

                        let mut digits = std::str::from_utf8(&digits[1..])
                            .map_err(|s| source.content_err(s.to_string()))?
                            .to_string();
                        digits.extend(std::iter::repeat('0').take(9 - digits_count));

                        (
                            u32::from_str(&digits)
                                .map_err(|s| source.content_err(s.to_string()))?,
                            remaining,
                        )
                    } else {
                        return Err(source.content_err(
                            "failed to locate timezone identifier after fractional seconds",
                        ));
                    }
                } else {
                    (0, remaining)
                }
            }
            false => (0, remaining),
        };

        let timezone = match tz {
            GeneralizedTimeAllowedTimezone::Z => {
                if remaining != b"Z" {
                    return Err(source.content_err(format!(
                        "timezone identifier {} not `Z`",
                        String::from_utf8_lossy(remaining)
                    )));
                }

                Zone::Utc
            }
            GeneralizedTimeAllowedTimezone::Any => {
                if remaining == b"Z" {
                    Zone::Utc
                } else {
                    if remaining.len() != 5 {
                        return Err(source.content_err(format!(
                            "`{}` is not a 5 character timezone identifier",
                            String::from_utf8_lossy(remaining)
                        )));
                    }

                    let east = match remaining[0] {
                        b'+' => true,
                        b'-' => false,
                        _ => {
                            return Err(source.content_err(format!(
                                "`{}` does not begin with a +- offset",
                                String::from_utf8_lossy(remaining)
                            )))
                        }
                    };

                    let offset_hours = u32::from_str(
                        std::str::from_utf8(&remaining[1..3])
                            .map_err(|s| source.content_err(s.to_string()))?,
                    )
                    .map_err(|s| source.content_err(s.to_string()))?;
                    let offset_minutes = u32::from_str(
                        std::str::from_utf8(&remaining[3..])
                            .map_err(|s| source.content_err(s.to_string()))?,
                    )
                    .map_err(|s| source.content_err(s.to_string()))?;

                    let offset_seconds = (offset_hours * 3600 + offset_minutes * 60) as i32;

                    Zone::Offset(if east {
                        chrono::FixedOffset::east_opt(offset_seconds)
                            .ok_or_else(|| source.content_err("bad timezone time"))?
                    } else {
                        chrono::FixedOffset::west_opt(offset_seconds)
                            .ok_or_else(|| source.content_err("bad timezone offset"))?
                    })
                }
            }
        };

        if let chrono::LocalResult::Single(dt) =
            chrono::Utc.with_ymd_and_hms(year, month, day, hour, minute, second)
        {
            if let Some(dt) = dt.with_nanosecond(nano) {
                Ok(Self {
                    time: dt.naive_utc(),
                    fractional_seconds: allow_fractional_seconds,
                    timezone,
                })
            } else {
                Err(source.content_err("invalid time value"))
            }
        } else {
            Err(source.content_err("invalid datetime value"))
        }
    }
}

impl ToString for GeneralizedTime {
    fn to_string(&self) -> String {
        format!(
            "{}{}",
            self.time.format(if self.fractional_seconds {
                "%Y%m%d%H%M%S%.f"
            } else {
                "%Y%m%d%H%M%S"
            }),
            self.timezone
        )
    }
}

impl From<GeneralizedTime> for chrono::DateTime<chrono::Utc> {
    fn from(gt: GeneralizedTime) -> Self {
        match gt.timezone {
            Zone::Utc => {
                chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(gt.time, chrono::Utc)
            }
            Zone::Offset(offset) => chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
                gt.time.add(offset),
                chrono::Utc,
            ),
        }
    }
}

impl From<chrono::DateTime<chrono::Utc>> for GeneralizedTime {
    fn from(utc: chrono::DateTime<chrono::Utc>) -> Self {
        Self {
            time: utc.naive_utc(),
            fractional_seconds: utc.timestamp_subsec_micros() > 0,
            timezone: Zone::Utc,
        }
    }
}

impl PrimitiveContent for GeneralizedTime {
    const TAG: Tag = Tag::GENERALIZED_TIME;

    fn encoded_len(&self, _: Mode) -> usize {
        self.to_string().len()
    }

    fn write_encoded<W: Write>(&self, _: Mode, target: &mut W) -> Result<(), std::io::Error> {
        target.write_all(self.to_string().as_bytes())
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UtcTime(chrono::DateTime<chrono::Utc>);

impl From<chrono::DateTime<chrono::Utc>> for UtcTime {
    fn from(value: chrono::DateTime<chrono::Utc>) -> Self {
        Self(value)
    }
}

impl From<UtcTime> for chrono::DateTime<chrono::Utc> {
    fn from(value: UtcTime) -> Self {
        *value.deref()
    }
}

impl UtcTime {
    /// Obtain a new instance with now as the time.
    pub fn now() -> Self {
        Self(chrono::Utc::now())
    }

    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        cons.take_primitive_if(Tag::UTC_TIME, |prim| Self::from_primitive(prim))
    }

    pub fn from_primitive<S: Source>(
        prim: &mut Primitive<S>,
    ) -> Result<Self, DecodeError<S::Error>> {
        let data = prim.take_all()?;

        if data.len() != "YYMMDDHHMMSSZ".len() {
            return Err(prim.content_err("UTCTime not of expected length"));
        }

        let year = i32::from_str(
            std::str::from_utf8(&data[0..2]).map_err(|s| prim.content_err(s.to_string()))?,
        )
        .map_err(|s| prim.content_err(s.to_string()))?;

        let year = if year >= 50 { year + 1900 } else { year + 2000 };

        let month = u32::from_str(
            std::str::from_utf8(&data[2..4]).map_err(|s| prim.content_err(s.to_string()))?,
        )
        .map_err(|s| prim.content_err(s.to_string()))?;
        let day = u32::from_str(
            std::str::from_utf8(&data[4..6]).map_err(|s| prim.content_err(s.to_string()))?,
        )
        .map_err(|s| prim.content_err(s.to_string()))?;
        let hour = u32::from_str(
            std::str::from_utf8(&data[6..8]).map_err(|s| prim.content_err(s.to_string()))?,
        )
        .map_err(|s| prim.content_err(s.to_string()))?;
        let minute = u32::from_str(
            std::str::from_utf8(&data[8..10]).map_err(|s| prim.content_err(s.to_string()))?,
        )
        .map_err(|s| prim.content_err(s.to_string()))?;
        let second = u32::from_str(
            std::str::from_utf8(&data[10..12]).map_err(|s| prim.content_err(s.to_string()))?,
        )
        .map_err(|s| prim.content_err(s.to_string()))?;

        if data[12] != b'Z' {
            return Err(prim.content_err("UTCTime must end with `Z`"));
        }

        if let chrono::LocalResult::Single(dt) =
            chrono::Utc.with_ymd_and_hms(year, month, day, hour, minute, second)
        {
            Ok(Self(dt))
        } else {
            Err(prim.content_err("invalid year month day hour minute second value"))
        }
    }
}

impl ToString for UtcTime {
    fn to_string(&self) -> String {
        format!(
            "{:02}{:02}{:02}{:02}{:02}{:02}Z",
            self.0.year() % 100,
            self.0.month(),
            self.0.day(),
            self.0.hour(),
            self.0.minute(),
            self.0.second()
        )
    }
}

impl Deref for UtcTime {
    type Target = chrono::DateTime<chrono::Utc>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl PrimitiveContent for UtcTime {
    const TAG: Tag = Tag::UTC_TIME;

    fn encoded_len(&self, _: Mode) -> usize {
        self.to_string().len()
    }

    fn write_encoded<W: Write>(&self, _: Mode, target: &mut W) -> Result<(), std::io::Error> {
        target.write_all(self.to_string().as_bytes())
    }
}

#[cfg(test)]
mod test {
    use {super::*, bcder::decode::ContentError};

    #[test]
    fn generalized_time() -> Result<(), ContentError> {
        let gt = GeneralizedTime {
            time: chrono::NaiveDateTime::from_timestamp_opt(1643510772, 0).unwrap(),
            fractional_seconds: false,
            timezone: Zone::Utc,
        };
        assert_eq!(gt.to_string(), "20220130024612Z");

        let gt = GeneralizedTime {
            time: chrono::NaiveDateTime::from_timestamp_opt(1643510772, 0).unwrap(),
            fractional_seconds: false,
            timezone: Zone::Offset(chrono::FixedOffset::east_opt(3600).unwrap()),
        };
        assert_eq!(gt.to_string(), "20220130024612+0100");

        let gt = GeneralizedTime {
            time: chrono::NaiveDateTime::from_timestamp_opt(1643510772, 0).unwrap(),
            fractional_seconds: false,
            timezone: Zone::Offset(chrono::FixedOffset::west_opt(7200).unwrap()),
        };
        assert_eq!(gt.to_string(), "20220130024612-0200");

        let gt = GeneralizedTime::parse(
            SliceSource::new(b"20220129133742Z"),
            true,
            GeneralizedTimeAllowedTimezone::Z,
        )?;
        assert_eq!(gt.time.year(), 2022);
        assert_eq!(gt.time.month(), 1);
        assert_eq!(gt.time.day(), 29);
        assert_eq!(gt.time.hour(), 13);
        assert_eq!(gt.time.minute(), 37);
        assert_eq!(gt.time.second(), 42);
        assert_eq!(gt.time.nanosecond(), 0);
        assert_eq!(format!("{}", gt.timezone), "Z");

        assert_eq!(gt.to_string(), "20220129133742Z");

        let gt = GeneralizedTime::parse(
            SliceSource::new(b"20220129133742.333Z"),
            true,
            GeneralizedTimeAllowedTimezone::Z,
        )?;
        assert_eq!(gt.to_string(), "20220129133742.333Z");

        let gt = GeneralizedTime::parse(
            SliceSource::new(b"20220129133742-0800"),
            false,
            GeneralizedTimeAllowedTimezone::Any,
        )?;
        assert_eq!(format!("{}", gt.timezone), "-0800");

        let gt = GeneralizedTime::parse(
            SliceSource::new(b"20220129133742+1000"),
            false,
            GeneralizedTimeAllowedTimezone::Any,
        )?;
        assert_eq!(format!("{}", gt.timezone), "+1000");

        let gt = GeneralizedTime::parse(
            SliceSource::new(b"20220129133742.333-0800"),
            true,
            GeneralizedTimeAllowedTimezone::Any,
        )?;
        assert_eq!(gt.to_string(), "20220129133742.333-0800");

        Ok(())
    }

    #[test]
    fn generalized_time_invalid() {
        for allow_fractional_seconds in [false, true] {
            for allowed_timezone in [
                GeneralizedTimeAllowedTimezone::Any,
                GeneralizedTimeAllowedTimezone::Z,
            ] {
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b""),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"abcd"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"2022"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"202201"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"2022013012"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"202201301230"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015."),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015.a"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015.0a"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015a"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015-"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015+"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015+01"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015+01000"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015+0100a"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015-01000"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
                assert!(GeneralizedTime::parse(
                    SliceSource::new(b"20220130123015-0100a"),
                    allow_fractional_seconds,
                    allowed_timezone
                )
                .is_err());
            }
        }
    }
}