user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] nntp: handle 2-digit year "70" properly
@ 2020-01-01  3:28 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2020-01-01  3:28 UTC (permalink / raw)
  To: meta

Time::Local has the concept of a "rolling century" which is
defined at 50 years on either side of the current year.  Since
it's now 2020 and >50 years since the Unix epoch, the year "70"
gets interpreted by Time::Local as 2070-01-01 instead of
1970-01-01.

Since NNTP servers are unlikely to store messages from the
future, we'll feed 4-digit year to Time::Local::{timegm,timelocal}
and hopefully not have to worry about things until Y10K.

This fixes test failures on t/v2writable.t and t/nntpd.t since
2020-01-01.
---
 lib/PublicInbox/NNTP.pm | 11 ++++++-----
 t/nntp.t                | 18 +++++++++++-------
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 6845e872..95c9082d 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -220,16 +220,17 @@ sub parse_time ($$;$) {
 		$gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
 		$gmt = 1;
 	}
-	my @now = $gmt ? gmtime : localtime;
 	my ($YYYY, $MM, $DD);
 	if (bytes::length($date) == 8) { # RFC 3977 allows YYYYMMDD
 		($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
 	} else { # legacy clients send YYMMDD
-		($YYYY, $MM, $DD) = unpack('A2A2A2', $date);
+		my $YY;
+		($YY, $MM, $DD) = unpack('A2A2A2', $date);
+		my @now = $gmt ? gmtime : localtime;
 		my $cur_year = $now[5] + 1900;
-		if ($YYYY > $cur_year) {
-			$YYYY += int($cur_year / 1000) * 1000 - 100;
-		}
+		my $cur_cent = int($cur_year / 100) * 100;
+		$YYYY = (($YY + $cur_cent) > $cur_year) ?
+			($YY + 1900) : ($YY + $cur_cent);
 	}
 	if ($gmt) {
 		timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
diff --git a/t/nntp.t b/t/nntp.t
index 11a175bb..961b3d6a 100644
--- a/t/nntp.t
+++ b/t/nntp.t
@@ -69,11 +69,9 @@ use_ok 'PublicInbox::Inbox';
 		my $m = join(' ', @_);
 		my $ts = PublicInbox::NNTP::parse_time(@_);
 		my @t = gmtime($ts);
-		my ($d, $t);
-		if (length($date) == 8) {
-			($d, $t) = split(' ', strftime('%Y%m%d %H%M%S', @t));
-		} else {
-			($d, $t) = split(' ', strftime('%g%m%d %H%M%S', @t));
+		my ($d, $t) = split(' ', strftime('%Y%m%d %H%M%S', @t));
+		if (length($date) != 8) { # Net::NNTP uses YYMMDD :<
+			$d =~ s/^[0-9]{2}//;
 		}
 		is_deeply([$d, $t], [$date, $time], "roundtripped: $m");
 		$ts;
@@ -81,13 +79,19 @@ use_ok 'PublicInbox::Inbox';
 	my $x1 = time_roundtrip(qw(20141109 060606 GMT));
 	my $x2 = time_roundtrip(qw(141109 060606 GMT));
 	my $x3 = time_roundtrip(qw(930724 060606 GMT));
-
+	my $x5 = time_roundtrip(qw(710101 000000));
+	my $x6 = time_roundtrip(qw(720101 000000));
 	SKIP: {
-		skip('YYMMDD test needs updating', 2) if (time > 0x7fffffff);
+		skip('YYMMDD test needs updating', 6) if (time > 0x7fffffff);
 		# our world probably ends in 2038, but if not we'll try to
 		# remember to update the test then
 		is($x1, $x2, 'YYYYMMDD and YYMMDD parse identically');
 		is(strftime('%Y', gmtime($x3)), '1993', '930724 was in 1993');
+
+		my $epoch = time_roundtrip(qw(700101 000000 GMT));
+		is($epoch, 0, 'epoch parsed correctly');
+		ok($x6 > $x5, '1972 > 1971');
+		ok($x5 > $epoch, '1971 > Unix epoch');
 	}
 }
 

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-01-01  3:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-01  3:28 [PATCH] nntp: handle 2-digit year "70" properly Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).