timezone_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "testing"
  6. "time"
  7. // Make sure these tests pass when the timezone database is not installed on the host system.
  8. _ "time/tzdata"
  9. )
  10. func TestNow(t *testing.T) {
  11. tz := "Europe/Paris"
  12. now := Now(tz)
  13. if now.Location().String() != tz {
  14. t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), tz)
  15. }
  16. }
  17. func TestNowWithInvalidTimezone(t *testing.T) {
  18. tz := "Invalid Timezone"
  19. expected := time.Local
  20. now := Now(tz)
  21. if now.Location().String() != expected.String() {
  22. t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), expected)
  23. }
  24. }
  25. func TestConvertTimeWithNoTimezoneInformation(t *testing.T) {
  26. tz := "Canada/Pacific"
  27. input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.FixedZone("", 0))
  28. output := Convert(tz, input)
  29. if output.Location().String() != tz {
  30. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  31. }
  32. hours, minutes, secs := output.Clock()
  33. if hours != 14 || minutes != 2 || secs != 3 {
  34. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  35. }
  36. }
  37. func TestConvertTimeWithDifferentTimezone(t *testing.T) {
  38. tz := "Canada/Central"
  39. input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.UTC)
  40. output := Convert(tz, input)
  41. if output.Location().String() != tz {
  42. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  43. }
  44. hours, minutes, secs := output.Clock()
  45. if hours != 8 || minutes != 2 || secs != 3 {
  46. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  47. }
  48. }
  49. func TestConvertTimeWithIdenticalTimezone(t *testing.T) {
  50. tz := "Canada/Central"
  51. loc, _ := time.LoadLocation(tz)
  52. input := time.Date(2018, 3, 1, 14, 2, 3, 0, loc)
  53. output := Convert(tz, input)
  54. if output.Location().String() != tz {
  55. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  56. }
  57. hours, minutes, secs := output.Clock()
  58. if hours != 14 || minutes != 2 || secs != 3 {
  59. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  60. }
  61. }
  62. func TestConvertPostgresDateTimeWithNegativeTimezoneOffset(t *testing.T) {
  63. tz := "US/Eastern"
  64. input := time.Date(0, 1, 1, 0, 0, 0, 0, time.FixedZone("", -5))
  65. output := Convert(tz, input)
  66. if output.Location().String() != tz {
  67. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  68. }
  69. if year := output.Year(); year != 0 {
  70. t.Fatalf(`Unexpected year, got %d instead of 0`, year)
  71. }
  72. }