parser_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. "testing"
  6. )
  7. func TestParseEmptyDate(t *testing.T) {
  8. if _, err := Parse(" "); err == nil {
  9. t.Fatalf(`Empty dates should return an error`)
  10. }
  11. }
  12. func TestParseInvalidDate(t *testing.T) {
  13. if _, err := Parse("invalid"); err == nil {
  14. t.Fatalf(`Invalid dates should return an error`)
  15. }
  16. }
  17. func TestParseAtomDate(t *testing.T) {
  18. date, err := Parse("2017-12-22T22:09:49+00:00")
  19. if err != nil {
  20. t.Fatalf(`Atom dates should be parsed correctly`)
  21. }
  22. expectedTS := int64(1513980589)
  23. if date.Unix() != expectedTS {
  24. t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
  25. }
  26. _, offset := date.Zone()
  27. expectedOffset := 0
  28. if offset != expectedOffset {
  29. t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
  30. }
  31. }
  32. func TestParseRSSDateTimezone(t *testing.T) {
  33. date, err := Parse("Fri, 31 Mar 2023 20:19:00 America/Los_Angeles")
  34. if err != nil {
  35. t.Fatalf(`RSS dates should be parsed correctly`)
  36. }
  37. expectedTS := int64(1680319140)
  38. if date.Unix() != expectedTS {
  39. t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
  40. }
  41. expectedLocation := "America/Los_Angeles"
  42. if date.Location().String() != expectedLocation {
  43. t.Errorf(`The location should be %q instead of %q`, expectedLocation, date.Location())
  44. }
  45. name, offset := date.Zone()
  46. expectedName := "PDT"
  47. if name != expectedName {
  48. t.Errorf(`The zone name should be %q instead of %q`, expectedName, name)
  49. }
  50. expectedOffset := -25200
  51. if offset != expectedOffset {
  52. t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
  53. }
  54. }
  55. func TestParseRSSDateGMT(t *testing.T) {
  56. date, err := Parse("Tue, 03 Jun 2003 09:39:21 GMT")
  57. if err != nil {
  58. t.Fatalf(`RSS dates should be parsed correctly`)
  59. }
  60. expectedTS := int64(1054633161)
  61. if date.Unix() != expectedTS {
  62. t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
  63. }
  64. expectedLocation := "GMT"
  65. if date.Location().String() != expectedLocation {
  66. t.Errorf(`The location should be %q instead of %q`, expectedLocation, date.Location())
  67. }
  68. name, offset := date.Zone()
  69. expectedName := "GMT"
  70. if name != expectedName {
  71. t.Errorf(`The zone name should be %q instead of %q`, expectedName, name)
  72. }
  73. expectedOffset := 0
  74. if offset != expectedOffset {
  75. t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
  76. }
  77. }
  78. func TestParseRSSDatePST(t *testing.T) {
  79. date, err := Parse("Wed, 26 Dec 2018 10:00:54 PST")
  80. if err != nil {
  81. t.Fatalf(`RSS dates with PST timezone should be parsed correctly: %v`, err)
  82. }
  83. expectedTS := int64(1545847254)
  84. if date.Unix() != expectedTS {
  85. t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
  86. }
  87. expectedLocation := "America/Los_Angeles"
  88. if date.Location().String() != expectedLocation {
  89. t.Errorf(`The location should be %q instead of %q`, expectedLocation, date.Location())
  90. }
  91. name, offset := date.Zone()
  92. expectedName := "PST"
  93. if name != expectedName {
  94. t.Errorf(`The zone name should be %q instead of %q`, expectedName, name)
  95. }
  96. expectedOffset := -28800
  97. if offset != expectedOffset {
  98. t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
  99. }
  100. }
  101. func TestParseRSSDateEST(t *testing.T) {
  102. date, err := Parse("Wed, 10 Feb 2021 22:46:00 EST")
  103. if err != nil {
  104. t.Fatalf(`RSS dates with EST timezone should be parsed correctly: %v`, err)
  105. }
  106. expectedTS := int64(1613015160)
  107. if date.Unix() != expectedTS {
  108. t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
  109. }
  110. expectedLocation := "America/New_York"
  111. if date.Location().String() != expectedLocation {
  112. t.Errorf(`The location should be %q instead of %q`, expectedLocation, date.Location())
  113. }
  114. name, offset := date.Zone()
  115. expectedName := "EST"
  116. if name != expectedName {
  117. t.Errorf(`The zone name should be %q instead of %q`, expectedName, name)
  118. }
  119. expectedOffset := -18000
  120. if offset != expectedOffset {
  121. t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
  122. }
  123. }
  124. func TestParseRSSDateOffset(t *testing.T) {
  125. date, err := Parse("Sun, 28 Oct 2018 13:48:00 +0100")
  126. if err != nil {
  127. t.Fatalf(`RSS dates with offset should be parsed correctly: %v`, err)
  128. }
  129. expectedTS := int64(1540730880)
  130. if date.Unix() != expectedTS {
  131. t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
  132. }
  133. _, offset := date.Zone()
  134. expectedOffset := 3600
  135. if offset != expectedOffset {
  136. t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
  137. }
  138. }
  139. func TestParseWeirdDateFormat(t *testing.T) {
  140. dates := []string{
  141. "Sun, 17 Dec 2017 1:55 PM EST",
  142. "9 Dec 2016 12:00 GMT",
  143. "Friday, December 22, 2017 - 3:09pm",
  144. "Friday, December 8, 2017 - 3:07pm",
  145. "Thu, 25 Feb 2016 00:00:00 Europe/Brussels",
  146. "Mon, 09 Apr 2018, 16:04",
  147. "Di, 23 Jan 2018 00:00:00 +0100",
  148. "Do, 29 Mär 2018 00:00:00 +0200",
  149. "mer, 9 avr 2018 00:00:00 +0200",
  150. "1520932969",
  151. "Tue 16 Feb 2016, 23:16:00 EDT",
  152. "Tue, 16 Feb 2016 23:16:00 EDT",
  153. "Tue, Feb 16 2016 23:16:00 EDT",
  154. "March 30 2020 07:02:38 PM",
  155. "Mon, 30 Mar 2020 19:53 +0000",
  156. "Mon, 03/30/2020 - 19:19",
  157. "2018-12-12T12:12",
  158. "2020-11-08T16:20:00-05:00Z",
  159. "Nov. 16, 2020, 10:57 a.m.",
  160. "Friday 06 November 2020",
  161. "Mon, November 16, 2020, 11:12 PM EST",
  162. "Lundi, 16. Novembre 2020 - 15:54",
  163. "Thu Nov 12 2020 17:00:00 GMT+0000 (Coordinated Universal Time)",
  164. "Sat, 11 04 2020 08:51:49 +0100",
  165. "Mon, 16th Nov 2020 13:16:28 GMT",
  166. "Nov. 2020",
  167. "ven., 03 juil. 2020 15:09:58 +0000",
  168. "Fri, 26/06/2020",
  169. "Thu, 29 Oct 2020 07:36:03 GMT-07:00",
  170. "jeu., 02 avril 2020 00:00:00 +0200",
  171. "Jan. 4, 2016, 12:37 p.m.",
  172. "2018-10-23 04:07:42 +00:00",
  173. "5 August, 2019",
  174. "mar., 01 déc. 2020 16:11:02 +0000",
  175. "Thurs, 15 Oct 2020 00:00:39 +0000",
  176. "Thur, 19 Nov 2020 00:00:39 +0000",
  177. "26 Sep 2022 GMT",
  178. "Thu, June 22, 2023 at 01:11 PM EDT",
  179. "Apr 16, 2023 08:01 GMT",
  180. "Jun 23, 2023 19:00 GMT",
  181. "09/15/2014 4:20 pm PST",
  182. "Fri, 23rd Jun 2023 09:32:20 GMT",
  183. "Sat, Oct 28 2023 08:28:28 PM",
  184. "Monday, October 6, 2023 - 16:29\n",
  185. "10/30/23 21:55:58",
  186. "30.10.23",
  187. }
  188. for _, date := range dates {
  189. if _, err := Parse(date); err != nil {
  190. t.Errorf(`Unable to parse date: %q (%v)`, date, err)
  191. }
  192. }
  193. }
  194. func TestParseDateWithTimezoneOutOfRange(t *testing.T) {
  195. date, err := Parse("2023-05-29 00:00:00-23:00")
  196. if err != nil {
  197. t.Errorf(`Unable to parse date: %v`, err)
  198. }
  199. _, offset := date.Zone()
  200. if offset != 0 {
  201. t.Errorf(`The offset should be reinitialized to 0 instead of %v because it's out of range`, offset)
  202. }
  203. }