Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3869)

Delta Between Two Patch Sets: src/pkg/time/time_test.go

Issue 5392041: code review 5392041: time: new Time, Duration, ZoneInfo types (Closed)
Left Patch Set: Created 13 years, 4 months ago
Right Patch Set: diff -r 3c286b9b2206 https://go.googlecode.com/hg/ Created 13 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/time/time.go ('k') | src/pkg/time/zoneinfo.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package time_test 5 package time_test
6 6
7 import ( 7 import (
8 "strconv" 8 "strconv"
9 "strings" 9 "strings"
10 "testing" 10 "testing"
11 "testing/quick" 11 "testing/quick"
12 . "time" 12 . "time"
13 ) 13 )
14 14
15 // We should be in PST/PDT, but if the time zone files are missing we 15 // We should be in PST/PDT, but if the time zone files are missing we
16 // won't be. The purpose of this test is to at least explain why some of 16 // won't be. The purpose of this test is to at least explain why some of
17 // the subsequent tests fail. 17 // the subsequent tests fail.
18 func TestZoneData(t *testing.T) { 18 func TestZoneData(t *testing.T) {
19 » lt := LocalTime() 19 » lt := Now()
20 // PST is 8 hours west, PDT is 7 hours west. We could use the name but it's not unique. 20 // PST is 8 hours west, PDT is 7 hours west. We could use the name but it's not unique.
21 » if off := lt.ZoneOffset; off != -8*60*60 && off != -7*60*60 { 21 » if name, off := lt.Zone(); off != -8*60*60 && off != -7*60*60 {
22 » » t.Errorf("Unable to find US Pacific time zone data for testing; time zone is %q offset %d", lt.Zone, off) 22 » » t.Errorf("Unable to find US Pacific time zone data for testing; time zone is %q offset %d", name, off)
23 t.Error("Likely problem: the time zone files have not been insta lled.") 23 t.Error("Likely problem: the time zone files have not been insta lled.")
24 } 24 }
25 }
26
27 // parsedTime is the struct representing a parsed time value.
28 type parsedTime struct {
29 Year int
30 Month Month
31 Day int
32 Hour, Minute, Second int // 15:04:05 is 15, 4, 5.
33 Nanosecond int // Fractional second.
34 Weekday Weekday
35 ZoneOffset int // seconds east of UTC, e.g. -7*60*60 for -0 700
36 Zone string // e.g., "MST"
25 } 37 }
26 38
27 type TimeTest struct { 39 type TimeTest struct {
28 seconds int64 40 seconds int64
29 » golden Time 41 » golden parsedTime
30 } 42 }
31 43
32 var utctests = []TimeTest{ 44 var utctests = []TimeTest{
33 » {0, Time{1970, 1, 1, 0, 0, 0, 0, 0, "UTC"}}, 45 » {0, parsedTime{1970, January, 1, 0, 0, 0, 0, Thursday, 0, "UTC"}},
34 » {1221681866, Time{2008, 9, 17, 20, 4, 26, 0, 0, "UTC"}}, 46 » {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 0, Wednesday, 0, "UTC"}},
35 » {-1221681866, Time{1931, 4, 16, 3, 55, 34, 0, 0, "UTC"}}, 47 » {-1221681866, parsedTime{1931, April, 16, 3, 55, 34, 0, Thursday, 0, "UT C"}},
36 » {-11644473600, Time{1601, 1, 1, 0, 0, 0, 0, 0, "UTC"}}, 48 » {-11644473600, parsedTime{1601, January, 1, 0, 0, 0, 0, Monday, 0, "UTC" }},
37 » {599529660, Time{1988, 12, 31, 0, 1, 0, 0, 0, "UTC"}}, 49 » {599529660, parsedTime{1988, December, 31, 0, 1, 0, 0, Saturday, 0, "UTC "}},
38 » {978220860, Time{2000, 12, 31, 0, 1, 0, 0, 0, "UTC"}}, 50 » {978220860, parsedTime{2000, December, 31, 0, 1, 0, 0, Sunday, 0, "UTC"} },
39 » {1e18, Time{31688740476, 10, 23, 1, 46, 40, 0, 0, "UTC"}},
40 » {-1e18, Time{-31688736537, 3, 10, 22, 13, 20, 0, 0, "UTC"}},
41 » {0x7fffffffffffffff, Time{292277026596, 12, 4, 15, 30, 7, 0, 0, "UTC"}},
42 » {-0x8000000000000000, Time{-292277022657, 1, 27, 8, 29, 52, 0, 0, "UTC"} },
43 } 51 }
44 52
45 var nanoutctests = []TimeTest{ 53 var nanoutctests = []TimeTest{
46 » {0, Time{1970, 1, 1, 0, 0, 0, 1e8, 0, "UTC"}}, 54 » {0, parsedTime{1970, January, 1, 0, 0, 0, 1e8, Thursday, 0, "UTC"}},
47 » {1221681866, Time{2008, 9, 17, 20, 4, 26, 2e8, 0, "UTC"}}, 55 » {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 2e8, Wednesday, 0, "UTC"}},
48 } 56 }
49 57
50 var localtests = []TimeTest{ 58 var localtests = []TimeTest{
51 » {0, Time{1969, 12, 31, 16, 0, 0, 0, -8 * 60 * 60, "PST"}}, 59 » {0, parsedTime{1969, December, 31, 16, 0, 0, 0, Wednesday, -8 * 60 * 60, "PST"}},
52 » {1221681866, Time{2008, 9, 17, 13, 4, 26, 0, -7 * 60 * 60, "PDT"}}, 60 » {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 0, Wednesday, -7 * 60 * 60, "PDT"}},
53 } 61 }
54 62
55 var nanolocaltests = []TimeTest{ 63 var nanolocaltests = []TimeTest{
56 » {0, Time{1969, 12, 31, 16, 0, 0, 1e8, -8 * 60 * 60, "PST"}}, 64 » {0, parsedTime{1969, December, 31, 16, 0, 0, 1e8, Wednesday, -8 * 60 * 6 0, "PST"}},
57 » {1221681866, Time{2008, 9, 17, 13, 4, 26, 3e8, -7 * 60 * 60, "PDT"}}, 65 » {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 3e8, Wednesday, -7 * 60 * 60, "PDT"}},
58 } 66 }
59 67
60 func same(t, u *Time) bool { 68 func same(t Time, u *parsedTime) bool {
61 » return t.Year == u.Year && 69 » // Check aggregates.
62 » » t.Month == u.Month && 70 » year, month, day := t.Date()
63 » » t.Day == u.Day && 71 » hour, min, sec := t.Clock()
64 » » t.Hour == u.Hour && 72 » name, offset := t.Zone()
65 » » t.Minute == u.Minute && 73 » if year != u.Year || month != u.Month || day != u.Day ||
66 » » t.Second == u.Second && 74 » » hour != u.Hour || min != u.Minute || sec != u.Second ||
67 » » t.Nanosecond == u.Nanosecond && 75 » » name != u.Zone || offset != u.ZoneOffset {
68 » » t.Weekday() == u.Weekday() && 76 » » return false
69 » » t.ZoneOffset == u.ZoneOffset && 77 » }
70 » » t.Zone == u.Zone 78 » // Check individual entries.
79 » return t.Year() == u.Year &&
80 » » t.Month() == u.Month &&
81 » » t.Day() == u.Day &&
82 » » t.Hour() == u.Hour &&
83 » » t.Minute() == u.Minute &&
84 » » t.Second() == u.Second &&
85 » » t.Nanosecond() == u.Nanosecond &&
86 » » t.Weekday() == u.Weekday
71 } 87 }
72 88
73 func TestSecondsToUTC(t *testing.T) { 89 func TestSecondsToUTC(t *testing.T) {
74 for _, test := range utctests { 90 for _, test := range utctests {
75 sec := test.seconds 91 sec := test.seconds
76 golden := &test.golden 92 golden := &test.golden
77 » » tm := SecondsToUTC(sec) 93 » » tm := Unix(sec, 0).UTC()
78 » » newsec := tm.Seconds() 94 » » newsec := tm.Unix()
79 if newsec != sec { 95 if newsec != sec {
80 t.Errorf("SecondsToUTC(%d).Seconds() = %d", sec, newsec) 96 t.Errorf("SecondsToUTC(%d).Seconds() = %d", sec, newsec)
81 } 97 }
82 if !same(tm, golden) { 98 if !same(tm, golden) {
83 » » » t.Errorf("SecondsToUTC(%d):", sec) 99 » » » t.Errorf("SecondsToUTC(%d): // %#v", sec, tm)
84 t.Errorf(" want=%+v", *golden) 100 t.Errorf(" want=%+v", *golden)
85 » » » t.Errorf(" have=%+v", *tm) 101 » » » t.Errorf(" have=%v", tm.Format(RFC3339+" MST"))
86 } 102 }
87 } 103 }
88 } 104 }
89 105
90 func TestNanosecondsToUTC(t *testing.T) { 106 func TestNanosecondsToUTC(t *testing.T) {
91 for _, test := range nanoutctests { 107 for _, test := range nanoutctests {
92 golden := &test.golden 108 golden := &test.golden
93 nsec := test.seconds*1e9 + int64(golden.Nanosecond) 109 nsec := test.seconds*1e9 + int64(golden.Nanosecond)
94 » » tm := NanosecondsToUTC(nsec) 110 » » tm := Unix(0, nsec).UTC()
95 » » newnsec := tm.Nanoseconds() 111 » » newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
96 if newnsec != nsec { 112 if newnsec != nsec {
97 t.Errorf("NanosecondsToUTC(%d).Nanoseconds() = %d", nsec , newnsec) 113 t.Errorf("NanosecondsToUTC(%d).Nanoseconds() = %d", nsec , newnsec)
98 } 114 }
99 if !same(tm, golden) { 115 if !same(tm, golden) {
100 t.Errorf("NanosecondsToUTC(%d):", nsec) 116 t.Errorf("NanosecondsToUTC(%d):", nsec)
101 t.Errorf(" want=%+v", *golden) 117 t.Errorf(" want=%+v", *golden)
102 » » » t.Errorf(" have=%+v", *tm) 118 » » » t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
103 } 119 }
104 } 120 }
105 } 121 }
106 122
107 func TestSecondsToLocalTime(t *testing.T) { 123 func TestSecondsToLocalTime(t *testing.T) {
108 for _, test := range localtests { 124 for _, test := range localtests {
109 sec := test.seconds 125 sec := test.seconds
110 golden := &test.golden 126 golden := &test.golden
111 » » tm := SecondsToLocalTime(sec) 127 » » tm := Unix(sec, 0)
112 » » newsec := tm.Seconds() 128 » » newsec := tm.Unix()
113 if newsec != sec { 129 if newsec != sec {
114 t.Errorf("SecondsToLocalTime(%d).Seconds() = %d", sec, n ewsec) 130 t.Errorf("SecondsToLocalTime(%d).Seconds() = %d", sec, n ewsec)
115 } 131 }
116 if !same(tm, golden) { 132 if !same(tm, golden) {
117 t.Errorf("SecondsToLocalTime(%d):", sec) 133 t.Errorf("SecondsToLocalTime(%d):", sec)
118 t.Errorf(" want=%+v", *golden) 134 t.Errorf(" want=%+v", *golden)
119 » » » t.Errorf(" have=%+v", *tm) 135 » » » t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
120 » » } 136 » » }
121 » } 137 » }
122 } 138 }
123 139
124 func TestNanoecondsToLocalTime(t *testing.T) { 140 func TestNanosecondsToLocalTime(t *testing.T) {
125 for _, test := range nanolocaltests { 141 for _, test := range nanolocaltests {
126 golden := &test.golden 142 golden := &test.golden
127 nsec := test.seconds*1e9 + int64(golden.Nanosecond) 143 nsec := test.seconds*1e9 + int64(golden.Nanosecond)
128 » » tm := NanosecondsToLocalTime(nsec) 144 » » tm := Unix(0, nsec)
129 » » newnsec := tm.Nanoseconds() 145 » » newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
130 if newnsec != nsec { 146 if newnsec != nsec {
131 t.Errorf("NanosecondsToLocalTime(%d).Seconds() = %d", ns ec, newnsec) 147 t.Errorf("NanosecondsToLocalTime(%d).Seconds() = %d", ns ec, newnsec)
132 } 148 }
133 if !same(tm, golden) { 149 if !same(tm, golden) {
134 t.Errorf("NanosecondsToLocalTime(%d):", nsec) 150 t.Errorf("NanosecondsToLocalTime(%d):", nsec)
135 t.Errorf(" want=%+v", *golden) 151 t.Errorf(" want=%+v", *golden)
136 » » » t.Errorf(" have=%+v", *tm) 152 » » » t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
137 } 153 }
138 } 154 }
139 } 155 }
140 156
141 func TestSecondsToUTCAndBack(t *testing.T) { 157 func TestSecondsToUTCAndBack(t *testing.T) {
142 » f := func(sec int64) bool { return SecondsToUTC(sec).Seconds() == sec } 158 » f := func(sec int64) bool { return Unix(sec, 0).UTC().Unix() == sec }
143 f32 := func(sec int32) bool { return f(int64(sec)) } 159 f32 := func(sec int32) bool { return f(int64(sec)) }
144 cfg := &quick.Config{MaxCount: 10000} 160 cfg := &quick.Config{MaxCount: 10000}
145 161
146 // Try a reasonable date first, then the huge ones. 162 // Try a reasonable date first, then the huge ones.
147 if err := quick.Check(f32, cfg); err != nil { 163 if err := quick.Check(f32, cfg); err != nil {
148 t.Fatal(err) 164 t.Fatal(err)
149 } 165 }
150 if err := quick.Check(f, cfg); err != nil { 166 if err := quick.Check(f, cfg); err != nil {
151 t.Fatal(err) 167 t.Fatal(err)
152 } 168 }
153 } 169 }
154 170
155 func TestNanosecondsToUTCAndBack(t *testing.T) { 171 func TestNanosecondsToUTCAndBack(t *testing.T) {
156 » f := func(nsec int64) bool { return NanosecondsToUTC(nsec).Nanoseconds() == nsec } 172 » f := func(nsec int64) bool {
173 » » t := Unix(0, nsec).UTC()
174 » » ns := t.Unix()*1e9 + int64(t.Nanosecond())
175 » » return ns == nsec
176 » }
157 f32 := func(nsec int32) bool { return f(int64(nsec)) } 177 f32 := func(nsec int32) bool { return f(int64(nsec)) }
158 cfg := &quick.Config{MaxCount: 10000} 178 cfg := &quick.Config{MaxCount: 10000}
159 179
160 // Try a small date first, then the large ones. (The span is only a few hundred years 180 // Try a small date first, then the large ones. (The span is only a few hundred years
161 // for nanoseconds in an int64.) 181 // for nanoseconds in an int64.)
162 if err := quick.Check(f32, cfg); err != nil { 182 if err := quick.Check(f32, cfg); err != nil {
163 t.Fatal(err) 183 t.Fatal(err)
164 } 184 }
165 if err := quick.Check(f, cfg); err != nil { 185 if err := quick.Check(f, cfg); err != nil {
166 t.Fatal(err) 186 t.Fatal(err)
167 } 187 }
168 } 188 }
169 189
170 type TimeFormatTest struct { 190 type TimeFormatTest struct {
171 time Time 191 time Time
172 formattedValue string 192 formattedValue string
173 } 193 }
174 194
175 var rfc3339Formats = []TimeFormatTest{ 195 var rfc3339Formats = []TimeFormatTest{
176 » {Time{2008, 9, 17, 20, 4, 26, 0, 0, "UTC"}, "2008-09-17T20:04:26Z"}, 196 » {Date(2008, 9, 17, 20, 4, 26, 0, UTC), "2008-09-17T20:04:26Z"},
177 » {Time{1994, 9, 17, 20, 4, 26, 0, -18000, "EST"}, "1994-09-17T20:04:26-05 :00"}, 197 » {Date(1994, 9, 17, 20, 4, 26, 0, FixedZone("EST", -18000)), "1994-09-17T 20:04:26-05:00"},
178 » {Time{2000, 12, 26, 1, 15, 6, 0, 15600, "OTO"}, "2000-12-26T01:15:06+04: 20"}, 198 » {Date(2000, 12, 26, 1, 15, 6, 0, FixedZone("OTO", 15600)), "2000-12-26T0 1:15:06+04:20"},
179 } 199 }
180 200
181 func TestRFC3339Conversion(t *testing.T) { 201 func TestRFC3339Conversion(t *testing.T) {
182 for _, f := range rfc3339Formats { 202 for _, f := range rfc3339Formats {
183 if f.time.Format(RFC3339) != f.formattedValue { 203 if f.time.Format(RFC3339) != f.formattedValue {
184 t.Error("RFC3339:") 204 t.Error("RFC3339:")
185 t.Errorf(" want=%+v", f.formattedValue) 205 t.Errorf(" want=%+v", f.formattedValue)
186 t.Errorf(" have=%+v", f.time.Format(RFC3339)) 206 t.Errorf(" have=%+v", f.time.Format(RFC3339))
187 } 207 }
188 } 208 }
(...skipping 20 matching lines...) Expand all
209 {"two-digit year", "06 01 02", "09 02 04"}, 229 {"two-digit year", "06 01 02", "09 02 04"},
210 // Time stamps, Fractional seconds. 230 // Time stamps, Fractional seconds.
211 {"Stamp", Stamp, "Feb 4 21:00:57"}, 231 {"Stamp", Stamp, "Feb 4 21:00:57"},
212 {"StampMilli", StampMilli, "Feb 4 21:00:57.012"}, 232 {"StampMilli", StampMilli, "Feb 4 21:00:57.012"},
213 {"StampMicro", StampMicro, "Feb 4 21:00:57.012345"}, 233 {"StampMicro", StampMicro, "Feb 4 21:00:57.012345"},
214 {"StampNano", StampNano, "Feb 4 21:00:57.012345678"}, 234 {"StampNano", StampNano, "Feb 4 21:00:57.012345678"},
215 } 235 }
216 236
217 func TestFormat(t *testing.T) { 237 func TestFormat(t *testing.T) {
218 // The numeric time represents Thu Feb 4 21:00:57.012345678 PST 2010 238 // The numeric time represents Thu Feb 4 21:00:57.012345678 PST 2010
219 » time := NanosecondsToLocalTime(1233810057012345678) 239 » time := Unix(0, 1233810057012345678)
220 for _, test := range formatTests { 240 for _, test := range formatTests {
221 result := time.Format(test.format) 241 result := time.Format(test.format)
222 if result != test.result { 242 if result != test.result {
223 t.Errorf("%s expected %q got %q", test.name, test.result , result) 243 t.Errorf("%s expected %q got %q", test.name, test.result , result)
224 } 244 }
225 } 245 }
226 } 246 }
227 247
228 type ParseTest struct { 248 type ParseTest struct {
229 name string 249 name string
230 format string 250 format string
231 value string 251 value string
232 » hasTZ bool // contains a time zone 252 » hasTZ bool // contains a time zone
233 » hasWD bool // contains a weekday 253 » hasWD bool // contains a weekday
234 » yearSign int64 // sign of year 254 » yearSign int // sign of year
235 » fracDigits int // number of digits of fractional second 255 » fracDigits int // number of digits of fractional second
236 } 256 }
237 257
238 var parseTests = []ParseTest{ 258 var parseTests = []ParseTest{
239 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0}, 259 {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
240 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010", true, true, 1, 0} , 260 {"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010", true, true, 1, 0} ,
241 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0}, 261 {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010", true, true, 1, 0},
242 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0} , 262 {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST", true, true, 1, 0} ,
243 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0}, 263 {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST", true, true, 1, 0},
244 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1, 0}, 264 {"RFC1123Z", RFC1123Z, "Thu, 04 Feb 2010 21:00:57 -0800", true, true, 1, 0},
245 {"RFC3339", RFC3339, "2010-02-04T21:00:57-08:00", true, false, 1, 0}, 265 {"RFC3339", RFC3339, "2010-02-04T21:00:57-08:00", true, false, 1, 0},
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 for _, test := range rubyTests { 311 for _, test := range rubyTests {
292 time, err := Parse(test.format, test.value) 312 time, err := Parse(test.format, test.value)
293 if err != nil { 313 if err != nil {
294 t.Errorf("%s error: %v", test.name, err) 314 t.Errorf("%s error: %v", test.name, err)
295 } else { 315 } else {
296 checkTime(time, &test, t) 316 checkTime(time, &test, t)
297 } 317 }
298 } 318 }
299 } 319 }
300 320
301 func checkTime(time *Time, test *ParseTest, t *testing.T) { 321 func checkTime(time Time, test *ParseTest, t *testing.T) {
302 // The time should be Thu Feb 4 21:00:57 PST 2010 322 // The time should be Thu Feb 4 21:00:57 PST 2010
303 » if test.yearSign*time.Year != 2010 { 323 » if test.yearSign*time.Year() != 2010 {
304 » » t.Errorf("%s: bad year: %d not %d", test.name, time.Year, 2010) 324 » » t.Errorf("%s: bad year: %d not %d", test.name, time.Year(), 2010 )
305 » } 325 » }
306 » if time.Month != 2 { 326 » if time.Month() != February {
307 » » t.Errorf("%s: bad month: %d not %d", test.name, time.Month, 2) 327 » » t.Errorf("%s: bad month: %s not %s", test.name, time.Month(), Fe bruary)
308 » } 328 » }
309 » if time.Day != 4 { 329 » if time.Day() != 4 {
310 » » t.Errorf("%s: bad day: %d not %d", test.name, time.Day, 4) 330 » » t.Errorf("%s: bad day: %d not %d", test.name, time.Day(), 4)
311 » } 331 » }
312 » if time.Hour != 21 { 332 » if time.Hour() != 21 {
313 » » t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour, 21) 333 » » t.Errorf("%s: bad hour: %d not %d", test.name, time.Hour(), 21)
314 » } 334 » }
315 » if time.Minute != 0 { 335 » if time.Minute() != 0 {
316 » » t.Errorf("%s: bad minute: %d not %d", test.name, time.Minute, 0) 336 » » t.Errorf("%s: bad minute: %d not %d", test.name, time.Minute(), 0)
317 » } 337 » }
318 » if time.Second != 57 { 338 » if time.Second() != 57 {
319 » » t.Errorf("%s: bad second: %d not %d", test.name, time.Second, 57 ) 339 » » t.Errorf("%s: bad second: %d not %d", test.name, time.Second(), 57)
320 } 340 }
321 // Nanoseconds must be checked against the precision of the input. 341 // Nanoseconds must be checked against the precision of the input.
322 nanosec, err := strconv.Atoui("012345678"[:test.fracDigits] + "000000000 "[:9-test.fracDigits]) 342 nanosec, err := strconv.Atoui("012345678"[:test.fracDigits] + "000000000 "[:9-test.fracDigits])
323 if err != nil { 343 if err != nil {
324 panic(err) 344 panic(err)
325 } 345 }
326 » if time.Nanosecond != int(nanosec) { 346 » if time.Nanosecond() != int(nanosec) {
327 » » t.Errorf("%s: bad nanosecond: %d not %d", test.name, time.Nanose cond, nanosec) 347 » » t.Errorf("%s: bad nanosecond: %d not %d", test.name, time.Nanose cond(), nanosec)
328 » } 348 » }
329 » if test.hasTZ && time.ZoneOffset != -28800 { 349 » name, offset := time.Zone()
330 » » t.Errorf("%s: bad tz offset: %d not %d", test.name, time.ZoneOff set, -28800) 350 » if test.hasTZ && offset != -28800 {
331 » } 351 » » t.Errorf("%s: bad tz offset: %s %d not %d", test.name, name, off set, -28800)
332 » if test.hasWD && time.Weekday() != 4 { 352 » }
333 » » t.Errorf("%s: bad weekday: %d not %d", test.name, time.Weekday() , 4) 353 » if test.hasWD && time.Weekday() != Thursday {
354 » » t.Errorf("%s: bad weekday: %s not %s", test.name, time.Weekday() , Thursday)
334 } 355 }
335 } 356 }
336 357
337 func TestFormatAndParse(t *testing.T) { 358 func TestFormatAndParse(t *testing.T) {
338 const fmt = "Mon MST " + RFC3339 // all fields 359 const fmt = "Mon MST " + RFC3339 // all fields
339 f := func(sec int64) bool { 360 f := func(sec int64) bool {
340 » » t1 := SecondsToLocalTime(sec) 361 » » t1 := Unix(sec, 0)
341 » » if t1.Year < 1000 || t1.Year > 9999 { 362 » » if t1.Year() < 1000 || t1.Year() > 9999 {
342 // not required to work 363 // not required to work
343 return true 364 return true
344 } 365 }
345 t2, err := Parse(fmt, t1.Format(fmt)) 366 t2, err := Parse(fmt, t1.Format(fmt))
346 if err != nil { 367 if err != nil {
347 t.Errorf("error: %s", err) 368 t.Errorf("error: %s", err)
348 return false 369 return false
349 } 370 }
350 » » if !same(t1, t2) { 371 » » if t1.Unix() != t2.Unix() || t1.Nanosecond() != t2.Nanosecond() {
351 » » » t.Errorf("different: %q %q", t1, t2) 372 » » » t.Errorf("FormatAndParse %d: %q(%d) %q(%d)", sec, t1, t1 .Unix(), t2, t2.Unix())
352 return false 373 return false
353 } 374 }
354 return true 375 return true
355 } 376 }
356 f32 := func(sec int32) bool { return f(int64(sec)) } 377 f32 := func(sec int32) bool { return f(int64(sec)) }
357 cfg := &quick.Config{MaxCount: 10000} 378 cfg := &quick.Config{MaxCount: 10000}
358 379
359 // Try a reasonable date first, then the huge ones. 380 // Try a reasonable date first, then the huge ones.
360 if err := quick.Check(f32, cfg); err != nil { 381 if err := quick.Check(f32, cfg); err != nil {
361 t.Fatal(err) 382 t.Fatal(err)
(...skipping 25 matching lines...) Expand all
387 _, err := Parse(test.format, test.value) 408 _, err := Parse(test.format, test.value)
388 if err == nil { 409 if err == nil {
389 t.Errorf("expected error for %q %q", test.format, test.v alue) 410 t.Errorf("expected error for %q %q", test.format, test.v alue)
390 } else if strings.Index(err.Error(), test.expect) < 0 { 411 } else if strings.Index(err.Error(), test.expect) < 0 {
391 t.Errorf("expected error with %q for %q %q; got %s", tes t.expect, test.format, test.value, err) 412 t.Errorf("expected error with %q for %q %q; got %s", tes t.expect, test.format, test.value, err)
392 } 413 }
393 } 414 }
394 } 415 }
395 416
396 func TestNoonIs12PM(t *testing.T) { 417 func TestNoonIs12PM(t *testing.T) {
397 » noon := Time{Hour: 12} 418 » noon := Date(0, January, 1, 12, 0, 0, 0, UTC)
398 const expect = "12:00PM" 419 const expect = "12:00PM"
399 got := noon.Format("3:04PM") 420 got := noon.Format("3:04PM")
400 if got != expect { 421 if got != expect {
401 t.Errorf("got %q; expect %q", got, expect) 422 t.Errorf("got %q; expect %q", got, expect)
402 } 423 }
403 got = noon.Format("03:04PM") 424 got = noon.Format("03:04PM")
404 if got != expect { 425 if got != expect {
405 t.Errorf("got %q; expect %q", got, expect) 426 t.Errorf("got %q; expect %q", got, expect)
406 } 427 }
407 } 428 }
408 429
409 func TestMidnightIs12AM(t *testing.T) { 430 func TestMidnightIs12AM(t *testing.T) {
410 » midnight := Time{Hour: 0} 431 » midnight := Date(0, January, 1, 0, 0, 0, 0, UTC)
411 expect := "12:00AM" 432 expect := "12:00AM"
412 got := midnight.Format("3:04PM") 433 got := midnight.Format("3:04PM")
413 if got != expect { 434 if got != expect {
414 t.Errorf("got %q; expect %q", got, expect) 435 t.Errorf("got %q; expect %q", got, expect)
415 } 436 }
416 got = midnight.Format("03:04PM") 437 got = midnight.Format("03:04PM")
417 if got != expect { 438 if got != expect {
418 t.Errorf("got %q; expect %q", got, expect) 439 t.Errorf("got %q; expect %q", got, expect)
419 } 440 }
420 } 441 }
421 442
422 func Test12PMIsNoon(t *testing.T) { 443 func Test12PMIsNoon(t *testing.T) {
423 noon, err := Parse("3:04PM", "12:00PM") 444 noon, err := Parse("3:04PM", "12:00PM")
424 if err != nil { 445 if err != nil {
425 t.Fatal("error parsing date:", err) 446 t.Fatal("error parsing date:", err)
426 } 447 }
427 » if noon.Hour != 12 { 448 » if noon.Hour() != 12 {
428 » » t.Errorf("got %d; expect 12", noon.Hour) 449 » » t.Errorf("got %d; expect 12", noon.Hour())
429 } 450 }
430 noon, err = Parse("03:04PM", "12:00PM") 451 noon, err = Parse("03:04PM", "12:00PM")
431 if err != nil { 452 if err != nil {
432 t.Fatal("error parsing date:", err) 453 t.Fatal("error parsing date:", err)
433 } 454 }
434 » if noon.Hour != 12 { 455 » if noon.Hour() != 12 {
435 » » t.Errorf("got %d; expect 12", noon.Hour) 456 » » t.Errorf("got %d; expect 12", noon.Hour())
436 } 457 }
437 } 458 }
438 459
439 func Test12AMIsMidnight(t *testing.T) { 460 func Test12AMIsMidnight(t *testing.T) {
440 midnight, err := Parse("3:04PM", "12:00AM") 461 midnight, err := Parse("3:04PM", "12:00AM")
441 if err != nil { 462 if err != nil {
442 t.Fatal("error parsing date:", err) 463 t.Fatal("error parsing date:", err)
443 } 464 }
444 » if midnight.Hour != 0 { 465 » if midnight.Hour() != 0 {
445 » » t.Errorf("got %d; expect 0", midnight.Hour) 466 » » t.Errorf("got %d; expect 0", midnight.Hour())
446 } 467 }
447 midnight, err = Parse("03:04PM", "12:00AM") 468 midnight, err = Parse("03:04PM", "12:00AM")
448 if err != nil { 469 if err != nil {
449 t.Fatal("error parsing date:", err) 470 t.Fatal("error parsing date:", err)
450 } 471 }
451 » if midnight.Hour != 0 { 472 » if midnight.Hour() != 0 {
452 » » t.Errorf("got %d; expect 0", midnight.Hour) 473 » » t.Errorf("got %d; expect 0", midnight.Hour())
453 } 474 }
454 } 475 }
455 476
456 // Check that a time without a Zone still produces a (numeric) time zone 477 // Check that a time without a Zone still produces a (numeric) time zone
457 // when formatted with MST as a requested zone. 478 // when formatted with MST as a requested zone.
458 func TestMissingZone(t *testing.T) { 479 func TestMissingZone(t *testing.T) {
459 time, err := Parse(RubyDate, "Thu Feb 02 16:10:03 -0500 2006") 480 time, err := Parse(RubyDate, "Thu Feb 02 16:10:03 -0500 2006")
460 if err != nil { 481 if err != nil {
461 t.Fatal("error parsing date:", err) 482 t.Fatal("error parsing date:", err)
462 } 483 }
463 expect := "Thu Feb 2 16:10:03 -0500 2006" // -0500 not EST 484 expect := "Thu Feb 2 16:10:03 -0500 2006" // -0500 not EST
464 str := time.Format(UnixDate) // uses MST as its time zone 485 str := time.Format(UnixDate) // uses MST as its time zone
465 if str != expect { 486 if str != expect {
466 » » t.Errorf("expected %q got %q", expect, str) 487 » » t.Errorf("got %s; expect %s", str, expect)
467 } 488 }
468 } 489 }
469 490
470 func TestMinutesInTimeZone(t *testing.T) { 491 func TestMinutesInTimeZone(t *testing.T) {
471 time, err := Parse(RubyDate, "Mon Jan 02 15:04:05 +0123 2006") 492 time, err := Parse(RubyDate, "Mon Jan 02 15:04:05 +0123 2006")
472 if err != nil { 493 if err != nil {
473 t.Fatal("error parsing date:", err) 494 t.Fatal("error parsing date:", err)
474 } 495 }
475 expected := (1*60 + 23) * 60 496 expected := (1*60 + 23) * 60
476 » if time.ZoneOffset != expected { 497 » _, offset := time.Zone()
477 » » t.Errorf("ZoneOffset incorrect, expected %d got %d", expected, t ime.ZoneOffset) 498 » if offset != expected {
499 » » t.Errorf("ZoneOffset = %d, want %d", offset, expected)
478 } 500 }
479 } 501 }
480 502
481 type ISOWeekTest struct { 503 type ISOWeekTest struct {
482 » year int64 // year 504 » year int // year
483 » month, day int // month and day 505 » month, day int // month and day
484 » yex int64 // expected year 506 » yex int // expected year
485 » wex int // expected week 507 » wex int // expected week
486 } 508 }
487 509
488 var isoWeekTests = []ISOWeekTest{ 510 var isoWeekTests = []ISOWeekTest{
489 {1981, 1, 1, 1981, 1}, {1982, 1, 1, 1981, 53}, {1983, 1, 1, 1982, 52}, 511 {1981, 1, 1, 1981, 1}, {1982, 1, 1, 1981, 53}, {1983, 1, 1, 1982, 52},
490 {1984, 1, 1, 1983, 52}, {1985, 1, 1, 1985, 1}, {1986, 1, 1, 1986, 1}, 512 {1984, 1, 1, 1983, 52}, {1985, 1, 1, 1985, 1}, {1986, 1, 1, 1986, 1},
491 {1987, 1, 1, 1987, 1}, {1988, 1, 1, 1987, 53}, {1989, 1, 1, 1988, 52}, 513 {1987, 1, 1, 1987, 1}, {1988, 1, 1, 1987, 53}, {1989, 1, 1, 1988, 52},
492 {1990, 1, 1, 1990, 1}, {1991, 1, 1, 1991, 1}, {1992, 1, 1, 1992, 1}, 514 {1990, 1, 1, 1990, 1}, {1991, 1, 1, 1991, 1}, {1992, 1, 1, 1992, 1},
493 {1993, 1, 1, 1992, 53}, {1994, 1, 1, 1993, 52}, {1995, 1, 2, 1995, 1}, 515 {1993, 1, 1, 1992, 53}, {1994, 1, 1, 1993, 52}, {1995, 1, 2, 1995, 1},
494 {1996, 1, 1, 1996, 1}, {1996, 1, 7, 1996, 1}, {1996, 1, 8, 1996, 2}, 516 {1996, 1, 1, 1996, 1}, {1996, 1, 7, 1996, 1}, {1996, 1, 8, 1996, 2},
495 {1997, 1, 1, 1997, 1}, {1998, 1, 1, 1998, 1}, {1999, 1, 1, 1998, 53}, 517 {1997, 1, 1, 1997, 1}, {1998, 1, 1, 1998, 1}, {1999, 1, 1, 1998, 53},
(...skipping 21 matching lines...) Expand all
517 {2027, 1, 1, 2026, 53}, {2028, 1, 1, 2027, 52}, {2029, 1, 1, 2029, 1}, 539 {2027, 1, 1, 2026, 53}, {2028, 1, 1, 2027, 52}, {2029, 1, 1, 2029, 1},
518 {2030, 1, 1, 2030, 1}, {2031, 1, 1, 2031, 1}, {2032, 1, 1, 2032, 1}, 540 {2030, 1, 1, 2030, 1}, {2031, 1, 1, 2031, 1}, {2032, 1, 1, 2032, 1},
519 {2033, 1, 1, 2032, 53}, {2034, 1, 1, 2033, 52}, {2035, 1, 1, 2035, 1}, 541 {2033, 1, 1, 2032, 53}, {2034, 1, 1, 2033, 52}, {2035, 1, 1, 2035, 1},
520 {2036, 1, 1, 2036, 1}, {2037, 1, 1, 2037, 1}, {2038, 1, 1, 2037, 53}, 542 {2036, 1, 1, 2036, 1}, {2037, 1, 1, 2037, 1}, {2038, 1, 1, 2037, 53},
521 {2039, 1, 1, 2038, 52}, {2040, 1, 1, 2039, 52}, 543 {2039, 1, 1, 2038, 52}, {2040, 1, 1, 2039, 52},
522 } 544 }
523 545
524 func TestISOWeek(t *testing.T) { 546 func TestISOWeek(t *testing.T) {
525 // Selected dates and corner cases 547 // Selected dates and corner cases
526 for _, wt := range isoWeekTests { 548 for _, wt := range isoWeekTests {
527 » » dt := &Time{Year: wt.year, Month: wt.month, Day: wt.day} 549 » » dt := Date(wt.year, Month(wt.month), wt.day, 0, 0, 0, 0, UTC)
528 y, w := dt.ISOWeek() 550 y, w := dt.ISOWeek()
529 if w != wt.wex || y != wt.yex { 551 if w != wt.wex || y != wt.yex {
530 t.Errorf("got %d/%d; expected %d/%d for %d-%02d-%02d", 552 t.Errorf("got %d/%d; expected %d/%d for %d-%02d-%02d",
531 y, w, wt.yex, wt.wex, wt.year, wt.month, wt.day) 553 y, w, wt.yex, wt.wex, wt.year, wt.month, wt.day)
532 } 554 }
533 } 555 }
534 556
535 // The only real invariant: Jan 04 is in week 1 557 // The only real invariant: Jan 04 is in week 1
536 » for year := int64(1950); year < 2100; year++ { 558 » for year := 1950; year < 2100; year++ {
537 » » if y, w := (&Time{Year: year, Month: 1, Day: 4}).ISOWeek(); y != year || w != 1 { 559 » » if y, w := Date(year, January, 4, 0, 0, 0, 0, UTC).ISOWeek(); y != year || w != 1 {
538 t.Errorf("got %d/%d; expected %d/1 for Jan 04", y, w, ye ar) 560 t.Errorf("got %d/%d; expected %d/1 for Jan 04", y, w, ye ar)
539 } 561 }
540 } 562 }
541 } 563 }
542 564
543 func BenchmarkSeconds(b *testing.B) { 565 var durationTests = []struct {
566 » str string
567 » d Duration
568 }{
569 » {"0", 0},
570 » {"1ns", 1 * Nanosecond},
571 » {"1.1us", 1100 * Nanosecond},
572 » {"2.2ms", 2200 * Microsecond},
573 » {"3.3s", 3300 * Millisecond},
574 » {"4m5s", 4*Minute + 5*Second},
575 » {"4m5.001s", 4*Minute + 5001*Millisecond},
576 » {"5h6m7.001s", 5*Hour + 6*Minute + 7001*Millisecond},
577 » {"8m0.000000001s", 8*Minute + 1*Nanosecond},
578 » {"2562047h47m16.854775807s", 1<<63 - 1},
579 » {"-2562047h47m16.854775808s", -1 << 63},
580 }
581
582 func TestDurationString(t *testing.T) {
583 » for _, tt := range durationTests {
584 » » if str := tt.d.String(); str != tt.str {
585 » » » t.Errorf("Duration(%d).String() = %s, want %s", int64(tt .d), str, tt.str)
586 » » }
587 » » if tt.d > 0 {
588 » » » if str := (-tt.d).String(); str != "-"+tt.str {
589 » » » » t.Errorf("Duration(%d).String() = %s, want %s", int64(-tt.d), str, "-"+tt.str)
590 » » » }
591 » » }
592 » }
593 }
594
595 var dateTests = []struct {
596 » year, month, day, hour, min, sec, nsec int
597 » z *Location
598 » unix int64
599 }{
600 » {2011, 11, 6, 1, 0, 0, 0, Local, 1320566400}, // 1:00:00 PDT
601 » {2011, 11, 6, 1, 59, 59, 0, Local, 1320569999}, // 1:59:59 PDT
602 » {2011, 11, 6, 2, 0, 0, 0, Local, 1320573600}, // 2:00:00 PST
603
604 » {2011, 3, 13, 1, 0, 0, 0, Local, 1300006800}, // 1:00:00 PST
605 » {2011, 3, 13, 1, 59, 59, 0, Local, 1300010399}, // 1:59:59 PST
606 » {2011, 3, 13, 3, 0, 0, 0, Local, 1300010400}, // 3:00:00 PDT
607 » {2011, 3, 13, 2, 30, 0, 0, Local, 1300008600}, // 2:30:00 PDT ≡ 1:30 PS T
608
609 » // Many names for Fri Nov 18 7:56:35 PST 2011
610 » {2011, 11, 18, 7, 56, 35, 0, Local, 1321631795}, // Nov 18 7:56:35
611 » {2011, 11, 19, -17, 56, 35, 0, Local, 1321631795}, // Nov 19 -17:56:35
612 » {2011, 11, 17, 31, 56, 35, 0, Local, 1321631795}, // Nov 17 31:56:35
613 » {2011, 11, 18, 6, 116, 35, 0, Local, 1321631795}, // Nov 18 6:116:35
614 » {2011, 10, 49, 7, 56, 35, 0, Local, 1321631795}, // Oct 49 7:56:35
615 » {2011, 11, 18, 7, 55, 95, 0, Local, 1321631795}, // Nov 18 7:55:95
616 » {2011, 11, 18, 7, 56, 34, 1e9, Local, 1321631795}, // Nov 18 7:56:34 + 10⁹ns
617 » {2011, 12, -12, 7, 56, 35, 0, Local, 1321631795}, // Dec -21 7:56:35
618 » {2012, 1, -43, 7, 56, 35, 0, Local, 1321631795}, // Jan -52 7:56:35 2012
619 » {2012, int(January - 2), 18, 7, 56, 35, 0, Local, 1321631795}, // (Jan -2) 18 7:56:35 2012
620 » {2010, int(December + 11), 18, 7, 56, 35, 0, Local, 1321631795}, // (Dec +11) 18 7:56:35 2010
621 }
622
623 func TestDate(t *testing.T) {
624 » for _, tt := range dateTests {
625 » » time := Date(tt.year, Month(tt.month), tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z)
626 » » want := Unix(tt.unix, 0)
627 » » if !time.Equal(want) {
628 » » » t.Errorf("Date(%d, %d, %d, %d, %d, %d, %d, %s) = %v, wan t %v",
629 » » » » tt.year, tt.month, tt.day, tt.hour, tt.min, tt.s ec, tt.nsec, tt.z,
630 » » » » time, want)
631 » » }
632 » }
633 }
634
635 func BenchmarkNow(b *testing.B) {
544 for i := 0; i < b.N; i++ { 636 for i := 0; i < b.N; i++ {
545 » » Seconds() 637 » » Now()
546 » }
547 }
548
549 func BenchmarkNanoseconds(b *testing.B) {
550 » for i := 0; i < b.N; i++ {
551 » » Nanoseconds()
552 } 638 }
553 } 639 }
554 640
555 func BenchmarkFormat(b *testing.B) { 641 func BenchmarkFormat(b *testing.B) {
556 » time := SecondsToLocalTime(1265346057) 642 » time := Unix(1265346057, 0)
557 for i := 0; i < b.N; i++ { 643 for i := 0; i < b.N; i++ {
558 time.Format("Mon Jan 2 15:04:05 2006") 644 time.Format("Mon Jan 2 15:04:05 2006")
559 } 645 }
560 } 646 }
561 647
562 func BenchmarkParse(b *testing.B) { 648 func BenchmarkParse(b *testing.B) {
563 for i := 0; i < b.N; i++ { 649 for i := 0; i < b.N; i++ {
564 Parse(ANSIC, "Mon Jan 2 15:04:05 2006") 650 Parse(ANSIC, "Mon Jan 2 15:04:05 2006")
565 } 651 }
566 } 652 }
653
654 func BenchmarkHour(b *testing.B) {
655 t := Now()
656 for i := 0; i < b.N; i++ {
657 _ = t.Hour()
658 }
659 }
660
661 func BenchmarkSecond(b *testing.B) {
662 t := Now()
663 for i := 0; i < b.N; i++ {
664 _ = t.Second()
665 }
666 }
667
668 func BenchmarkYear(b *testing.B) {
669 t := Now()
670 for i := 0; i < b.N; i++ {
671 _ = t.Year()
672 }
673 }
674
675 func BenchmarkDay(b *testing.B) {
676 t := Now()
677 for i := 0; i < b.N; i++ {
678 _ = t.Day()
679 }
680 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b