timezone_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }