1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::MsgTime;
7 our @EXPORT_OK = qw(msg_timestamp);
8 use Date::Parse qw(str2time);
9 use Time::Zone qw(tz_offset);
11 sub msg_timestamp ($) {
12 my ($hdr) = @_; # Email::MIME::Header
15 my @recvd = $hdr->header_raw('Received');
16 foreach my $r (@recvd) {
18 $r =~ /\s*(\d+\s+[[:alpha:]]+\s+\d{2,4}\s+
19 \d+\D\d+(?:\D\d+)\s+([\+\-]\d+))/sx or next;
21 $ts = eval { str2time($1) } and last;
22 $mid ||= $hdr->header_raw('Message-ID');
23 warn "no date in $mid Received: $r\n";
25 unless (defined $ts) {
26 my @date = $hdr->header_raw('Date');
27 foreach my $d (@date) {
29 $ts = eval { str2time($d) };
31 $mid ||= $hdr->header_raw('Message-ID');
32 warn "bad Date: $d in $mid: $@\n";
33 } elsif ($d =~ /\s+([\+\-]\d+)\s*\z/) {
38 $ts = time unless defined $ts;
39 return $ts unless wantarray;
42 # "-1200" is the furthest westermost zone offset,
43 # but git fast-import is liberal so we use "-1400"
44 if ($zone >= 1400 || $zone <= -1400) {
45 warn "bogus TZ offset: $zone, ignoring and assuming +0000\n";