|
1 | 1 | using TimeZones.TZData: parse_components |
| 2 | +using TimeZones: Transition |
2 | 3 |
|
| 4 | +dt = DateTime(1942,12,25,1,23,45) |
| 5 | +custom_dt = DateTime(1800,1,1) |
| 6 | + |
| 7 | +utc = FixedTimeZone("UTC") |
| 8 | +gmt = FixedTimeZone("GMT", 0) |
| 9 | +foo = FixedTimeZone("FOO", 0) |
3 | 10 | null = FixedTimeZone("", 10800) |
4 | 11 | fixed = FixedTimeZone("UTC+01:00") |
5 | 12 | est = FixedTimeZone("EST", -18000) |
6 | 13 | warsaw = first(compile("Europe/Warsaw", tzdata["europe"])) |
7 | | -apia = first(compile("Pacific/Apia", tzdata["australasia"])) |
8 | | -honolulu = first(compile("Pacific/Honolulu", tzdata["northamerica"])) # Uses cutoff |
| 14 | +apia = cache_tz(compile("Pacific/Apia", tzdata["australasia"])) |
| 15 | +honolulu = cache_tz(compile("Pacific/Honolulu", tzdata["northamerica"])) # Uses cutoff |
9 | 16 | ulyanovsk = first(compile("Europe/Ulyanovsk", tzdata["europe"])) # No named abbreviations |
10 | 17 | new_york = first(compile("America/New_York", tzdata["northamerica"])) # Underscore in name |
11 | | -dt = DateTime(1942,12,25,1,23,45) |
12 | | - |
13 | | -# TimeZones as a string |
14 | | -@test string(null) == "UTC+03:00" |
15 | | -@test string(fixed) == "UTC+01:00" |
16 | | -@test string(est) == "EST" |
17 | | -@test string(warsaw) == "Europe/Warsaw" |
18 | | -@test string(apia) == "Pacific/Apia" |
19 | | -@test string(honolulu) == "Pacific/Honolulu" |
20 | | -@test string(ulyanovsk) == "Europe/Ulyanovsk" |
21 | | - |
22 | | -# Alternatively in Julia 0.7.0-DEV.4517 we could use |
23 | | -# `sprint(show, ..., context=:compact => true)` |
24 | | -show_compact = (io, args...) -> show(IOContext(io, :compact => true), args...) |
25 | | -@test sprint(show_compact, null) == "UTC+03:00" |
26 | | -@test sprint(show_compact, fixed) == "UTC+01:00" |
27 | | -@test sprint(show_compact, est) == "EST" |
28 | | -@test sprint(show_compact, warsaw) == "Europe/Warsaw" |
29 | | -@test sprint(show_compact, apia) == "Pacific/Apia" |
30 | | -@test sprint(show_compact, honolulu) == "Pacific/Honolulu" |
31 | | -@test sprint(show_compact, ulyanovsk) == "Europe/Ulyanovsk" |
32 | | - |
33 | | -@test sprint(show, null) == "UTC+03:00" |
34 | | -@test sprint(show, fixed) == "UTC+01:00" |
35 | | -@test sprint(show, est) == "EST (UTC-5)" |
36 | | -@test sprint(show, warsaw) == "Europe/Warsaw (UTC+1/UTC+2)" |
37 | | -@test sprint(show, apia) == "Pacific/Apia (UTC+13/UTC+14)" |
38 | | -@test sprint(show, honolulu) == "Pacific/Honolulu (UTC-10)" |
39 | | -@test sprint(show, ulyanovsk) == "Europe/Ulyanovsk (UTC+4)" |
40 | | - |
41 | | -# UTC and GMT are special cases |
42 | | -@test sprint(show, FixedTimeZone("UTC")) == "UTC" |
43 | | -@test sprint(show, FixedTimeZone("GMT", 0)) == "GMT" |
44 | | -@test sprint(show, FixedTimeZone("FOO", 0)) == "FOO (UTC+0)" |
| 18 | +custom = VariableTimeZone("Test/Custom", [Transition(custom_dt, utc)]) # Non-cached variable time zone |
| 19 | + |
| 20 | +@test sprint(print, utc) == "UTC" |
| 21 | +@test sprint(print, gmt) == "GMT" |
| 22 | +@test sprint(print, foo) == "FOO" |
| 23 | +@test sprint(print, null) == "UTC+03:00" |
| 24 | +@test sprint(print, fixed) == "UTC+01:00" |
| 25 | +@test sprint(print, est) == "EST" |
| 26 | +@test sprint(print, warsaw) == "Europe/Warsaw" |
| 27 | +@test sprint(print, apia) == "Pacific/Apia" |
| 28 | +@test sprint(print, honolulu) == "Pacific/Honolulu" |
| 29 | +@test sprint(print, ulyanovsk) == "Europe/Ulyanovsk" |
| 30 | +@test sprint(print, custom) == "Test/Custom" |
| 31 | + |
| 32 | +@test sprint(show_compact, utc) == "tz\"UTC\"" |
| 33 | +@test sprint(show_compact, gmt) == "tz\"GMT\"" |
| 34 | +@test sprint(show_compact, foo) == "FixedTimeZone(\"FOO\", 0)" |
| 35 | +@test sprint(show_compact, null) == "FixedTimeZone(\"\", 10800)" |
| 36 | +@test sprint(show_compact, fixed) == "tz\"UTC+01:00\"" |
| 37 | +@test sprint(show_compact, est) == "tz\"EST\"" |
| 38 | +@test sprint(show_compact, warsaw) == "tz\"Europe/Warsaw\"" |
| 39 | +@test sprint(show_compact, apia) == "tz\"Pacific/Apia\"" |
| 40 | +@test sprint(show_compact, honolulu) == "tz\"Pacific/Honolulu\"" |
| 41 | +@test sprint(show_compact, ulyanovsk) == "tz\"Europe/Ulyanovsk\"" |
| 42 | +@test sprint(show_compact, custom) == "VariableTimeZone(\"Test/Custom\", ...)" |
| 43 | + |
| 44 | +@test sprint(show, utc) == "tz\"UTC\"" |
| 45 | +@test sprint(show, gmt) == "tz\"GMT\"" |
| 46 | +@test sprint(show, foo) == "FixedTimeZone(\"FOO\", 0)" |
| 47 | +@test sprint(show, null) == "FixedTimeZone(\"\", 10800)" |
| 48 | +@test sprint(show, fixed) == "tz\"UTC+01:00\"" |
| 49 | +@test sprint(show, est) == "tz\"EST\"" |
| 50 | +@test sprint(show, warsaw) == "tz\"Europe/Warsaw\"" |
| 51 | +@test sprint(show, apia) == "tz\"Pacific/Apia\"" |
| 52 | +@test sprint(show, honolulu) == "tz\"Pacific/Honolulu\"" |
| 53 | +@test sprint(show, ulyanovsk) == "tz\"Europe/Ulyanovsk\"" |
| 54 | +@test sprint(show, custom) == "VariableTimeZone(\"Test/Custom\", Transition[Transition($(repr(custom_dt)), tz\"UTC\")], nothing)" |
| 55 | + |
| 56 | +@test sprint(show, MIME("text/plain"), utc) == "UTC" |
| 57 | +@test sprint(show, MIME("text/plain"), gmt) == "GMT" |
| 58 | +@test sprint(show, MIME("text/plain"), foo) == "FOO (UTC+0)" |
| 59 | +@test sprint(show, MIME("text/plain"), null) == "UTC+03:00" |
| 60 | +@test sprint(show, MIME("text/plain"), fixed) == "UTC+01:00" |
| 61 | +@test sprint(show, MIME("text/plain"), est) == "EST (UTC-5)" |
| 62 | +@test sprint(show, MIME("text/plain"), warsaw) == "Europe/Warsaw (UTC+1/UTC+2)" |
| 63 | +@test sprint(show, MIME("text/plain"), apia) == "Pacific/Apia (UTC+13/UTC+14)" |
| 64 | +@test sprint(show, MIME("text/plain"), honolulu) == "Pacific/Honolulu (UTC-10)" |
| 65 | +@test sprint(show, MIME("text/plain"), ulyanovsk) == "Europe/Ulyanovsk (UTC+4)" |
| 66 | +@test sprint(show, MIME("text/plain"), custom) == "Test/Custom (UTC+0)" |
45 | 67 |
|
46 | 68 | # ZonedDateTime as a string |
47 | 69 | zdt = ZonedDateTime(dt, warsaw) |
48 | 70 | @test string(zdt) == "1942-12-25T01:23:45+01:00" |
49 | | -@test sprint(show, zdt) == "1942-12-25T01:23:45+01:00" |
| 71 | +@test sprint(show_compact, zdt) == "1942-12-25T01:23:45+01:00" |
| 72 | +@test sprint(show, zdt) == "ZonedDateTime(1942, 12, 25, 1, 23, 45, tz\"Europe/Warsaw\")" |
| 73 | +@test sprint(show, MIME("text/plain"), zdt) == "1942-12-25T01:23:45+01:00" |
50 | 74 |
|
51 | 75 |
|
52 | 76 | # TimeZone parsing |
@@ -105,3 +129,38 @@ f = "yyyy/m/d H:M:S zzz" |
105 | 129 | df = Dates.DateFormat("yyyy-mm-ddTHH:MM:SS ZZZ") |
106 | 130 | zdt = ZonedDateTime(dt, warsaw) |
107 | 131 | @test_throws ArgumentError parse(ZonedDateTime, Dates.format(zdt, df), df) |
| 132 | + |
| 133 | + |
| 134 | +# Displaying VariableTimeZone's vector of transitions on the REPL has a special printing |
| 135 | +@testset "Transitions I/O" begin |
| 136 | + transitions = honolulu.transitions # Only contains a few transitions |
| 137 | + t = transitions[2] # 1896-01-13T22:31:26 UTC-10:30/+0 (HST) |
| 138 | + |
| 139 | + @testset "basic" begin |
| 140 | + @test sprint(print, t) == "1896-01-13T22:31:26 UTC-10:30/+0 (HST)" |
| 141 | + @test sprint(show_compact, t) == "1896-01-13T22:31:26 UTC-10:30/+0 (HST)" |
| 142 | + @test sprint(show, t) == "Transition($(repr(t.utc_datetime)), FixedTimeZone(\"HST\", -37800))" |
| 143 | + @test sprint(show, MIME("text/plain"), t) == "1896-01-13T22:31:26 UTC-10:30/+0 (HST)" |
| 144 | + end |
| 145 | + |
| 146 | + @testset "REPL vector" begin |
| 147 | + expected_full = string( |
| 148 | + TimeZones.Transition, |
| 149 | + "[", |
| 150 | + join(map(t -> sprint(show, t, context=:compact => false), transitions), ", "), |
| 151 | + "]", |
| 152 | + ) |
| 153 | + |
| 154 | + # Note: The output here is different from the interactive REPL but is representative |
| 155 | + # of the output. |
| 156 | + expected_repl = string( |
| 157 | + TimeZones.Transition, |
| 158 | + "[", |
| 159 | + join(map(t -> sprint(show, t, context=:compact => true), transitions), ", "), |
| 160 | + "]", |
| 161 | + ) |
| 162 | + |
| 163 | + @test sprint(show, transitions, context=:compact => false) == expected_full |
| 164 | + @test sprint(show, transitions; context=:limit => true) == expected_repl |
| 165 | + end |
| 166 | +end |
0 commit comments