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
//! Datetimes with a variable UTC offset, and time zone calculations.

use std::borrow::Cow;
use std::sync::Arc;

use duration::Duration;
use instant::Instant;
use cal::{LocalDateTime, DatePiece, TimePiece, Month, Weekday};
use util::RangeExt;


/// A **time zone**, which here is a list of timespans, each containing a
/// fixed offset for the current location’s time from UTC.
#[derive(Debug, Clone)]
pub struct TimeZone(pub TimeZoneSource<'static>);

#[derive(Debug, Clone)]
pub enum TimeZoneSource<'a> {
    Static(&'a StaticTimeZone<'a>),
    Runtime(Arc<runtime::OwnedTimeZone>),
}

#[derive(PartialEq, Debug)]
pub struct StaticTimeZone<'a> {

    /// This zone’s name in the zoneinfo database, such as “America/New_York”.
    pub name: &'a str,

    /// The set of timespans used in this time zone.
    pub fixed_timespans: FixedTimespanSet<'a>,
}

impl TimeZone {

    pub fn zone_name(&self) -> Option<&str> {
        match self.0 {
            TimeZoneSource::Static(ref tz)   => Some(tz.name),
            TimeZoneSource::Runtime(ref arc) => arc.name.as_ref().map(|x| &**x),
        }
    }

    /// Returns the total offset from UTC, in seconds, that this time zone
    /// has at the given datetime.
    pub fn offset(&self, datetime: LocalDateTime) -> i64 {
        match self.0 {
            TimeZoneSource::Static(ref tz)   => tz.fixed_timespans.offset(datetime),
            TimeZoneSource::Runtime(ref arc) => arc.fixed_timespans.borrow().offset(datetime),
        }
    }

    /// Returns the time zone abbreviation that this time zone has at the
    /// given datetime. As always, abbreviations are notoriously vague, and
    /// should only be used when referring to a known timezone.
    pub fn name(&self, datetime: LocalDateTime) -> String {
        match self.0 {
            TimeZoneSource::Static(ref tz)   => tz.fixed_timespans.name(datetime),
            TimeZoneSource::Runtime(ref arc) => arc.fixed_timespans.borrow().name(datetime),
        }
    }

    /// Whether this time zone is “fixed”: a fixed time zone has no
    /// transitions, meaning it will always be at the same offset from UTC.
    ///
    /// There are relatively few of these, namely the European timezones
    /// WET, CET, MET, and EET, and the North American timezones EST5EDT,
    /// CST6CDT, MST7MDT, and PST8PDT, none of which actually corresponds to
    /// a geographical location.
    pub fn is_fixed(&self) -> bool {
        match self.0 {
            TimeZoneSource::Static(ref tz)   => tz.fixed_timespans.is_fixed(),
            TimeZoneSource::Runtime(ref arc) => arc.fixed_timespans.borrow().is_fixed(),
        }
    }

    /// Converts a local datetime in UTC to a zoned datetime that uses this
    /// time zone.
    pub fn to_zoned(&self, datetime: LocalDateTime) -> LocalDateTime {
        datetime + Duration::of(self.offset(datetime))
    }

    /// Converts a local datetime that is *already* informally in this time
    /// zone into a zoned datetime that actually uses this time zone.
    ///
    /// For example, say you have the current time for a time zone, but you
    /// *don’t* know what the current offset from UTC is. This method
    /// computes the offset, then *subtracts* rather than adds it, resulting
    /// in a value that gets displayed as the current time. In other words,
    /// calling `hour()` or `year()` or any of the other view methods on one
    /// of the resulting values will *always* return the same as the
    /// datetime initially passed in, no matter what the current offset is.
    ///
    /// This method can return 0, 1, or 2 values, depending on whether the
    /// datetime passed in falls between two timespans (an impossible time)
    /// or overlaps two separate timespans (an ambiguous time). The result
    /// will *almost* always be precise, but there are edge cases you need
    /// to watch out for.
    pub fn convert_local(&self, local: LocalDateTime) -> LocalTimes {
        match self.0 {
            TimeZoneSource::Static(ref tz)   => tz.fixed_timespans.convert_local(local, &self.0),
            TimeZoneSource::Runtime(ref arc) => arc.fixed_timespans.borrow().convert_local(local, &self.0),
        }
    }
}


/// A set of timespans, separated by the instances at which the timespans
/// change over. There will always be one more timespan than transitions.
#[derive(PartialEq, Debug, Clone)]
pub struct FixedTimespanSet<'a> {

    /// The first timespan, which is assumed to have been in effect up until
    /// the initial transition instant (if any). Each set has to have at
    /// least one timespan.
    pub first: FixedTimespan<'a>,

    /// The rest of the timespans, as a slice of tuples, each containing:
    ///
    /// 1. A transition instant at which the previous timespan ends and the
    ///    next one begins, stored as a Unix timestamp;
    /// 2. The actual timespan to transition into.
    pub rest: &'a [ (i64, FixedTimespan<'a>) ],
}

/// An individual timespan with a fixed offset.
#[derive(PartialEq, Debug, Clone)]
pub struct FixedTimespan<'a> {

    /// The *total* offset in effect during this timespan, in seconds. This
    /// is the sum of the standard offset from UTC (the zone’s standard
    /// time), and any extra daylight-saving offset.
    pub offset: i64,

    /// Whether there was any daylight-saving offset in effect during this
    /// timespan.
    pub is_dst: bool,

    /// The abbreviation in use during this timespan, such as “GMT” or
    /// “PDT”. Abbreviations are notoriously vague, and should only be used
    /// for referring to a known timezone.
    pub name: Cow<'a, str>,
}

impl<'a> FixedTimespanSet<'a> {
    fn find(&self, time: i64) -> &FixedTimespan {
        match self.rest.iter().take_while(|t| t.0 < time).last() {
            None     => &self.first,
            Some(zd) => &zd.1,
        }
    }

    fn offset(&self, datetime: LocalDateTime) -> i64 {
        let unix_timestamp = datetime.to_instant().seconds();
        self.find(unix_timestamp).offset
    }

    fn name(&self, datetime: LocalDateTime) -> String {
        let unix_timestamp = datetime.to_instant().seconds();
        self.find(unix_timestamp).name.to_string()
    }

    fn is_fixed(&self) -> bool {
        self.rest.is_empty()
    }

    fn convert_local(&self, local: LocalDateTime, source: &TimeZoneSource<'a>) -> LocalTimes<'a> {
        let unix_timestamp = local.to_instant().seconds();

        let zonify = |offset| ZonedDateTime {
            adjusted: local,
            current_offset: offset,
            time_zone: source.clone(),
        };

        let timespans = self.find_with_surroundings(unix_timestamp);

        if let Some((previous_zone, previous_transition_time)) = timespans.previous {

            assert!(timespans.current.offset != previous_zone.offset,
                    "Offsets cannot be equal! Is this a non-transition transition?");

            println!("unix timestamp {:?}, previous time {:?}", unix_timestamp, previous_transition_time);

            // Test whether this timestamp is in the *overlap* after the
            // current timespan starts but before the previous one ends.
            if previous_zone.offset > timespans.current.offset
            && (unix_timestamp - previous_transition_time).is_within(timespans.current.offset .. previous_zone.offset) {
                return LocalTimes::Ambiguous {
                    earlier:  zonify(previous_zone.offset),
                    later:    zonify(timespans.current.offset),
                };
            }

            // Test whether this timestamp is in the *space* after the
            // previous timespan ends but before the current one starts.
            if previous_zone.offset < timespans.current.offset
            && (unix_timestamp - previous_transition_time).is_within(previous_zone.offset .. timespans.current.offset) {
                return LocalTimes::Impossible;
            }
        }

        if let Some(&(next_transition_time, ref next_zone)) = timespans.next {

            assert!(timespans.current.offset != next_zone.offset,
                "Offsets cannot be equal! Is this a non-transition transition?");

            println!("unix timestamp {:?}, next time {:?}", unix_timestamp, next_transition_time);
            println!("offset 1 {:?}, offset 2 {:?}", next_zone.offset, timespans.current.offset);

            // Test whether this timestamp is in the *overlap* after the
            // next timespan starts but before the current one ends.
            if timespans.current.offset > next_zone.offset
            && (unix_timestamp - next_transition_time).is_within(next_zone.offset .. timespans.current.offset) {
                return LocalTimes::Ambiguous {
                    earlier:  zonify(timespans.current.offset),
                    later:    zonify(next_zone.offset),
                };
            }

            // Test whether this timestamp is in the *space* after the
            // current timespan ends but before the next one starts.
            if timespans.current.offset < next_zone.offset
            && (unix_timestamp - next_transition_time).is_within(timespans.current.offset .. next_zone.offset) {
                return LocalTimes::Impossible;
            }
        }

        LocalTimes::Precise(zonify(timespans.current.offset))
    }

    fn find_with_surroundings(&self, time: i64) -> Surroundings {
        if let Some((position, _)) = self.rest.iter().enumerate().take_while(|&(_, t)| t.0 < time).last() {
            // There’s a matching time in the ‘rest’ list, so return that
            // time along with the two sets of details around it.

            let previous_details = if position == 0 {
                &self.first
            }
            else {
                &self.rest[position - 1].1
            };

            Surroundings {
                previous:  Some((previous_details, self.rest[position].0)),
                current:   &self.rest[position].1,
                next:      self.rest.get(position + 1),
            }
        }
        else {
            // If there’s no matching time in the ‘rest’ list, it must be
            // the ‘first’ one.
            Surroundings {
                previous: None,
                current:  &self.first,
                next:     self.rest.get(0),
            }
        }
    }
}


#[derive(PartialEq, Debug)]
struct Surroundings<'a> {
    previous:  Option<(&'a FixedTimespan<'a>, i64)>,
    current:   &'a FixedTimespan<'a>,
    next:      Option<&'a (i64, FixedTimespan<'a>)>,
}


/// The result of converting a *local* time to a *zoned* time with the same
/// time components. See `TimeZone::convert_local` for more information.
#[derive(Debug)]
pub enum LocalTimes<'a> {

    /// This local time is impossible (when a time occurs between two
    /// timespans, which should never be shown on a wall clock).
    Impossible,

    /// This local time can be defined unambiguously.
    Precise(ZonedDateTime<'a>),

    /// This local time is ambiguous (when a time overlaps two timespans,
    /// which happens twice on a wall clock rather than once).
    Ambiguous { earlier: ZonedDateTime<'a>, later: ZonedDateTime<'a> },
}

impl<'a> LocalTimes<'a> {

    /// Extracts the *precise* zoned date time, if present; **panics otherwise**.
    ///
    /// It is almost always preferable to use pattern matching on a
    /// `LocalTimes` value and handle the impossible/ambiguous cases
    /// explicitly, rather than risking a panic.
    pub fn unwrap_precise(self) -> ZonedDateTime<'a> {
        match self {
            LocalTimes::Precise(p)        => p,
            LocalTimes::Impossible        => panic!("called `LocalTimes::unwrap()` on an `Impossible` value"),
            LocalTimes::Ambiguous { .. }  => panic!("called `LocalTimes::unwrap()` on an `Ambiguous` value: {:?}", self),
        }
    }

    /// Returns whether this local times result is impossible (when a time
    /// occurs between two timespans, which should never be shown on a wall
    /// clock).
    pub fn is_impossible(&self) -> bool {
        match *self {
            LocalTimes::Impossible => true,
            _                      => false,
        }
    }

    /// Returns whether this local times result is ambiguous (when a time
    /// overlaps two timespans, which happens twice on a wall clock rather
    /// than once).
    pub fn is_ambiguous(&self) -> bool {
        match *self {
            LocalTimes::Ambiguous { .. } => true,
            _                            => false,
        }
    }
}


#[derive(Debug)]
pub struct ZonedDateTime<'a> {
    adjusted: LocalDateTime,
    current_offset: i64,
    time_zone: TimeZoneSource<'a>,
}

impl<'a> ZonedDateTime<'a> {
    pub fn to_instant(&self) -> Instant {
        (self.adjusted - Duration::of(self.current_offset)).to_instant()
    }
}

impl<'a> DatePiece for ZonedDateTime<'a> {
    fn year(&self) -> i64 { self.adjusted.year() }
    fn month(&self) -> Month { self.adjusted.month() }
    fn day(&self) -> i8 { self.adjusted.day() }
    fn yearday(&self) -> i16 { self.adjusted.yearday() }
    fn weekday(&self) -> Weekday { self.adjusted.weekday() }
}

impl<'a> TimePiece for ZonedDateTime<'a> {
    fn hour(&self) -> i8 { self.adjusted.hour() }
    fn minute(&self) -> i8 { self.adjusted.minute() }
    fn second(&self) -> i8 { self.adjusted.second() }
    fn millisecond(&self) -> i16 { self.adjusted.millisecond() }
}


/// The “type” of time that a transition is specified in.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum TimeType {

    /// Wall-clock time: a transition specified when the current time in
    /// that zone, including any daylight-saving matches, matches the
    /// transition’s time spec.
    Wall,

    /// Standard Time: a transition specified when the *standard* time in
    /// that zone, which excludes any daylight-saving offset, matches the
    /// transition’s time spec.
    Standard,

    /// UTC: a transition specified when the time in UTC matches the
    /// transition’s time spec.
    UTC,
}

pub mod runtime {
    use super::{FixedTimespan, FixedTimespanSet};

    #[derive(PartialEq, Debug)]
    pub struct OwnedTimeZone {
        pub name: Option<String>,
        pub fixed_timespans: OwnedFixedTimespanSet,
    }

    #[derive(PartialEq, Debug)]
    pub struct OwnedFixedTimespanSet {
        pub first: FixedTimespan<'static>,
        pub rest: Vec<(i64, FixedTimespan<'static>)>,
    }

    impl OwnedFixedTimespanSet {
        pub fn borrow(&self) -> FixedTimespanSet {
            FixedTimespanSet {
                first: self.first.clone(),
                rest: &*self.rest,
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use super::Surroundings;
    use std::borrow::Cow;

    const NONE: FixedTimespanSet<'static> = FixedTimespanSet {
        first: FixedTimespan {
            offset: 0,
            is_dst: false,
            name: Cow::Borrowed("ZONE_A"),
        },
        rest: &[],
    };

    #[test]
    fn empty() {
        assert_eq!(NONE.find_with_surroundings(1184000000), Surroundings {
            previous: None,
            current: &FixedTimespan {
                offset: 0,
                is_dst: false,
                name: Cow::Borrowed("ZONE_A"),
            },
            next: None,
        })
    }

    const ONE: FixedTimespanSet<'static> = FixedTimespanSet {
        first: FixedTimespan {
            offset: 0,
            is_dst: false,
            name: Cow::Borrowed("ZONE_A"),
        },
        rest: &[
            (1174784400, FixedTimespan {
                offset: 3600,
                is_dst: false,
                name: Cow::Borrowed("ZONE_B"),
            }),
        ],
    };

    #[test]
    fn just_one_first() {
        assert_eq!(ONE.find_with_surroundings(1184000000), Surroundings {
            previous: Some((
                &FixedTimespan {
                    offset: 0,
                    is_dst: false,
                    name: Cow::Borrowed("ZONE_A"),
                },
                1174784400,
            )),
            current: &FixedTimespan {
                offset: 3600,
                is_dst: false,
                name: Cow::Borrowed("ZONE_B"),
            },
            next: None,
        });
    }

    #[test]
    fn just_one_other() {
        assert_eq!(ONE.find_with_surroundings(1174000000), Surroundings {
            previous: None,
            current: &FixedTimespan {
                offset: 0,
                is_dst: false,
                name: Cow::Borrowed("ZONE_A"),
            },
            next: Some(&(
                1174784400,
                FixedTimespan {
                    offset: 3600,
                    is_dst: false,
                    name: Cow::Borrowed("ZONE_B"),
                },
            )),
        })
    }

    const MANY: FixedTimespanSet<'static> = FixedTimespanSet {
        first: FixedTimespan {
            offset: 0,
            is_dst: false,
            name: Cow::Borrowed("ZONE_A"),
        },
        rest: &[
            (1174784400, FixedTimespan {
                offset: 3600,
                is_dst: false,
                name: Cow::Borrowed("ZONE_B"),
            }),
            (1193533200, FixedTimespan {
                offset: 0,
                is_dst: false,
                name: Cow::Borrowed("ZONE_C"),
            }),
        ],
    };

    #[test]
    fn multiple_second() {
        assert_eq!(MANY.find_with_surroundings(1184000000), Surroundings {
            previous: Some((
                &FixedTimespan {
                    offset: 0,
                    is_dst: false,
                    name: Cow::Borrowed("ZONE_A"),
                },
                1174784400,
            )),
            current: &FixedTimespan {
                offset: 3600,
                is_dst: false,
                name: Cow::Borrowed("ZONE_B"),
            },
            next: Some(&(
                1193533200,
                FixedTimespan {
                    offset: 0,
                    is_dst: false,
                    name: Cow::Borrowed("ZONE_C"),
                }
            )),
        });
    }

    #[test]
    fn multiple_last() {
        assert_eq!(MANY.find_with_surroundings(1200000000), Surroundings {
            previous: Some((
                &FixedTimespan {
                    offset: 3600,
                    is_dst: false,
                    name: Cow::Borrowed("ZONE_B"),
                },
                1193533200,
            )),
            current: &FixedTimespan {
                offset: 0,
                is_dst: false,
                name: Cow::Borrowed("ZONE_C"),
            },
            next: None,
        });
    }
}