]> Sergey Matveev's repositories - public-inbox.git/blob - xt/msgtime_cmp.t
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / xt / msgtime_cmp.t
1 #!perl -w
2 # Copyright (C) 2019-2020 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::MIME;
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::MIME->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 $git->cat_async_begin;
63 while (<$fh>) {
64         my ($oid, $type) = split / /;
65         next if $type ne 'blob';
66         $git->cat_async($oid, *compare);
67 }
68 $git->cat_async_wait;
69 ok(1);
70 done_testing;
71
72 # old date/time-related functions
73 package Old;
74 use strict;
75 use warnings;
76
77 sub str2date_zone ($) {
78         my ($date) = @_;
79
80         my $ts = Date::Parse::str2time($date);
81         return undef unless(defined $ts);
82
83         # off is the time zone offset in seconds from GMT
84         my ($ss,$mm,$hh,$day,$month,$year,$off) = Date::Parse::strptime($date);
85         return undef unless(defined $off);
86
87         # Compute the time zone from offset
88         my $sign = ($off < 0) ? '-' : '+';
89         my $hour = abs(int($off / 3600));
90         my $min  = ($off / 60) % 60;
91
92         # deal with weird offsets like '-0420' properly
93         $min = 60 - $min if ($min && $off < 0);
94
95         my $zone = sprintf('%s%02d%02d', $sign, $hour, $min);
96
97         # "-1200" is the furthest westermost zone offset,
98         # but git fast-import is liberal so we use "-1400"
99         if ($zone >= 1400 || $zone <= -1400) {
100                 warn "bogus TZ offset: $zone, ignoring and assuming +0000\n";
101                 $zone = '+0000';
102         }
103         [$ts, $zone];
104 }
105
106 sub time_response ($) {
107         my ($ret) = @_;
108         wantarray ? @$ret : $ret->[0];
109 }
110
111 sub msg_received_at ($) {
112         my ($hdr) = @_; # Email::MIME::Header
113         my @recvd = $hdr->header_raw('Received');
114         my ($ts);
115         foreach my $r (@recvd) {
116                 $r =~ /\s*([0-9]+\s+[a-zA-Z]+\s+[0-9]{2,4}\s+
117                         [0-9]+[^0-9][0-9]+(?:[^0-9][0-9]+)
118                         \s+([\+\-][0-9]+))/sx or next;
119                 $ts = eval { str2date_zone($1) } and return $ts;
120                 my $mid = $hdr->header_raw('Message-ID');
121                 warn "no date in $mid Received: $r\n";
122         }
123         undef;
124 }
125
126 sub msg_date_only ($) {
127         my ($hdr) = @_; # Email::MIME::Header
128         my @date = $hdr->header_raw('Date');
129         my ($ts);
130         foreach my $d (@date) {
131                 # Y2K problems: 3-digit years
132                 $d =~ s!([A-Za-z]{3}) ([0-9]{3}) ([0-9]{2}:[0-9]{2}:[0-9]{2})!
133                         my $yyyy = $2 + 1900; "$1 $yyyy $3"!e;
134                 $ts = eval { str2date_zone($d) } and return $ts;
135                 if ($@) {
136                         my $mid = $hdr->header_raw('Message-ID');
137                         warn "bad Date: $d in $mid: $@\n";
138                 }
139         }
140         undef;
141 }
142
143 # Favors Received header for sorting globally
144 sub msg_timestamp ($) {
145         my ($hdr) = @_; # Email::MIME::Header
146         my $ret;
147         $ret = msg_received_at($hdr) and return time_response($ret);
148         $ret = msg_date_only($hdr) and return time_response($ret);
149         wantarray ? (time, '+0000') : time;
150 }
151
152 # Favors the Date: header for display and sorting within a thread
153 sub msg_datestamp ($) {
154         my ($hdr) = @_; # Email::MIME::Header
155         my $ret;
156         $ret = msg_date_only($hdr) and return time_response($ret);
157         $ret = msg_received_at($hdr) and return time_response($ret);
158         wantarray ? (time, '+0000') : time;
159 }
160
161 1;