timezone_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package timezone // import "miniflux.app/v2/internal/timezone"
  4. import (
  5. "slices"
  6. "testing"
  7. "time"
  8. // Make sure these tests pass when the timezone database is not installed on the host system.
  9. _ "time/tzdata"
  10. )
  11. func TestNow(t *testing.T) {
  12. tz := "Europe/Paris"
  13. now := Now(tz)
  14. if now.Location().String() != tz {
  15. t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), tz)
  16. }
  17. }
  18. func TestNowWithInvalidTimezone(t *testing.T) {
  19. tz := "Invalid Timezone"
  20. expected := time.Local
  21. now := Now(tz)
  22. if now.Location().String() != expected.String() {
  23. t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), expected)
  24. }
  25. }
  26. func TestConvertTimeWithNoTimezoneInformation(t *testing.T) {
  27. tz := "Canada/Pacific"
  28. input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.FixedZone("", 0))
  29. output := Convert(tz, input)
  30. if output.Location().String() != tz {
  31. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  32. }
  33. hours, minutes, secs := output.Clock()
  34. if hours != 14 || minutes != 2 || secs != 3 {
  35. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  36. }
  37. }
  38. func TestConvertTimeWithDifferentTimezone(t *testing.T) {
  39. tz := "Canada/Central"
  40. input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.UTC)
  41. output := Convert(tz, input)
  42. if output.Location().String() != tz {
  43. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  44. }
  45. hours, minutes, secs := output.Clock()
  46. if hours != 8 || minutes != 2 || secs != 3 {
  47. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  48. }
  49. }
  50. func TestConvertTimeWithIdenticalTimezone(t *testing.T) {
  51. tz := "Canada/Central"
  52. loc, _ := time.LoadLocation(tz)
  53. input := time.Date(2018, 3, 1, 14, 2, 3, 0, loc)
  54. output := Convert(tz, input)
  55. if output.Location().String() != tz {
  56. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  57. }
  58. hours, minutes, secs := output.Clock()
  59. if hours != 14 || minutes != 2 || secs != 3 {
  60. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  61. }
  62. }
  63. func TestConvertTimeWithEquivalentTimezone(t *testing.T) {
  64. tz := "UTC"
  65. // FixedZone creates a distinct pointer that has the same name as the cached location.
  66. // The old pointer comparison would treat these as different, calling t.In() unnecessarily.
  67. loc := time.FixedZone("UTC", 0)
  68. input := time.Date(2024, 6, 15, 12, 0, 0, 0, loc)
  69. output := Convert(tz, input)
  70. if output.Location() != loc {
  71. t.Fatal("Convert replaced the location even though timezone names match")
  72. }
  73. }
  74. func TestConvertPostgresDateTimeWithNegativeTimezoneOffset(t *testing.T) {
  75. tz := "US/Eastern"
  76. input := time.Date(0, 1, 1, 0, 0, 0, 0, time.FixedZone("", -5))
  77. output := Convert(tz, input)
  78. if output.Location().String() != tz {
  79. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  80. }
  81. if year := output.Year(); year != 0 {
  82. t.Fatalf(`Unexpected year, got %d instead of 0`, year)
  83. }
  84. }
  85. func TestIsValid(t *testing.T) {
  86. validTZ := []string{
  87. "Antarctica/Davis",
  88. "GMT",
  89. "UTC",
  90. }
  91. for _, tz := range validTZ {
  92. if !IsValid(tz) {
  93. t.Fatalf(`Timezone %q should be valid and it's not`, tz)
  94. }
  95. }
  96. invalidTZ := []string{
  97. "MAP",
  98. "Europe/Fronce",
  99. }
  100. for _, tz := range invalidTZ {
  101. if IsValid(tz) {
  102. t.Fatalf(`Timezone %q should be invalid and it's not`, tz)
  103. }
  104. }
  105. }
  106. func TestAvailableTimezones(t *testing.T) {
  107. var got []string
  108. for tz := range AvailableTimezones() {
  109. got = append(got, tz)
  110. }
  111. if !slices.Equal(got, timezones) {
  112. t.Fatalf("available timezones differ from source slice: expected %d entries, got %d", len(timezones), len(got))
  113. }
  114. }