zoneinfo_test.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Copyright 2014 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. "errors"
  7. "fmt"
  8. "os"
  9. "reflect"
  10. "testing"
  11. "time"
  12. )
  13. func init() {
  14. if time.ZoneinfoForTesting() != nil {
  15. panic(fmt.Errorf("zoneinfo initialized before first LoadLocation"))
  16. }
  17. }
  18. func TestEnvVarUsage(t *testing.T) {
  19. time.ResetZoneinfoForTesting()
  20. const testZoneinfo = "foo.zip"
  21. const env = "ZONEINFO"
  22. t.Setenv(env, testZoneinfo)
  23. // Result isn't important, we're testing the side effect of this command
  24. time.LoadLocation("Asia/Jerusalem")
  25. defer time.ResetZoneinfoForTesting()
  26. if zoneinfo := time.ZoneinfoForTesting(); testZoneinfo != *zoneinfo {
  27. t.Errorf("zoneinfo does not match env variable: got %q want %q", *zoneinfo, testZoneinfo)
  28. }
  29. }
  30. func TestBadLocationErrMsg(t *testing.T) {
  31. time.ResetZoneinfoForTesting()
  32. loc := "Asia/SomethingNotExist"
  33. want := errors.New("unknown time zone " + loc)
  34. _, err := time.LoadLocation(loc)
  35. if err.Error() != want.Error() {
  36. t.Errorf("LoadLocation(%q) error = %v; want %v", loc, err, want)
  37. }
  38. }
  39. func TestLoadLocationValidatesNames(t *testing.T) {
  40. time.ResetZoneinfoForTesting()
  41. const env = "ZONEINFO"
  42. t.Setenv(env, "")
  43. bad := []string{
  44. "/usr/foo/Foo",
  45. "\\UNC\foo",
  46. "..",
  47. "a..",
  48. }
  49. for _, v := range bad {
  50. _, err := time.LoadLocation(v)
  51. if err != time.ErrLocation {
  52. t.Errorf("LoadLocation(%q) error = %v; want ErrLocation", v, err)
  53. }
  54. }
  55. }
  56. func TestVersion3(t *testing.T) {
  57. t.Skip("gccgo does not use the zip file")
  58. time.ForceZipFileForTesting(true)
  59. defer time.ForceZipFileForTesting(false)
  60. _, err := time.LoadLocation("Asia/Jerusalem")
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. }
  65. // Test that we get the correct results for times before the first
  66. // transition time. To do this we explicitly check early dates in a
  67. // couple of specific timezones.
  68. func TestFirstZone(t *testing.T) {
  69. t.Skip("gccgo does not use the zip file")
  70. time.ForceZipFileForTesting(true)
  71. defer time.ForceZipFileForTesting(false)
  72. const format = "Mon, 02 Jan 2006 15:04:05 -0700 (MST)"
  73. var tests = []struct {
  74. zone string
  75. unix int64
  76. want1 string
  77. want2 string
  78. }{
  79. {
  80. "PST8PDT",
  81. -1633269601,
  82. "Sun, 31 Mar 1918 01:59:59 -0800 (PST)",
  83. "Sun, 31 Mar 1918 03:00:00 -0700 (PDT)",
  84. },
  85. {
  86. "Pacific/Fakaofo",
  87. 1325242799,
  88. "Thu, 29 Dec 2011 23:59:59 -1100 (-11)",
  89. "Sat, 31 Dec 2011 00:00:00 +1300 (+13)",
  90. },
  91. }
  92. for _, test := range tests {
  93. z, err := time.LoadLocation(test.zone)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. s := time.Unix(test.unix, 0).In(z).Format(format)
  98. if s != test.want1 {
  99. t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want1)
  100. }
  101. s = time.Unix(test.unix+1, 0).In(z).Format(format)
  102. if s != test.want2 {
  103. t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want2)
  104. }
  105. }
  106. }
  107. func TestLocationNames(t *testing.T) {
  108. if time.Local.String() != "Local" {
  109. t.Errorf(`invalid Local location name: got %q want "Local"`, time.Local)
  110. }
  111. if time.UTC.String() != "UTC" {
  112. t.Errorf(`invalid UTC location name: got %q want "UTC"`, time.UTC)
  113. }
  114. }
  115. func TestLoadLocationFromTZData(t *testing.T) {
  116. t.Skip("gccgo does not use the zip file")
  117. time.ForceZipFileForTesting(true)
  118. defer time.ForceZipFileForTesting(false)
  119. const locationName = "Asia/Jerusalem"
  120. reference, err := time.LoadLocation(locationName)
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. tzinfo, err := time.LoadTzinfo(locationName, time.OrigZoneSources[len(time.OrigZoneSources)-1])
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. sample, err := time.LoadLocationFromTZData(locationName, tzinfo)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. if !reflect.DeepEqual(reference, sample) {
  133. t.Errorf("return values of LoadLocationFromTZData and LoadLocation don't match")
  134. }
  135. }
  136. // Issue 30099.
  137. func TestEarlyLocation(t *testing.T) {
  138. t.Skip("gccgo does not use the zip file")
  139. time.ForceZipFileForTesting(true)
  140. defer time.ForceZipFileForTesting(false)
  141. const locName = "America/New_York"
  142. loc, err := time.LoadLocation(locName)
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. d := time.Date(1900, time.January, 1, 0, 0, 0, 0, loc)
  147. tzName, tzOffset := d.Zone()
  148. if want := "EST"; tzName != want {
  149. t.Errorf("Zone name == %s, want %s", tzName, want)
  150. }
  151. if want := -18000; tzOffset != want {
  152. t.Errorf("Zone offset == %d, want %d", tzOffset, want)
  153. }
  154. }
  155. func TestMalformedTZData(t *testing.T) {
  156. // The goal here is just that malformed tzdata results in an error, not a panic.
  157. issue29437 := "TZif\x00000000000000000\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0000"
  158. _, err := time.LoadLocationFromTZData("abc", []byte(issue29437))
  159. if err == nil {
  160. t.Error("expected error, got none")
  161. }
  162. }
  163. var slimTests = []struct {
  164. zoneName string
  165. fileName string
  166. date func(*time.Location) time.Time
  167. wantName string
  168. wantOffset int
  169. }{
  170. {
  171. // 2020b slim tzdata for Europe/Berlin.
  172. zoneName: "Europe/Berlin",
  173. fileName: "2020b_Europe_Berlin",
  174. date: func(loc *time.Location) time.Time { return time.Date(2020, time.October, 29, 15, 30, 0, 0, loc) },
  175. wantName: "CET",
  176. wantOffset: 3600,
  177. },
  178. {
  179. // 2021a slim tzdata for America/Nuuk.
  180. zoneName: "America/Nuuk",
  181. fileName: "2021a_America_Nuuk",
  182. date: func(loc *time.Location) time.Time { return time.Date(2020, time.October, 29, 15, 30, 0, 0, loc) },
  183. wantName: "-03",
  184. wantOffset: -10800,
  185. },
  186. {
  187. // 2021a slim tzdata for Asia/Gaza.
  188. zoneName: "Asia/Gaza",
  189. fileName: "2021a_Asia_Gaza",
  190. date: func(loc *time.Location) time.Time { return time.Date(2020, time.October, 29, 15, 30, 0, 0, loc) },
  191. wantName: "EET",
  192. wantOffset: 7200,
  193. },
  194. {
  195. // 2021a slim tzdata for Europe/Dublin.
  196. zoneName: "Europe/Dublin",
  197. fileName: "2021a_Europe_Dublin",
  198. date: func(loc *time.Location) time.Time { return time.Date(2021, time.April, 2, 11, 12, 13, 0, loc) },
  199. wantName: "IST",
  200. wantOffset: 3600,
  201. },
  202. }
  203. func TestLoadLocationFromTZDataSlim(t *testing.T) {
  204. for _, test := range slimTests {
  205. tzData, err := os.ReadFile("testdata/" + test.fileName)
  206. if err != nil {
  207. t.Error(err)
  208. continue
  209. }
  210. reference, err := time.LoadLocationFromTZData(test.zoneName, tzData)
  211. if err != nil {
  212. t.Error(err)
  213. continue
  214. }
  215. d := test.date(reference)
  216. tzName, tzOffset := d.Zone()
  217. if tzName != test.wantName {
  218. t.Errorf("Zone name == %s, want %s", tzName, test.wantName)
  219. }
  220. if tzOffset != test.wantOffset {
  221. t.Errorf("Zone offset == %d, want %d", tzOffset, test.wantOffset)
  222. }
  223. }
  224. }
  225. func TestTzset(t *testing.T) {
  226. for _, test := range []struct {
  227. inStr string
  228. inEnd int64
  229. inSec int64
  230. name string
  231. off int
  232. start int64
  233. end int64
  234. isDST bool
  235. ok bool
  236. }{
  237. {"", 0, 0, "", 0, 0, 0, false, false},
  238. {"PST8PDT,M3.2.0,M11.1.0", 0, 2159200800, "PDT", -7 * 60 * 60, 2152173600, 2172733200, true, true},
  239. {"PST8PDT,M3.2.0,M11.1.0", 0, 2152173599, "PST", -8 * 60 * 60, 2145916800, 2152173600, false, true},
  240. {"PST8PDT,M3.2.0,M11.1.0", 0, 2152173600, "PDT", -7 * 60 * 60, 2152173600, 2172733200, true, true},
  241. {"PST8PDT,M3.2.0,M11.1.0", 0, 2152173601, "PDT", -7 * 60 * 60, 2152173600, 2172733200, true, true},
  242. {"PST8PDT,M3.2.0,M11.1.0", 0, 2172733199, "PDT", -7 * 60 * 60, 2152173600, 2172733200, true, true},
  243. {"PST8PDT,M3.2.0,M11.1.0", 0, 2172733200, "PST", -8 * 60 * 60, 2172733200, 2177452800, false, true},
  244. {"PST8PDT,M3.2.0,M11.1.0", 0, 2172733201, "PST", -8 * 60 * 60, 2172733200, 2177452800, false, true},
  245. } {
  246. name, off, start, end, isDST, ok := time.Tzset(test.inStr, test.inEnd, test.inSec)
  247. if name != test.name || off != test.off || start != test.start || end != test.end || isDST != test.isDST || ok != test.ok {
  248. t.Errorf("tzset(%q, %d, %d) = %q, %d, %d, %d, %t, %t, want %q, %d, %d, %d, %t, %t", test.inStr, test.inEnd, test.inSec, name, off, start, end, isDST, ok, test.name, test.off, test.start, test.end, test.isDST, test.ok)
  249. }
  250. }
  251. }
  252. func TestTzsetName(t *testing.T) {
  253. for _, test := range []struct {
  254. in string
  255. name string
  256. out string
  257. ok bool
  258. }{
  259. {"", "", "", false},
  260. {"X", "", "", false},
  261. {"PST", "PST", "", true},
  262. {"PST8PDT", "PST", "8PDT", true},
  263. {"PST-08", "PST", "-08", true},
  264. {"<A+B>+08", "A+B", "+08", true},
  265. } {
  266. name, out, ok := time.TzsetName(test.in)
  267. if name != test.name || out != test.out || ok != test.ok {
  268. t.Errorf("tzsetName(%q) = %q, %q, %t, want %q, %q, %t", test.in, name, out, ok, test.name, test.out, test.ok)
  269. }
  270. }
  271. }
  272. func TestTzsetOffset(t *testing.T) {
  273. for _, test := range []struct {
  274. in string
  275. off int
  276. out string
  277. ok bool
  278. }{
  279. {"", 0, "", false},
  280. {"X", 0, "", false},
  281. {"+", 0, "", false},
  282. {"+08", 8 * 60 * 60, "", true},
  283. {"-01:02:03", -1*60*60 - 2*60 - 3, "", true},
  284. {"01", 1 * 60 * 60, "", true},
  285. {"100", 100 * 60 * 60, "", true},
  286. {"1000", 0, "", false},
  287. {"8PDT", 8 * 60 * 60, "PDT", true},
  288. } {
  289. off, out, ok := time.TzsetOffset(test.in)
  290. if off != test.off || out != test.out || ok != test.ok {
  291. t.Errorf("tzsetName(%q) = %d, %q, %t, want %d, %q, %t", test.in, off, out, ok, test.off, test.out, test.ok)
  292. }
  293. }
  294. }
  295. func TestTzsetRule(t *testing.T) {
  296. for _, test := range []struct {
  297. in string
  298. r time.Rule
  299. out string
  300. ok bool
  301. }{
  302. {"", time.Rule{}, "", false},
  303. {"X", time.Rule{}, "", false},
  304. {"J10", time.Rule{Kind: time.RuleJulian, Day: 10, Time: 2 * 60 * 60}, "", true},
  305. {"20", time.Rule{Kind: time.RuleDOY, Day: 20, Time: 2 * 60 * 60}, "", true},
  306. {"M1.2.3", time.Rule{Kind: time.RuleMonthWeekDay, Mon: 1, Week: 2, Day: 3, Time: 2 * 60 * 60}, "", true},
  307. {"30/03:00:00", time.Rule{Kind: time.RuleDOY, Day: 30, Time: 3 * 60 * 60}, "", true},
  308. {"M4.5.6/03:00:00", time.Rule{Kind: time.RuleMonthWeekDay, Mon: 4, Week: 5, Day: 6, Time: 3 * 60 * 60}, "", true},
  309. {"M4.5.7/03:00:00", time.Rule{}, "", false},
  310. {"M4.5.6/-04", time.Rule{Kind: time.RuleMonthWeekDay, Mon: 4, Week: 5, Day: 6, Time: -4 * 60 * 60}, "", true},
  311. } {
  312. r, out, ok := time.TzsetRule(test.in)
  313. if r != test.r || out != test.out || ok != test.ok {
  314. t.Errorf("tzsetName(%q) = %#v, %q, %t, want %#v, %q, %t", test.in, r, out, ok, test.r, test.out, test.ok)
  315. }
  316. }
  317. }