]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MsgTime.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / MsgTime.pm
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>
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,4})\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                 # 4-digit dates in non-spam from 1900s and 1910s exist in
71                 # lore archives
72                 return if $ts < 0;
73
74                 # Compute the time offset from [+-]HHMM
75                 $tz //= 0;
76                 my ($tz_hh, $tz_mm);
77                 if (length($tz) == 1) {
78                         $tz_hh = $tz;
79                         $tz_mm = 0;
80                 } elsif (length($tz) == 2) {
81                         $tz_hh = 0;
82                         $tz_mm = $tz;
83                 } else {
84                         $tz_hh = $tz;
85                         $tz_hh =~ s/([0-9]{2})\z//;
86                         $tz_mm = $1;
87                 }
88                 while ($tz_mm >= 60) {
89                         $tz_mm -= 60;
90                         $tz_hh += 1;
91                 }
92                 $sign //= '+';
93                 my $off = $sign . ($tz_mm * 60 + ($tz_hh * 60 * 60));
94                 $ts -= $off;
95                 $sign = '+' if $off == 0;
96                 $zone = sprintf('%s%02d%02d', $sign, $tz_hh, $tz_mm);
97
98         # Time::Zone and Date::Parse are part of the same distibution,
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);
103
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 undef unless(defined $off);
108
109                 # Compute the time zone from offset
110                 my $sign = ($off < 0) ? '-' : '+';
111                 my $hour = abs(int($off / 3600));
112                 my $min  = ($off / 60) % 60;
113
114                 # deal with weird offsets like '-0420' properly
115                 $min = 60 - $min if ($min && $off < 0);
116
117                 $zone = sprintf('%s%02d%02d', $sign, $hour, $min);
118         } else {
119                 warn "Date::Parse missing for non-RFC822 date: $date\n";
120                 return undef;
121         }
122
123         # Note: we've already applied the offset to $ts at this point,
124         # but we want to keep "git fsck" happy.
125         # "-1200" is the furthest westermost zone offset,
126         # but git fast-import is liberal so we use "-1400"
127         if ($zone >= 1400 || $zone <= -1400) {
128                 warn "bogus TZ offset: $zone, ignoring and assuming +0000\n";
129                 $zone = '+0000';
130         }
131         [$ts, $zone];
132 }
133
134 sub time_response ($) {
135         my ($ret) = @_;
136         wantarray ? @$ret : $ret->[0];
137 }
138
139 sub msg_received_at ($) {
140         my ($hdr) = @_; # Email::MIME::Header
141         my @recvd = $hdr->header_raw('Received');
142         my ($ts);
143         foreach my $r (@recvd) {
144                 $r =~ /\s*([0-9]+\s+[a-zA-Z]+\s+[0-9]{2,4}\s+
145                         [0-9]+[^0-9][0-9]+(?:[^0-9][0-9]+)
146                         \s+([\+\-][0-9]+))/sx or next;
147                 $ts = eval { str2date_zone($1) } and return $ts;
148                 my $mid = $hdr->header_raw('Message-ID');
149                 warn "no date in $mid Received: $r\n";
150         }
151         undef;
152 }
153
154 sub msg_date_only ($) {
155         my ($hdr) = @_; # Email::MIME::Header
156         my @date = $hdr->header_raw('Date');
157         my ($ts);
158         foreach my $d (@date) {
159                 $ts = eval { str2date_zone($d) } and return $ts;
160                 if ($@) {
161                         my $mid = $hdr->header_raw('Message-ID');
162                         warn "bad Date: $d in $mid: $@\n";
163                 }
164         }
165         undef;
166 }
167
168 # Favors Received header for sorting globally
169 sub msg_timestamp ($) {
170         my ($hdr) = @_; # Email::MIME::Header
171         my $ret;
172         $ret = msg_received_at($hdr) and return time_response($ret);
173         $ret = msg_date_only($hdr) and return time_response($ret);
174         wantarray ? (time, '+0000') : time;
175 }
176
177 # Favors the Date: header for display and sorting within a thread
178 sub msg_datestamp ($) {
179         my ($hdr) = @_; # Email::MIME::Header
180         my $ret;
181         $ret = msg_date_only($hdr) and return time_response($ret);
182         $ret = msg_received_at($hdr) and return time_response($ret);
183         wantarray ? (time, '+0000') : time;
184 }
185
186 1;