timezone_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. )
  8. func TestNow(t *testing.T) {
  9. tz := "Europe/Paris"
  10. now := Now(tz)
  11. if now.Location().String() != tz {
  12. t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), tz)
  13. }
  14. }
  15. func TestNowWithInvalidTimezone(t *testing.T) {
  16. tz := "Invalid Timezone"
  17. expected := time.Local
  18. now := Now(tz)
  19. if now.Location().String() != expected.String() {
  20. t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), expected)
  21. }
  22. }
  23. func TestConvertTimeWithNoTimezoneInformation(t *testing.T) {
  24. tz := "Canada/Pacific"
  25. input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.FixedZone("", 0))
  26. output := Convert(tz, input)
  27. if output.Location().String() != tz {
  28. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  29. }
  30. hours, minutes, secs := output.Clock()
  31. if hours != 14 || minutes != 2 || secs != 3 {
  32. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  33. }
  34. }
  35. func TestConvertTimeWithDifferentTimezone(t *testing.T) {
  36. tz := "Canada/Central"
  37. input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.UTC)
  38. output := Convert(tz, input)
  39. if output.Location().String() != tz {
  40. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  41. }
  42. hours, minutes, secs := output.Clock()
  43. if hours != 8 || minutes != 2 || secs != 3 {
  44. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  45. }
  46. }
  47. func TestConvertTimeWithIdenticalTimezone(t *testing.T) {
  48. tz := "Canada/Central"
  49. loc, _ := time.LoadLocation(tz)
  50. input := time.Date(2018, 3, 1, 14, 2, 3, 0, loc)
  51. output := Convert(tz, input)
  52. if output.Location().String() != tz {
  53. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  54. }
  55. hours, minutes, secs := output.Clock()
  56. if hours != 14 || minutes != 2 || secs != 3 {
  57. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  58. }
  59. }