4
0

timezone.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package date // import "miniflux.app/v2/internal/reader/date"
  4. import (
  5. "strings"
  6. "time"
  7. )
  8. // The offsets of real time zones go from UTC−12:00 to UTC+14:00, and PostgreSQL
  9. // rejects dates outside of this range with "time zone displacement out of range".
  10. // See https://en.wikipedia.org/wiki/List_of_UTC_offsets
  11. const (
  12. minOffset = -12 * 60 * 60
  13. maxOffset = 14 * 60 * 60
  14. )
  15. // Dates are parsed in fixed locations, never in time.Local, so that the result
  16. // does not depend on the time zone of the server. A location is nil when the
  17. // time zone database is not available.
  18. var (
  19. losAngelesLocation, _ = time.LoadLocation("America/Los_Angeles")
  20. newYorkLocation, _ = time.LoadLocation("America/New_York")
  21. brusselsLocation, _ = time.LoadLocation("Europe/Brussels")
  22. )
  23. type namedLocation struct {
  24. name string
  25. location *time.Location
  26. }
  27. // timezoneAbbreviations lists the time zone abbreviations that are resolved in
  28. // a location that uses them, when a date ends with one. Go gives any other
  29. // abbreviation, except UTC and GMT, a zero offset.
  30. var timezoneAbbreviations = [...]namedLocation{
  31. {"PST", losAngelesLocation},
  32. {"PDT", losAngelesLocation},
  33. {"EST", newYorkLocation},
  34. {"EDT", newYorkLocation},
  35. }
  36. // ianaTimezones lists the IANA time zone names found in some feed dates.
  37. var ianaTimezones = [...]namedLocation{
  38. {"America/Los_Angeles", losAngelesLocation},
  39. {"Europe/Brussels", brusselsLocation},
  40. }
  41. // replaceIANATimezone replaces an IANA time zone name, which no layout accepts,
  42. // with "UTC", which every layout with a time zone abbreviation accepts. It
  43. // returns the location the parsed wall clock time belongs to, so that daylight
  44. // saving time is taken into account, or nil when the value has no such name or
  45. // the time zone database is not available.
  46. func replaceIANATimezone(value string) (string, *time.Location) {
  47. for _, timezone := range ianaTimezones {
  48. if strings.Contains(value, timezone.name) {
  49. return strings.Replace(value, timezone.name, "UTC", 1), timezone.location
  50. }
  51. }
  52. return value, nil
  53. }
  54. // timezoneLocation returns the location to parse a date in. Go resolves a time
  55. // zone abbreviation only with the zones of that location.
  56. func timezoneLocation(value string) *time.Location {
  57. for _, timezone := range timezoneAbbreviations {
  58. if timezone.location != nil && strings.HasSuffix(value, timezone.name) {
  59. return timezone.location
  60. }
  61. }
  62. return time.UTC
  63. }
  64. // withWallClockIn returns the time with the same wall clock time in location.
  65. func withWallClockIn(t time.Time, location *time.Location) time.Time {
  66. return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), location)
  67. }
  68. // padShortOffset pads a numeric offset written with three digits at the end of
  69. // a date, such as the "-700" of "Tue, Feb 16 2016 23:16:00 -700", to the four
  70. // digits that Go accepts.
  71. func padShortOffset(value string) string {
  72. offsetStart := len(value) - 4
  73. if offsetStart < 1 || value[offsetStart-1] != ' ' || (value[offsetStart] != '+' && value[offsetStart] != '-') {
  74. return value
  75. }
  76. for _, digit := range value[offsetStart+1:] {
  77. if digit < '0' || digit > '9' {
  78. return value
  79. }
  80. }
  81. return value[:offsetStart+1] + "0" + value[offsetStart+1:]
  82. }
  83. // checkTimezoneRange converts t to UTC when its offset is out of range.
  84. func checkTimezoneRange(t time.Time) time.Time {
  85. if _, offset := t.Zone(); offset < minOffset || offset > maxOffset {
  86. return t.UTC()
  87. }
  88. return t
  89. }