]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MsgTime.pm
msgtime: drop Date::Parse for RFC2822
[public-inbox.git] / lib / PublicInbox / MsgTime.pm
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>
3
4 # Various date/time-related functions
5 package PublicInbox::MsgTime;
6 use strict;
7 use warnings;
8 use base qw(Exporter);
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);
13 my %MoY;
14 @MoY{@MoY} = (0..11);
15 @MoY{map { substr($_, 0, 3) } @MoY} = (0..11);
16
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',
23
24         # RFC2822 states:
25         #   The 1 character military time zones were defined in a non-standard
26         #   way in [RFC822] and are therefore unpredictable in their meaning.
27 );
28 my $OBSOLETE_TZ = join('|', keys %OBSOLETE_TZ);
29
30 sub str2date_zone ($) {
31         my ($date) = @_;
32         my ($ts, $zone);
33
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
39                         ([0-9]+),?\s+  # dd
40                         ([A-Za-z]+)\s+ # mon
41                         ([0-9]{2,})\s+ # YYYY or YY (or YYY :P)
42                         ([0-9]+)[:\.] # HH:
43                                 ((?:[0-9]{2})|(?:\s?[0-9])) # MM
44                                 (?:[:\.]((?:[0-9]{2})|(?:\s?[0-9])))? # :SS
45                         \s+     # a TZ offset is required:
46                                 ([\+\-])? # TZ sign
47                                 [\+\-]* # I've seen extra "-" e.g. "--500"
48                                 ([0-9]+|$OBSOLETE_TZ)(?:\s|$) # TZ offset
49                         /xo) {
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;
54
55                 if (defined(my $off = $OBSOLETE_TZ{$tz})) {
56                         $sign = substr($off, 0, 1);
57                         $tz = substr($off, 1);
58                 }
59
60                 # Y2K problems: 3-digit years, follow RFC2822
61                 if (length($yyyy) <= 3) {
62                         $yyyy += 1900;
63
64                         # and 2-digit years from '09 (2009) (0..49)
65                         $yyyy += 100 if $yyyy < 1950;
66                 }
67
68                 $ts = timegm($ss // 0, $mm, $hh, $dd, $mon, $yyyy);
69
70                 # Compute the time offset from [+-]HHMM
71                 $tz //= 0;
72                 my ($tz_hh, $tz_mm);
73                 if (length($tz) == 1) {
74                         $tz_hh = $tz;
75                         $tz_mm = 0;
76                 } elsif (length($tz) == 2) {
77                         $tz_hh = 0;
78                         $tz_mm = $tz;
79                 } else {
80                         $tz_hh = $tz;
81                         $tz_hh =~ s/([0-9]{2})\z//;
82                         $tz_mm = $1;
83                 }
84                 while ($tz_mm >= 60) {
85                         $tz_mm -= 60;
86                         $tz_hh += 1;
87                 }
88                 $sign //= '+';
89                 my $off = $sign . ($tz_mm * 60 + ($tz_hh * 60 * 60));
90                 $ts -= $off;
91                 $sign = '+' if $off == 0;
92                 $zone = sprintf('%s%02d%02d', $sign, $tz_hh, $tz_mm);
93
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);
99
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);
104
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;
109
110                 # deal with weird offsets like '-0420' properly
111                 $min = 60 - $min if ($min && $off < 0);
112
113                 $zone = sprintf('%s%02d%02d', $sign, $hour, $min);
114         } else {
115                 warn "Date::Parse missing for non-RFC822 date: $date\n";
116                 return undef;
117         }
118
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";
125                 $zone = '+0000';
126         }
127         [$ts, $zone];
128 }
129
130 sub time_response ($) {
131         my ($ret) = @_;
132         wantarray ? @$ret : $ret->[0];
133 }
134
135 sub msg_received_at ($) {
136         my ($hdr) = @_; # Email::MIME::Header
137         my @recvd = $hdr->header_raw('Received');
138         my ($ts);
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";
146         }
147         undef;
148 }
149
150 sub msg_date_only ($) {
151         my ($hdr) = @_; # Email::MIME::Header
152         my @date = $hdr->header_raw('Date');
153         my ($ts);
154         foreach my $d (@date) {
155                 $ts = eval { str2date_zone($d) } and return $ts;
156                 if ($@) {
157                         my $mid = $hdr->header_raw('Message-ID');
158                         warn "bad Date: $d in $mid: $@\n";
159                 }
160         }
161         undef;
162 }
163
164 # Favors Received header for sorting globally
165 sub msg_timestamp ($) {
166         my ($hdr) = @_; # Email::MIME::Header
167         my $ret;
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;
171 }
172
173 # Favors the Date: header for display and sorting within a thread
174 sub msg_datestamp ($) {
175         my ($hdr) = @_; # Email::MIME::Header
176         my $ret;
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;
180 }
181
182 1;