1 # Copyright (C) 2018-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Various date/time-related functions
5 package PublicInbox::MsgTime;
9 our @EXPORT_OK = qw(msg_timestamp msg_datestamp);
10 use Time::Local qw(timegm);
11 my @MoY = qw(january february march april may june
12 july august september october november december);
15 @MoY{map { substr($_, 0, 3) } @MoY} = (0..11);
17 my %OBSOLETE_TZ = ( # RFC2822 4.3 (Obsolete Date and Time)
18 EST => '-0500', EDT => '-0400',
19 CST => '-0600', CDT => '-0500',
20 MST => '-0700', MDT => '-0600',
21 PST => '-0800', PDT => '-0700',
22 UT => '+0000', GMT => '+0000', Z => '+0000',
25 # The 1 character military time zones were defined in a non-standard
26 # way in [RFC822] and are therefore unpredictable in their meaning.
28 my $OBSOLETE_TZ = join('|', keys %OBSOLETE_TZ);
30 sub str2date_zone ($) {
34 # RFC822 is most likely for email, but we can tolerate an extra comma
35 # or punctuation as long as all the data is there.
36 # We'll use '\s' since Unicode spaces won't affect our parsing.
37 # SpamAssassin ignores commas and redundant spaces, too.
38 if ($date =~ /(?:[A-Za-z]+,?\s+)? # day-of-week
41 ([0-9]{2,})\s+ # YYYY or YY (or YYY :P)
43 ((?:[0-9]{2})|(?:\s?[0-9])) # MM
44 (?:[:\.]((?:[0-9]{2})|(?:\s?[0-9])))? # :SS
45 \s+ # a TZ offset is required:
47 [\+\-]* # I've seen extra "-" e.g. "--500"
48 ([0-9]+|$OBSOLETE_TZ)(?:\s|$) # TZ offset
50 my ($dd, $m, $yyyy, $hh, $mm, $ss, $sign, $tz) =
51 ($1, $2, $3, $4, $5, $6, $7, $8);
52 # don't accept non-English months
53 defined(my $mon = $MoY{lc($m)}) or return;
55 if (defined(my $off = $OBSOLETE_TZ{$tz})) {
56 $sign = substr($off, 0, 1);
57 $tz = substr($off, 1);
60 # Y2K problems: 3-digit years, follow RFC2822
61 if (length($yyyy) <= 3) {
64 # and 2-digit years from '09 (2009) (0..49)
65 $yyyy += 100 if $yyyy < 1950;
68 $ts = timegm($ss // 0, $mm, $hh, $dd, $mon, $yyyy);
70 # Compute the time offset from [+-]HHMM
73 if (length($tz) == 1) {
76 } elsif (length($tz) == 2) {
81 $tz_hh =~ s/([0-9]{2})\z//;
84 while ($tz_mm >= 60) {
89 my $off = $sign . ($tz_mm * 60 + ($tz_hh * 60 * 60));
91 $sign = '+' if $off == 0;
92 $zone = sprintf('%s%02d%02d', $sign, $tz_hh, $tz_mm);
94 # Time::Zone and Date::Parse are part of the same distibution,
95 # and we need Time::Zone to deal with tz names like "EDT"
96 } elsif (eval { require Date::Parse }) {
97 $ts = Date::Parse::str2time($date);
98 return undef unless(defined $ts);
100 # off is the time zone offset in seconds from GMT
101 my ($ss,$mm,$hh,$day,$month,$year,$off) =
102 Date::Parse::strptime($date);
103 return undef unless(defined $off);
105 # Compute the time zone from offset
106 my $sign = ($off < 0) ? '-' : '+';
107 my $hour = abs(int($off / 3600));
108 my $min = ($off / 60) % 60;
110 # deal with weird offsets like '-0420' properly
111 $min = 60 - $min if ($min && $off < 0);
113 $zone = sprintf('%s%02d%02d', $sign, $hour, $min);
115 warn "Date::Parse missing for non-RFC822 date: $date\n";
119 # Note: we've already applied the offset to $ts at this point,
120 # but we want to keep "git fsck" happy.
121 # "-1200" is the furthest westermost zone offset,
122 # but git fast-import is liberal so we use "-1400"
123 if ($zone >= 1400 || $zone <= -1400) {
124 warn "bogus TZ offset: $zone, ignoring and assuming +0000\n";
130 sub time_response ($) {
132 wantarray ? @$ret : $ret->[0];
135 sub msg_received_at ($) {
136 my ($hdr) = @_; # Email::MIME::Header
137 my @recvd = $hdr->header_raw('Received');
139 foreach my $r (@recvd) {
140 $r =~ /\s*([0-9]+\s+[a-zA-Z]+\s+[0-9]{2,4}\s+
141 [0-9]+[^0-9][0-9]+(?:[^0-9][0-9]+)
142 \s+([\+\-][0-9]+))/sx or next;
143 $ts = eval { str2date_zone($1) } and return $ts;
144 my $mid = $hdr->header_raw('Message-ID');
145 warn "no date in $mid Received: $r\n";
150 sub msg_date_only ($) {
151 my ($hdr) = @_; # Email::MIME::Header
152 my @date = $hdr->header_raw('Date');
154 foreach my $d (@date) {
155 $ts = eval { str2date_zone($d) } and return $ts;
157 my $mid = $hdr->header_raw('Message-ID');
158 warn "bad Date: $d in $mid: $@\n";
164 # Favors Received header for sorting globally
165 sub msg_timestamp ($) {
166 my ($hdr) = @_; # Email::MIME::Header
168 $ret = msg_received_at($hdr) and return time_response($ret);
169 $ret = msg_date_only($hdr) and return time_response($ret);
170 wantarray ? (time, '+0000') : time;
173 # Favors the Date: header for display and sorting within a thread
174 sub msg_datestamp ($) {
175 my ($hdr) = @_; # Email::MIME::Header
177 $ret = msg_date_only($hdr) and return time_response($ret);
178 $ret = msg_received_at($hdr) and return time_response($ret);
179 wantarray ? (time, '+0000') : time;