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>
6 use PublicInbox::TestCommon;
8 use PublicInbox::Inbox;
10 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
11 use POSIX qw(strftime);
12 require_mods('Date::Parse');
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);
22 plan skip_all => "GIANT_INBOX_DIR not set for $0";
24 my @cat = qw(cat-file --buffer --batch-check --batch-all-objects);
25 if (require_git(2.19, 1)) {
26 push @cat, '--unordered';
28 warn "git <2.19, cat-file lacks --unordered, locality suffers\n";
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]))) {
39 $_->[2] = strftime('%Y-%m-%d %k:%M:%S', gmtime($_->[0]))
41 is_deeply($cur, $old, "$func $oid");
42 diag('got: ', explain($cur));
43 diag('exp: ', explain($old));
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);
61 my $fh = $git->popen(@cat);
62 $git->cat_async_begin;
64 my ($oid, $type) = split / /;
65 next if $type ne 'blob';
66 $git->cat_async($oid, *compare);
72 # old date/time-related functions
77 sub str2date_zone ($) {
80 my $ts = Date::Parse::str2time($date);
81 return undef unless(defined $ts);
83 # off is the time zone offset in seconds from GMT
84 my ($ss,$mm,$hh,$day,$month,$year,$off) = Date::Parse::strptime($date);
86 # new behavior which wasn't in the original old version:
87 if ('commit d857e7dc0d816b635a7ead09c3273f8c2d2434be') {
88 # "msgtime: assume +0000 if TZ missing when using Date::Parse"
92 return undef unless(defined $off);
94 # Compute the time zone from offset
95 my $sign = ($off < 0) ? '-' : '+';
96 my $hour = abs(int($off / 3600));
97 my $min = ($off / 60) % 60;
99 # deal with weird offsets like '-0420' properly
100 $min = 60 - $min if ($min && $off < 0);
102 my $zone = sprintf('%s%02d%02d', $sign, $hour, $min);
104 # "-1200" is the furthest westermost zone offset,
105 # but git fast-import is liberal so we use "-1400"
106 if ($zone >= 1400 || $zone <= -1400) {
107 warn "bogus TZ offset: $zone, ignoring and assuming +0000\n";
113 sub time_response ($) {
115 wantarray ? @$ret : $ret->[0];
118 sub msg_received_at ($) {
119 my ($hdr) = @_; # Email::MIME::Header
120 my @recvd = $hdr->header_raw('Received');
122 foreach my $r (@recvd) {
123 $r =~ /\s*([0-9]+\s+[a-zA-Z]+\s+[0-9]{2,4}\s+
124 [0-9]+[^0-9][0-9]+(?:[^0-9][0-9]+)
125 \s+([\+\-][0-9]+))/sx or next;
126 $ts = eval { str2date_zone($1) } and return $ts;
127 my $mid = $hdr->header_raw('Message-ID');
128 warn "no date in $mid Received: $r\n";
133 sub msg_date_only ($) {
134 my ($hdr) = @_; # Email::MIME::Header
135 my @date = $hdr->header_raw('Date');
137 foreach my $d (@date) {
138 # Y2K problems: 3-digit years
139 $d =~ s!([A-Za-z]{3}) ([0-9]{3}) ([0-9]{2}:[0-9]{2}:[0-9]{2})!
140 my $yyyy = $2 + 1900; "$1 $yyyy $3"!e;
141 $ts = eval { str2date_zone($d) } and return $ts;
143 my $mid = $hdr->header_raw('Message-ID');
144 warn "bad Date: $d in $mid: $@\n";
150 # Favors Received header for sorting globally
151 sub msg_timestamp ($) {
152 my ($hdr) = @_; # Email::MIME::Header
154 $ret = msg_received_at($hdr) and return time_response($ret);
155 $ret = msg_date_only($hdr) and return time_response($ret);
156 wantarray ? (time, '+0000') : time;
159 # Favors the Date: header for display and sorting within a thread
160 sub msg_datestamp ($) {
161 my ($hdr) = @_; # Email::MIME::Header
163 $ret = msg_date_only($hdr) and return time_response($ret);
164 $ret = msg_received_at($hdr) and return time_response($ret);
165 wantarray ? (time, '+0000') : time;