greg-tz 0.2.9

greg timezone data
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use greg::{
	ymd_hms,
	utc,
	Span,
	Frame,
	Calendar,
	real::Scale,
	calendar::Weekday
};

use crate::*;

const TEST_ZONES: [Zone; 8] = [
	Zone::Europe__Berlin,
	Zone::America__New_York,
	Zone::Asia__Hong_Kong,
	Zone::Australia__Sydney,
	Zone::Antarctica__McMurdo,
	Zone::Asia__Omsk,
	Zone::Iceland,
	Zone::MST
];

const ALL_SCALES: [Scale; 7] = [
	Scale::Years,
	Scale::Months,
	Scale::Weeks,
	Scale::Days,
	Scale::Hours,
	Scale::Minutes,
	Scale::Seconds
];

const FRAMES_TO_CHECK: usize = 10;

fn floor_check(zone: Zone, case: Point, expected: Point, scale: Scale) {
	let cal = Calendar(zone);
	let received = cal.date_floor_lossy(scale, case);
	assert_eq!(
		expected,
		received,
		"\n{} by {:?} should round down to {} but received {}",
		cal.lookup(case),
		scale,
		cal.lookup(expected),
		cal.lookup(received)
	);
	if scale == Scale::Weeks {
		assert_eq!(cal.weekday(received), Weekday::Monday);
	}
}

fn fmt_frame<Z: Shift>(cal: &Calendar<Z>, frame: Frame) -> String {
	let start = cal.lookup(frame.start);
	let stop = cal.lookup(frame.stop);
	format!("{start}{stop}")
}


#[test]
fn cet() {
	let dt = ymd_hms!(2020-01-01);
	let p = Calendar(Zone::Europe__Berlin).resolve_lossy(dt);

	let rev = Calendar(Zone::Europe__Berlin).lookup(p);
	assert_eq!(dt, rev);

	let utc = Utc::resolve(dt);
	let off = utc - p;
	assert_eq!(off, Span::from_seconds(60 * 60));

	let name = Zone::Europe__Berlin.offset_at(p).name();
	assert_eq!(name, "CET");

	let dt = ymd_hms!(2022-03-27 02:00:00);
	let in_utc = utc!(2022-03-27 01:00:00);
	let result = Calendar(Zone::Europe__Berlin).try_resolve(dt);
	let skipped = Ambiguity::Skipped(in_utc);
	assert_eq!(result, Err(skipped));
}

#[test]
fn cest() {
	let dt = ymd_hms!(2020-07-01);
	let p = Calendar(Zone::Europe__Berlin).resolve_lossy(dt);

	let rev = Calendar(Zone::Europe__Berlin).lookup(p);
	assert_eq!(dt, rev);

	let utc = Utc::resolve(dt);
	let off = utc - p;
	assert_eq!(off, Span::from_seconds(2 * 60 * 60));

	let name = Zone::Europe__Berlin.offset_at(p).name();
	assert_eq!(name, "CEST");

	let dt = ymd_hms!(2022-10-30 02:00:00);
	let first = utc!(2022-10-30 00:00:00);
	let second = utc!(2022-10-30 01:00:00);
	let result = Calendar(Zone::Europe__Berlin).try_resolve(dt);
	let repeated = Ambiguity::Repeated([
		(first, Offset::new("CEST", Span::parse("2h").seconds as i64)),
		(second, Offset::new("CET", Span::parse("1h").seconds as i64))
	]);
	assert_eq!(result, Err(repeated));
}

#[test]
fn date_floor() {

	for zone in TEST_ZONES {
		let cal = Calendar(zone);

		let jan_1: Point = cal.resolve_lossy(ymd_hms!(2020-01-01));
		let jan_2: Point = cal.resolve_lossy(ymd_hms!(2020-01-02));
		let jan_31: Point = cal.resolve_lossy(ymd_hms!(2020-01-31));
		let dec_31: Point = cal.resolve_lossy(ymd_hms!(2020-12-31));

		for scale in [Scale::Years, Scale::Months] {
			floor_check(zone, jan_1, jan_1, scale);
			floor_check(zone, jan_2, jan_1, scale);
			floor_check(zone, jan_31, jan_1, scale);
		}
		floor_check(zone, dec_31, jan_1, Scale::Years);

		// This was a Monday, so it's already floored for all scales
		let jan_1_2018: Point = cal.resolve_lossy(ymd_hms!(2018-01-01));

		for scale in ALL_SCALES {
			// this test would fail for a timezone that skipped 2018-01-01
			floor_check(zone, jan_1_2018, jan_1_2018, scale);
		}

		/*
		 *	WEEK ROUNDING (SPECIAL)
		 */
		// Monday
		let jan_3: Point = cal.resolve_lossy(ymd_hms!(2022-01-03));

		for days in 0..6 {
			let weekday = jan_3 + Span::DAY * days;
			floor_check(zone, weekday, jan_3, Scale::Weeks);
		}
		// Another Monday
		let sep_12: Point = cal.resolve_lossy(ymd_hms!(2022-09-12));

		for days in 0..6 {
			let weekday = sep_12 + Span::DAY * days;
			floor_check(zone, weekday, sep_12, Scale::Weeks);
		}
	}

}

#[test]
fn frames() {
	for zone in TEST_ZONES {
		println!("\nZONE: {}\n", zone.name);
		let cal = Calendar(zone);
		let frames = cal.frames(Scale::Weeks, Point::now());
		for f @ Frame {start, stop} in frames.take(FRAMES_TO_CHECK) {
			assert_eq!(cal.weekday(start), Weekday::Monday);
			assert_eq!(cal.weekday(stop), Weekday::Monday);
			//assert_eq!(f.span(), Scale::Weeks.into());
			let start = cal.lookup(start);
			let stop = cal.lookup(stop);
			assert_eq!(start.1.as_seconds(), 0);
			assert_eq!(stop.1.as_seconds(), 0);
			println!("{start}{stop} ({})", f.span());
		}

		for scale in ALL_SCALES {
			println!("\nScale: {scale:12?} | Start: {}", cal.lookup(Point::now()));
			let frames = cal.frames(scale, Point::now());
			let iter = frames.take(FRAMES_TO_CHECK).enumerate();

			for (i, f @ Frame {start, stop}) in iter {
				println!("{i} | {}", fmt_frame(&cal, f));
				assert_eq!(
					cal.date_floor_lossy(scale, start),
					start,
					"\nstart isn't date_floored: got {}, date_floor {}",
					cal.lookup(start),
					cal.lookup(cal.date_floor_lossy(scale, start))
				);
				assert_eq!(
					cal.date_floor_lossy(scale, stop),
					stop,
					"\nstop isn't date_floored: got {}, date_floor {}",
					cal.lookup(stop),
					cal.lookup(cal.date_floor_lossy(scale, stop))
				);
			}
		}
	}
}

#[test]
fn non_existent() {
	struct Case {
		zone: Zone,
		first: DateTime,
		last: DateTime,
		skipped: [DateTime; 2],
		transition_point: Point
	}

	let cases = [
		// At 2022-03-22 02:00:00 local time, from +1 to +2
		Case {
			zone: Zone::Europe__Berlin,
			first: ymd_hms!(2022-03-27 02:00:00),
			last: ymd_hms!(2022-03-27 02:59:59),
			skipped: [
				ymd_hms!(2022-03-27 02:15:00),
				ymd_hms!(2022-03-27 02:30:00),
			],
			transition_point: utc!(2022-03-27 01:00:00)
		},
		// At 2022-09-25 02:00:00 local time, from +12 to +13
		Case {
			zone: Zone::Pacific__Auckland,
			first: ymd_hms!(2022-09-25 02:00:00),
			last: ymd_hms!(2022-09-25 02:59:59),
			skipped: [
				ymd_hms!(2022-09-25 02:15:00),
				ymd_hms!(2022-09-25 02:30:00),
			],
			transition_point: utc!(2022-09-24 14:00:00)
		},
		// Edge case: Iceland switched from LMT of -0:16:08 to UTC in 1912
		Case {
			zone: Zone::Iceland,
			first: ymd_hms!(1912-01-01 00:00:00),
			last: ymd_hms!(1912-01-01 00:16:07),
			skipped: [
				ymd_hms!(1912-01-01 00:00:01),
				ymd_hms!(1912-01-01 00:05:30),
			],
			transition_point: utc!(1912-01-01 00:16:08)
		},
		// Edge case: the Islands of Tokelau switched sides of the IDL
		// At 2011-12-30 00:00:00 local time, from -11 to +13
		Case {
			zone: Zone::Pacific__Fakaofo,
			first: ymd_hms!(2011-12-30 00:00:00),
			last: ymd_hms!(2011-12-30 23:59:59),
			skipped: [
				ymd_hms!(2011-12-30 10:15:00),
				ymd_hms!(2011-12-30 14:30:00),
			],
			transition_point: utc!(2011-12-30 11:00:00)
		}
	];

	for Case {zone, first, last, skipped, transition_point} in cases {
		println!("Zone: {}", zone.name);
		let cal = Calendar(zone);
		println!(
			"Transition Point:\n\t{} UTC\n\t{}\n",
			Calendar(Utc).format(transition_point),
			zone.format_with_name(transition_point)
		);
		for &dt in &[first, skipped[0], skipped[1], last] {
			println!("\tSkipped local time: {dt}");
			let res = cal.resolve_lossy(dt);
			println!("\tResolved to local:  {}", cal.format(res));
			println!("\tWhich is UTC time:  {}", Calendar(Utc).format(res));
			println!();
			assert_eq!(res, transition_point);
			let skipped_utc = Utc::resolve(dt);
			let skipped_local = cal.0.revert_lossy(skipped_utc);
			assert_eq!(skipped_local, transition_point);

			assert_eq!(
				cal.try_resolve(dt),
				Err(Ambiguity::Skipped(transition_point))
			);
		}

		assert_eq!(
			cal.0.revert_lossy(Utc::resolve(first) - Span::SECOND),
			transition_point - Span::SECOND,
			"time is linear before the transition"
		);

		assert_eq!(
			cal.0.revert_lossy(Utc::resolve(last) + Span::SECOND),
			transition_point,
			"the first point not skipped must be the transition point"
		);

		assert_eq!(
			cal.0.revert_lossy(Utc::resolve(last) + Span::SECOND * 2),
			transition_point + Span::SECOND,
			"time is linear after the transition"
		);
	}
}


#[test]
fn ambiguous() {
	struct Case {
		zone: Zone,
		ambiguous: Vec<DateTime>,
		before: Offset,
		after: Offset
	}

	let cases = [
		// At 2022-10-30 03:00:00 local time, from +2 to +1
		Case {
			zone: Zone::Europe__Berlin,
			ambiguous: vec![
				ymd_hms!(2022-10-30 02:00:00),
				ymd_hms!(2022-10-30 02:15:00),
				ymd_hms!(2022-10-30 02:30:00),
				ymd_hms!(2022-10-30 02:59:59)
			],
			before: Offset::new("CEST", Span::parse("2h").seconds as i64),
			after: Offset::new("CET", Span::parse("1h").seconds as i64)
			//transition_point: utc!(2022-10-30 01:00:00)
		},
		// At 2022-04-03 03:00:00 local time, from +13 to +12
		Case {
			zone: Zone::Pacific__Auckland,
			ambiguous: vec![
				ymd_hms!(2022-04-03 02:00:00),
				ymd_hms!(2022-04-03 02:15:00),
				ymd_hms!(2022-04-03 02:30:00),
				ymd_hms!(2022-04-03 02:59:59)
			],
			before: Offset::new("NZDT", Span::parse("13h").seconds as i64),
			after: Offset::new("NZST", Span::parse("12h").seconds as i64)
		},
		// Edge case: Kwajalein (Marshall Islands) in October 1969
		// from +11 to -12 (no precise time available)
		// If this test fails, check if the TZ DB changed
		Case {
			zone: Zone::Pacific__Kwajalein,
			ambiguous: vec![
				ymd_hms!(1969-09-30 01:00:00),
				ymd_hms!(1969-09-30 05:30:00),
				ymd_hms!(1969-09-30 15:15:00),
				ymd_hms!(1969-09-30 23:59:59)
			],
			before: Offset::new("+11", Span::parse("11h").seconds as i64),
			after: Offset::new("-12", -(Span::parse("12h").seconds as i64))
		},
	];
	for Case {zone, ambiguous, before, after} in cases {
		let amount = (before.seconds - after.seconds) as u64;
		let amount = Span::from_seconds(amount);
		println!("Zone: {}", zone.name);
		println!("Ambiguous period: {amount}");
		let cal = Calendar(zone);
		for &dt in &ambiguous {
			println!("\tAmbiguous local time: {dt}");
			let res_early = cal.resolve_lossy(dt);
			let again_at = res_early + amount;
			let repeated = cal.lookup(again_at);
			assert_eq!(dt, repeated);
			assert_ne!(
				zone.format_with_name(res_early),
				zone.format_with_name(again_at),
				"date & time are supposed to match but not the name"
			);
			println!("\tOccurred first: {} UTC", Utc::lookup(res_early));
			println!("\tThen again at:  {} UTC", Utc::lookup(again_at));
			println!();

			assert_eq!(
				cal.try_resolve(dt),
				Err(Ambiguity::Repeated([
					(res_early, before),
					(again_at, after)
				]))
			)
		}
	}
}

#[test]
fn offset_short_names() {
	let cases = [
		(
			Zone::Pacific__Chatham,
			Point::from_epoch(1712412000),
			"+1245"
		),
		(
			Zone::Pacific__Kiritimati,
			Point::from_epoch(-2177415040),
			"-1040"
		),
		(
			Zone::Etc__GMTMinus3,
			Point::from_epoch(0),
			"+03"
		),
		(
			Zone::Europe__Berlin,
			Point::from_epoch(1737481416),
			"CET"
		)
	];

	for (zone, point, name) in cases {
		assert_eq!(zone.offset_at(point).name, name);
	}

	//credit: chrono-tz as fn test_numeric_names
	let cases = [
		(
			Zone::America__Scoresbysund,
			ymd_hms!(2024-05-01),
			"-01"
		),
		(
			Zone::Antarctica__Casey,
			ymd_hms!(2022-11-01),
			"+11"
		),
		(
			Zone::Africa__Addis_Ababa,
			ymd_hms!(1937-02-01),
			"+0245"
		),
	];
	for (zone, date, name) in cases {
		let point = Calendar(zone).resolve_lossy(date);
		assert_eq!(zone.offset_at(point).name, name);
	}
}

#[test]
fn lmt() {
	// Iceland switched from LMT offset -0:16:08 to UTC in 1912
	let before = ymd_hms!(1911-12-31 23:59:59);

	let before_local = Calendar(Zone::Iceland).resolve_lossy(before);
	let before_utc = Calendar(Utc).resolve(before);

	assert_eq!(before_utc - before_local, Span::parse("16m8s"));
}

#[test]
#[should_panic]
fn too_early() {
	let too_early = ymd_hms!(1799-12-31 23:45:56);
	let _invalid = Calendar(Zone::Europe__Berlin).resolve_lossy(too_early);
}


#[test]
#[should_panic]
fn too_late() {
	let too_late = ymd_hms!(2121-01-01 01:23:45);
	let _invalid = Calendar(Zone::Europe__Berlin).resolve_lossy(too_late);
}