| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions |
| 7 |
# are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer in |
| 14 |
# the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# |
| 17 |
# 3. The name "Fastmail Pty Ltd" must not be used to |
| 18 |
# endorse or promote products derived from this software without |
| 19 |
# prior written permission. For permission or any legal |
| 20 |
# details, please contact |
| 21 |
# FastMail Pty Ltd |
| 22 |
# PO Box 234 |
| 23 |
# Collins St West 8007 |
| 24 |
# Victoria |
| 25 |
# Australia |
| 26 |
# |
| 27 |
# 4. Redistributions of any form whatsoever must retain the following |
| 28 |
# acknowledgment: |
| 29 |
# "This product includes software developed by Fastmail Pty. Ltd." |
| 30 |
# |
| 31 |
# FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 32 |
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 33 |
# EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT |
| 34 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF |
| 35 |
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 36 |
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 37 |
# OF THIS SOFTWARE. |
| 38 |
# |
| 39 |
|
| 40 |
package Cassandane::Util::DateTime; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use DateTime; |
| 44 |
use POSIX qw(strftime); |
| 45 |
|
| 46 |
use Exporter (); |
| 47 |
our @ISA = qw(Exporter); |
| 48 |
our @EXPORT = qw( |
| 49 |
&from_iso8601 &to_iso8601 |
| 50 |
&from_rfc822 &to_rfc822 |
| 51 |
&from_rfc3501 &to_rfc3501 |
| 52 |
); |
| 53 |
|
| 54 |
# |
| 55 |
# Construct and return a DateTime object using a string in the |
| 56 |
# "combined basic" format defined in ISO8601, specifically |
| 57 |
# |
| 58 |
# <year><month><day>T<hour24><minute><second>[Z]. |
| 59 |
# |
| 60 |
# Each field is fixed width zero-padded decimal numeric, |
| 61 |
# 4 digits for year and 2 digits for all the others. |
| 62 |
# The optional Z suffix indicates Zulu (UTC aka GMT) |
| 63 |
# time, otherwise localtime is assumed. |
| 64 |
# |
| 65 |
sub from_iso8601($) |
| 66 |
{ |
| 67 |
my ($s) = @_; |
| 68 |
my ($year, $mon, $day, $hour, $min, $sec, $zulu) = |
| 69 |
($s =~ m/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(Z?)$/); |
| 70 |
return unless defined $sec; |
| 71 |
return if ($year < 1970 || $year > 2037); |
| 72 |
return if ($mon < 1 || $mon > 12); |
| 73 |
return if ($day < 1 || $day > 31); |
| 74 |
return if ($hour < 0 || $hour > 23); |
| 75 |
return if ($min < 0 || $min > 59); |
| 76 |
return if ($sec < 0 || $sec > 60); # allow for leap second |
| 77 |
|
| 78 |
# printf STDERR "%s -> year=%u mon=%u day=%u hour=%u min=%u sec=%u\n", |
| 79 |
# $s, $year, $mon, $day, $hour, $min, $sec; |
| 80 |
|
| 81 |
my $tz = ($zulu ? 'GMT' : 'local'); |
| 82 |
return DateTime->new( |
| 83 |
year => $year, |
| 84 |
month => $mon, |
| 85 |
day => $day, |
| 86 |
hour => $hour, |
| 87 |
minute => $min, |
| 88 |
second => $sec, |
| 89 |
time_zone => $tz |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
# |
| 94 |
# Given a DateTime, generate and return a string in ISO8601 |
| 95 |
# combined basic format. |
| 96 |
# |
| 97 |
sub to_iso8601($) |
| 98 |
{ |
| 99 |
my ($dt) = @_; |
| 100 |
return strftime("%Y%m%dT%H%M%SZ", gmtime($dt->epoch)); |
| 101 |
} |
| 102 |
|
| 103 |
# Brief sanity test for parse_iso8601_datetime |
| 104 |
# gnb@enki 566> date +%s -u -d '14 Oct 2010 16:19:52' |
| 105 |
# 1287073192 |
| 106 |
# die "Woops, from_iso8601 is broken" |
| 107 |
# unless (from_iso8601('20101014T161952Z') == 1287073192); |
| 108 |
|
| 109 |
our %rfc822_months = ( |
| 110 |
Jan => 1, |
| 111 |
Feb => 2, |
| 112 |
Mar => 3, |
| 113 |
Apr => 4, |
| 114 |
May => 5, |
| 115 |
Jun => 6, |
| 116 |
Jul => 7, |
| 117 |
Aug => 8, |
| 118 |
Sep => 9, |
| 119 |
Oct => 10, |
| 120 |
Nov => 11, |
| 121 |
Dec => 12 |
| 122 |
); |
| 123 |
our @rfc822_months = ( |
| 124 |
'Jan', |
| 125 |
'Feb', |
| 126 |
'Mar', |
| 127 |
'Apr', |
| 128 |
'May', |
| 129 |
'Jun', |
| 130 |
'Jul', |
| 131 |
'Aug', |
| 132 |
'Sep', |
| 133 |
'Oct', |
| 134 |
'Nov', |
| 135 |
'Dec' |
| 136 |
); |
| 137 |
|
| 138 |
our @rfc822_days = ( |
| 139 |
'Sun', |
| 140 |
'Mon', |
| 141 |
'Tue', |
| 142 |
'Wed', |
| 143 |
'Thu', |
| 144 |
'Fri', |
| 145 |
'Sat', |
| 146 |
'Sun' |
| 147 |
); |
| 148 |
|
| 149 |
# |
| 150 |
# Construct and return a DateTime object using a string in the |
| 151 |
# format defined in RFC822 and its successors, which define |
| 152 |
# the internet email message format. |
| 153 |
# Example: Tue, 05 Oct 2010 11:19:52 +1100 |
| 154 |
# |
| 155 |
sub from_rfc822($) |
| 156 |
{ |
| 157 |
my ($s) = @_; |
| 158 |
my ($wdayn, $day, $mon, $year, $hour, $min, $sec, $tzsign, $tzhour, $tzmin) = |
| 159 |
($s =~ m/^([A-Z][a-z][a-z]), (\d+) ([A-Z][a-z][a-z]) (\d{4}) (\d{2}):(\d{2}):(\d{2}) ([-+])(\d{2})(\d{2})$/); |
| 160 |
return unless defined $tzmin; |
| 161 |
return if ($year < 1970 || $year > 2037); |
| 162 |
$mon = $rfc822_months{$mon}; |
| 163 |
return unless defined $mon; |
| 164 |
return if ($day < 1 || $day > 31); |
| 165 |
return if ($hour < 0 || $hour > 23); |
| 166 |
return if ($min < 0 || $min > 59); |
| 167 |
return if ($sec < 0 || $sec > 60); # allow for leap second |
| 168 |
return if ($tzhour < 0 || $tzhour > 23); |
| 169 |
return if ($tzmin < 0 || $tzmin > 59); |
| 170 |
|
| 171 |
# printf STDERR "%s -> year=%u mon=%u day=%u hour=%u min=%u sec=%u tzsign=%s tzhour=%u tzmin=%u\n", |
| 172 |
# $s, $year, $mon, $day, $hour, $min, $sec, $tzsign, $tzhour, $tzmin; |
| 173 |
|
| 174 |
return DateTime->new( |
| 175 |
year => $year, |
| 176 |
month => $mon, |
| 177 |
day => $day, |
| 178 |
hour => $hour, |
| 179 |
minute => $min, |
| 180 |
second => $sec, |
| 181 |
time_zone => "$tzsign$tzhour$tzmin" |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
# |
| 186 |
# Given a DateTime, generate and return a string in RFC822 format. |
| 187 |
# |
| 188 |
sub to_rfc822($) |
| 189 |
{ |
| 190 |
my ($dt) = @_; |
| 191 |
|
| 192 |
# We can't mix DateTime methods and strftime, because other parts of |
| 193 |
# Cassandane foolishly construct DateTime using the 'from_epoch' but |
| 194 |
# not the 'time_zone' parameters, resulting in a DT object in the |
| 195 |
# UTC timezone instead of local. But conversely strftime() doesn't |
| 196 |
# have a portable way to emit the fixed (non-local-specific) strings |
| 197 |
# that the RFC expects. |
| 198 |
my @lt = localtime($dt->epoch); |
| 199 |
return strftime($rfc822_days[$lt[6]] . |
| 200 |
", %d " . |
| 201 |
$rfc822_months[$lt[4]] . |
| 202 |
" %Y %T %z", @lt); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
# die "Woops, from_rfc822 is broken" |
| 207 |
# unless (from_rfc822('Fri, 15 Oct 2010 03:19:52 +1100') == 1287073192); |
| 208 |
|
| 209 |
# |
| 210 |
# Construct and return a DateTime object using a string in the |
| 211 |
# format defined in RFC3501 which defines the IMAP protocol. |
| 212 |
# Example: " 5-Oct-2010 09:19:52 +1100" (note leading space) |
| 213 |
# |
| 214 |
sub from_rfc3501($) |
| 215 |
{ |
| 216 |
my ($s) = @_; |
| 217 |
my ($day, $mon, $year, $hour, $min, $sec, $tzsign, $tzhour, $tzmin) = |
| 218 |
($s =~ m/^\s*(\d+)-([A-Z][a-z][a-z])-(\d{4}) (\d{2}):(\d{2}):(\d{2}) ([-+])(\d{2})(\d{2})$/); |
| 219 |
return unless defined $tzmin; |
| 220 |
return if ($year < 1970 || $year > 2037); |
| 221 |
$mon = $rfc822_months{$mon}; |
| 222 |
return unless defined $mon; |
| 223 |
return if ($day < 1 || $day > 31); |
| 224 |
return if ($hour < 0 || $hour > 23); |
| 225 |
return if ($min < 0 || $min > 59); |
| 226 |
return if ($sec < 0 || $sec > 60); # allow for leap second |
| 227 |
return if ($tzhour < 0 || $tzhour > 23); |
| 228 |
return if ($tzmin < 0 || $tzmin > 59); |
| 229 |
|
| 230 |
# printf STDERR "%s -> year=%u mon=%u day=%u hour=%u min=%u sec=%u tzsign=%s tzhour=%u tzmin=%u\n", |
| 231 |
# $s, $year, $mon, $day, $hour, $min, $sec, $tzsign, $tzhour, $tzmin; |
| 232 |
|
| 233 |
return DateTime->new( |
| 234 |
year => $year, |
| 235 |
month => $mon, |
| 236 |
day => $day, |
| 237 |
hour => $hour, |
| 238 |
minute => $min, |
| 239 |
second => $sec, |
| 240 |
time_zone => "$tzsign$tzhour$tzmin" |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
# die "Woops, from_rfc3501 is broken" |
| 245 |
# unless (from_rfc3501('15-Oct-2010 03:19:52 +1100') == 1287073192); |
| 246 |
|
| 247 |
# |
| 248 |
# Given a DateTime, generate and return a string in RFC3501 format. |
| 249 |
# |
| 250 |
sub to_rfc3501($) |
| 251 |
{ |
| 252 |
my ($dt) = @_; |
| 253 |
|
| 254 |
my @lt = localtime($dt->epoch); |
| 255 |
return strftime("%e-" |
| 256 |
. $rfc822_months[$lt[4]] |
| 257 |
. "-%Y %T %z", |
| 258 |
@lt); |
| 259 |
} |
| 260 |
|
| 261 |
1; |