parser_test.go 6.9 KB

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