time_test.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package time_test
  5. import (
  6. "bytes"
  7. "encoding/gob"
  8. "encoding/json"
  9. "fmt"
  10. "math/big"
  11. "math/rand"
  12. "runtime"
  13. "testing"
  14. "testing/quick"
  15. . "time"
  16. )
  17. // We should be in PST/PDT, but if the time zone files are missing we
  18. // won't be. The purpose of this test is to at least explain why some of
  19. // the subsequent tests fail.
  20. func TestZoneData(t *testing.T) {
  21. lt := Now()
  22. // PST is 8 hours west, PDT is 7 hours west. We could use the name but it's not unique.
  23. if name, off := lt.Zone(); off != -8*60*60 && off != -7*60*60 {
  24. t.Errorf("Unable to find US Pacific time zone data for testing; time zone is %q offset %d", name, off)
  25. t.Error("Likely problem: the time zone files have not been installed.")
  26. }
  27. }
  28. // parsedTime is the struct representing a parsed time value.
  29. type parsedTime struct {
  30. Year int
  31. Month Month
  32. Day int
  33. Hour, Minute, Second int // 15:04:05 is 15, 4, 5.
  34. Nanosecond int // Fractional second.
  35. Weekday Weekday
  36. ZoneOffset int // seconds east of UTC, e.g. -7*60*60 for -0700
  37. Zone string // e.g., "MST"
  38. }
  39. type TimeTest struct {
  40. seconds int64
  41. golden parsedTime
  42. }
  43. var utctests = []TimeTest{
  44. {0, parsedTime{1970, January, 1, 0, 0, 0, 0, Thursday, 0, "UTC"}},
  45. {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 0, Wednesday, 0, "UTC"}},
  46. {-1221681866, parsedTime{1931, April, 16, 3, 55, 34, 0, Thursday, 0, "UTC"}},
  47. {-11644473600, parsedTime{1601, January, 1, 0, 0, 0, 0, Monday, 0, "UTC"}},
  48. {599529660, parsedTime{1988, December, 31, 0, 1, 0, 0, Saturday, 0, "UTC"}},
  49. {978220860, parsedTime{2000, December, 31, 0, 1, 0, 0, Sunday, 0, "UTC"}},
  50. }
  51. var nanoutctests = []TimeTest{
  52. {0, parsedTime{1970, January, 1, 0, 0, 0, 1e8, Thursday, 0, "UTC"}},
  53. {1221681866, parsedTime{2008, September, 17, 20, 4, 26, 2e8, Wednesday, 0, "UTC"}},
  54. }
  55. var localtests = []TimeTest{
  56. {0, parsedTime{1969, December, 31, 16, 0, 0, 0, Wednesday, -8 * 60 * 60, "PST"}},
  57. {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 0, Wednesday, -7 * 60 * 60, "PDT"}},
  58. }
  59. var nanolocaltests = []TimeTest{
  60. {0, parsedTime{1969, December, 31, 16, 0, 0, 1e8, Wednesday, -8 * 60 * 60, "PST"}},
  61. {1221681866, parsedTime{2008, September, 17, 13, 4, 26, 3e8, Wednesday, -7 * 60 * 60, "PDT"}},
  62. }
  63. func same(t Time, u *parsedTime) bool {
  64. // Check aggregates.
  65. year, month, day := t.Date()
  66. hour, min, sec := t.Clock()
  67. name, offset := t.Zone()
  68. if year != u.Year || month != u.Month || day != u.Day ||
  69. hour != u.Hour || min != u.Minute || sec != u.Second ||
  70. name != u.Zone || offset != u.ZoneOffset {
  71. return false
  72. }
  73. // Check individual entries.
  74. return t.Year() == u.Year &&
  75. t.Month() == u.Month &&
  76. t.Day() == u.Day &&
  77. t.Hour() == u.Hour &&
  78. t.Minute() == u.Minute &&
  79. t.Second() == u.Second &&
  80. t.Nanosecond() == u.Nanosecond &&
  81. t.Weekday() == u.Weekday
  82. }
  83. func TestSecondsToUTC(t *testing.T) {
  84. for _, test := range utctests {
  85. sec := test.seconds
  86. golden := &test.golden
  87. tm := Unix(sec, 0).UTC()
  88. newsec := tm.Unix()
  89. if newsec != sec {
  90. t.Errorf("SecondsToUTC(%d).Seconds() = %d", sec, newsec)
  91. }
  92. if !same(tm, golden) {
  93. t.Errorf("SecondsToUTC(%d): // %#v", sec, tm)
  94. t.Errorf(" want=%+v", *golden)
  95. t.Errorf(" have=%v", tm.Format(RFC3339+" MST"))
  96. }
  97. }
  98. }
  99. func TestNanosecondsToUTC(t *testing.T) {
  100. for _, test := range nanoutctests {
  101. golden := &test.golden
  102. nsec := test.seconds*1e9 + int64(golden.Nanosecond)
  103. tm := Unix(0, nsec).UTC()
  104. newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
  105. if newnsec != nsec {
  106. t.Errorf("NanosecondsToUTC(%d).Nanoseconds() = %d", nsec, newnsec)
  107. }
  108. if !same(tm, golden) {
  109. t.Errorf("NanosecondsToUTC(%d):", nsec)
  110. t.Errorf(" want=%+v", *golden)
  111. t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
  112. }
  113. }
  114. }
  115. func TestSecondsToLocalTime(t *testing.T) {
  116. for _, test := range localtests {
  117. sec := test.seconds
  118. golden := &test.golden
  119. tm := Unix(sec, 0)
  120. newsec := tm.Unix()
  121. if newsec != sec {
  122. t.Errorf("SecondsToLocalTime(%d).Seconds() = %d", sec, newsec)
  123. }
  124. if !same(tm, golden) {
  125. t.Errorf("SecondsToLocalTime(%d):", sec)
  126. t.Errorf(" want=%+v", *golden)
  127. t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
  128. }
  129. }
  130. }
  131. func TestNanosecondsToLocalTime(t *testing.T) {
  132. for _, test := range nanolocaltests {
  133. golden := &test.golden
  134. nsec := test.seconds*1e9 + int64(golden.Nanosecond)
  135. tm := Unix(0, nsec)
  136. newnsec := tm.Unix()*1e9 + int64(tm.Nanosecond())
  137. if newnsec != nsec {
  138. t.Errorf("NanosecondsToLocalTime(%d).Seconds() = %d", nsec, newnsec)
  139. }
  140. if !same(tm, golden) {
  141. t.Errorf("NanosecondsToLocalTime(%d):", nsec)
  142. t.Errorf(" want=%+v", *golden)
  143. t.Errorf(" have=%+v", tm.Format(RFC3339+" MST"))
  144. }
  145. }
  146. }
  147. func TestSecondsToUTCAndBack(t *testing.T) {
  148. f := func(sec int64) bool { return Unix(sec, 0).UTC().Unix() == sec }
  149. f32 := func(sec int32) bool { return f(int64(sec)) }
  150. cfg := &quick.Config{MaxCount: 10000}
  151. // Try a reasonable date first, then the huge ones.
  152. if err := quick.Check(f32, cfg); err != nil {
  153. t.Fatal(err)
  154. }
  155. if err := quick.Check(f, cfg); err != nil {
  156. t.Fatal(err)
  157. }
  158. }
  159. func TestNanosecondsToUTCAndBack(t *testing.T) {
  160. f := func(nsec int64) bool {
  161. t := Unix(0, nsec).UTC()
  162. ns := t.Unix()*1e9 + int64(t.Nanosecond())
  163. return ns == nsec
  164. }
  165. f32 := func(nsec int32) bool { return f(int64(nsec)) }
  166. cfg := &quick.Config{MaxCount: 10000}
  167. // Try a small date first, then the large ones. (The span is only a few hundred years
  168. // for nanoseconds in an int64.)
  169. if err := quick.Check(f32, cfg); err != nil {
  170. t.Fatal(err)
  171. }
  172. if err := quick.Check(f, cfg); err != nil {
  173. t.Fatal(err)
  174. }
  175. }
  176. // The time routines provide no way to get absolute time
  177. // (seconds since zero), but we need it to compute the right
  178. // answer for bizarre roundings like "to the nearest 3 ns".
  179. // Compute as t - year1 = (t - 1970) + (1970 - 2001) + (2001 - 1).
  180. // t - 1970 is returned by Unix and Nanosecond.
  181. // 1970 - 2001 is -(31*365+8)*86400 = -978307200 seconds.
  182. // 2001 - 1 is 2000*365.2425*86400 = 63113904000 seconds.
  183. const unixToZero = -978307200 + 63113904000
  184. // abs returns the absolute time stored in t, as seconds and nanoseconds.
  185. func abs(t Time) (sec, nsec int64) {
  186. unix := t.Unix()
  187. nano := t.Nanosecond()
  188. return unix + unixToZero, int64(nano)
  189. }
  190. // absString returns abs as a decimal string.
  191. func absString(t Time) string {
  192. sec, nsec := abs(t)
  193. if sec < 0 {
  194. sec = -sec
  195. nsec = -nsec
  196. if nsec < 0 {
  197. nsec += 1e9
  198. sec--
  199. }
  200. return fmt.Sprintf("-%d%09d", sec, nsec)
  201. }
  202. return fmt.Sprintf("%d%09d", sec, nsec)
  203. }
  204. var truncateRoundTests = []struct {
  205. t Time
  206. d Duration
  207. }{
  208. {Date(-1, January, 1, 12, 15, 30, 5e8, UTC), 3},
  209. {Date(-1, January, 1, 12, 15, 31, 5e8, UTC), 3},
  210. {Date(2012, January, 1, 12, 15, 30, 5e8, UTC), Second},
  211. {Date(2012, January, 1, 12, 15, 31, 5e8, UTC), Second},
  212. }
  213. func TestTruncateRound(t *testing.T) {
  214. var (
  215. bsec = new(big.Int)
  216. bnsec = new(big.Int)
  217. bd = new(big.Int)
  218. bt = new(big.Int)
  219. br = new(big.Int)
  220. bq = new(big.Int)
  221. b1e9 = new(big.Int)
  222. )
  223. b1e9.SetInt64(1e9)
  224. testOne := func(ti, tns, di int64) bool {
  225. t0 := Unix(ti, int64(tns)).UTC()
  226. d := Duration(di)
  227. if d < 0 {
  228. d = -d
  229. }
  230. if d <= 0 {
  231. d = 1
  232. }
  233. // Compute bt = absolute nanoseconds.
  234. sec, nsec := abs(t0)
  235. bsec.SetInt64(sec)
  236. bnsec.SetInt64(nsec)
  237. bt.Mul(bsec, b1e9)
  238. bt.Add(bt, bnsec)
  239. // Compute quotient and remainder mod d.
  240. bd.SetInt64(int64(d))
  241. bq.DivMod(bt, bd, br)
  242. // To truncate, subtract remainder.
  243. // br is < d, so it fits in an int64.
  244. r := br.Int64()
  245. t1 := t0.Add(-Duration(r))
  246. // Check that time.Truncate works.
  247. if trunc := t0.Truncate(d); trunc != t1 {
  248. t.Errorf("Time.Truncate(%s, %s) = %s, want %s\n"+
  249. "%v trunc %v =\n%v want\n%v",
  250. t0.Format(RFC3339Nano), d, trunc, t1.Format(RFC3339Nano),
  251. absString(t0), int64(d), absString(trunc), absString(t1))
  252. return false
  253. }
  254. // To round, add d back if remainder r > d/2 or r == exactly d/2.
  255. // The commented out code would round half to even instead of up,
  256. // but that makes it time-zone dependent, which is a bit strange.
  257. if r > int64(d)/2 || r+r == int64(d) /*&& bq.Bit(0) == 1*/ {
  258. t1 = t1.Add(Duration(d))
  259. }
  260. // Check that time.Round works.
  261. if rnd := t0.Round(d); rnd != t1 {
  262. t.Errorf("Time.Round(%s, %s) = %s, want %s\n"+
  263. "%v round %v =\n%v want\n%v",
  264. t0.Format(RFC3339Nano), d, rnd, t1.Format(RFC3339Nano),
  265. absString(t0), int64(d), absString(rnd), absString(t1))
  266. return false
  267. }
  268. return true
  269. }
  270. // manual test cases
  271. for _, tt := range truncateRoundTests {
  272. testOne(tt.t.Unix(), int64(tt.t.Nanosecond()), int64(tt.d))
  273. }
  274. // exhaustive near 0
  275. for i := 0; i < 100; i++ {
  276. for j := 1; j < 100; j++ {
  277. testOne(unixToZero, int64(i), int64(j))
  278. testOne(unixToZero, -int64(i), int64(j))
  279. if t.Failed() {
  280. return
  281. }
  282. }
  283. }
  284. if t.Failed() {
  285. return
  286. }
  287. // randomly generated test cases
  288. cfg := &quick.Config{MaxCount: 100000}
  289. if testing.Short() {
  290. cfg.MaxCount = 1000
  291. }
  292. // divisors of Second
  293. f1 := func(ti int64, tns int32, logdi int32) bool {
  294. d := Duration(1)
  295. a, b := uint(logdi%9), (logdi>>16)%9
  296. d <<= a
  297. for i := 0; i < int(b); i++ {
  298. d *= 5
  299. }
  300. return testOne(ti, int64(tns), int64(d))
  301. }
  302. quick.Check(f1, cfg)
  303. // multiples of Second
  304. f2 := func(ti int64, tns int32, di int32) bool {
  305. d := Duration(di) * Second
  306. if d < 0 {
  307. d = -d
  308. }
  309. return testOne(ti, int64(tns), int64(d))
  310. }
  311. quick.Check(f2, cfg)
  312. // halfway cases
  313. f3 := func(tns, di int64) bool {
  314. di &= 0xfffffffe
  315. if di == 0 {
  316. di = 2
  317. }
  318. tns -= tns % di
  319. if tns < 0 {
  320. tns += di / 2
  321. } else {
  322. tns -= di / 2
  323. }
  324. return testOne(0, tns, di)
  325. }
  326. quick.Check(f3, cfg)
  327. // full generality
  328. f4 := func(ti int64, tns int32, di int64) bool {
  329. return testOne(ti, int64(tns), di)
  330. }
  331. quick.Check(f4, cfg)
  332. }
  333. type ISOWeekTest struct {
  334. year int // year
  335. month, day int // month and day
  336. yex int // expected year
  337. wex int // expected week
  338. }
  339. var isoWeekTests = []ISOWeekTest{
  340. {1981, 1, 1, 1981, 1}, {1982, 1, 1, 1981, 53}, {1983, 1, 1, 1982, 52},
  341. {1984, 1, 1, 1983, 52}, {1985, 1, 1, 1985, 1}, {1986, 1, 1, 1986, 1},
  342. {1987, 1, 1, 1987, 1}, {1988, 1, 1, 1987, 53}, {1989, 1, 1, 1988, 52},
  343. {1990, 1, 1, 1990, 1}, {1991, 1, 1, 1991, 1}, {1992, 1, 1, 1992, 1},
  344. {1993, 1, 1, 1992, 53}, {1994, 1, 1, 1993, 52}, {1995, 1, 2, 1995, 1},
  345. {1996, 1, 1, 1996, 1}, {1996, 1, 7, 1996, 1}, {1996, 1, 8, 1996, 2},
  346. {1997, 1, 1, 1997, 1}, {1998, 1, 1, 1998, 1}, {1999, 1, 1, 1998, 53},
  347. {2000, 1, 1, 1999, 52}, {2001, 1, 1, 2001, 1}, {2002, 1, 1, 2002, 1},
  348. {2003, 1, 1, 2003, 1}, {2004, 1, 1, 2004, 1}, {2005, 1, 1, 2004, 53},
  349. {2006, 1, 1, 2005, 52}, {2007, 1, 1, 2007, 1}, {2008, 1, 1, 2008, 1},
  350. {2009, 1, 1, 2009, 1}, {2010, 1, 1, 2009, 53}, {2010, 1, 1, 2009, 53},
  351. {2011, 1, 1, 2010, 52}, {2011, 1, 2, 2010, 52}, {2011, 1, 3, 2011, 1},
  352. {2011, 1, 4, 2011, 1}, {2011, 1, 5, 2011, 1}, {2011, 1, 6, 2011, 1},
  353. {2011, 1, 7, 2011, 1}, {2011, 1, 8, 2011, 1}, {2011, 1, 9, 2011, 1},
  354. {2011, 1, 10, 2011, 2}, {2011, 1, 11, 2011, 2}, {2011, 6, 12, 2011, 23},
  355. {2011, 6, 13, 2011, 24}, {2011, 12, 25, 2011, 51}, {2011, 12, 26, 2011, 52},
  356. {2011, 12, 27, 2011, 52}, {2011, 12, 28, 2011, 52}, {2011, 12, 29, 2011, 52},
  357. {2011, 12, 30, 2011, 52}, {2011, 12, 31, 2011, 52}, {1995, 1, 1, 1994, 52},
  358. {2012, 1, 1, 2011, 52}, {2012, 1, 2, 2012, 1}, {2012, 1, 8, 2012, 1},
  359. {2012, 1, 9, 2012, 2}, {2012, 12, 23, 2012, 51}, {2012, 12, 24, 2012, 52},
  360. {2012, 12, 30, 2012, 52}, {2012, 12, 31, 2013, 1}, {2013, 1, 1, 2013, 1},
  361. {2013, 1, 6, 2013, 1}, {2013, 1, 7, 2013, 2}, {2013, 12, 22, 2013, 51},
  362. {2013, 12, 23, 2013, 52}, {2013, 12, 29, 2013, 52}, {2013, 12, 30, 2014, 1},
  363. {2014, 1, 1, 2014, 1}, {2014, 1, 5, 2014, 1}, {2014, 1, 6, 2014, 2},
  364. {2015, 1, 1, 2015, 1}, {2016, 1, 1, 2015, 53}, {2017, 1, 1, 2016, 52},
  365. {2018, 1, 1, 2018, 1}, {2019, 1, 1, 2019, 1}, {2020, 1, 1, 2020, 1},
  366. {2021, 1, 1, 2020, 53}, {2022, 1, 1, 2021, 52}, {2023, 1, 1, 2022, 52},
  367. {2024, 1, 1, 2024, 1}, {2025, 1, 1, 2025, 1}, {2026, 1, 1, 2026, 1},
  368. {2027, 1, 1, 2026, 53}, {2028, 1, 1, 2027, 52}, {2029, 1, 1, 2029, 1},
  369. {2030, 1, 1, 2030, 1}, {2031, 1, 1, 2031, 1}, {2032, 1, 1, 2032, 1},
  370. {2033, 1, 1, 2032, 53}, {2034, 1, 1, 2033, 52}, {2035, 1, 1, 2035, 1},
  371. {2036, 1, 1, 2036, 1}, {2037, 1, 1, 2037, 1}, {2038, 1, 1, 2037, 53},
  372. {2039, 1, 1, 2038, 52}, {2040, 1, 1, 2039, 52},
  373. }
  374. func TestISOWeek(t *testing.T) {
  375. // Selected dates and corner cases
  376. for _, wt := range isoWeekTests {
  377. dt := Date(wt.year, Month(wt.month), wt.day, 0, 0, 0, 0, UTC)
  378. y, w := dt.ISOWeek()
  379. if w != wt.wex || y != wt.yex {
  380. t.Errorf("got %d/%d; expected %d/%d for %d-%02d-%02d",
  381. y, w, wt.yex, wt.wex, wt.year, wt.month, wt.day)
  382. }
  383. }
  384. // The only real invariant: Jan 04 is in week 1
  385. for year := 1950; year < 2100; year++ {
  386. if y, w := Date(year, January, 4, 0, 0, 0, 0, UTC).ISOWeek(); y != year || w != 1 {
  387. t.Errorf("got %d/%d; expected %d/1 for Jan 04", y, w, year)
  388. }
  389. }
  390. }
  391. type YearDayTest struct {
  392. year, month, day int
  393. yday int
  394. }
  395. // Test YearDay in several different scenarios
  396. // and corner cases
  397. var yearDayTests = []YearDayTest{
  398. // Non-leap-year tests
  399. {2007, 1, 1, 1},
  400. {2007, 1, 15, 15},
  401. {2007, 2, 1, 32},
  402. {2007, 2, 15, 46},
  403. {2007, 3, 1, 60},
  404. {2007, 3, 15, 74},
  405. {2007, 4, 1, 91},
  406. {2007, 12, 31, 365},
  407. // Leap-year tests
  408. {2008, 1, 1, 1},
  409. {2008, 1, 15, 15},
  410. {2008, 2, 1, 32},
  411. {2008, 2, 15, 46},
  412. {2008, 3, 1, 61},
  413. {2008, 3, 15, 75},
  414. {2008, 4, 1, 92},
  415. {2008, 12, 31, 366},
  416. // Looks like leap-year (but isn't) tests
  417. {1900, 1, 1, 1},
  418. {1900, 1, 15, 15},
  419. {1900, 2, 1, 32},
  420. {1900, 2, 15, 46},
  421. {1900, 3, 1, 60},
  422. {1900, 3, 15, 74},
  423. {1900, 4, 1, 91},
  424. {1900, 12, 31, 365},
  425. // Year one tests (non-leap)
  426. {1, 1, 1, 1},
  427. {1, 1, 15, 15},
  428. {1, 2, 1, 32},
  429. {1, 2, 15, 46},
  430. {1, 3, 1, 60},
  431. {1, 3, 15, 74},
  432. {1, 4, 1, 91},
  433. {1, 12, 31, 365},
  434. // Year minus one tests (non-leap)
  435. {-1, 1, 1, 1},
  436. {-1, 1, 15, 15},
  437. {-1, 2, 1, 32},
  438. {-1, 2, 15, 46},
  439. {-1, 3, 1, 60},
  440. {-1, 3, 15, 74},
  441. {-1, 4, 1, 91},
  442. {-1, 12, 31, 365},
  443. // 400 BC tests (leap-year)
  444. {-400, 1, 1, 1},
  445. {-400, 1, 15, 15},
  446. {-400, 2, 1, 32},
  447. {-400, 2, 15, 46},
  448. {-400, 3, 1, 61},
  449. {-400, 3, 15, 75},
  450. {-400, 4, 1, 92},
  451. {-400, 12, 31, 366},
  452. // Special Cases
  453. // Gregorian calendar change (no effect)
  454. {1582, 10, 4, 277},
  455. {1582, 10, 15, 288},
  456. }
  457. // Check to see if YearDay is location sensitive
  458. var yearDayLocations = []*Location{
  459. FixedZone("UTC-8", -8*60*60),
  460. FixedZone("UTC-4", -4*60*60),
  461. UTC,
  462. FixedZone("UTC+4", 4*60*60),
  463. FixedZone("UTC+8", 8*60*60),
  464. }
  465. func TestYearDay(t *testing.T) {
  466. for _, loc := range yearDayLocations {
  467. for _, ydt := range yearDayTests {
  468. dt := Date(ydt.year, Month(ydt.month), ydt.day, 0, 0, 0, 0, loc)
  469. yday := dt.YearDay()
  470. if yday != ydt.yday {
  471. t.Errorf("got %d, expected %d for %d-%02d-%02d in %v",
  472. yday, ydt.yday, ydt.year, ydt.month, ydt.day, loc)
  473. }
  474. }
  475. }
  476. }
  477. var durationTests = []struct {
  478. str string
  479. d Duration
  480. }{
  481. {"0", 0},
  482. {"1ns", 1 * Nanosecond},
  483. {"1.1µs", 1100 * Nanosecond},
  484. {"2.2ms", 2200 * Microsecond},
  485. {"3.3s", 3300 * Millisecond},
  486. {"4m5s", 4*Minute + 5*Second},
  487. {"4m5.001s", 4*Minute + 5001*Millisecond},
  488. {"5h6m7.001s", 5*Hour + 6*Minute + 7001*Millisecond},
  489. {"8m0.000000001s", 8*Minute + 1*Nanosecond},
  490. {"2562047h47m16.854775807s", 1<<63 - 1},
  491. {"-2562047h47m16.854775808s", -1 << 63},
  492. }
  493. func TestDurationString(t *testing.T) {
  494. for _, tt := range durationTests {
  495. if str := tt.d.String(); str != tt.str {
  496. t.Errorf("Duration(%d).String() = %s, want %s", int64(tt.d), str, tt.str)
  497. }
  498. if tt.d > 0 {
  499. if str := (-tt.d).String(); str != "-"+tt.str {
  500. t.Errorf("Duration(%d).String() = %s, want %s", int64(-tt.d), str, "-"+tt.str)
  501. }
  502. }
  503. }
  504. }
  505. var dateTests = []struct {
  506. year, month, day, hour, min, sec, nsec int
  507. z *Location
  508. unix int64
  509. }{
  510. {2011, 11, 6, 1, 0, 0, 0, Local, 1320566400}, // 1:00:00 PDT
  511. {2011, 11, 6, 1, 59, 59, 0, Local, 1320569999}, // 1:59:59 PDT
  512. {2011, 11, 6, 2, 0, 0, 0, Local, 1320573600}, // 2:00:00 PST
  513. {2011, 3, 13, 1, 0, 0, 0, Local, 1300006800}, // 1:00:00 PST
  514. {2011, 3, 13, 1, 59, 59, 0, Local, 1300010399}, // 1:59:59 PST
  515. {2011, 3, 13, 3, 0, 0, 0, Local, 1300010400}, // 3:00:00 PDT
  516. {2011, 3, 13, 2, 30, 0, 0, Local, 1300008600}, // 2:30:00 PDT ≡ 1:30 PST
  517. // Many names for Fri Nov 18 7:56:35 PST 2011
  518. {2011, 11, 18, 7, 56, 35, 0, Local, 1321631795}, // Nov 18 7:56:35
  519. {2011, 11, 19, -17, 56, 35, 0, Local, 1321631795}, // Nov 19 -17:56:35
  520. {2011, 11, 17, 31, 56, 35, 0, Local, 1321631795}, // Nov 17 31:56:35
  521. {2011, 11, 18, 6, 116, 35, 0, Local, 1321631795}, // Nov 18 6:116:35
  522. {2011, 10, 49, 7, 56, 35, 0, Local, 1321631795}, // Oct 49 7:56:35
  523. {2011, 11, 18, 7, 55, 95, 0, Local, 1321631795}, // Nov 18 7:55:95
  524. {2011, 11, 18, 7, 56, 34, 1e9, Local, 1321631795}, // Nov 18 7:56:34 + 10⁹ns
  525. {2011, 12, -12, 7, 56, 35, 0, Local, 1321631795}, // Dec -21 7:56:35
  526. {2012, 1, -43, 7, 56, 35, 0, Local, 1321631795}, // Jan -52 7:56:35 2012
  527. {2012, int(January - 2), 18, 7, 56, 35, 0, Local, 1321631795}, // (Jan-2) 18 7:56:35 2012
  528. {2010, int(December + 11), 18, 7, 56, 35, 0, Local, 1321631795}, // (Dec+11) 18 7:56:35 2010
  529. }
  530. func TestDate(t *testing.T) {
  531. for _, tt := range dateTests {
  532. time := Date(tt.year, Month(tt.month), tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z)
  533. want := Unix(tt.unix, 0)
  534. if !time.Equal(want) {
  535. t.Errorf("Date(%d, %d, %d, %d, %d, %d, %d, %s) = %v, want %v",
  536. tt.year, tt.month, tt.day, tt.hour, tt.min, tt.sec, tt.nsec, tt.z,
  537. time, want)
  538. }
  539. }
  540. }
  541. // Several ways of getting from
  542. // Fri Nov 18 7:56:35 PST 2011
  543. // to
  544. // Thu Mar 19 7:56:35 PST 2016
  545. var addDateTests = []struct {
  546. years, months, days int
  547. }{
  548. {4, 4, 1},
  549. {3, 16, 1},
  550. {3, 15, 30},
  551. {5, -6, -18 - 30 - 12},
  552. }
  553. func TestAddDate(t *testing.T) {
  554. t0 := Date(2011, 11, 18, 7, 56, 35, 0, UTC)
  555. t1 := Date(2016, 3, 19, 7, 56, 35, 0, UTC)
  556. for _, at := range addDateTests {
  557. time := t0.AddDate(at.years, at.months, at.days)
  558. if !time.Equal(t1) {
  559. t.Errorf("AddDate(%d, %d, %d) = %v, want %v",
  560. at.years, at.months, at.days,
  561. time, t1)
  562. }
  563. }
  564. }
  565. var daysInTests = []struct {
  566. year, month, di int
  567. }{
  568. {2011, 1, 31}, // January, first month, 31 days
  569. {2011, 2, 28}, // February, non-leap year, 28 days
  570. {2012, 2, 29}, // February, leap year, 29 days
  571. {2011, 6, 30}, // June, 30 days
  572. {2011, 12, 31}, // December, last month, 31 days
  573. }
  574. func TestDaysIn(t *testing.T) {
  575. // The daysIn function is not exported.
  576. // Test the daysIn function via the `var DaysIn = daysIn`
  577. // statement in the internal_test.go file.
  578. for _, tt := range daysInTests {
  579. di := DaysIn(Month(tt.month), tt.year)
  580. if di != tt.di {
  581. t.Errorf("got %d; expected %d for %d-%02d",
  582. di, tt.di, tt.year, tt.month)
  583. }
  584. }
  585. }
  586. func TestAddToExactSecond(t *testing.T) {
  587. // Add an amount to the current time to round it up to the next exact second.
  588. // This test checks that the nsec field still lies within the range [0, 999999999].
  589. t1 := Now()
  590. t2 := t1.Add(Second - Duration(t1.Nanosecond()))
  591. sec := (t1.Second() + 1) % 60
  592. if t2.Second() != sec || t2.Nanosecond() != 0 {
  593. t.Errorf("sec = %d, nsec = %d, want sec = %d, nsec = 0", t2.Second(), t2.Nanosecond(), sec)
  594. }
  595. }
  596. func equalTimeAndZone(a, b Time) bool {
  597. aname, aoffset := a.Zone()
  598. bname, boffset := b.Zone()
  599. return a.Equal(b) && aoffset == boffset && aname == bname
  600. }
  601. var gobTests = []Time{
  602. Date(0, 1, 2, 3, 4, 5, 6, UTC),
  603. Date(7, 8, 9, 10, 11, 12, 13, FixedZone("", 0)),
  604. Unix(81985467080890095, 0x76543210), // Time.sec: 0x0123456789ABCDEF
  605. {}, // nil location
  606. Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", 32767*60)),
  607. Date(1, 2, 3, 4, 5, 6, 7, FixedZone("", -32768*60)),
  608. }
  609. func TestTimeGob(t *testing.T) {
  610. var b bytes.Buffer
  611. enc := gob.NewEncoder(&b)
  612. dec := gob.NewDecoder(&b)
  613. for _, tt := range gobTests {
  614. var gobtt Time
  615. if err := enc.Encode(&tt); err != nil {
  616. t.Errorf("%v gob Encode error = %q, want nil", tt, err)
  617. } else if err := dec.Decode(&gobtt); err != nil {
  618. t.Errorf("%v gob Decode error = %q, want nil", tt, err)
  619. } else if !equalTimeAndZone(gobtt, tt) {
  620. t.Errorf("Decoded time = %v, want %v", gobtt, tt)
  621. }
  622. b.Reset()
  623. }
  624. }
  625. var invalidEncodingTests = []struct {
  626. bytes []byte
  627. want string
  628. }{
  629. {[]byte{}, "Time.UnmarshalBinary: no data"},
  630. {[]byte{0, 2, 3}, "Time.UnmarshalBinary: unsupported version"},
  631. {[]byte{1, 2, 3}, "Time.UnmarshalBinary: invalid length"},
  632. }
  633. func TestInvalidTimeGob(t *testing.T) {
  634. for _, tt := range invalidEncodingTests {
  635. var ignored Time
  636. err := ignored.GobDecode(tt.bytes)
  637. if err == nil || err.Error() != tt.want {
  638. t.Errorf("time.GobDecode(%#v) error = %v, want %v", tt.bytes, err, tt.want)
  639. }
  640. err = ignored.UnmarshalBinary(tt.bytes)
  641. if err == nil || err.Error() != tt.want {
  642. t.Errorf("time.UnmarshalBinary(%#v) error = %v, want %v", tt.bytes, err, tt.want)
  643. }
  644. }
  645. }
  646. var notEncodableTimes = []struct {
  647. time Time
  648. want string
  649. }{
  650. {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 1)), "Time.MarshalBinary: zone offset has fractional minute"},
  651. {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -1*60)), "Time.MarshalBinary: unexpected zone offset"},
  652. {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", -32769*60)), "Time.MarshalBinary: unexpected zone offset"},
  653. {Date(0, 1, 2, 3, 4, 5, 6, FixedZone("", 32768*60)), "Time.MarshalBinary: unexpected zone offset"},
  654. }
  655. func TestNotGobEncodableTime(t *testing.T) {
  656. for _, tt := range notEncodableTimes {
  657. _, err := tt.time.GobEncode()
  658. if err == nil || err.Error() != tt.want {
  659. t.Errorf("%v GobEncode error = %v, want %v", tt.time, err, tt.want)
  660. }
  661. _, err = tt.time.MarshalBinary()
  662. if err == nil || err.Error() != tt.want {
  663. t.Errorf("%v MarshalBinary error = %v, want %v", tt.time, err, tt.want)
  664. }
  665. }
  666. }
  667. var jsonTests = []struct {
  668. time Time
  669. json string
  670. }{
  671. {Date(9999, 4, 12, 23, 20, 50, 520*1e6, UTC), `"9999-04-12T23:20:50.52Z"`},
  672. {Date(1996, 12, 19, 16, 39, 57, 0, Local), `"1996-12-19T16:39:57-08:00"`},
  673. {Date(0, 1, 1, 0, 0, 0, 1, FixedZone("", 1*60)), `"0000-01-01T00:00:00.000000001+00:01"`},
  674. }
  675. func TestTimeJSON(t *testing.T) {
  676. for _, tt := range jsonTests {
  677. var jsonTime Time
  678. if jsonBytes, err := json.Marshal(tt.time); err != nil {
  679. t.Errorf("%v json.Marshal error = %v, want nil", tt.time, err)
  680. } else if string(jsonBytes) != tt.json {
  681. t.Errorf("%v JSON = %#q, want %#q", tt.time, string(jsonBytes), tt.json)
  682. } else if err = json.Unmarshal(jsonBytes, &jsonTime); err != nil {
  683. t.Errorf("%v json.Unmarshal error = %v, want nil", tt.time, err)
  684. } else if !equalTimeAndZone(jsonTime, tt.time) {
  685. t.Errorf("Unmarshaled time = %v, want %v", jsonTime, tt.time)
  686. }
  687. }
  688. }
  689. func TestInvalidTimeJSON(t *testing.T) {
  690. var tt Time
  691. err := json.Unmarshal([]byte(`{"now is the time":"buddy"}`), &tt)
  692. _, isParseErr := err.(*ParseError)
  693. if !isParseErr {
  694. t.Errorf("expected *time.ParseError unmarshaling JSON, got %v", err)
  695. }
  696. }
  697. var notJSONEncodableTimes = []struct {
  698. time Time
  699. want string
  700. }{
  701. {Date(10000, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"},
  702. {Date(-1, 1, 1, 0, 0, 0, 0, UTC), "Time.MarshalJSON: year outside of range [0,9999]"},
  703. }
  704. func TestNotJSONEncodableTime(t *testing.T) {
  705. for _, tt := range notJSONEncodableTimes {
  706. _, err := tt.time.MarshalJSON()
  707. if err == nil || err.Error() != tt.want {
  708. t.Errorf("%v MarshalJSON error = %v, want %v", tt.time, err, tt.want)
  709. }
  710. }
  711. }
  712. var parseDurationTests = []struct {
  713. in string
  714. ok bool
  715. want Duration
  716. }{
  717. // simple
  718. {"0", true, 0},
  719. {"5s", true, 5 * Second},
  720. {"30s", true, 30 * Second},
  721. {"1478s", true, 1478 * Second},
  722. // sign
  723. {"-5s", true, -5 * Second},
  724. {"+5s", true, 5 * Second},
  725. {"-0", true, 0},
  726. {"+0", true, 0},
  727. // decimal
  728. {"5.0s", true, 5 * Second},
  729. {"5.6s", true, 5*Second + 600*Millisecond},
  730. {"5.s", true, 5 * Second},
  731. {".5s", true, 500 * Millisecond},
  732. {"1.0s", true, 1 * Second},
  733. {"1.00s", true, 1 * Second},
  734. {"1.004s", true, 1*Second + 4*Millisecond},
  735. {"1.0040s", true, 1*Second + 4*Millisecond},
  736. {"100.00100s", true, 100*Second + 1*Millisecond},
  737. // different units
  738. {"10ns", true, 10 * Nanosecond},
  739. {"11us", true, 11 * Microsecond},
  740. {"12µs", true, 12 * Microsecond}, // U+00B5
  741. {"12μs", true, 12 * Microsecond}, // U+03BC
  742. {"13ms", true, 13 * Millisecond},
  743. {"14s", true, 14 * Second},
  744. {"15m", true, 15 * Minute},
  745. {"16h", true, 16 * Hour},
  746. // composite durations
  747. {"3h30m", true, 3*Hour + 30*Minute},
  748. {"10.5s4m", true, 4*Minute + 10*Second + 500*Millisecond},
  749. {"-2m3.4s", true, -(2*Minute + 3*Second + 400*Millisecond)},
  750. {"1h2m3s4ms5us6ns", true, 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond},
  751. {"39h9m14.425s", true, 39*Hour + 9*Minute + 14*Second + 425*Millisecond},
  752. // large value
  753. {"52763797000ns", true, 52763797000 * Nanosecond},
  754. // more than 9 digits after decimal point, see http://golang.org/issue/6617
  755. {"0.3333333333333333333h", true, 20 * Minute},
  756. // errors
  757. {"", false, 0},
  758. {"3", false, 0},
  759. {"-", false, 0},
  760. {"s", false, 0},
  761. {".", false, 0},
  762. {"-.", false, 0},
  763. {".s", false, 0},
  764. {"+.s", false, 0},
  765. {"3000000h", false, 0}, // overflow
  766. }
  767. func TestParseDuration(t *testing.T) {
  768. for _, tc := range parseDurationTests {
  769. d, err := ParseDuration(tc.in)
  770. if tc.ok && (err != nil || d != tc.want) {
  771. t.Errorf("ParseDuration(%q) = %v, %v, want %v, nil", tc.in, d, err, tc.want)
  772. } else if !tc.ok && err == nil {
  773. t.Errorf("ParseDuration(%q) = _, nil, want _, non-nil", tc.in)
  774. }
  775. }
  776. }
  777. func TestParseDurationRoundTrip(t *testing.T) {
  778. for i := 0; i < 100; i++ {
  779. // Resolutions finer than milliseconds will result in
  780. // imprecise round-trips.
  781. d0 := Duration(rand.Int31()) * Millisecond
  782. s := d0.String()
  783. d1, err := ParseDuration(s)
  784. if err != nil || d0 != d1 {
  785. t.Errorf("round-trip failed: %d => %q => %d, %v", d0, s, d1, err)
  786. }
  787. }
  788. }
  789. // golang.org/issue/4622
  790. func TestLocationRace(t *testing.T) {
  791. ResetLocalOnceForTest() // reset the Once to trigger the race
  792. c := make(chan string, 1)
  793. go func() {
  794. c <- Now().String()
  795. }()
  796. Now().String()
  797. <-c
  798. Sleep(100 * Millisecond)
  799. // Back to Los Angeles for subsequent tests:
  800. ForceUSPacificForTesting()
  801. }
  802. var (
  803. t Time
  804. u int64
  805. )
  806. var mallocTest = []struct {
  807. count int
  808. desc string
  809. fn func()
  810. }{
  811. {0, `time.Now()`, func() { t = Now() }},
  812. {0, `time.Now().UnixNano()`, func() { u = Now().UnixNano() }},
  813. }
  814. func TestCountMallocs(t *testing.T) {
  815. if testing.Short() {
  816. t.Skip("skipping malloc count in short mode")
  817. }
  818. if runtime.GOMAXPROCS(0) > 1 {
  819. t.Skip("skipping; GOMAXPROCS>1")
  820. }
  821. for _, mt := range mallocTest {
  822. allocs := int(testing.AllocsPerRun(100, mt.fn))
  823. if allocs > mt.count {
  824. t.Errorf("%s: %d allocs, want %d", mt.desc, allocs, mt.count)
  825. }
  826. }
  827. }
  828. func TestLoadFixed(t *testing.T) {
  829. // Issue 4064: handle locations without any zone transitions.
  830. loc, err := LoadLocation("Etc/GMT+1")
  831. if err != nil {
  832. t.Fatal(err)
  833. }
  834. // The tzdata name Etc/GMT+1 uses "east is negative",
  835. // but Go and most other systems use "east is positive".
  836. // So GMT+1 corresponds to -3600 in the Go zone, not +3600.
  837. name, offset := Now().In(loc).Zone()
  838. if name != "GMT+1" || offset != -1*60*60 {
  839. t.Errorf("Now().In(loc).Zone() = %q, %d, want %q, %d", name, offset, "GMT+1", -1*60*60)
  840. }
  841. }
  842. const (
  843. minDuration Duration = -1 << 63
  844. maxDuration Duration = 1<<63 - 1
  845. )
  846. var subTests = []struct {
  847. t Time
  848. u Time
  849. d Duration
  850. }{
  851. {Time{}, Time{}, Duration(0)},
  852. {Date(2009, 11, 23, 0, 0, 0, 1, UTC), Date(2009, 11, 23, 0, 0, 0, 0, UTC), Duration(1)},
  853. {Date(2009, 11, 23, 0, 0, 0, 0, UTC), Date(2009, 11, 24, 0, 0, 0, 0, UTC), -24 * Hour},
  854. {Date(2009, 11, 24, 0, 0, 0, 0, UTC), Date(2009, 11, 23, 0, 0, 0, 0, UTC), 24 * Hour},
  855. {Date(-2009, 11, 24, 0, 0, 0, 0, UTC), Date(-2009, 11, 23, 0, 0, 0, 0, UTC), 24 * Hour},
  856. {Time{}, Date(2109, 11, 23, 0, 0, 0, 0, UTC), Duration(minDuration)},
  857. {Date(2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, Duration(maxDuration)},
  858. {Time{}, Date(-2109, 11, 23, 0, 0, 0, 0, UTC), Duration(maxDuration)},
  859. {Date(-2109, 11, 23, 0, 0, 0, 0, UTC), Time{}, Duration(minDuration)},
  860. {Date(2290, 1, 1, 0, 0, 0, 0, UTC), Date(2000, 1, 1, 0, 0, 0, 0, UTC), 290*365*24*Hour + 71*24*Hour},
  861. {Date(2300, 1, 1, 0, 0, 0, 0, UTC), Date(2000, 1, 1, 0, 0, 0, 0, UTC), Duration(maxDuration)},
  862. {Date(2000, 1, 1, 0, 0, 0, 0, UTC), Date(2290, 1, 1, 0, 0, 0, 0, UTC), -290*365*24*Hour - 71*24*Hour},
  863. {Date(2000, 1, 1, 0, 0, 0, 0, UTC), Date(2300, 1, 1, 0, 0, 0, 0, UTC), Duration(minDuration)},
  864. }
  865. func TestSub(t *testing.T) {
  866. for i, st := range subTests {
  867. got := st.t.Sub(st.u)
  868. if got != st.d {
  869. t.Errorf("#%d: Sub(%v, %v): got %v; want %v", i, st.t, st.u, got, st.d)
  870. }
  871. }
  872. }
  873. var nsDurationTests = []struct {
  874. d Duration
  875. want int64
  876. }{
  877. {Duration(-1000), -1000},
  878. {Duration(-1), -1},
  879. {Duration(1), 1},
  880. {Duration(1000), 1000},
  881. }
  882. func TestDurationNanoseconds(t *testing.T) {
  883. for _, tt := range nsDurationTests {
  884. if got := tt.d.Nanoseconds(); got != tt.want {
  885. t.Errorf("d.Nanoseconds() = %d; want: %d", got, tt.want)
  886. }
  887. }
  888. }
  889. var minDurationTests = []struct {
  890. d Duration
  891. want float64
  892. }{
  893. {Duration(-60000000000), -1},
  894. {Duration(-1), -1 / 60e9},
  895. {Duration(1), 1 / 60e9},
  896. {Duration(60000000000), 1},
  897. }
  898. func TestDurationMinutes(t *testing.T) {
  899. for _, tt := range minDurationTests {
  900. if got := tt.d.Minutes(); got != tt.want {
  901. t.Errorf("d.Minutes() = %g; want: %g", got, tt.want)
  902. }
  903. }
  904. }
  905. var hourDurationTests = []struct {
  906. d Duration
  907. want float64
  908. }{
  909. {Duration(-3600000000000), -1},
  910. {Duration(-1), -1 / 3600e9},
  911. {Duration(1), 1 / 3600e9},
  912. {Duration(3600000000000), 1},
  913. }
  914. func TestDurationHours(t *testing.T) {
  915. for _, tt := range hourDurationTests {
  916. if got := tt.d.Hours(); got != tt.want {
  917. t.Errorf("d.Hours() = %g; want: %g", got, tt.want)
  918. }
  919. }
  920. }
  921. func BenchmarkNow(b *testing.B) {
  922. for i := 0; i < b.N; i++ {
  923. t = Now()
  924. }
  925. }
  926. func BenchmarkNowUnixNano(b *testing.B) {
  927. for i := 0; i < b.N; i++ {
  928. u = Now().UnixNano()
  929. }
  930. }
  931. func BenchmarkFormat(b *testing.B) {
  932. t := Unix(1265346057, 0)
  933. for i := 0; i < b.N; i++ {
  934. t.Format("Mon Jan 2 15:04:05 2006")
  935. }
  936. }
  937. func BenchmarkFormatNow(b *testing.B) {
  938. // Like BenchmarkFormat, but easier, because the time zone
  939. // lookup cache is optimized for the present.
  940. t := Now()
  941. for i := 0; i < b.N; i++ {
  942. t.Format("Mon Jan 2 15:04:05 2006")
  943. }
  944. }
  945. func BenchmarkParse(b *testing.B) {
  946. for i := 0; i < b.N; i++ {
  947. Parse(ANSIC, "Mon Jan 2 15:04:05 2006")
  948. }
  949. }
  950. func BenchmarkHour(b *testing.B) {
  951. t := Now()
  952. for i := 0; i < b.N; i++ {
  953. _ = t.Hour()
  954. }
  955. }
  956. func BenchmarkSecond(b *testing.B) {
  957. t := Now()
  958. for i := 0; i < b.N; i++ {
  959. _ = t.Second()
  960. }
  961. }
  962. func BenchmarkYear(b *testing.B) {
  963. t := Now()
  964. for i := 0; i < b.N; i++ {
  965. _ = t.Year()
  966. }
  967. }
  968. func BenchmarkDay(b *testing.B) {
  969. t := Now()
  970. for i := 0; i < b.N; i++ {
  971. _ = t.Day()
  972. }
  973. }