parser_test.go 8.0 KB

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