]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MsgTime.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / MsgTime.pm
1 # Copyright (C) 2018-2021 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 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);
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 unless defined($year);
108                 $off //= 0;
109
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;
114
115                 # deal with weird offsets like '-0420' properly
116                 $min = 60 - $min if ($min && $off < 0);
117
118                 $zone = sprintf('%s%02d%02d', $sign, $hour, $min);
119         } else {
120                 warn "Date::Parse missing for non-RFC822 date: $date\n";
121                 return undef;
122         }
123
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";
130                 $zone = '+0000';
131         }
132         [$ts, $zone];
133 }
134
135 sub time_response ($) {
136         my ($ret) = @_;
137         wantarray ? @$ret : $ret->[0];
138 }
139
140 sub msg_received_at ($) {
141         my ($hdr) = @_; # PublicInbox::Eml
142         my @recvd = $hdr->header_raw('Received');
143         my ($ts);
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";
151         }
152         undef;
153 }
154
155 sub msg_date_only ($) {
156         my ($hdr) = @_; # PublicInbox::Eml
157         my @date = $hdr->header_raw('Date');
158         my ($ts);
159         foreach my $d (@date) {
160                 $ts = eval { str2date_zone($d) } and return $ts;
161                 if ($@) {
162                         my $mid = $hdr->header_raw('Message-ID');
163                         warn "bad Date: $d in $mid: $@\n";
164                 }
165         }
166         undef;
167 }
168
169 # Favors Received header for sorting globally
170 sub msg_timestamp ($;$) {
171         my ($hdr, $fallback) = @_; # PublicInbox::Eml
172         my $ret;
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' ]);
176 }
177
178 # Favors the Date: header for display and sorting within a thread
179 sub msg_datestamp ($;$) {
180         my ($hdr, $fallback) = @_; # PublicInbox::Eml
181         my $ret;
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' ]);
185 }
186
187 1;