timezone_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package timezone // import "miniflux.app/timezone"
  5. import (
  6. "testing"
  7. "time"
  8. )
  9. func TestNow(t *testing.T) {
  10. tz := "Europe/Paris"
  11. now := Now(tz)
  12. if now.Location().String() != tz {
  13. t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), tz)
  14. }
  15. }
  16. func TestNowWithInvalidTimezone(t *testing.T) {
  17. tz := "Invalid Timezone"
  18. expected := time.Local
  19. now := Now(tz)
  20. if now.Location().String() != expected.String() {
  21. t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), expected)
  22. }
  23. }
  24. func TestConvertTimeWithNoTimezoneInformation(t *testing.T) {
  25. tz := "Canada/Pacific"
  26. input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.FixedZone("", 0))
  27. output := Convert(tz, input)
  28. if output.Location().String() != tz {
  29. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  30. }
  31. hours, minutes, secs := output.Clock()
  32. if hours != 14 || minutes != 2 || secs != 3 {
  33. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  34. }
  35. }
  36. func TestConvertTimeWithDifferentTimezone(t *testing.T) {
  37. tz := "Canada/Central"
  38. input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.UTC)
  39. output := Convert(tz, input)
  40. if output.Location().String() != tz {
  41. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  42. }
  43. hours, minutes, secs := output.Clock()
  44. if hours != 8 || minutes != 2 || secs != 3 {
  45. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  46. }
  47. }
  48. func TestConvertTimeWithIdenticalTimezone(t *testing.T) {
  49. tz := "Canada/Central"
  50. loc, _ := time.LoadLocation(tz)
  51. input := time.Date(2018, 3, 1, 14, 2, 3, 0, loc)
  52. output := Convert(tz, input)
  53. if output.Location().String() != tz {
  54. t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
  55. }
  56. hours, minutes, secs := output.Clock()
  57. if hours != 14 || minutes != 2 || secs != 3 {
  58. t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
  59. }
  60. }