From 92dfc055ce7a24bc8120c7eece3caca0ae84b653 Mon Sep 17 00:00:00 2001
From: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 26 Mar 2023 18:40:31 +0200
Subject: sfeed.c: parsetime improve parsing RFC2822 obsolete short year

https://datatracker.ietf.org/doc/html/rfc2822#section-4.3
"Where a two or three digit year occurs in a date, the year is to be
interpreted as follows: If a two digit year is encountered whose
value is between 00 and 49, the year is interpreted by adding 2000,
ending up with a value between 2000 and 2049.  If a two digit year is
encountered with a value between 50 and 99, or any three digit year
is encountered, the year is interpreted by adding 1900."

Improvement on commit 7086670e4335714e1df982bf1058082b7400b653

For example (output from TZ=UTC sfeed_plain):

input: Sun, 26 Jul 049 19:26:34
was: 2049-07-26 19:26
now: 1949-07-26 19:26 (because this is a 3-digit year)

input: Sun, 26 Jul 1 19:26:34
was: 2001-07-26 19:26
now: 0001-07-26 19:26 (because this is a 1-digit year and doesn't match the short year rule)

input: Sun, 26 Jul 001 19:26:34
was: 2001-07-26 19:26
now: 1901-07-26 19:26 (because this is a 3 digit year)

These cases are all added to the tests in the sfeed_tests repo (times.xml file).
---
 sfeed.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sfeed.c b/sfeed.c
index 4337300..ab779b1 100644
--- a/sfeed.c
+++ b/sfeed.c
@@ -613,8 +613,8 @@ parsetime(const char *s, long long *tp)
 		for (v = 0, i = 0; i < 4 && ISDIGIT((unsigned char)*s); s++, i++)
 			v = (v * 10) + (*s - '0');
 		/* obsolete short year: RFC2822 4.3 */
-		if (i <= 3)
-			v += (v >= 0 && v <= 49) ? 2000 : 1900;
+		if (i == 2 || i == 3)
+			v += (i == 2 && v >= 0 && v <= 49) ? 2000 : 1900;
 		va[0] = v; /* year */
 		for (; ISSPACE((unsigned char)*s); s++)
 			;
-- 
cgit v1.2.3