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
//! This library reads the system timezone information files provided by IANA and returns a Tz struct containing the TZfile
//! fields as described in the man page (<http://man7.org/linux/man-pages/man5/tzfile.5.html>).
//!
//! For higher level parsing, you can enable the **parse** or **json** features (merged from the former [tzparse](https://crates.io/crates/tzparse) library).
//! 
//! In this documentation's examples, *tzfile* is the TZfile's path, for instance "/usr/share/zoneinfo/Europe/Paris".
//!
//! Without any feature enabled, one available method : new(), which returns a Tz struct:
//!```rust
//! # let tzfile = if cfg!(windows) { "c:\\Users\\nbauw\\Dev\\zoneinfo\\America\\Phoenix" } else { "/usr/share/zoneinfo/Europe/Paris" };
//! use libtzfile::Tz;
//! println!("{:?}", Tz::new(tzfile).unwrap());
//!```
//!
//!```text
//! Tz { tzh_timecnt_data: [-2717643600, -1633273200, -1615132800, -1601823600, -1583683200, -880210800, -820519140, -812653140, -796845540, -84380400, -68659200], tzh_timecnt_indices: [2, 1, 2, 1, 2, 3, 2, 3, 2, 1, 2], tzh_typecnt: [Ttinfo { tt_gmtoff: -26898, tt_isdst: 0, tt_abbrind: 0 }, Ttinfo { tt_gmtoff: -21600, tt_isdst: 1, tt_abbrind: 1 }, Ttinfo { tt_gmtoff: -25200, tt_isdst: 0, tt_abbrind: 2 }, Ttinfo { tt_gmtoff: -21600, tt_isdst: 1, tt_abbrind: 3 }], tz_abbr: ["LMT", "MDT", "MST", "MWT"] }
//! ```
//! 
//! With the parse or json features enabled, you have access to additional methods.
//! For instance, to display 2020 DST transitions in France, you can use the transition_times method:
//! 
//! ```rust
//! # let tzfile = if cfg!(windows) { "c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Paris" } else { "/usr/share/zoneinfo/Europe/Paris" };
//! use libtzfile::Tz;
//! println!("{:?}", Tz::new(tzfile).unwrap().transition_times(Some(2020)).unwrap());
//! ```
//! 
//! ```text
//! [TransitionTime { time: 2020-03-29T01:00:00Z, utc_offset: 7200, isdst: true, abbreviation: "CEST" }, TransitionTime { time: 2020-10-25T01:00:00Z, utc_offset: 3600, isdst: false, abbreviation: "CET" }]
//! ```
//! 
//! If you want more complete information about the timezone, you can use the zoneinfo method, which returns a more complete structure:
//! 
//! ```rust
//! # let tzfile = if cfg!(windows) { "c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Paris" } else { "/usr/share/zoneinfo/Europe/Paris" };
//! use libtzfile::Tz;
//! println!("{:?}", Tz::new(tzfile).unwrap().zoneinfo().unwrap());
//!```
//! 
//! ```text
//! Tzinfo { timezone: "Europe/Paris", utc_datetime: 2020-09-05T16:41:44.279502100Z, datetime: 2020-09-05T18:41:44.279502100+02:00, dst_from: Some(2020-03-29T01:00:00Z), dst_until: Some(2020-10-25T01:00:00Z), dst_period: true, raw_offset: 3600, dst_offset: 7200, utc_offset: +02:00, abbreviation: "CEST", week_number: 36 }
//! ```
//! 
//! This more complete structure can be transformed to json via a method of the json feature (which includes methods from the parse feature):
//!```rust
//! # let tzfile = if cfg!(windows) { "c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Paris" } else { "/usr/share/zoneinfo/Europe/Paris" };
//! use libtzfile::{Tz, TzError};
//! let tz = Tz::new(tzfile)?
//!     .zoneinfo()?
//!     .to_json()?;
//! println!("{}", tz);
//! # Ok::<(), TzError>(())
//!```
//! 
//!```text
//! {"timezone":"Europe/Paris","utc_datetime":"2020-09-05T18:04:50.546668500Z","datetime":"2020-09-05T20:04:50.546668500+02:00","dst_from":"2020-03-29T01:00:00Z","dst_until":"2020-10-25T01:00:00Z","dst_period":true,"raw_offset":3600,"dst_offset":7200,"utc_offset":"+02:00","abbreviation":"CEST","week_number":36}
//!```
//! 
//! This last method and feature is used for instance in my [world time API](https://github.com/nicolasbauw/world-time-api).
//! 
//! The tests (cargo test --features=json) are written to match [ubuntu version](https://techblog.pagesperso-orange.fr/files/tz2020a.tar.gz) of the [2020a timezone database](https://data.iana.org/time-zones/tz-link.html).

use byteorder::{ByteOrder, BE};
#[cfg(any(feature = "parse", feature = "json"))]
use chrono::{DateTime, TimeZone, Utc, FixedOffset};
use std::{error, fmt, fs::File, io::prelude::*, str::from_utf8};
#[cfg(feature = "json")]
use serde::Serialize;

#[cfg(feature = "json")]
mod offset_serializer {
    use serde::Serialize;
    fn offset_to_json(t: chrono::FixedOffset) -> String {
        format!("{:?}", t)
    }

    pub fn serialize<S: serde::Serializer>(
        time: &chrono::FixedOffset,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        offset_to_json(time.clone()).serialize(serializer)
    }
}

// TZif magic four bytes
const MAGIC: u32 = 0x545A6966;
// Header length
const HEADER_LEN: usize = 0x2C;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum TzError {
    // Invalid timezone
    InvalidTimezone,
    // Invalid file format.
    InvalidMagic,
    // Bad utf8 string
    BadUtf8String,
    // Only V2 format is supported
    UnsupportedFormat,
    // No data matched the request
    NoData,
    // Parsing Error
    ParseError,
    // Empty String
    EmptyString,
    // Json conversion error
    JsonError
}

impl fmt::Display for TzError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("TZfile error : ")?;
        f.write_str(match self {
            TzError::InvalidTimezone => "Invalid timezone",
            TzError::InvalidMagic => "Invalid TZfile",
            TzError::BadUtf8String => "Bad utf8 string",
            TzError::UnsupportedFormat => "Only V2 format is supported",
            TzError::NoData => "No data matched the request",
            TzError::ParseError => "Parsing error",
            TzError::EmptyString => "Empty string",
            TzError::JsonError => "Could not convert to json"
        })
    }
}

impl From<std::io::Error> for TzError {
    fn from(_e: std::io::Error) -> TzError {
        TzError::InvalidTimezone
    }
}

impl From<std::num::ParseIntError> for TzError {
    fn from(_e: std::num::ParseIntError) -> TzError {
        TzError::ParseError
    }
}

impl From<std::str::Utf8Error> for TzError {
    fn from(_e: std::str::Utf8Error) -> TzError {
        TzError::BadUtf8String
    }
}

impl From<TzError> for std::io::Error {
    fn from(e: TzError) -> std::io::Error {
        std::io::Error::new(std::io::ErrorKind::Other, e)
    }
}

#[cfg(feature = "json")]
impl From<serde_json::error::Error> for TzError {
    fn from(_e: serde_json::error::Error) -> TzError {
        TzError::JsonError
    }
}

impl error::Error for TzError {}

/// This is the crate's primary structure, which contains the splitted TZfile fields and optional (via features) methods.
#[derive(Debug)]
pub struct Tz {
    /// transition times timestamps table
    pub tzh_timecnt_data: Vec<i64>,
    /// indices for the next field
    pub tzh_timecnt_indices: Vec<u8>,
    /// a struct containing UTC offset, daylight saving time, abbreviation index
    pub tzh_typecnt: Vec<Ttinfo>,
    /// abbreviations table
    pub tz_abbr: Vec<String>,
    // Zone name
    name: String,
}

/// This sub-structure of the Tz struct is part of the TZfile format specifications, and contains UTC offset, daylight saving time, abbreviation index.
#[derive(Debug)]
pub struct Ttinfo {
    pub tt_gmtoff: isize,
    pub tt_isdst: u8,
    pub tt_abbrind: u8,
}

#[derive(Debug, PartialEq)]
struct Header {
    tzh_ttisgmtcnt: usize,
    tzh_ttisstdcnt: usize,
    tzh_leapcnt: usize,
    tzh_timecnt: usize,
    tzh_typecnt: usize,
    tzh_charcnt: usize,
    v2_header_start: usize,
}

#[cfg(any(feature = "parse", feature = "json"))]
/// The TransitionTime struct (available with the parse or json features) contains one transition time.
#[derive(Debug, PartialEq)]
pub struct TransitionTime {
    /// The UTC time and date of the transition time, BEFORE new parameters apply
    pub time: DateTime<Utc>,
    /// The UPCOMING offset to UTC
    pub utc_offset: isize,
    /// Is upcoming change dst ?
    pub isdst: bool,
    /// TZ abbreviation of upcoming change
    pub abbreviation: String,
}

/// Convenient and human-readable informations about a timezone (available with the parse or json features).
/// 
/// Some explanations about the offset fields:
/// - raw_offset : the "normal" offset to utc, in seconds
/// - dst_offset : the offset to utc during daylight saving time, in seconds
/// - utc_offset : the current offset to utc, taking into account daylight saving time or not (according to dst_from and dst_until), in +/- HH:MM
#[cfg(feature = "json")]
#[derive(Debug, Serialize)]
pub struct Tzinfo {
    /// Timezone name
    pub timezone: String,
    /// UTC time
    pub utc_datetime: DateTime<Utc>,
    /// Local time
    pub datetime: DateTime<FixedOffset>,
    /// Start of DST period
    pub dst_from: Option<DateTime<Utc>>,
    /// End of DST period
    pub dst_until: Option<DateTime<Utc>>,
    /// Are we in DST period ?
    pub dst_period: bool,
    /// Normal offset to UTC, in seconds
    pub raw_offset: isize,
    /// DST offset to UTC, in seconds
    pub dst_offset: isize,
    /// current offset to UTC, in +/-HH:MM
    #[serde(with = "offset_serializer")]
    pub utc_offset: FixedOffset,
    /// Timezone abbreviation
    pub abbreviation: String,
    /// Week number
    pub week_number: i32,
}

#[cfg(feature = "parse")]
#[derive(Debug)]
pub struct Tzinfo {
    /// Timezone name
    pub timezone: String,
    /// UTC time
    pub utc_datetime: DateTime<Utc>,
    /// Local time
    pub datetime: DateTime<FixedOffset>,
    /// Start of DST period
    pub dst_from: Option<DateTime<Utc>>,
    /// End of DST period
    pub dst_until: Option<DateTime<Utc>>,
    /// Are we in DST period ?
    pub dst_period: bool,
    /// Normal offset to UTC, in seconds
    pub raw_offset: isize,
    /// DST offset to UTC, in seconds
    pub dst_offset: isize,
    /// current offset to UTC, in +/-HH:MM
    pub utc_offset: FixedOffset,
    /// Timezone abbreviation
    pub abbreviation: String,
    /// Week number
    pub week_number: i32,
}

#[cfg(feature = "json")]
impl Tzinfo {
    /// Transforms the Tzinfo struct to a JSON string
    /// 
    ///```rust
    /// # let tzfile = if cfg!(windows) { "c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Paris" } else { "/usr/share/zoneinfo/Europe/Paris" };
    /// use libtzfile::{Tz, TzError};
    /// let tz = Tz::new(tzfile)?
    ///     .zoneinfo()?
    ///     .to_json()?;
    /// println!("{}", tz);
    /// # Ok::<(), TzError>(())
    ///```
    /// 
    ///```text
    /// {"timezone":"Europe/Paris","utc_datetime":"2020-09-05T18:04:50.546668500Z","datetime":"2020-09-05T20:04:50.546668500+02:00","dst_from":"2020-03-29T01:00:00Z","dst_until":"2020-10-25T01:00:00Z","dst_period":true,"raw_offset":3600,"dst_offset":7200,"utc_offset":"+02:00","abbreviation":"CEST","week_number":36}
    ///```
    pub fn to_json(&self) -> Result<String, serde_json::error::Error> {
        serde_json::to_string(self)
    }
}

impl Tz {
    /// Creates a Tz struct from a timezone file.
    ///
    /// ```rust
    /// # let tzfile = if cfg!(windows) { "c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Paris" } else { "/usr/share/zoneinfo/Europe/Paris" };
    /// use libtzfile::Tz;
    /// let tz = Tz::new(tzfile).unwrap();
    /// ```
    /// 
    ///```text
    /// Tz { tzh_timecnt_data: [-2717643600, -1633273200, -1615132800, -1601823600, -1583683200, -880210800, -820519140, -812653140, -796845540, -84380400, -68659200], tzh_timecnt_indices: [2, 1, 2, 1, 2, 3, 2, 3, 2, 1, 2], tzh_typecnt: [Ttinfo { tt_gmtoff: -26898, tt_isdst: 0, tt_abbrind: 0 }, Ttinfo { tt_gmtoff: -21600, tt_isdst: 1, tt_abbrind: 1 }, Ttinfo { tt_gmtoff: -25200, tt_isdst: 0, tt_abbrind: 2 }, Ttinfo { tt_gmtoff: -21600, tt_isdst: 1, tt_abbrind: 3 }], tz_abbr: ["LMT", "MDT", "MST", "MWT"] }
    /// ```
    pub fn new(tz: &str) -> Result<Tz, TzError> {
        // Reads TZfile
        let buf = Tz::read(tz)?;
        // Parses TZfile header
        let header = Tz::parse_header(&buf)?;
        // Parses data
        Tz::parse_data(&buf, header, tz)
    }

    #[cfg(any(feature = "parse", feature = "json"))]
    /// Returns year's transition times for a timezone.
    /// If year is Some(0), returns current year's transition times.
    /// If there's no transition time for selected year, returns the last occured transition time (zone's current parameters).
    /// If no year (None) is specified, returns all transition times recorded in the TZfile .
    /// 
    /// ```rust
    /// # let tzfile = if cfg!(windows) { "c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Paris" } else { "/usr/share/zoneinfo/Europe/Paris" };
    /// use libtzfile::Tz;
    /// println!("{:?}", Tz::new(tzfile).unwrap().transition_times(Some(2020)).unwrap());
    /// ```
    /// 
    /// ```text
    /// [TransitionTime { time: 2020-03-29T01:00:00Z, utc_offset: 7200, isdst: true, abbreviation: "CEST" }, TransitionTime { time: 2020-10-25T01:00:00Z, utc_offset: 3600, isdst: false, abbreviation: "CET" }]
    /// ```
    pub fn transition_times(&self, y: Option<i32>) -> Result<Vec<TransitionTime>, TzError> {
        let timezone = self;

        // used to store transition time indices
        let mut timechanges = Vec::new();
        let mut nearest_timechange: usize = 0;

        // Used to store parsed transition times
        let mut parsedtimechanges = Vec::new();

        // Get and store the transition time indices for requested year
        if y.is_some() {
            let d = Utc::now();
            let y = y.unwrap();
            // year = 0 ? current year is requested
            let y = if y == 0 {
                d.format("%Y").to_string().parse()?
            } else {
                y
            };
            // for year comparison
            let yearbeg = Utc.ymd(y, 1, 1).and_hms(0, 0, 0).timestamp();
            let yearend = Utc.ymd(y, 12, 31).and_hms(0, 0, 0).timestamp();
            for t in 0..timezone.tzh_timecnt_data.len() {
                if timezone.tzh_timecnt_data[t] > yearbeg && timezone.tzh_timecnt_data[t] < yearend
                {
                    timechanges.push(t);
                }
                if timezone.tzh_timecnt_data[t] < yearbeg {
                    nearest_timechange = t;
                };
            }
        } else {
            // No year requested ? stores all transition times
            for t in 0..timezone.tzh_timecnt_data.len() {
                /* patch : chrono panics on an overflowing timestamp, and a 0xF800000000000000 timestamp is present in some Debian 10 TZfiles.*/
                if timezone.tzh_timecnt_data[t] != -576460752303423488 {
                    timechanges.push(t)
                };
            }
        }

        // Populating returned Vec<Tt>
        if timechanges.len() != 0 {
            for t in 0..timechanges.len() {
                let tc = TransitionTime {
                    time: Utc.timestamp(timezone.tzh_timecnt_data[timechanges[t]], 0),
                    utc_offset: timezone.tzh_typecnt
                        [timezone.tzh_timecnt_indices[timechanges[t]] as usize]
                        .tt_gmtoff,
                    isdst: timezone.tzh_typecnt
                        [timezone.tzh_timecnt_indices[timechanges[t]] as usize]
                        .tt_isdst
                        == 1,
                    abbreviation: timezone.tz_abbr[timezone.tzh_typecnt
                        [timezone.tzh_timecnt_indices[timechanges[t]] as usize]
                        .tt_abbrind as usize]
                        .to_string(),
                };
                parsedtimechanges.push(tc);
            }
        } else {
            let tc = TransitionTime {
                time: Utc.timestamp(timezone.tzh_timecnt_data[nearest_timechange], 0),
                utc_offset: timezone.tzh_typecnt
                    [timezone.tzh_timecnt_indices[nearest_timechange] as usize]
                    .tt_gmtoff,
                isdst: timezone.tzh_typecnt
                    [timezone.tzh_timecnt_indices[nearest_timechange] as usize]
                    .tt_isdst
                    == 1,
                abbreviation: timezone.tz_abbr[timezone.tzh_typecnt
                    [timezone.tzh_timecnt_indices[nearest_timechange] as usize]
                    .tt_abbrind as usize]
                    .to_string(),
            };
            parsedtimechanges.push(tc);
        }
        Ok(parsedtimechanges)
    }

    #[cfg(any(feature = "parse", feature = "json"))]
    /// Returns convenient data about a timezone for current date and time.
    /// ```rust
    /// # let tzfile = if cfg!(windows) { "c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Paris" } else { "/usr/share/zoneinfo/Europe/Paris" };
    /// use libtzfile::Tz;
    /// println!("{:?}", Tz::new(tzfile).unwrap().zoneinfo().unwrap());
    /// ```
    /// 
    /// ```text
    /// Tzinfo { timezone: "Europe/Paris", utc_datetime: 2020-09-05T16:41:44.279502100Z, datetime: 2020-09-05T18:41:44.279502100+02:00, dst_from: Some(2020-03-29T01:00:00Z), dst_until: Some(2020-10-25T01:00:00Z), dst_period: true, raw_offset: 3600, dst_offset: 7200, utc_offset: +02:00, abbreviation: "CEST", week_number: 36 }
    /// ```
    pub fn zoneinfo(&self) -> Result<Tzinfo, TzError> {
        let parsedtimechanges = self.transition_times(Some(0))?;
        let d = Utc::now();
        if parsedtimechanges.len() == 2 {
            // 2 times changes the same year ? DST observed
            // Are we in a dst period ? true / false
            let dst = d > parsedtimechanges[0].time && d < parsedtimechanges[1].time;
            let utc_offset = if dst == true {
                FixedOffset::east(parsedtimechanges[0].utc_offset as i32)
            } else {
                FixedOffset::east(parsedtimechanges[1].utc_offset as i32)
            };
            Ok(Tzinfo {
                timezone: (self.name).clone(),
                week_number: d
                    .with_timezone(&utc_offset)
                    .format("%V")
                    .to_string()
                    .parse()?,
                utc_datetime: d,
                datetime: d.with_timezone(&utc_offset),
                dst_from: Some(parsedtimechanges[0].time),
                dst_until: Some(parsedtimechanges[1].time),
                dst_period: dst,
                raw_offset: parsedtimechanges[1].utc_offset,
                dst_offset: parsedtimechanges[0].utc_offset,
                utc_offset: utc_offset,
                abbreviation: if dst == true {
                    parsedtimechanges[0].abbreviation.clone()
                } else {
                    parsedtimechanges[1].abbreviation.clone()
                },
            })
        } else if parsedtimechanges.len() == 1 {
            let utc_offset = FixedOffset::east(parsedtimechanges[0].utc_offset as i32);
            Ok(Tzinfo {
                timezone: (self.name).clone(),
                week_number: d
                    .with_timezone(&utc_offset)
                    .format("%V")
                    .to_string()
                    .parse()?,
                utc_datetime: d,
                datetime: d.with_timezone(&utc_offset),
                dst_from: None,
                dst_until: None,
                dst_period: false,
                raw_offset: parsedtimechanges[0].utc_offset,
                dst_offset: 0,
                utc_offset: utc_offset,
                abbreviation: parsedtimechanges[0].abbreviation.clone(),
            })
        } else {
            Err(TzError::NoData)
        }
    }

    fn parse_header(buffer: &Vec<u8>) -> Result<Header, TzError> {
        let magic = BE::read_u32(&buffer[0x00..=0x03]);
        if magic != MAGIC {
            return Err(TzError::InvalidMagic);
        }
        if buffer[4] != 50 {
            return Err(TzError::UnsupportedFormat);
        }
        let tzh_ttisgmtcnt = BE::read_i32(&buffer[0x14..=0x17]) as usize;
        let tzh_ttisstdcnt = BE::read_i32(&buffer[0x18..=0x1B]) as usize;
        let tzh_leapcnt = BE::read_i32(&buffer[0x1C..=0x1F]) as usize;
        let tzh_timecnt = BE::read_i32(&buffer[0x20..=0x23]) as usize;
        let tzh_typecnt = BE::read_i32(&buffer[0x24..=0x27]) as usize;
        let tzh_charcnt = BE::read_i32(&buffer[0x28..=0x2b]) as usize;
        // V2 format data start
        let s: usize = tzh_timecnt * 5
            + tzh_typecnt * 6
            + tzh_leapcnt * 8
            + tzh_charcnt
            + tzh_ttisstdcnt
            + tzh_ttisgmtcnt
            + 44;
        Ok(Header {
            tzh_ttisgmtcnt: BE::read_i32(&buffer[s + 0x14..=s + 0x17]) as usize,
            tzh_ttisstdcnt: BE::read_i32(&buffer[s + 0x18..=s + 0x1B]) as usize,
            tzh_leapcnt: BE::read_i32(&buffer[s + 0x1C..=s + 0x1F]) as usize,
            tzh_timecnt: BE::read_i32(&buffer[s + 0x20..=s + 0x23]) as usize,
            tzh_typecnt: BE::read_i32(&buffer[s + 0x24..=s + 0x27]) as usize,
            tzh_charcnt: BE::read_i32(&buffer[s + 0x28..=s + 0x2b]) as usize,
            v2_header_start: s,
        })
    }

    fn parse_data(buffer: &Vec<u8>, header: Header, filename: &str) -> Result<Tz, TzError> {
        // Calculates fields lengths and indexes (Version 2 format)
        let tzh_timecnt_len: usize = header.tzh_timecnt * 9;
        let tzh_typecnt_len: usize = header.tzh_typecnt * 6;
        let tzh_leapcnt_len: usize = header.tzh_leapcnt * 12;
        let tzh_charcnt_len: usize = header.tzh_charcnt;
        let tzh_timecnt_end: usize = HEADER_LEN + header.v2_header_start + tzh_timecnt_len;
        let tzh_typecnt_end: usize = tzh_timecnt_end + tzh_typecnt_len;
        let tzh_leapcnt_end: usize = tzh_typecnt_end + tzh_leapcnt_len;
        let tzh_charcnt_end: usize = tzh_leapcnt_end + tzh_charcnt_len;

        // Extracting data fields
        let tzh_timecnt_data: Vec<i64> = buffer[HEADER_LEN + header.v2_header_start
            ..HEADER_LEN + header.v2_header_start + header.tzh_timecnt * 8]
            .chunks_exact(8)
            .map(|tt| BE::read_i64(tt))
            .collect();

        let tzh_timecnt_indices: &[u8] =
            &buffer[HEADER_LEN + header.v2_header_start + header.tzh_timecnt * 8..tzh_timecnt_end];

        let abbrs = from_utf8(&buffer[tzh_leapcnt_end..tzh_charcnt_end])?;

        let tzh_typecnt: Vec<Ttinfo> = buffer[tzh_timecnt_end..tzh_typecnt_end]
            .chunks_exact(6)
            .map(|tti| {
                let offset = tti[5];
                let index = abbrs
                    .chars()
                    .take(offset as usize)
                    .filter(|x| *x == '\0')
                    .count();
                Ttinfo {
                    tt_gmtoff: BE::read_i32(&tti[0..4]) as isize,
                    tt_isdst: tti[4],
                    tt_abbrind: index as u8,
                }
            })
            .collect();

        let mut tz_abbr: Vec<String> = abbrs.split("\u{0}").map(|st| st.to_string()).collect();
        // Removes last empty char
        if tz_abbr.pop().is_none() {
            return Err(TzError::EmptyString);
        };

        // Generating zone name (ie. Europe/Paris) from requested file name
        let mut timezone = String::new();
        #[cfg(not(windows))]
        let mut tz: Vec<&str> = filename.split("/").collect();
        #[cfg(windows)]
        let mut tz: Vec<&str> = filename.split("\\").collect();
        // To prevent crash (case of requested directory separator unmatching OS separator)
        if tz.len() < 3 {
            return Err(TzError::InvalidTimezone);
        }
        for _ in 0..(tz.len()) - 2 {
            tz.remove(0);
        }
        if tz[0] != "zoneinfo" {
            timezone.push_str(tz[0]);
            timezone.push_str("/");
        }
        timezone.push_str(tz[1]);

        Ok(Tz {
            tzh_timecnt_data: tzh_timecnt_data,
            tzh_timecnt_indices: tzh_timecnt_indices.to_vec(),
            tzh_typecnt: tzh_typecnt,
            tz_abbr: tz_abbr,
            name: timezone,
        })
    }

    fn read(tz: &str) -> Result<Vec<u8>, std::io::Error> {
        let mut f = File::open(tz)?;
        let mut buffer = Vec::new();
        f.read_to_end(&mut buffer)?;
        Ok(buffer)
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    #[cfg(target_os = "windows")]
    static TIMEZONE: &str = "c:\\Users\\nbauw\\Dev\\zoneinfo\\America\\Phoenix";
    #[cfg(target_os = "linux")]
    static TIMEZONE: &str = "/usr/share/zoneinfo/America/Phoenix";
    #[cfg(target_os = "macos")]
    static TIMEZONE: &str = "/Users/nicolasb/Dev/zoneinfo/America/Phoenix";
    #[test]
    fn read_file() {
        assert_eq!(Tz::read(TIMEZONE).is_ok(), true);
    }

    #[test]
    fn parse_hdr() {
        let buf = Tz::read(TIMEZONE).unwrap();
        let amph = Header {
            tzh_ttisgmtcnt: 4,
            tzh_ttisstdcnt: 4,
            tzh_leapcnt: 0,
            tzh_timecnt: 11,
            tzh_typecnt: 4,
            tzh_charcnt: 16,
            v2_header_start: 147,
        };
        assert_eq!(Tz::parse_header(&buf).unwrap(), amph);
    }

    #[test]
    fn parse_indices() {
        let amph: [u8; 11] = [2, 1, 2, 1, 2, 3, 2, 3, 2, 1, 2];
        assert_eq!(Tz::new(TIMEZONE).unwrap().tzh_timecnt_indices, amph);
    }

    #[test]
    fn parse_timedata() {
        let amph: Vec<i64> = vec![
            -2717643600,
            -1633273200,
            -1615132800,
            -1601823600,
            -1583683200,
            -880210800,
            -820519140,
            -812653140,
            -796845540,
            -84380400,
            -68659200,
        ];
        assert_eq!(Tz::new(TIMEZONE).unwrap().tzh_timecnt_data, amph);
    }

    #[test]
    fn parse_ttgmtoff() {
        let amph: [isize; 4] = [-26898, -21600, -25200, -21600];
        let c: Vec<isize> = Tz::new(TIMEZONE)
            .unwrap()
            .tzh_typecnt
            .iter()
            .map(|ttinfo| ttinfo.tt_gmtoff)
            .collect();
        assert_eq!(c, amph);
    }

    #[test]
    fn parse_abbr() {
        let abbr: Vec<String> = vec!["LMT", "MDT", "MST", "MWT"]
            .iter()
            .map(|x| x.to_string())
            .collect();
        assert_eq!(Tz::new(TIMEZONE).unwrap().tz_abbr, abbr);
    }

    #[test]
    fn parse_abbr_amsterdam() {
        #[cfg(target_os = "windows")]
        let timezone = "c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Amsterdam";
        #[cfg(target_os = "linux")]
        let timezone = "/usr/share/zoneinfo/Europe/Amsterdam";
        #[cfg(target_os = "macos")]
        let timezone = "/Users/nicolasb/Dev/zoneinfo/Europe/Amsterdam";
        let abbr: Vec<String> = vec!["LMT", "NST", "AMT", "+0020", "+0120", "CET", "CEST"]
            .iter()
            .map(|x| x.to_string())
            .collect();
        assert_eq!(Tz::new(timezone).unwrap().tz_abbr, abbr);
        dbg!(Tz::new(timezone).unwrap());
    }

    #[test]
    fn zonename() {
        let z = "America/Phoenix";
        assert_eq!(Tz::new(TIMEZONE).unwrap().name, z);
    }

    // cargo test --features=parse
    #[cfg(any(feature = "parse", feature = "json"))]
    #[test]
    fn partial_timechanges() {
        let tt = vec![
            TransitionTime {
                time: Utc.ymd(2019, 3, 31).and_hms(1, 0, 0),
                utc_offset: 7200,
                isdst: true,
                abbreviation: String::from("CEST"),
            },
            TransitionTime {
                time: Utc.ymd(2019, 10, 27).and_hms(1, 0, 0),
                utc_offset: 3600,
                isdst: false,
                abbreviation: String::from("CET"),
            },
        ];
        #[cfg(target_os = "linux")]
        let tz = Tz::new("/usr/share/zoneinfo/Europe/Paris").unwrap();
        #[cfg(target_os = "windows")]
        let tz = Tz::new("c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Paris").unwrap();
        #[cfg(target_os = "macos")]
        let tz = Tz::new("/Users/nicolasb/Dev/zoneinfo/Europe/Paris").unwrap();
        assert_eq!(tz.transition_times(Some(2019)).unwrap(), tt);
    }

    #[cfg(any(feature = "parse", feature = "json"))]
    #[test]
    fn total_timechanges() {
        let tt = vec![
            TransitionTime {
                time: Utc.ymd(1883, 11, 18).and_hms(19, 0, 0),
                utc_offset: -25200,
                isdst: false,
                abbreviation: String::from("MST"),
            },
            TransitionTime {
                time: Utc.ymd(1918, 03, 31).and_hms(9, 0, 0),
                utc_offset: -21600,
                isdst: true,
                abbreviation: String::from("MDT"),
            },
            TransitionTime {
                time: Utc.ymd(1918, 10, 27).and_hms(8, 0, 0),
                utc_offset: -25200,
                isdst: false,
                abbreviation: String::from("MST"),
            },
            TransitionTime {
                time: Utc.ymd(1919, 03, 30).and_hms(9, 0, 0),
                utc_offset: -21600,
                isdst: true,
                abbreviation: String::from("MDT"),
            },
            TransitionTime {
                time: Utc.ymd(1919, 10, 26).and_hms(8, 0, 0),
                utc_offset: -25200,
                isdst: false,
                abbreviation: String::from("MST"),
            },
            TransitionTime {
                time: Utc.ymd(1942, 02, 09).and_hms(9, 0, 0),
                utc_offset: -21600,
                isdst: true,
                abbreviation: String::from("MWT"),
            },
            TransitionTime {
                time: Utc.ymd(1944, 01, 01).and_hms(6, 1, 0),
                utc_offset: -25200,
                isdst: false,
                abbreviation: String::from("MST"),
            },
            TransitionTime {
                time: Utc.ymd(1944, 04, 01).and_hms(7, 1, 0),
                utc_offset: -21600,
                isdst: true,
                abbreviation: String::from("MWT"),
            },
            TransitionTime {
                time: Utc.ymd(1944, 10, 01).and_hms(6, 1, 0),
                utc_offset: -25200,
                isdst: false,
                abbreviation: String::from("MST"),
            },
            TransitionTime {
                time: Utc.ymd(1967, 04, 30).and_hms(9, 0, 0),
                utc_offset: -21600,
                isdst: true,
                abbreviation: String::from("MDT"),
            },
            TransitionTime {
                time: Utc.ymd(1967, 10, 29).and_hms(8, 0, 0),
                utc_offset: -25200,
                isdst: false,
                abbreviation: String::from("MST"),
            },
        ];
        let tz = Tz::new(TIMEZONE).unwrap();
        assert_eq!(tz.transition_times(None).unwrap(), tt);
    }

    #[cfg(any(feature = "parse", feature = "json"))]
    #[test]
    fn zoneinfo() {
        #[cfg(target_os = "linux")]
        let tztest = Tz::new("/usr/share/zoneinfo/Europe/Paris").unwrap().zoneinfo().unwrap();
        #[cfg(target_os = "windows")]
        let tztest = (Tz::new("c:\\Users\\nbauw\\Dev\\zoneinfo\\Europe\\Paris").unwrap()).zoneinfo().unwrap();
        #[cfg(target_os = "macos")]
        let tztest = Tz::new("/Users/nicolasb/Dev/zoneinfo/Europe/Paris").unwrap().zoneinfo().unwrap();
        assert_eq!(tztest.timezone, String::from("Europe/Paris"));
        assert_eq!(tztest.raw_offset, 3600);
        assert_eq!(tztest.dst_offset, 7200);
    }
}