]> Sergey Matveev's repositories - public-inbox.git/blob - xt/msgtime_cmp.t
a7ef5245bd8de4f37343578fcf28d73ed11d7cb9
[public-inbox.git] / xt / msgtime_cmp.t
1 #!perl -w
2 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Eml;
8 use PublicInbox::Inbox;
9 use PublicInbox::Git;
10 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
11 use POSIX qw(strftime);
12 require_mods('Date::Parse');
13 my $git;
14 my ($inboxdir, $git_dir) = @ENV{qw(GIANT_INBOX_DIR GIANT_GIT_DIR)};
15 if (defined $inboxdir) {
16         my $ibx = { inboxdir => $inboxdir, name => 'name' };
17         $git = PublicInbox::Inbox->new($ibx)->git;
18 } elsif (defined $git_dir) {
19         # sometimes just an old epoch is enough, since newer filters are nicer
20         $git = PublicInbox::Git->new($git_dir);
21 } else {
22         plan skip_all => "GIANT_INBOX_DIR not set for $0";
23 }
24 my @cat = qw(cat-file --buffer --batch-check --batch-all-objects);
25 if (require_git(2.19, 1)) {
26         push @cat, '--unordered';
27 } else {
28         warn "git <2.19, cat-file lacks --unordered, locality suffers\n";
29 }
30
31 # millions of "ok" lines are noise, just show mismatches:
32 sub quiet_is_deeply ($$$$$) {
33         my ($cur, $old, $func, $oid, $hdr) = @_;
34         if ((scalar(@$cur) != 2) ||
35                 (scalar(@$old) == 2 &&
36                         ($old->[0] != $cur->[0]) ||
37                         ($old->[1] != $cur->[1]))) {
38                 for ($cur, $old) {
39                         $_->[2] = strftime('%Y-%m-%d %k:%M:%S', gmtime($_->[0]))
40                 }
41                 is_deeply($cur, $old, "$func $oid");
42                 diag('got: ', explain($cur));
43                 diag('exp: ', explain($old));
44                 diag $hdr->as_string;
45         }
46 }
47
48 sub compare {
49         my ($bref, $oid, $type, $size) = @_;
50         local $SIG{__WARN__} = sub { diag "$oid: ", @_ };
51         my $mime = PublicInbox::Eml->new($$bref);
52         my $hdr = $mime->header_obj;
53         my @cur = msg_datestamp($hdr);
54         my @old = Old::msg_datestamp($hdr);
55         quiet_is_deeply(\@cur, \@old, 'datestamp', $oid, $hdr);
56         @cur = msg_timestamp($hdr);
57         @old = Old::msg_timestamp($hdr);
58         quiet_is_deeply(\@cur, \@old, 'timestamp', $oid, $hdr);
59 }
60
61 my $fh = $git->popen(@cat);
62 while (<$fh>) {
63         my ($oid, $type) = split / /;
64         next if $type ne 'blob';
65         $git->cat_async($oid, \&compare);
66 }
67 $git->async_wait_all;
68 ok(1);
69 done_testing;
70
71 # old date/time-related functions
72 package Old;
73 use strict;
74 use warnings;
75
76 sub str2date_zone ($) {
77         my ($date) = @_;
78
79         my $ts = Date::Parse::str2time($date);
80         return undef unless(defined $ts);
81
82         # off is the time zone offset in seconds from GMT
83         my ($ss,$mm,$hh,$day,$month,$year,$off) = Date::Parse::strptime($date);
84
85         # new behavior which wasn't in the original old version:
86         if ('commit d857e7dc0d816b635a7ead09c3273f8c2d2434be') {
87                 # "msgtime: assume +0000 if TZ missing when using Date::Parse"
88                 $off //= '+0000';
89         }
90
91         return undef unless(defined $off);
92
93         # Compute the time zone from offset
94         my $sign = ($off < 0) ? '-' : '+';
95         my $hour = abs(int($off / 3600));
96         my $min  = ($off / 60) % 60;
97
98         # deal with weird offsets like '-0420' properly
99         $min = 60 - $min if ($min && $off < 0);
100
101         my $zone = sprintf('%s%02d%02d', $sign, $hour, $min);
102
103         # "-1200" is the furthest westermost zone offset,
104         # but git fast-import is liberal so we use "-1400"
105         if ($zone >= 1400 || $zone <= -1400) {
106                 warn "bogus TZ offset: $zone, ignoring and assuming +0000\n";
107                 $zone = '+0000';
108         }
109         [$ts, $zone];
110 }
111
112 sub time_response ($) {
113         my ($ret) = @_;
114         wantarray ? @$ret : $ret->[0];
115 }
116
117 sub msg_received_at ($) {
118         my ($hdr) = @_; # PublicInbox::Eml
119         my @recvd = $hdr->header_raw('Received');
120         my ($ts);
121         foreach my $r (@recvd) {
122                 $r =~ /\s*([0-9]+\s+[a-zA-Z]+\s+[0-9]{2,4}\s+
123                         [0-9]+[^0-9][0-9]+(?:[^0-9][0-9]+)
124                         \s+([\+\-][0-9]+))/sx or next;
125                 $ts = eval { str2date_zone($1) } and return $ts;
126                 my $mid = $hdr->header_raw('Message-ID');
127                 warn "no date in $mid Received: $r\n";
128         }
129         undef;
130 }
131
132 sub msg_date_only ($) {
133         my ($hdr) = @_; # PublicInbox::Eml
134         my @date = $hdr->header_raw('Date');
135         my ($ts);
136         foreach my $d (@date) {
137                 # Y2K problems: 3-digit years
138                 $d =~ s!([A-Za-z]{3}) ([0-9]{3}) ([0-9]{2}:[0-9]{2}:[0-9]{2})!
139                         my $yyyy = $2 + 1900; "$1 $yyyy $3"!e;
140                 $ts = eval { str2date_zone($d) } and return $ts;
141                 if ($@) {
142                         my $mid = $hdr->header_raw('Message-ID');
143                         warn "bad Date: $d in $mid: $@\n";
144                 }
145         }
146         undef;
147 }
148
149 # Favors Received header for sorting globally
150 sub msg_timestamp ($) {
151         my ($hdr) = @_; # PublicInbox::Eml
152         my $ret;
153         $ret = msg_received_at($hdr) and return time_response($ret);
154         $ret = msg_date_only($hdr) and return time_response($ret);
155         wantarray ? (time, '+0000') : time;
156 }
157
158 # Favors the Date: header for display and sorting within a thread
159 sub msg_datestamp ($) {
160         my ($hdr) = @_; # PublicInbox::Eml
161         my $ret;
162         $ret = msg_date_only($hdr) and return time_response($ret);
163         $ret = msg_received_at($hdr) and return time_response($ret);
164         wantarray ? (time, '+0000') : time;
165 }
166
167 1;