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
use std::io::{self, Write as _};
use std::slice;
use std::str;
use std::sync::OnceLock;

use regex::Regex;
use tz::timezone::{LocalTimeType, TimeZoneRef};
use tzdb::time_zone::etc::GMT;

use super::error::{TimeError, TzOutOfRangeError, TzStringError};

const SECONDS_IN_MINUTE: i32 = 60;
const SECONDS_IN_HOUR: i32 = SECONDS_IN_MINUTE * 60;
const SECONDS_IN_DAY: i32 = SECONDS_IN_HOUR * 24;

/// The maximum allowed offset in seconds from UTC in the future for a fixed
/// offset.
///
/// This constant has magnitude to the number of seconds in 1 day, minus 1.
pub const MAX_OFFSET_SECONDS: i32 = SECONDS_IN_DAY - 1;

/// The maximum allowed offset in seconds from UTC in the past for a fixed
/// offset.
///
/// This constant has magnitude of the number of seconds in 1 day, minus 1.
pub const MIN_OFFSET_SECONDS: i32 = -MAX_OFFSET_SECONDS;

/// `tzdb` provides [`local_tz`] to get the local system timezone. If this ever
/// fails, we can assume `GMT`. `GMT` is used instead of `UTC` since it has a
/// [`time_zone_designation`] - which if it is an empty string, then it is
/// considered to be a UTC time.
///
/// Note: this matches MRI Ruby implementation. Where `TZ="" ruby -e "puts
/// Time::now"` will return a new _time_ with 0 offset from UTC, but still still
/// report as a non UTC time:
///
/// ```console
/// $ TZ="" ruby -e 'puts RUBY_VERSION' -e 't = Time.now' -e 'puts t' -e 'puts t.utc?'
/// 3.1.2
/// 2022-06-26 22:22:25 +0000
/// false
/// ```
///
/// [`local_tz`]: tzdb::local_tz
/// [`time_zone_designation`]: tz::timezone::LocalTimeType::time_zone_designation
#[inline]
#[must_use]
#[cfg(feature = "tzrs-local")]
fn local_time_zone() -> TimeZoneRef<'static> {
    // Per the docs, it is suggested to cache the result of fetching the
    // local timezone: https://docs.rs/tzdb/latest/tzdb/fn.local_tz.html.
    static LOCAL_TZ: OnceLock<TimeZoneRef<'static>> = OnceLock::new();

    *LOCAL_TZ.get_or_init(|| {
        let tz = iana_time_zone::get_timezone().ok().and_then(tzdb::tz_by_name);
        tz.unwrap_or(GMT)
    })
}

#[inline]
#[must_use]
#[cfg(not(feature = "tzrs-local"))]
fn local_time_zone() -> TimeZoneRef<'static> {
    GMT
}

/// Generates a [+/-]HHMM timezone format from a given number of seconds
/// Note: the actual seconds element is effectively ignored here
#[inline]
fn offset_hhmm_from_seconds(seconds: i32, buf: &mut [u8; 5]) -> io::Result<()> {
    let flag = if seconds < 0 { '-' } else { '+' };
    let minutes = seconds.abs() / 60;

    let offset_hours = minutes / 60;
    let offset_minutes = minutes - (offset_hours * 60);

    write!(buf.as_mut_slice(), "{flag}{offset_hours:0>2}{offset_minutes:0>2}")
}

/// Represents the number of seconds offset from UTC.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Offset {
    inner: OffsetType,
}

/// Represents the type of offset from UTC.
#[allow(variant_size_differences)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum OffsetType {
    /// UTC offset, zero offset, Zulu time
    Utc,
    /// Fixed offset from UTC.
    ///
    /// **Note**: A fixed offset of 0 is different from UTC time.
    Fixed(LocalTimeType),
    /// A time zone based offset.
    Tz(TimeZoneRef<'static>),
}

impl Offset {
    /// Generate a UTC based offset.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::tzrs::{Offset, Time, TimeError};
    /// # fn example() -> Result<(), TimeError> {
    /// let offset = Offset::utc();
    /// assert!(offset.is_utc());
    ///
    /// let time = Time::new(2022, 7, 29, 12, 36, 0, 0, offset)?;
    /// assert!(time.is_utc());
    /// # Ok(())
    /// # }
    /// # example().unwrap();
    /// ```
    #[inline]
    #[must_use]
    pub fn utc() -> Self {
        Self { inner: OffsetType::Utc }
    }

    /// Generate an offset based on the detected local time zone of the system.
    ///
    /// Detection is done by [`tzdb::local_tz`], and if it fails will return a
    /// GMT timezone.
    ///
    /// The system timezone is detected on the first call to this function and
    /// will be constant for the life of the program.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::tzrs::{Offset, Time, TimeError};
    /// # fn example() -> Result<(), TimeError> {
    /// let offset = Offset::local();
    /// assert!(!offset.is_utc());
    ///
    /// let time = Time::new(2022, 7, 29, 12, 36, 0, 0, offset)?;
    /// assert!(!time.is_utc());
    /// # Ok(())
    /// # }
    /// # example().unwrap();
    /// ```
    ///
    /// [`tzdb::local_tz`]: https://docs.rs/tzdb/latest/tzdb/fn.local_tz.html
    #[inline]
    #[must_use]
    pub fn local() -> Self {
        let local_tz = local_time_zone();
        let offset = OffsetType::Tz(local_tz);
        Self { inner: offset }
    }

    /// Generate an offset with a number of seconds from UTC.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::tzrs::{Offset, Time, TimeError};
    /// # fn example() -> Result<(), TimeError> {
    /// let offset = Offset::fixed(6600)?; // +0150
    /// assert!(!offset.is_utc());
    ///
    /// let time = Time::new(2022, 7, 29, 12, 36, 0, 0, offset)?;
    /// assert!(!time.is_utc());
    /// # Ok(())
    /// # }
    /// # example().unwrap();
    /// ```
    ///
    /// The offset must be in range:
    ///
    /// ```
    /// # use spinoso_time::tzrs::Offset;
    /// let offset = Offset::fixed(500_000); // +0150
    /// assert!(offset.is_err());
    /// ```
    ///
    /// # Errors
    ///
    /// Return a [`TimeError::TzOutOfRangeError`] when outside of range of
    /// acceptable offset of [`MIN_OFFSET_SECONDS`] to [`MAX_OFFSET_SECONDS`].
    #[inline]
    #[allow(clippy::missing_panics_doc)]
    pub fn fixed(offset: i32) -> Result<Self, TimeError> {
        if !(MIN_OFFSET_SECONDS..=MAX_OFFSET_SECONDS).contains(&offset) {
            return Err(TzOutOfRangeError::new().into());
        }

        let mut offset_name = [0; 5];

        offset_hhmm_from_seconds(offset, &mut offset_name).expect("offset name should be 5 bytes long");
        // Creation of the `LocalTimeType` is never expected to fail, since the
        // bounds we are more restrictive of the values than the struct itself.
        let local_time_type = LocalTimeType::new(offset, false, Some(&offset_name[..]))
            .expect("Failed to LocalTimeType for fixed offset");

        Ok(Self {
            inner: OffsetType::Fixed(local_time_type),
        })
    }

    /// Generate an offset based on a provided [`TimeZoneRef`].
    ///
    /// This can be combined with [`tzdb`] to generate offsets based on
    /// predefined IANA time zones.
    #[inline]
    #[must_use]
    fn tz(tz: TimeZoneRef<'static>) -> Self {
        Self {
            inner: OffsetType::Tz(tz),
        }
    }

    /// Returns whether this offset is UTC.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::tzrs::{Offset, Time, TimeError};
    /// # fn example() -> Result<(), TimeError> {
    /// let offset = Offset::utc();
    /// assert!(offset.is_utc());
    ///
    /// let offset = Offset::fixed(6600)?; // +0150
    /// assert!(!offset.is_utc());
    /// # Ok(())
    /// # }
    /// # example().unwrap();
    /// ```
    #[inline]
    #[must_use]
    pub fn is_utc(&self) -> bool {
        matches!(self.inner, OffsetType::Utc)
    }

    /// Returns a `TimeZoneRef` which can be used to generate and project
    /// _time_.
    #[inline]
    #[must_use]
    pub(crate) fn time_zone_ref(&self) -> TimeZoneRef<'_> {
        match self.inner {
            OffsetType::Utc => TimeZoneRef::utc(),
            OffsetType::Fixed(ref local_time_type) => {
                match TimeZoneRef::new(&[], slice::from_ref(local_time_type), &[], &None) {
                    Ok(tz) => tz,
                    Err(_) => GMT,
                }
            }

            OffsetType::Tz(zone) => zone,
        }
    }
}

impl TryFrom<&str> for Offset {
    type Error = TimeError;

    /// Construct a Offset based on the [accepted MRI values].
    ///
    /// Accepts:
    ///
    /// - `[+/-]HH[:]MM`
    /// - A-I representing +01:00 to +09:00.
    /// - K-M representing +10:00 to +12:00.
    /// - N-Y representing -01:00 to -12:00.
    /// - Z representing UTC/Zulu time (0 offset).
    ///
    /// [accepted MRI values]: https://ruby-doc.org/core-3.1.2/Time.html#method-c-new
    #[inline]
    fn try_from(input: &str) -> Result<Self, Self::Error> {
        match input {
            "A" => Ok(Self::fixed(SECONDS_IN_HOUR)?),
            "B" => Ok(Self::fixed(2 * SECONDS_IN_HOUR)?),
            "C" => Ok(Self::fixed(3 * SECONDS_IN_HOUR)?),
            "D" => Ok(Self::fixed(4 * SECONDS_IN_HOUR)?),
            "E" => Ok(Self::fixed(5 * SECONDS_IN_HOUR)?),
            "F" => Ok(Self::fixed(6 * SECONDS_IN_HOUR)?),
            "G" => Ok(Self::fixed(7 * SECONDS_IN_HOUR)?),
            "H" => Ok(Self::fixed(8 * SECONDS_IN_HOUR)?),
            "I" => Ok(Self::fixed(9 * SECONDS_IN_HOUR)?),
            "K" => Ok(Self::fixed(10 * SECONDS_IN_HOUR)?),
            "L" => Ok(Self::fixed(11 * SECONDS_IN_HOUR)?),
            "M" => Ok(Self::fixed(12 * SECONDS_IN_HOUR)?),
            "N" => Ok(Self::fixed(-SECONDS_IN_HOUR)?),
            "O" => Ok(Self::fixed(-2 * SECONDS_IN_HOUR)?),
            "P" => Ok(Self::fixed(-3 * SECONDS_IN_HOUR)?),
            "Q" => Ok(Self::fixed(-4 * SECONDS_IN_HOUR)?),
            "R" => Ok(Self::fixed(-5 * SECONDS_IN_HOUR)?),
            "S" => Ok(Self::fixed(-6 * SECONDS_IN_HOUR)?),
            "T" => Ok(Self::fixed(-7 * SECONDS_IN_HOUR)?),
            "U" => Ok(Self::fixed(-8 * SECONDS_IN_HOUR)?),
            "V" => Ok(Self::fixed(-9 * SECONDS_IN_HOUR)?),
            "W" => Ok(Self::fixed(-10 * SECONDS_IN_HOUR)?),
            "X" => Ok(Self::fixed(-11 * SECONDS_IN_HOUR)?),
            "Y" => Ok(Self::fixed(-12 * SECONDS_IN_HOUR)?),
            // ```console
            // [3.1.2] > Time.new(2022, 6, 26, 13, 57, 6, 'Z')
            // => 2022-06-26 13:57:06 UTC
            // [3.1.2] > Time.new(2022, 6, 26, 13, 57, 6, 'Z').utc?
            // => true
            // [3.1.2] > Time.new(2022, 6, 26, 13, 57, 6, 'UTC')
            // => 2022-06-26 13:57:06 UTC
            // [3.1.2] > Time.new(2022, 6, 26, 13, 57, 6, 'UTC').utc?
            // => true
            // ```
            "Z" | "UTC" => Ok(Self::utc()),
            _ => {
                static HH_MM_MATCHER: OnceLock<Regex> = OnceLock::new();

                let hh_mm_matcher = HH_MM_MATCHER.get_or_init(|| {
                    // With `Regex`, `\d` is a "Unicode friendly" Perl character
                    // class which matches Unicode property `Nd`. The `Nd` property
                    // includes all sorts of numerals, including Devanagari and
                    // Kannada, which don't parse into an `i32` using `FromStr`.
                    //
                    // `[[:digit:]]` is documented to be an ASCII character class
                    // for only digits 0-9.
                    //
                    // See:
                    // - https://docs.rs/regex/latest/regex/#perl-character-classes-unicode-friendly
                    // - https://docs.rs/regex/latest/regex/#ascii-character-classes
                    //
                    // Use `unwrap()` here because the regex must compile.
                    Regex::new(r"^([\-\+]{1})([[:digit:]]{2}):?([[:digit:]]{2})$").unwrap()
                });

                let caps = hh_mm_matcher.captures(input).ok_or_else(TzStringError::new)?;

                // Special handling of the +/- sign is required because `-00:30`
                // must parse to a negative offset and `i32::from_str_radix`
                // cannot preserve the `-` sign when parsing zero.
                let sign = if &caps[1] == "+" { 1 } else { -1 };

                // Both of these calls to `parse::<i32>()` ultimately boil down
                // to `i32::from_str_radix(s, 10)`. This function strips leading
                // zero padding as is present when parsing offsets like `+00:30`
                // or `-08:00`.
                let hours = caps[2].parse::<i32>().expect("Two ASCII digits fit in i32");
                let minutes = caps[3].parse::<i32>().expect("Two ASCII digits fit in i32");

                // Check that the parsed offset is in range, which goes from:
                // - `00:00` to `00:59`
                // - `00:00` to `23:59`
                if (0..=23).contains(&hours) && (0..=59).contains(&minutes) {
                    let offset_seconds: i32 = sign * ((hours * SECONDS_IN_HOUR) + (minutes * SECONDS_IN_MINUTE));
                    Ok(Self::fixed(offset_seconds)?)
                } else {
                    Err(TzOutOfRangeError::new().into())
                }
            }
        }
    }
}

impl TryFrom<&[u8]> for Offset {
    type Error = TimeError;

    fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
        let input = str::from_utf8(input).map_err(|_| TzStringError::new())?;
        Offset::try_from(input)
    }
}

impl TryFrom<String> for Offset {
    type Error = TimeError;

    fn try_from(input: String) -> Result<Self, Self::Error> {
        Offset::try_from(input.as_str())
    }
}

impl From<TimeZoneRef<'static>> for Offset {
    #[inline]
    #[must_use]
    fn from(tz: TimeZoneRef<'static>) -> Self {
        Self::tz(tz)
    }
}

impl TryFrom<i32> for Offset {
    type Error = TimeError;

    /// Construct a Offset with the offset in seconds from UTC.
    ///
    /// See [`Offset::fixed`].
    #[inline]
    fn try_from(seconds: i32) -> Result<Self, Self::Error> {
        Self::fixed(seconds)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::OnceLock;

    use tz::timezone::Transition;
    use tz::{LocalTimeType, TimeZone};

    use super::*;
    use crate::tzrs::error::TimeError;
    use crate::tzrs::Time;

    fn offset_seconds_from_fixed_offset(input: &str) -> Result<i32, TimeError> {
        let offset = Offset::try_from(input)?;
        let local_time_type = offset.time_zone_ref().local_time_types()[0];
        Ok(local_time_type.ut_offset())
    }

    fn fixed_offset_name(offset_seconds: i32) -> Result<String, TimeError> {
        let offset = Offset::fixed(offset_seconds)?;

        match offset.inner {
            OffsetType::Fixed(ref local_time_type) => Ok(local_time_type.time_zone_designation().to_string()),
            _ => unreachable!(),
        }
    }

    #[test]
    fn fixed_zero_is_not_utc() {
        let offset = Offset::try_from(0).unwrap();
        assert!(!offset.is_utc());
    }

    #[test]
    fn utc_is_utc() {
        let offset = Offset::utc();
        assert!(offset.is_utc());
    }

    #[test]
    fn z_is_utc() {
        let offset = Offset::try_from("Z").unwrap();
        assert!(offset.is_utc());
    }

    #[test]
    fn from_binary_string() {
        let tz: &[u8] = b"Z";
        let offset = Offset::try_from(tz).unwrap();
        assert!(offset.is_utc());
    }

    #[test]
    fn from_str_hh_mm() {
        assert_eq!(Some(0), offset_seconds_from_fixed_offset("+0000").ok());
        assert_eq!(Some(0), offset_seconds_from_fixed_offset("-0000").ok());
        assert_eq!(Some(60), offset_seconds_from_fixed_offset("+0001").ok());
        assert_eq!(Some(-60), offset_seconds_from_fixed_offset("-0001").ok());
        assert_eq!(Some(3600), offset_seconds_from_fixed_offset("+0100").ok());
        assert_eq!(Some(-3600), offset_seconds_from_fixed_offset("-0100").ok());
        assert_eq!(Some(7320), offset_seconds_from_fixed_offset("+0202").ok());
        assert_eq!(Some(-7320), offset_seconds_from_fixed_offset("-0202").ok());

        assert!(matches!(
            offset_seconds_from_fixed_offset("+2400").unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));

        assert!(matches!(
            offset_seconds_from_fixed_offset("-2400").unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));

        assert!(matches!(
            offset_seconds_from_fixed_offset("+0060").unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));

        assert!(matches!(
            offset_seconds_from_fixed_offset("-0060").unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));
    }

    #[test]
    fn from_str_hh_colon_mm() {
        assert_eq!(Some(0), offset_seconds_from_fixed_offset("+00:00").ok());
        assert_eq!(Some(0), offset_seconds_from_fixed_offset("-00:00").ok());
        assert_eq!(Some(60), offset_seconds_from_fixed_offset("+00:01").ok());
        assert_eq!(Some(-60), offset_seconds_from_fixed_offset("-00:01").ok());
        assert_eq!(Some(3600), offset_seconds_from_fixed_offset("+01:00").ok());
        assert_eq!(Some(-3600), offset_seconds_from_fixed_offset("-01:00").ok());
        assert_eq!(Some(7320), offset_seconds_from_fixed_offset("+02:02").ok());
        assert_eq!(Some(-7320), offset_seconds_from_fixed_offset("-02:02").ok());

        assert!(matches!(
            offset_seconds_from_fixed_offset("+24:00").unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));

        assert!(matches!(
            offset_seconds_from_fixed_offset("-24:00").unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));

        assert!(matches!(
            offset_seconds_from_fixed_offset("+00:60").unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));

        assert!(matches!(
            offset_seconds_from_fixed_offset("-00:60").unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));
    }

    #[test]
    fn from_str_invalid_fixed_strings() {
        let invalid_fixed_strings = [
            "+01:010", "+010:10", "+010:010", "0110", "01:10", "01-10", "+01-10", "+01::10",
        ];

        for invalid_string in invalid_fixed_strings {
            assert!(
                matches!(
                    Offset::try_from(invalid_string).unwrap_err(),
                    TimeError::TzStringError(_)
                ),
                "Expected TimeError::TzStringError for {invalid_string}",
            );
        }
    }

    #[test]
    fn from_str_non_ascii_numeral_fixed_strings() {
        // This offset string is constructed out of non-ASCII numerals in the
        // Unicode Nd character class. The sequence contains `+`, Devanagari 1,
        // Devanagari 0, Kannada 3, and Kannada 6.
        //
        // See:
        //
        // - https://en.wikipedia.org/wiki/Devanagari_numerals#Table
        // - https://en.wikipedia.org/wiki/Kannada_script#Numerals
        let offset = "+१०:೩೬";
        assert!(matches!(
            offset_seconds_from_fixed_offset(offset).unwrap_err(),
            TimeError::TzStringError(_)
        ));
    }

    #[test]
    fn from_str_fixed_strings_with_newlines() {
        assert!(matches!(
            offset_seconds_from_fixed_offset("+10:00\n+11:00").unwrap_err(),
            TimeError::TzStringError(_)
        ));
        assert!(matches!(
            offset_seconds_from_fixed_offset("+10:00\n").unwrap_err(),
            TimeError::TzStringError(_)
        ));
        assert!(matches!(
            offset_seconds_from_fixed_offset("\n+10:00").unwrap_err(),
            TimeError::TzStringError(_)
        ));
    }

    #[test]
    fn offset_hhmm_from_seconds_exhaustive_5_bytes() {
        // Check to make sure that the stack-allocated buffer for writing the
        // [+/-]HHMM offset is exactly sized for the entire range of valid
        // offset seconds.
        for offset in MIN_OFFSET_SECONDS..=MAX_OFFSET_SECONDS {
            let mut buf = [0xFF; 5];
            offset_hhmm_from_seconds(offset, &mut buf).unwrap();
            assert!(buf.into_iter().all(|b| b != 0xFF));
        }
    }

    #[test]
    fn fixed_time_zone_designation_exhaustive_no_panic() {
        // Check to make sure that the stack-allocated buffer for writing the
        // [+/-]HHMM offset is sufficiently sized for the entire range of valid
        // offset seconds.
        for offset in MIN_OFFSET_SECONDS..=MAX_OFFSET_SECONDS {
            fixed_offset_name(offset).unwrap();
        }
    }

    #[test]
    fn fixed_time_zone_designation() {
        assert_eq!("+0000", fixed_offset_name(0).unwrap());
        assert_eq!("+0000", fixed_offset_name(59).unwrap());
        assert_eq!("+0001", fixed_offset_name(60).unwrap());
        assert_eq!("-0001", fixed_offset_name(-60).unwrap());
        assert_eq!("+0100", fixed_offset_name(3600).unwrap());
        assert_eq!("-0100", fixed_offset_name(-3600).unwrap());
        assert_eq!("+0202", fixed_offset_name(7320).unwrap());
        assert_eq!("-0202", fixed_offset_name(-7320).unwrap());

        assert_eq!("+2359", fixed_offset_name(MAX_OFFSET_SECONDS).unwrap());
        assert_eq!("-2359", fixed_offset_name(MIN_OFFSET_SECONDS).unwrap());

        assert!(matches!(
            fixed_offset_name(MAX_OFFSET_SECONDS + 1).unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));

        assert!(matches!(
            fixed_offset_name(MIN_OFFSET_SECONDS - 1).unwrap_err(),
            TimeError::TzOutOfRangeError(_)
        ));
    }

    // https://github.com/x-hgg-x/tz-rs/issues/34#issuecomment-1206140198
    #[test]
    fn tzrs_gh_34_handle_missing_transition_tzif_v1() {
        static TZ: OnceLock<TimeZone> = OnceLock::new();

        let tz = TZ.get_or_init(|| {
            let local_time_types = vec![
                LocalTimeType::new(0, false, None).unwrap(),
                LocalTimeType::new(3600, false, None).unwrap(),
            ];

            TimeZone::new(
                vec![Transition::new(0, 1), Transition::new(86400, 1)],
                local_time_types,
                vec![],
                None,
            )
            .unwrap()
        });

        let offset = Offset {
            inner: OffsetType::Tz(tz.as_ref()),
        };
        assert!(matches!(
            Time::new(1970, 1, 2, 12, 0, 0, 0, offset).unwrap_err(),
            TimeError::Unknown,
        ));
    }
}