]> Sergey Matveev's repositories - public-inbox.git/blob - t/msgtime.t
t/*.t: use Email::MIME->create over PublicInbox::MIME->create
[public-inbox.git] / t / msgtime.t
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::MIME;
7 use PublicInbox::MsgTime;
8 use PublicInbox::TestCommon;
9 require_mods(qw(Email::MIME));
10
11 our $received_date = 'Mon, 22 Jan 2007 13:16:24 -0500';
12 sub datestamp ($) {
13         my ($date) = @_;
14         local $SIG{__WARN__} = sub {};  # Suppress warnings
15         my $mime = Email::MIME->create(
16                 header => [
17                         From => 'a@example.com',
18                         To => 'b@example.com',
19                         'Content-Type' => 'text/plain',
20                         Subject => 'this is a subject',
21                         'Message-ID' => '<a@example.com>',
22                         Date => $date,
23                         'Received' => <<EOF,
24 (majordomo\@vger.kernel.org) by vger.kernel.org via listexpand
25 \tid S932173AbXAVSQY (ORCPT <rfc822;w\@1wt.eu>);
26 \t$received_date
27 EOF
28                 ],
29                 body => "hello world\n",
30             );
31         my @ts = PublicInbox::MsgTime::msg_datestamp($mime->header_obj);
32         return \@ts;
33 }
34
35 sub timestamp ($) {
36         my ($received) = @_;
37         local $SIG{__WARN__} = sub {};  # Suppress warnings
38         my $mime = Email::MIME->create(
39                 header => [
40                         From => 'a@example.com',
41                         To => 'b@example.com',
42                         'Content-Type' => 'text/plain',
43                         Subject => 'this is a subject',
44                         'Message-ID' => '<a@example.com>',
45                         Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
46                         'Received' => '(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S932173AbXAVSQY (ORCPT <rfc822;w@1wt.eu>);\n\t' . $received,
47                 ],
48                 body => "hello world\n",
49             );
50         my @ts = PublicInbox::MsgTime::msg_timestamp($mime->header_obj);
51         return \@ts;
52 }
53
54 # Verify that the parser sucks up the timezone for dates
55 for (my $min = -1440; $min <= 1440; $min += 30) {
56         my $sign = ($min < 0) ? '-': '+';
57         my $h = abs(int($min / 60));
58         my $m = $min % 60;
59
60         my $ts_expect = 749520000 - ($min * 60);
61         my $tz_expect = sprintf('%s%02d%02d', $sign, $h, $m);
62         if ($tz_expect >= 1400 || $tz_expect <= -1400) {
63                 $tz_expect = '+0000';
64         }
65         my $date = sprintf("Fri, 02 Oct 1993 00:00:00 %s%02d%02d",
66                            $sign, $h, $m);
67         my $result = datestamp($date);
68         is_deeply($result, [ $ts_expect, $tz_expect ], $date);
69 }
70
71 # Verify that the parser sucks up the timezone and for received timestamps
72 for (my $min = -1440; $min <= 1440; $min += 30) {
73         my $sign = ($min < 0) ? '-' : '+';
74         my $h = abs(int($min / 60));
75         my $m = $min %60;
76
77         my $ts_expect = 1169471784 - ($min * 60);
78         my $tz_expect = sprintf('%s%02d%02d', $sign, $h, $m);
79         if ($tz_expect >= 1400 || $tz_expect <= -1400) {
80                 $tz_expect = '+0000';
81         }
82         my $received = sprintf('Mon, 22 Jan 2007 13:16:24 %s%02d%02d',
83                                $sign, $h, $m);
84         is_deeply(timestamp($received), [ $ts_expect, $tz_expect ],
85                 $received);
86 }
87
88 sub is_datestamp ($$) {
89         my ($date, $expect) = @_;
90         is_deeply(datestamp($date), $expect, $date);
91 }
92 is_datestamp('Wed, 13 Dec 2006 10:26:38 +1', [1166001998, '+0100']);
93 is_datestamp('Fri, 3 Feb 2006 18:11:22 -00', [1138990282, '+0000']);
94 is_datestamp('Thursday, 20 Feb 2003 01:14:34 +000', [1045703674, '+0000']);
95 is_datestamp('Fri, 28 Jun 2002 12:54:40 -700', [1025294080, '-0700']);
96 is_datestamp('Sat, 12 Jan 2002 12:52:57 -200', [1010847177, '-0200']);
97 is_datestamp('Mon, 05 Nov 2001 10:36:16 -800', [1004985376, '-0800']);
98 is_datestamp('Tue, 3 Jun 2003 8:58:23 --500', [1054648703, '-0500']);
99 is_datestamp('Thu, 18 May 100 10:40:43 +0200 (MET DST)', [958639243, '+0200']);
100 is_datestamp('Thu, 18 May 2000 10:40:43 +0200', [958639243, '+0200']);
101 is_datestamp('Tue, 27 Feb 2007 16:23:25 -0060', [1172597005, '-0100']);
102 is_datestamp('Wed, 20 Dec 2006 05:32:58 -0420', [1166608378, '-0420']);
103 is_datestamp('Wed, 20 Dec 2006 05:32:58 +0420', [1166577178, '+0420']);
104 is_datestamp('Thu, 14 Dec 2006 00:20:24 +0480', [1166036424, '+0520']);
105 is_datestamp('Thu, 14 Dec 2006 00:20:24 -0480', [1166074824, '-0520']);
106 is_datestamp('Mon, 14 Apr 2014 07:59:01 -0007', [1397462761, '-0007']);
107
108 SKIP: {
109         require_mods('Date::Parse', 1);
110         my $now = time;
111         if (join("\0", gmtime($now)) ne join("\0", localtime($now))) {
112                 skip('needs TZ=UTC to test zone-less parsing', 1);
113         }
114         is_datestamp('Sat, 27 Sep 1997 10:02:32', [875354552, '+0000']);
115 }
116
117 # obsolete formats described in RFC2822
118 for (qw(UT GMT Z)) {
119         is_datestamp('Fri, 02 Oct 1993 00:00:00 '.$_, [ 749520000, '+0000']);
120 }
121 is_datestamp('Fri, 02 Oct 1993 00:00:00 EDT', [ 749534400, '-0400']);
122
123 # fallback to Received: header if Date: is out-of-range:
124 is_datestamp('Fri, 1 Jan 1904 10:12:31 +0100',
125         PublicInbox::MsgTime::str2date_zone($received_date));
126 is_datestamp('Fri, 9 Mar 71685 18:45:56 +0000', # Y10K is not my problem :P
127         PublicInbox::MsgTime::str2date_zone($received_date));
128
129 done_testing();