1 # Copyright (C) 2018-2020 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,4})\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 # 4-digit dates in non-spam from 1900s and 1910s exist in
74 # Compute the time offset from [+-]HHMM
77 if (length($tz) == 1) {
80 } elsif (length($tz) == 2) {
85 $tz_hh =~ s/([0-9]{2})\z//;
88 while ($tz_mm >= 60) {
93 my $off = $sign . ($tz_mm * 60 + ($tz_hh * 60 * 60));
95 $sign = '+' if $off == 0;
96 $zone = sprintf('%s%02d%02d', $sign, $tz_hh, $tz_mm);
98 # Time::Zone and Date::Parse are part of the same distribution,
99 # and we need Time::Zone to deal with tz names like "EDT"
100 } elsif (eval { require Date::Parse }) {
101 $ts = Date::Parse::str2time($date);
102 return undef unless(defined $ts);
104 # off is the time zone offset in seconds from GMT
105 my ($ss,$mm,$hh,$day,$month,$year,$off) =
106 Date::Parse::strptime($date);
107 return unless defined($year);
110 # Compute the time zone from offset
111 my $sign = ($off < 0) ? '-' : '+';
112 my $hour = abs(int($off / 3600));
113 my $min = ($off / 60) % 60;
115 # deal with weird offsets like '-0420' properly
116 $min = 60 - $min if ($min && $off < 0);
118 $zone = sprintf('%s%02d%02d', $sign, $hour, $min);
120 warn "Date::Parse missing for non-RFC822 date: $date\n";
124 # Note: we've already applied the offset to $ts at this point,
125 # but we want to keep "git fsck" happy.
126 # "-1200" is the furthest westermost zone offset,
127 # but git fast-import is liberal so we use "-1400"
128 if ($zone >= 1400 || $zone <= -1400) {
129 warn "bogus TZ offset: $zone, ignoring and assuming +0000\n";
135 sub time_response ($) {
137 wantarray ? @$ret : $ret->[0];
140 sub msg_received_at ($) {
141 my ($hdr) = @_; # PublicInbox::Eml
142 my @recvd = $hdr->header_raw('Received');
144 foreach my $r (@recvd) {
145 $r =~ /\s*([0-9]+\s+[a-zA-Z]+\s+[0-9]{2,4}\s+
146 [0-9]+[^0-9][0-9]+(?:[^0-9][0-9]+)
147 \s+([\+\-][0-9]+))/sx or next;
148 $ts = eval { str2date_zone($1) } and return $ts;
149 my $mid = $hdr->header_raw('Message-ID');
150 warn "no date in $mid Received: $r\n";
155 sub msg_date_only ($) {
156 my ($hdr) = @_; # PublicInbox::Eml
157 my @date = $hdr->header_raw('Date');
159 foreach my $d (@date) {
160 $ts = eval { str2date_zone($d) } and return $ts;
162 my $mid = $hdr->header_raw('Message-ID');
163 warn "bad Date: $d in $mid: $@\n";
169 # Favors Received header for sorting globally
170 sub msg_timestamp ($;$) {
171 my ($hdr, $fallback) = @_; # PublicInbox::Eml
173 $ret = msg_received_at($hdr) and return time_response($ret);
174 $ret = msg_date_only($hdr) and return time_response($ret);
175 time_response([ $fallback // time, '+0000' ]);
178 # Favors the Date: header for display and sorting within a thread
179 sub msg_datestamp ($;$) {
180 my ($hdr, $fallback) = @_; # PublicInbox::Eml
182 $ret = msg_date_only($hdr) and return time_response($ret);
183 $ret = msg_received_at($hdr) and return time_response($ret);
184 time_response([ $fallback // time, '+0000' ]);