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::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);
61 my $fh = $git->popen(@cat);
63 my ($oid, $type) = split / /;
64 next if $type ne 'blob';
65 $git->cat_async($oid, *compare);
71 # old date/time-related functions
76 sub str2date_zone ($) {
79 my $ts = Date::Parse::str2time($date);
80 return undef unless(defined $ts);
82 # off is the time zone offset in seconds from GMT
83 my ($ss,$mm,$hh,$day,$month,$year,$off) = Date::Parse::strptime($date);
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"
91 return undef unless(defined $off);
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;
98 # deal with weird offsets like '-0420' properly
99 $min = 60 - $min if ($min && $off < 0);
101 my $zone = sprintf('%s%02d%02d', $sign, $hour, $min);
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";
112 sub time_response ($) {
114 wantarray ? @$ret : $ret->[0];
117 sub msg_received_at ($) {
118 my ($hdr) = @_; # PublicInbox::Eml
119 my @recvd = $hdr->header_raw('Received');
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";
132 sub msg_date_only ($) {
133 my ($hdr) = @_; # PublicInbox::Eml
134 my @date = $hdr->header_raw('Date');
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;
142 my $mid = $hdr->header_raw('Message-ID');
143 warn "bad Date: $d in $mid: $@\n";
149 # Favors Received header for sorting globally
150 sub msg_timestamp ($) {
151 my ($hdr) = @_; # PublicInbox::Eml
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;
158 # Favors the Date: header for display and sorting within a thread
159 sub msg_datestamp ($) {
160 my ($hdr) = @_; # PublicInbox::Eml
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;