| 1 | ##---------------------------------------------------------------------------## | ||
| 2 | ## File: | ||
| 3 | ## $Id: mhtime.pl,v 2.10 2001/09/17 16:09:35 ehood Exp $ | ||
| 4 | ## Author: | ||
| 5 | ## Earl Hood mhonarc@mhonarc.org | ||
| 6 | ## Description: | ||
| 7 | ## Time related routines for mhonarc | ||
| 8 | ##---------------------------------------------------------------------------## | ||
| 9 | ## Copyright (C) 1996-1999 Earl Hood, mhonarc@mhonarc.org | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify | ||
| 12 | ## it under the terms of the GNU General Public License as published by | ||
| 13 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 14 | ## (at your option) any later version. | ||
| 15 | ## | ||
| 16 | ## This program is distributed in the hope that it will be useful, | ||
| 17 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 18 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 19 | ## GNU General Public License for more details. | ||
| 20 | ## | ||
| 21 | ## You should have received a copy of the GNU General Public License | ||
| 22 | ## along with this program; if not, write to the Free Software | ||
| 23 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
| 24 | ## 02111-1307, USA | ||
| 25 | ##---------------------------------------------------------------------------## | ||
| 26 | |||
| 27 | package mhonarc; | ||
| 28 | |||
| 29 | ##---------------------------------------------------------------------------## | ||
| 30 | ## Date variables for date routines | ||
| 31 | ## | ||
| 32 | my %Month2Num = ( | ||
| 33 | 'jan', 0, 'feb', 1, 'mar', 2, 'apr', 3, | ||
| 34 | 'may', 4, 'jun', 5, 'jul', 6, 'aug', 7, | ||
| 35 | 'sep', 8, 'oct', 9, 'nov', 10, 'dec', 11, | ||
| 36 | 'january', 0, 'february', 1, 'march', 2, 'april', 3, | ||
| 37 | 'may', 4, 'june', 5, 'july', 6, 'august', 7, | ||
| 38 | 'september', 8, 'october', 9, 'november', 10, 'december', 11, | ||
| 39 | ); | ||
| 40 | my %WDay2Num = ( | ||
| 41 | 'sun', 0, 'mon', 1, 'tue', 2, 'wed', 3, | ||
| 42 | 'thu', 4, 'fri', 5, 'sat', 6, 'sunday', 0, | ||
| 43 | 'monday', 1, 'tuesday', 2, 'wednesday', 3, 'thursday', 4, | ||
| 44 | 'friday', 5, 'saturday', 6, | ||
| 45 | ); | ||
| 46 | |||
| 47 | my @wdays = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'); | ||
| 48 | my @Wdays = ( | ||
| 49 | 'Sunday', 'Monday', 'Tuesday', 'Wednesday', | ||
| 50 | 'Thursday', 'Friday', 'Saturday' | ||
| 51 | ); | ||
| 52 | my @mons = ( | ||
| 53 | 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', | ||
| 54 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' | ||
| 55 | ); | ||
| 56 | my @Mons = ( | ||
| 57 | 'January', 'February', 'March', 'April', | ||
| 58 | 'May', 'June', 'July', 'August', | ||
| 59 | 'September', 'October', 'November', 'December' | ||
| 60 | ); | ||
| 61 | |||
| 62 | ## The following used in parse_date() regexes | ||
| 63 | my $p_weekdays = 'Mon|Tue|Wed|Thu|Fri|Sat|Sun'; | ||
| 64 | my $p_Weekdays = 'Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday'; | ||
| 65 | my $p_months = 'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec'; | ||
| 66 | my $p_Months = 'January|February|March|April|May|June|July|August' | ||
| 67 | . '|September|October|November|December'; | ||
| 68 | my $p_hrminsec = '\d{1,2}:\d\d:\d\d'; | ||
| 69 | my $p_hrmin = '\d{1,2}:\d\d'; | ||
| 70 | my $p_day = '\d{1,2}'; | ||
| 71 | my $p_year = '\d\d\d\d|\d\d'; | ||
| 72 | |||
| 73 | ##--------------------------------------------------------------------------- | ||
| 74 | ## Set weekday and month names. This allows localization of | ||
| 75 | ## names. | ||
| 76 | ## | ||
| 77 | sub set_date_names { | ||
| 78 | my ($in_wd, $in_Wd, $in_m, $in_M) = @_; | ||
| 79 | @wdays = @$in_wd if defined($in_wd) && scalar(@$in_wd); | ||
| 80 | @Wdays = @$in_Wd if defined($in_Wd) && scalar(@$in_Wd); | ||
| 81 | @mons = @$in_m if defined($in_m) && scalar(@$in_m); | ||
| 82 | @Mons = @$in_M if defined($in_M) && scalar(@$in_M); | ||
| 83 | } | ||
| 84 | |||
| 85 | ##--------------------------------------------------------------------------- | ||
| 86 | ## Get date in date(1)-like format. $local flag is if local time | ||
| 87 | ## should be used. | ||
| 88 | ## | ||
| 89 | sub getdate { | ||
| 90 | &time2str('', time, $_[0]); | ||
| 91 | } | ||
| 92 | |||
| 93 | ##--------------------------------------------------------------------------- | ||
| 94 | ## Convert a calander time to a string. | ||
| 95 | ## | ||
| 96 | sub time2str { | ||
| 97 | my ($fmt, $time, $local) = @_; | ||
| 98 | my ($date) = ""; | ||
| 99 | |||
| 100 | ## Get current date/time | ||
| 101 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = | ||
| 102 | ($local ? localtime($time) : gmtime($time)); | ||
| 103 | |||
| 104 | ## If format string blank, use default format | ||
| 105 | if ($fmt !~ /\S/) { | ||
| 106 | $fmt = '%a %b %d %H:%M:%S'; | ||
| 107 | $fmt .= ' GMT' unless $local; | ||
| 108 | $fmt .= ' %Y'; | ||
| 109 | } | ||
| 110 | |||
| 111 | POSIXMODCHK: { | ||
| 112 | last POSIXMODCHK unless $POSIXstrftime; | ||
| 113 | eval { require POSIX; }; | ||
| 114 | last POSIXMODCHK if ($@) || !defined(&POSIX::strftime); | ||
| 115 | return POSIX::strftime( | ||
| 116 | $fmt, $sec, $min, $hour, $mday, | ||
| 117 | $mon, $year, $wday, $yday, $isdst | ||
| 118 | ); | ||
| 119 | } | ||
| 120 | |||
| 121 | ## Get here, we have to do it ourselves. | ||
| 122 | my ($yearfull, $hour12); | ||
| 123 | $yearfull = $year + 1900; | ||
| 124 | $year = $year % 100; | ||
| 125 | $hour12 = $hour > 12 ? $hour - 12 : $hour; | ||
| 126 | |||
| 127 | ## Format output | ||
| 128 | $fmt =~ s/\%c/\%a \%b \%d \%H:\%M:\%S \%Y/g; | ||
| 129 | |||
| 130 | $fmt =~ s/\%a/$wdays[$wday]/g; | ||
| 131 | $fmt =~ s/\%A/$Wdays[$wday]/g; | ||
| 132 | $fmt =~ s/\%[bh]/$mons[$mon]/g; | ||
| 133 | $fmt =~ s/\%B/$Mons[$mon]/g; | ||
| 134 | |||
| 135 | $sec = sprintf("%02d", $sec); | ||
| 136 | $min = sprintf("%02d", $min); | ||
| 137 | $hour = sprintf("%02d", $hour); | ||
| 138 | $hour12 = sprintf("%02d", $hour12); | ||
| 139 | $mday = sprintf("%02d", $mday); | ||
| 140 | $mon = sprintf("%02d", $mon + 1); | ||
| 141 | $year = sprintf("%02d", $year); | ||
| 142 | $yearfull = sprintf("%04d", $yearfull); | ||
| 143 | $wday = sprintf("%02d", $wday + 1); | ||
| 144 | $yday = sprintf("%03d", $yday); | ||
| 145 | |||
| 146 | $fmt =~ s/\%d/$mday/g; | ||
| 147 | $fmt =~ s/\%H/$hour/g; | ||
| 148 | $fmt =~ s/\%I/$hour12/g; | ||
| 149 | $fmt =~ s/\%j/$yday/g; | ||
| 150 | $fmt =~ s/\%m/$mon/g; | ||
| 151 | $fmt =~ s/\%M/$min/g; | ||
| 152 | $fmt =~ s/\%n/\n/g; | ||
| 153 | $fmt =~ s/\%p/am/g if ($hour < 12); | ||
| 154 | $fmt =~ s/\%p/pm/g if ($hour >= 12); | ||
| 155 | $fmt =~ s/\%P/AM/g if ($hour < 12); | ||
| 156 | $fmt =~ s/\%P/PM/g if ($hour >= 12); | ||
| 157 | $fmt =~ s/\%S/$sec/g; | ||
| 158 | $fmt =~ s/\%w/$wday/g; | ||
| 159 | $fmt =~ s/\%y/$year/g; | ||
| 160 | $fmt =~ s/\%Y/$yearfull/g; | ||
| 161 | |||
| 162 | $fmt =~ s/\%\%/\%/g; | ||
| 163 | |||
| 164 | $date = $fmt; | ||
| 165 | |||
| 166 | $date; | ||
| 167 | } | ||
| 168 | |||
| 169 | ##--------------------------------------------------------------------------- | ||
| 170 | ## parse_date takes a string date specified like the output of | ||
| 171 | ## date(1) into its components. Parsing a string for a date is | ||
| 172 | ## ugly since we have to watch out for differing formats. | ||
| 173 | ## | ||
| 174 | ## The following date formats are looked for: | ||
| 175 | ## | ||
| 176 | ## Wdy DD Mon YY HH:MM:SS Zone | ||
| 177 | ## DD Mon YY HH:MM:SS Zone | ||
| 178 | ## Wdy Mon DD HH:MM:SS Zone YYYY | ||
| 179 | ## Wdy Mon DD HH:MM:SS YYYY | ||
| 180 | ## | ||
| 181 | ## The routine keys off of the day of time field "HH:MM:SS" and | ||
| 182 | ## scans realtive to its location. | ||
| 183 | ## | ||
| 184 | ## If the parse fails, a null array is returned. Thus the routine | ||
| 185 | ## may be used as follows: | ||
| 186 | ## | ||
| 187 | ## if ( (@x = &parse_date($date)) ) { Success } | ||
| 188 | ## else { Fail } | ||
| 189 | ## | ||
| 190 | ## If success the array contents are as follows: | ||
| 191 | ## | ||
| 192 | ## (Weekday (0-6), Day of the month (1-31), Month (0-11), | ||
| 193 | ## Year, Hour, Minutes, Seconds, Time Zone) | ||
| 194 | ## | ||
| 195 | ## Contributer(s): Frank J. Manion <FJ_Manion@fccc.edu> | ||
| 196 | ## | ||
| 197 | sub parse_date { | ||
| 198 | my ($date) = $_[0]; | ||
| 199 | my ($wday, $mday, $mon, $yr, $time, $hr, $min, $sec, $zone); | ||
| 200 | my (@array); | ||
| 201 | my ($start, $rest); | ||
| 202 | |||
| 203 | # Try to find the date by focusing on the "\d\d:\d\d" field. | ||
| 204 | # All parsing is then done relative to this location. | ||
| 205 | # | ||
| 206 | $date =~ s/^\s+//; | ||
| 207 | $time = ""; | ||
| 208 | $rest = ""; | ||
| 209 | # Don't use $p_hrmin(sec) vars in split due to bug in perl 5.003. | ||
| 210 | ($start, $time, $rest) = split(/(\b\d{1,2}:\d\d:\d\d)/o, $date, 2); | ||
| 211 | ($start, $time, $rest) = split(/(\b\d{1,2}:\d\d)/o, $date, 2) | ||
| 212 | if !defined($time) | ||
| 213 | or $time eq ""; | ||
| 214 | return () | ||
| 215 | unless defined($time) | ||
| 216 | and $time ne ""; | ||
| 217 | |||
| 218 | ($hr, $min, $sec) = split(/:/, $time); | ||
| 219 | $sec = 0 unless $sec; # Sometimes seconds not defined | ||
| 220 | |||
| 221 | # Strip $start of all but the last 4 tokens, | ||
| 222 | # and stuff all tokens in $rest into @array | ||
| 223 | # | ||
| 224 | @array = split(' ', $start); | ||
| 225 | $start = join(' ', | ||
| 226 | ($#array - 3 < 0) | ||
| 227 | ? @array[0 .. $#array] | ||
| 228 | : @array[$#array - 3 .. $#array]); | ||
| 229 | @array = split(' ', $rest); | ||
| 230 | $rest = join( | ||
| 231 | ' ', ($#array >= 1) | ||
| 232 | ? @array[0 .. 1] | ||
| 233 | : $array[0] | ||
| 234 | ); | ||
| 235 | # Wdy DD Mon YY HH:MM:SS Zone | ||
| 236 | if ($start =~ /($p_weekdays),*\s+($p_day)\s+($p_months)\s+($p_year)$/io) { | ||
| 237 | |||
| 238 | ($wday, $mday, $mon, $yr, $zone) = ($1, $2, $3, $4, $array[0]); | ||
| 239 | |||
| 240 | # DD Mon YY HH:MM:SS Zone | ||
| 241 | } elsif ($start =~ /($p_day)\s+($p_months)\s+($p_year)$/io) { | ||
| 242 | ($mday, $mon, $yr, $zone) = ($1, $2, $3, $array[0]); | ||
| 243 | |||
| 244 | # Wdy Mon DD HH:MM:SS Zone YYYY | ||
| 245 | # Wdy Mon DD HH:MM:SS YYYY | ||
| 246 | } elsif ($start =~ /($p_weekdays),?\s+($p_months)\s+($p_day)$/io) { | ||
| 247 | ($wday, $mon, $mday) = ($1, $2, $3); | ||
| 248 | if ($rest =~ /^(\S+)\s+($p_year)/o) { # Zone YYYY | ||
| 249 | ($zone, $yr) = ($1, $2); | ||
| 250 | } elsif ($rest =~ /^($p_year)/o) { # YYYY | ||
| 251 | ($yr) = ($1); | ||
| 252 | } else { # zilch, use current year | ||
| 253 | warn "Warning: No year in date ($date), using current\n"; | ||
| 254 | $yr = (localtime(time))[5]; | ||
| 255 | } | ||
| 256 | |||
| 257 | # Weekday Month DD YYYY HH:MM Zone | ||
| 258 | } elsif ( | ||
| 259 | $start =~ /($p_Weekdays),?\s+($p_Months)\s+($p_day),?\s+($p_year)$/) { | ||
| 260 | ($wday, $mon, $mday, $yr, $zone) = ($1, $2, $3, $4, $array[0]); | ||
| 261 | |||
| 262 | # All else fails! | ||
| 263 | } else { | ||
| 264 | return (); | ||
| 265 | } | ||
| 266 | |||
| 267 | # Modify month and weekday for lookup | ||
| 268 | $mon = $Month2Num{lc $mon} if defined($mon); | ||
| 269 | $wday = $WDay2Num{lc $wday} if defined($wday); | ||
| 270 | |||
| 271 | ($wday, $mday, $mon, $yr, $hr, $min, $sec, $zone); | ||
| 272 | } | ||
| 273 | |||
| 274 | ##--------------------------------------------------------------------------- | ||
| 275 | ## Routine to convert time in seconds to a month, day, and year | ||
| 276 | ## format. The format can be "mmddyy", "yymmdd", "ddmmyy". The | ||
| 277 | ## year can be specifed as "yyyy" if a 4 digit year is needed. | ||
| 278 | ## | ||
| 279 | sub time2mmddyy { | ||
| 280 | my ($time, $fmt) = ($_[0], $_[1]); | ||
| 281 | my ($day, $mon, $year, $ylen, $tmp); | ||
| 282 | if ($time) { | ||
| 283 | ($day, $mon, $year) = (localtime($time))[3, 4, 5]; | ||
| 284 | $year += 1900; | ||
| 285 | |||
| 286 | ## Compute length for year field | ||
| 287 | $ylen = $fmt =~ s/y/y/g; | ||
| 288 | substr($year, 0, 4 - $ylen) = ''; | ||
| 289 | |||
| 290 | ## Create string | ||
| 291 | if ($fmt =~ /ddmmyy/i) { # DDMMYY | ||
| 292 | $tmp = sprintf("%02d/%02d/%0${ylen}d", $day, $mon + 1, $year); | ||
| 293 | |||
| 294 | } elsif ($fmt =~ /yymmdd/i) { # YYMMDD | ||
| 295 | $tmp = sprintf("%0${ylen}d/%02d/%02d", $year, $mon + 1, $day); | ||
| 296 | |||
| 297 | } else { # MMDDYY | ||
| 298 | $tmp = sprintf("%02d/%02d/%0${ylen}d", $mon + 1, $day, $year); | ||
| 299 | } | ||
| 300 | |||
| 301 | } else { | ||
| 302 | $tmp = "--/--/--"; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | ##--------------------------------------------------------------------------- | ||
| 307 | ## zone_offset_to_secs translates a [+-]HHMM zone offset to | ||
| 308 | ## seconds. | ||
| 309 | ## | ||
| 310 | sub zone_offset_to_secs { | ||
| 311 | my ($off) = shift; | ||
| 312 | my ($sign, $min); | ||
| 313 | |||
| 314 | ## Check if just an hour specification | ||
| 315 | if (length($off) < 4) { | ||
| 316 | return $off * 3600; | ||
| 317 | } | ||
| 318 | ## Check for sign | ||
| 319 | if ($off =~ s/-//) { | ||
| 320 | $sign = -1; | ||
| 321 | } else { | ||
| 322 | $sign = 1; | ||
| 323 | s/\+//; | ||
| 324 | } | ||
| 325 | ## Extract minutes | ||
| 326 | $min = substr($off, -2, 2); | ||
| 327 | substr($off, -2, 2) = ""; # Just leave hour in $off | ||
| 328 | |||
| 329 | ## Translate to seconds | ||
| 330 | $sign * (($off * 3600) + ($min * 60)); | ||
| 331 | } | ||
| 332 | |||
| 333 | ##---------------------------------------------------------------------------## | ||
| 334 | 1; |