| 1 |
package Date::Manip::Base; |
| 2 |
# Copyright (c) 1995-2026 Sullivan Beck. All rights reserved. |
| 3 |
# This program is free software; you can redistribute it and/or modify it |
| 4 |
# under the same terms as Perl itself. |
| 5 |
|
| 6 |
############################################################################### |
| 7 |
# Any routine that starts with an underscore (_) is NOT intended for |
| 8 |
# public use. They are for internal use in the the Date::Manip |
| 9 |
# modules and are subject to change without warning or notice. |
| 10 |
# |
| 11 |
# ABSOLUTELY NO USER SUPPORT IS OFFERED FOR THESE ROUTINES! |
| 12 |
############################################################################### |
| 13 |
|
| 14 |
require 5.010000; |
| 15 |
use strict; |
| 16 |
use warnings; |
| 17 |
use integer; |
| 18 |
use utf8; |
| 19 |
use Carp; |
| 20 |
#use re 'debug'; |
| 21 |
|
| 22 |
use Date::Manip::Obj; |
| 23 |
use Date::Manip::TZ_Base; |
| 24 |
our @ISA = qw(Date::Manip::Obj Date::Manip::TZ_Base); |
| 25 |
|
| 26 |
use Encode qw(encode_utf8 from_to find_encoding decode _utf8_off _utf8_on is_utf8); |
| 27 |
require Date::Manip::Lang::index; |
| 28 |
|
| 29 |
our $VERSION; |
| 30 |
$VERSION='6.99'; |
| 31 |
END { undef $VERSION; } |
| 32 |
|
| 33 |
############################################################################### |
| 34 |
# BASE METHODS |
| 35 |
############################################################################### |
| 36 |
|
| 37 |
sub _init { |
| 38 |
my($self) = @_; |
| 39 |
|
| 40 |
$self->_init_cache(); |
| 41 |
$self->_init_language(); |
| 42 |
$self->_init_config(); |
| 43 |
$self->_init_events(); |
| 44 |
$self->_init_holidays(); |
| 45 |
$self->_init_now(); |
| 46 |
|
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
# The base object has some config-independant information which is |
| 51 |
# always reused, and only needs to be initialized once. |
| 52 |
sub _init_cache { |
| 53 |
my($self) = @_; |
| 54 |
return if (exists $$self{'cache'}{'init'}); |
| 55 |
$$self{'cache'}{'init'} = 1; |
| 56 |
|
| 57 |
# ly => {Y} = 0/1 1 if it is a leap year |
| 58 |
# ds1_mon => {Y}{M} = N days since 1BC for Y/M/1 |
| 59 |
# dow_mon => {Y}{M} = DOW day of week of Y/M/1 |
| 60 |
|
| 61 |
$$self{'cache'}{'ly'} = {}; |
| 62 |
$$self{'cache'}{'ds1_mon'} = {}; |
| 63 |
$$self{'cache'}{'dow_mon'} = {}; |
| 64 |
|
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
# Config dependent data. Needs to be reset every time the config is reset. |
| 69 |
sub _init_data { |
| 70 |
my($self,$force) = @_; |
| 71 |
return if (exists $$self{'data'}{'calc'} && ! $force); |
| 72 |
|
| 73 |
$$self{'data'}{'calc'} = {}; # Calculated values |
| 74 |
|
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
# Initializes config dependent data |
| 79 |
sub _init_config { |
| 80 |
my($self,$force) = @_; |
| 81 |
return if (exists $$self{'data'}{'sections'}{'conf'} && ! $force); |
| 82 |
$self->_init_data(); |
| 83 |
|
| 84 |
# |
| 85 |
# Set config defaults |
| 86 |
# |
| 87 |
|
| 88 |
$$self{'data'}{'sections'}{'conf'} = |
| 89 |
{ |
| 90 |
# Reset config, holiday lists, or events lists |
| 91 |
|
| 92 |
'defaults' => '', |
| 93 |
'eraseholidays' => '', |
| 94 |
'eraseevents' => '', |
| 95 |
|
| 96 |
# Which language to use when parsing dates. |
| 97 |
|
| 98 |
'language' => '', |
| 99 |
|
| 100 |
# 12/10 = Dec 10 (US) or Oct 12 (anything else) |
| 101 |
|
| 102 |
'dateformat' => '', |
| 103 |
|
| 104 |
# Define the work week (1=monday, 7=sunday) |
| 105 |
# |
| 106 |
# These have to be predefined to avoid a bootstrap issue, but |
| 107 |
# the true defaults are defined below. |
| 108 |
|
| 109 |
'workweekbeg' => 1, |
| 110 |
'workweekend' => 5, |
| 111 |
|
| 112 |
# If non-nil, a work day is treated as 24 hours long |
| 113 |
# (WorkDayBeg/WorkDayEnd ignored) |
| 114 |
|
| 115 |
'workday24hr' => '', |
| 116 |
|
| 117 |
# Start and end time of the work day (any time format allowed, |
| 118 |
# seconds ignored). If the defaults change, be sure to change |
| 119 |
# the starting value of bdlength above. |
| 120 |
|
| 121 |
'workdaybeg' => '', |
| 122 |
'workdayend' => '', |
| 123 |
|
| 124 |
# 2 digit years fall into the 100 year period given by [ CURR-N, |
| 125 |
# CURR+(99-N) ] where N is 0-99. Default behavior is 89, but |
| 126 |
# other useful numbers might be 0 (forced to be this year or |
| 127 |
# later) and 99 (forced to be this year or earlier). It can |
| 128 |
# also be set to 'c' (current century) or 'cNN' (i.e. c18 |
| 129 |
# forces the year to bet 1800-1899). Also accepts the form |
| 130 |
# cNNNN to give the 100 year period NNNN to NNNN+99. |
| 131 |
|
| 132 |
'yytoyyyy' => '', |
| 133 |
|
| 134 |
# First day of the week (1=monday, 7=sunday). ISO 8601 says |
| 135 |
# monday. |
| 136 |
|
| 137 |
'firstday' => '', |
| 138 |
|
| 139 |
# If this is a value 'jan1' to 'jan7', then it means that the first |
| 140 |
# week of the year contains that date. If the value is 'dow1' to |
| 141 |
# 'dow7', then the first week of the year is the one that contains |
| 142 |
# the first occurence of the DoW given. If the value is 'firstday', |
| 143 |
# then the first week of the year is the one that contains the first |
| 144 |
# occurence of the DoW given in the FirstDay config variable. |
| 145 |
# |
| 146 |
# The default ISO 8601 definition is the first week is the one which |
| 147 |
# contains Jan 4 (so at least 4 of the days). |
| 148 |
|
| 149 |
'week1ofyear' => '', |
| 150 |
|
| 151 |
# Date::Manip printable format |
| 152 |
# 0 = YYYYMMDDHH:MN:SS |
| 153 |
# 1 = YYYYHHMMDDHHMNSS |
| 154 |
# 2 = YYYY-MM-DD-HH:MN:SS |
| 155 |
|
| 156 |
'printable' => '', |
| 157 |
|
| 158 |
# If 'today' is a holiday, we look either to 'tomorrow' or |
| 159 |
# 'yesterday' for the nearest business day. By default, we'll |
| 160 |
# always look 'tomorrow' first. |
| 161 |
|
| 162 |
'tomorrowfirst' => 1, |
| 163 |
|
| 164 |
# Used to set the current date/time/timezone. |
| 165 |
|
| 166 |
'forcedate' => 0, |
| 167 |
'setdate' => 0, |
| 168 |
|
| 169 |
# Use this to set the default range of the recurrence. |
| 170 |
|
| 171 |
'recurrange' => '', |
| 172 |
'maxrecurattempts' => 100, |
| 173 |
|
| 174 |
# Use this to set the default time. |
| 175 |
|
| 176 |
'defaulttime' => 'midnight', |
| 177 |
|
| 178 |
# Whether or not to use a period as a time separator. |
| 179 |
|
| 180 |
'periodtimesep' => 0, |
| 181 |
|
| 182 |
# How to parse mmm#### strings |
| 183 |
|
| 184 |
'format_mmmyyyy' => '', |
| 185 |
|
| 186 |
# Whether to use the default printf formats or the POSIX ones. |
| 187 |
|
| 188 |
'use_posix_printf' => 0, |
| 189 |
|
| 190 |
# *** DEPRECATED 7.0 *** |
| 191 |
|
| 192 |
'tz' => '', |
| 193 |
'jan1week1' => '', |
| 194 |
}; |
| 195 |
|
| 196 |
# |
| 197 |
# Calculate delta field lengths |
| 198 |
# |
| 199 |
|
| 200 |
# non-business |
| 201 |
$$self{'data'}{'len'}{'standard'} = |
| 202 |
{ 'yl' => 31556952, # 365.2425 * 24 * 3600 |
| 203 |
'ml' => 2629746, # yl / 12 |
| 204 |
'wl' => 604800, # 6 * 24 * 3600 |
| 205 |
'dl' => 86400, # 24 * 3600 |
| 206 |
}; |
| 207 |
$self->_calc_workweek(); |
| 208 |
|
| 209 |
# |
| 210 |
# Initialize some config variables that do some additional work. |
| 211 |
# |
| 212 |
|
| 213 |
$self->_config_var('workday24hr', 1); |
| 214 |
$self->_config_var('workdaybeg', '08:00:00'); |
| 215 |
$self->_config_var('workdayend', '17:00:00'); |
| 216 |
$self->_config_var('workday24hr', 0); |
| 217 |
$self->_config_var('dateformat', 'US'); |
| 218 |
$self->_config_var('yytoyyyy', 89); |
| 219 |
$self->_config_var('printable', 0); |
| 220 |
$self->_config_var('firstday', 1); |
| 221 |
$self->_config_var('week1ofyear', 'jan4'); |
| 222 |
$self->_config_var('workweekbeg', 1); |
| 223 |
$self->_config_var('workweekend', 5); |
| 224 |
$self->_config_var('language', 'english'); |
| 225 |
$self->_config_var('recurrange', 'none'); |
| 226 |
$self->_config_var('maxrecurattempts', 100); |
| 227 |
$self->_config_var('defaulttime', 'midnight'); |
| 228 |
|
| 229 |
# Set OS specific defaults |
| 230 |
|
| 231 |
my $os = $self->_os(); |
| 232 |
|
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
sub _calc_workweek { |
| 237 |
my($self,$beg,$end) = @_; |
| 238 |
|
| 239 |
$beg = $self->_config('workweekbeg') if (! $beg); |
| 240 |
$end = $self->_config('workweekend') if (! $end); |
| 241 |
|
| 242 |
$$self{'data'}{'len'}{'workweek'} = $end - $beg + 1; |
| 243 |
|
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
sub _calc_bdlength { |
| 248 |
my($self) = @_; |
| 249 |
|
| 250 |
my @beg = @{ $$self{'data'}{'calc'}{'workdaybeg'} }; |
| 251 |
my @end = @{ $$self{'data'}{'calc'}{'workdayend'} }; |
| 252 |
|
| 253 |
$$self{'data'}{'len'}{'bdlength'} = |
| 254 |
($end[0]-$beg[0])*3600 + ($end[1]-$beg[1])*60 + ($end[2]-$beg[2]); |
| 255 |
|
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
sub _init_business_length { |
| 260 |
my($self) = @_; |
| 261 |
|
| 262 |
no integer; |
| 263 |
my $x = $$self{'data'}{'len'}{'workweek'}; |
| 264 |
my $y_to_d = $x/7 * 365.2425; |
| 265 |
my $d_to_s = $$self{'data'}{'len'}{'bdlength'}; |
| 266 |
my $w_to_d = $x; |
| 267 |
|
| 268 |
$$self{'data'}{'len'}{'business'} = { 'yl' => $y_to_d * $d_to_s, |
| 269 |
'ml' => $y_to_d * $d_to_s / 12, |
| 270 |
'wl' => $w_to_d * $d_to_s, |
| 271 |
'dl' => $d_to_s, |
| 272 |
}; |
| 273 |
|
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
# Events and holidays are reset only when they are read in. |
| 278 |
sub _init_events { |
| 279 |
my($self,$force) = @_; |
| 280 |
return if (exists $$self{'data'}{'events'} && ! $force); |
| 281 |
|
| 282 |
# {data}{sections}{events} = [ STRING, EVENT_NAME, ... ] |
| 283 |
# |
| 284 |
# {data}{events}{I}{type} = TYPE |
| 285 |
# {name} = NAME |
| 286 |
# TYPE: specified An event with a start/end date (only parsed once) |
| 287 |
# {beg} = DATE_OBJECT |
| 288 |
# {end} = DATE_OBJECT |
| 289 |
# TYPE: ym |
| 290 |
# {beg} = YM_STRING |
| 291 |
# {end} = YM_STRING (only for YM;YM) |
| 292 |
# {YEAR} = [ DATE_OBJECT, DATE_OBJECT ] |
| 293 |
# TYPE: date An event specified by a date string and delta |
| 294 |
# {beg} = DATE_STRING |
| 295 |
# {end} = DATE_STRING (only for Date;Date) |
| 296 |
# {delta} = DELTA_OBJECT (only for Date;Delta) |
| 297 |
# {YEAR} = [ DATE_OBJECT, DATE_OBJECT ] |
| 298 |
# TYPE: recur |
| 299 |
# {recur} = RECUR_OBJECT |
| 300 |
# {delta} = DELTA_OBJECT |
| 301 |
# |
| 302 |
# {data}{eventyears}{YEAR} = 0/1 |
| 303 |
# {data}{eventobjs} = 0/1 |
| 304 |
|
| 305 |
$$self{'data'}{'events'} = {}; |
| 306 |
$$self{'data'}{'sections'}{'events'} = []; |
| 307 |
$$self{'data'}{'eventyears'} = {}; |
| 308 |
$$self{'data'}{'eventobjs'} = 0; |
| 309 |
|
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
sub _init_holidays { |
| 314 |
my($self,$force) = @_; |
| 315 |
return if (exists $$self{'data'}{'holidays'} && ! $force); |
| 316 |
|
| 317 |
# {data}{sections}{holidays} = [ STRING, HOLIDAY_NAME, ... ] |
| 318 |
# |
| 319 |
# {data}{holidays}{init} = 1 if holidays have been initialized |
| 320 |
# {ydone} = { Y => 1 } |
| 321 |
# {yhols} = { Y => NAME => [Y,M,D] } |
| 322 |
# {hols} = { NAME => Y => [Y,M,D] } |
| 323 |
# {dates} = { Y => M => D => NAME } |
| 324 |
# {defs} = [ NAME DEF NAME DEF ... ] |
| 325 |
# NAME is the name of a holiday (it will |
| 326 |
# be 'DMunnamed I' for the Ith unnamed |
| 327 |
# holiday) |
| 328 |
# DEF is a string or a Recur |
| 329 |
# {data}{init_holidays} = 1 if currently initializing holidays |
| 330 |
|
| 331 |
$$self{'data'}{'holidays'} = {}; |
| 332 |
$$self{'data'}{'sections'}{'holidays'} = []; |
| 333 |
$$self{'data'}{'init_holidays'} = 0; |
| 334 |
|
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
sub _init_now { |
| 339 |
my($self) = @_; |
| 340 |
|
| 341 |
# {'data'}{'now'} = { |
| 342 |
# date => [Y,M,D,H,MN,S] now |
| 343 |
# isdst => ISDST |
| 344 |
# offset => [H,MN,S] |
| 345 |
# abb => ABBREV |
| 346 |
# |
| 347 |
# force => 0/1 SetDate/ForceDate information |
| 348 |
# set => 0/1 |
| 349 |
# setsecs => SECS time (secs since epoch) when |
| 350 |
# SetDate was called |
| 351 |
# setdate => [Y,M,D,H,MN,S] date (IN GMT) we're calling |
| 352 |
# now when SetDate was called |
| 353 |
# |
| 354 |
# tz => ZONE timezone we're working in |
| 355 |
# systz => ZONE timezone of the system |
| 356 |
# } |
| 357 |
# |
| 358 |
|
| 359 |
$$self{'data'}{'now'} = {}; |
| 360 |
$$self{'data'}{'now'}{'force'} = 0; |
| 361 |
$$self{'data'}{'now'}{'set'} = 0; |
| 362 |
$$self{'data'}{'tmpnow'} = []; |
| 363 |
|
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
# Language information only needs to be initialized if the language changes. |
| 368 |
sub _init_language { |
| 369 |
my($self,$force) = @_; |
| 370 |
return if (exists $$self{'data'}{'lang'} && ! $force); |
| 371 |
|
| 372 |
$$self{'data'}{'lang'} = {}; # Current language info |
| 373 |
$$self{'data'}{'rx'} = {}; # Regexps generated from language |
| 374 |
$$self{'data'}{'words'} = {}; # Types of words in the language |
| 375 |
$$self{'data'}{'wordval'} = {}; # Value of words in the language |
| 376 |
|
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
############################################################################### |
| 381 |
# MAIN METHODS |
| 382 |
############################################################################### |
| 383 |
|
| 384 |
# Use an algorithm from Calendar FAQ (except that I subtract 305 to get |
| 385 |
# Jan 1, 0001 = day #1). |
| 386 |
# |
| 387 |
sub days_since_1BC { |
| 388 |
my($self,$arg) = @_; |
| 389 |
|
| 390 |
if (ref($arg)) { |
| 391 |
my($y,$m,$d) = @$arg; |
| 392 |
$m = ($m + 9) % 12; |
| 393 |
$y = $y - $m/10; |
| 394 |
return 365*$y + $y/4 - $y/100 + $y/400 + ($m*306 + 5)/10 + ($d - 1) - 305; |
| 395 |
} else { |
| 396 |
my $g = $arg + 305; |
| 397 |
no integer; |
| 398 |
my $y = int((10000*$g + 14780)/3652425); |
| 399 |
use integer; |
| 400 |
my $ddd = $g - (365*$y + $y/4 - $y/100 + $y/400); |
| 401 |
if ($ddd < 0) { |
| 402 |
$y = $y - 1; |
| 403 |
$ddd = $g - (365*$y + $y/4 - $y/100 + $y/400); |
| 404 |
} |
| 405 |
my $mi = (100*$ddd + 52)/3060; |
| 406 |
my $mm = ($mi + 2) % 12 + 1; |
| 407 |
$y = $y + ($mi + 2)/12; |
| 408 |
my $dd = $ddd - ($mi*306 + 5)/10 + 1; |
| 409 |
return [$y, $mm, $dd]; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
# Algorithm from the Calendar FAQ |
| 414 |
# |
| 415 |
sub day_of_week { |
| 416 |
my($self,$date) = @_; |
| 417 |
my($y,$m,$d) = @$date; |
| 418 |
|
| 419 |
my $a = (14-$m)/12; |
| 420 |
$y = $y-$a; |
| 421 |
$m = $m + 12*$a - 2; |
| 422 |
my $dow = ($d + $y + $y/4 - $y/100 + $y/400 + (31*$m)/12) % 7; |
| 423 |
$dow = 7 if ($dow==0); |
| 424 |
return $dow; |
| 425 |
} |
| 426 |
|
| 427 |
sub leapyear { |
| 428 |
my($self,$y) = @_; |
| 429 |
return 1 if ( ( ($y % 4 == 0) and ($y % 100 != 0) ) or |
| 430 |
$y % 400 == 0 ); |
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
sub days_in_year { |
| 435 |
my($self,$y) = @_; |
| 436 |
return ($self->leapyear($y) ? 366 : 365); |
| 437 |
} |
| 438 |
|
| 439 |
# Uses algorithm from: |
| 440 |
# http://www.dispersiondesign.com/articles/time/number_of_days_in_a_month |
| 441 |
# |
| 442 |
sub days_in_month { |
| 443 |
my($self,$y,$m) = @_; |
| 444 |
if (! $m) { |
| 445 |
return (31,29,31,30, 31,30,31,31, 30,31,30,31) if ($self->leapyear($y)); |
| 446 |
return (31,28,31,30, 31,30,31,31, 30,31,30,31); |
| 447 |
|
| 448 |
} elsif ($m == 2) { |
| 449 |
return 28 + $self->leapyear($y); |
| 450 |
|
| 451 |
} else { |
| 452 |
return 31 - ($m-1) % 7 % 2; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
{ |
| 457 |
# DinM = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) |
| 458 |
my(@doy_days) = ( [0, 31, 59, 90,120,151,181,212,243,273,304,334,365], |
| 459 |
[0, 31, 60, 91,121,152,182,213,244,274,305,335,366], |
| 460 |
); |
| 461 |
|
| 462 |
|
| 463 |
sub day_of_year { |
| 464 |
my($self,@args) = @_; |
| 465 |
no integer; |
| 466 |
my($n,$ly,$tmp,$remain,$day,$y,$m,$d,$h,$mn,$s,$time); |
| 467 |
|
| 468 |
if (@args == 2) { |
| 469 |
# $date = day_of_year($y,$day); |
| 470 |
|
| 471 |
($y,$tmp) = @args; |
| 472 |
|
| 473 |
$ly = $self->leapyear($y); |
| 474 |
$time = 1 if ($tmp =~ /\./); |
| 475 |
$n = int($tmp); |
| 476 |
$remain = $tmp - $n; |
| 477 |
|
| 478 |
# Calculate the month and the day |
| 479 |
for ($m=1; $m<=12; $m++) { |
| 480 |
last if ($n<=($doy_days[$ly][$m])); |
| 481 |
} |
| 482 |
$d = $n-($doy_days[$ly][$m-1]); |
| 483 |
return [$y,$m,$d] if (! $time); |
| 484 |
|
| 485 |
# Calculate the hours, minutes, and seconds into the day. |
| 486 |
|
| 487 |
$s = $remain * 86400; |
| 488 |
$mn = int($s/60); |
| 489 |
$s = $s - ($mn*60); |
| 490 |
$s = sprintf('%0.2f',$s) if ("$s" ne int($s)); |
| 491 |
$h = int($mn/60); |
| 492 |
$mn = $mn % 60; |
| 493 |
|
| 494 |
return [$y,$m,$d,$h,$mn,$s]; |
| 495 |
|
| 496 |
} else { |
| 497 |
($y,$m,$d,$h,$mn,$s) = @{ $args[0] }; |
| 498 |
|
| 499 |
$ly = ($m > 2 ? $self->leapyear($y) : 0); |
| 500 |
$day = ($doy_days[$ly][$m-1]+$d); |
| 501 |
|
| 502 |
return $day if (! defined $h); |
| 503 |
|
| 504 |
$day += ($h*3600 + $mn*60 + $s)/86400; |
| 505 |
return $day; |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
# Can be the nth DoW of year or month (if $m given). Returns undef if |
| 511 |
# the date doesn't exists (i.e. 5th Sunday in a month with only 4). |
| 512 |
# |
| 513 |
sub nth_day_of_week { |
| 514 |
my($self,$y,$n,$dow,$m) = @_; |
| 515 |
$y += 0; |
| 516 |
$m = ($m ? $m+0 : 0); |
| 517 |
|
| 518 |
# $d is the current DoM (if $m) or DoY |
| 519 |
# $max is the max value allowed for $d |
| 520 |
# $ddow is the DoW of $d |
| 521 |
|
| 522 |
my($d,$max,$ddow); |
| 523 |
|
| 524 |
if ($m) { |
| 525 |
$max = $self->days_in_month($y,$m); |
| 526 |
$d = ($n<0 ? $max : 1); |
| 527 |
$ddow = $self->day_of_week([$y,$m,$d]); |
| 528 |
} else { |
| 529 |
$max = $self->days_in_year($y); |
| 530 |
$d = ($n<0 ? $max : 1); |
| 531 |
if ($n<0) { |
| 532 |
$d = $max; |
| 533 |
$ddow = $self->day_of_week([$y,12,31]); |
| 534 |
} else { |
| 535 |
$d = 1; |
| 536 |
$ddow = $self->day_of_week([$y,1,1]); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
# Find the first occurrence of $dow on or after $d (if $n>0) |
| 541 |
# or the last occurrence of $dow on or before $d (if ($n<0); |
| 542 |
|
| 543 |
if ($dow < $ddow) { |
| 544 |
$d += 7 - ($ddow-$dow); |
| 545 |
} else { |
| 546 |
$d += ($dow-$ddow); |
| 547 |
} |
| 548 |
$d -= 7 if ($d > $max); |
| 549 |
|
| 550 |
# Find the nth occurrence of $dow |
| 551 |
|
| 552 |
if ($n > 1) { |
| 553 |
$d += 7*($n-1); |
| 554 |
return undef if ($d > $max); |
| 555 |
} elsif ($n < -1) { |
| 556 |
$d -= 7*(-1*$n-1); |
| 557 |
return undef if ($d < 1); |
| 558 |
} |
| 559 |
|
| 560 |
# Return the date |
| 561 |
|
| 562 |
if ($m) { |
| 563 |
return [$y,$m,$d]; |
| 564 |
} |
| 565 |
return $self->day_of_year($y,$d); |
| 566 |
} |
| 567 |
|
| 568 |
{ |
| 569 |
# Integer arithmetic doesn't work due to the size of the numbers. |
| 570 |
no integer; |
| 571 |
# my $sec_70 =($self->days_since_1BC([1970,1,1])-1)*24*3600; |
| 572 |
my $sec_70 = 62135596800; |
| 573 |
|
| 574 |
# Using 'global' variables saves 4% |
| 575 |
my($y,$m,$d,$h,$mn,$s,$sec,$sec_0,$tmp); |
| 576 |
sub secs_since_1970 { |
| 577 |
my($self,$arg) = @_; |
| 578 |
|
| 579 |
if (ref($arg)) { |
| 580 |
($y,$m,$d,$h,$mn,$s) = @$arg; |
| 581 |
$sec_0 = ($self->days_since_1BC([$y,$m,$d])-1)*24*3600 + $h*3600 + |
| 582 |
$mn*60 + $s; |
| 583 |
$sec = $sec_0 - $sec_70; |
| 584 |
return $sec; |
| 585 |
|
| 586 |
} else { |
| 587 |
($sec) = $arg; |
| 588 |
$sec_0 = $sec_70 + $sec; |
| 589 |
$tmp = int($sec_0/24/3600)+1; |
| 590 |
my $ymd = $self->days_since_1BC($tmp); |
| 591 |
($y,$m,$d) = @$ymd; |
| 592 |
$sec_0 -= ($tmp-1)*24*3600; |
| 593 |
$h = int($sec_0/3600); |
| 594 |
$sec_0 -= $h*3600; |
| 595 |
$mn = int($sec_0/60); |
| 596 |
$s = $sec_0 - $mn*60; |
| 597 |
return [$y,$m,$d,$h,$mn,$s]; |
| 598 |
} |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
sub check { |
| 603 |
my($self,$date) = @_; |
| 604 |
my($y,$m,$d,$h,$mn,$s) = @$date; |
| 605 |
|
| •606 |
# The range tests below numify each field, and numifying truncates a |
| 607 |
# string at the first character which is not an ASCII digit, so a |
| 608 |
# field such as "202\x{664}" would pass them as 202. Require ASCII |
| 609 |
# digits. |
| 610 |
|
| 611 |
foreach my $val ($y,$m,$d) { |
| 612 |
return 0 if (! defined($val) || $val !~ /^[0-9]+$/); |
| 613 |
} |
| 614 |
|
| 615 |
return 0 if (! $self->check_time([$h,$mn,$s]) || |
| 616 |
$y<1 || $y>9999 || |
| 617 |
$m<1 || $m>12); |
| 618 |
|
| 619 |
my $days = $self->days_in_month($y,$m); |
| 620 |
|
| 621 |
return 0 if ($d<1 || $d>$days); |
| 622 |
return 1; |
| 623 |
} |
| 624 |
|
| 625 |
sub check_time { |
| 626 |
my($self,$hms) = @_; |
| 627 |
my($h,$mn,$s) = @$hms; |
| 628 |
|
| •629 |
return 0 if ("$h:$mn:$s" !~ /^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}$/o || |
| 630 |
$h > 24 || $mn > 59 || $s > 59 || |
| 631 |
($h == 24 && ($mn || $s))); |
| 632 |
return 1; |
| 633 |
} |
| 634 |
|
| 635 |
sub week1_day1 { |
| 636 |
my($self,$year) = @_; |
| 637 |
my $firstday = $self->_config('firstday'); |
| 638 |
my $week1ofyear = $self->_config('week1ofyear'); |
| 639 |
$self->_week1_day1($year,$firstday,$week1ofyear); |
| 640 |
} |
| 641 |
sub _week1_day1 { |
| 642 |
my($self,$year,$firstday,$week1ofyear) = @_; |
| 643 |
return $$self{'cache'}{'week1day1'}{$firstday}{$week1ofyear}{$year} |
| 644 |
if (exists $$self{'cache'}{'week1day1'}{$firstday}{$week1ofyear}{$year}); |
| 645 |
|
| 646 |
my($y,$m,$d,$firstdow); |
| 647 |
|
| 648 |
# Get the starting date. |
| 649 |
|
| 650 |
if ($week1ofyear =~ /^jan([1-7])$/) { |
| 651 |
# First week contains 1/D/YYYY where D is $1 |
| 652 |
($y,$m,$d) = ($year,1,$1); |
| 653 |
} else { |
| 654 |
# First week contains the first DOW given by $1 or firstday |
| 655 |
# We'll start at Jan 1. |
| 656 |
if ($week1ofyear =~ /^dow([1-7])$/) { |
| 657 |
$firstdow = $1; |
| 658 |
} else { |
| 659 |
$firstdow = $firstday; |
| 660 |
} |
| 661 |
|
| 662 |
# Start at Jan 1 and move forward to the first DOW |
| 663 |
($y,$m,$d) = ($year,1,1); |
| 664 |
} |
| 665 |
|
| 666 |
my $dow = $self->day_of_week([$y,$m,$d]); |
| 667 |
|
| 668 |
# If we're looking for the first occurence of a DOW, move forward to it |
| 669 |
# (counting today). |
| 670 |
|
| 671 |
if ($firstdow) { |
| 672 |
my $forward = $firstdow - $dow; |
| 673 |
$forward += 7 if ($forward < 0); |
| 674 |
$d += $forward; |
| 675 |
$dow = $firstdow; |
| 676 |
} |
| 677 |
|
| 678 |
# Go back to the previous (counting today) $firstday |
| 679 |
|
| 680 |
my $backward = $dow - $firstday; |
| 681 |
$backward += 7 if ($backward < 0); |
| 682 |
$d -= $backward; |
| 683 |
if ($d<1) { |
| 684 |
$y--; |
| 685 |
$m = 12; |
| 686 |
$d += 31; |
| 687 |
} |
| 688 |
|
| 689 |
$$self{'cache'}{'week1day1'}{$firstday}{$week1ofyear}{$year} = [ $y,$m,$d ]; |
| 690 |
return [$y,$m,$d]; |
| 691 |
} |
| 692 |
|
| 693 |
sub weeks_in_year { |
| 694 |
my($self,$y) = @_; |
| 695 |
my $firstday = $self->_config('firstday'); |
| 696 |
my $week1ofyear = $self->_config('week1ofyear'); |
| 697 |
return $$self{'cache'}{'wiy'}{$firstday}{$week1ofyear}{$y} |
| 698 |
if (exists $$self{'cache'}{'wiy'}{$firstday}{$week1ofyear}{$y}); |
| 699 |
|
| 700 |
# Get the week1 day1 dates for this year and the next one. |
| 701 |
my ($y1,$m1,$d1) = @{ $self->_week1_day1($y,$firstday,$week1ofyear) }; |
| 702 |
my ($y2,$m2,$d2) = @{ $self->_week1_day1($y+1,$firstday,$week1ofyear) }; |
| 703 |
|
| 704 |
# Calculate the number of days between them. |
| 705 |
my $diy = $self->days_in_year($y); |
| 706 |
if ($y1 < $y) { |
| 707 |
$diy += (32-$d1); |
| 708 |
} else { |
| 709 |
$diy -= ($d1-1); |
| 710 |
} |
| 711 |
if ($y2 < $y+1) { |
| 712 |
$diy -= (32-$d2); |
| 713 |
} else { |
| 714 |
$diy += ($d2-1); |
| 715 |
} |
| 716 |
|
| 717 |
$diy = $diy/7; |
| 718 |
$$self{'cache'}{'wiy'}{$firstday}{$week1ofyear}{$y} = $diy; |
| 719 |
return $diy; |
| 720 |
} |
| 721 |
|
| 722 |
sub week_of_year { |
| 723 |
my($self,@args) = @_; |
| 724 |
|
| 725 |
my $firstday = $self->_config('firstday'); |
| 726 |
my $week1ofyear = $self->_config('week1ofyear'); |
| 727 |
$self->_week_of_year($firstday,$week1ofyear,@args); |
| 728 |
} |
| 729 |
sub _week_of_year { |
| 730 |
my($self,$firstday,$week1ofyear,@args) = @_; |
| 731 |
|
| 732 |
if ($#args == 1) { |
| 733 |
# (y,m,d) = week_of_year(y,w) |
| 734 |
my($year,$w) = @args; |
| 735 |
|
| 736 |
return $$self{'cache'}{'woy'}{$firstday}{$week1ofyear}{$year}{$w} |
| 737 |
if (exists $$self{'cache'}{'woy'}{$firstday}{$week1ofyear}{$year}{$w}); |
| 738 |
|
| 739 |
my $ymd = $self->_week1_day1($year,$firstday,$week1ofyear); |
| 740 |
$ymd = $self->calc_date_days($ymd,($w-1)*7) if ($w > 1); |
| 741 |
|
| 742 |
$$self{'cache'}{'woy'}{$firstday}{$week1ofyear}{$year}{$w} = $ymd; |
| 743 |
return $ymd; |
| 744 |
} |
| 745 |
|
| 746 |
# (y,w) = week_of_year([y,m,d]) |
| 747 |
my($y,$m,$d) = @{ $args[0] }; |
| 748 |
|
| 749 |
# Get the first day of the first week. If the date is before that, |
| 750 |
# it's the last week of last year. |
| 751 |
|
| 752 |
my($y0,$m0,$d0) = @{ $self->_week1_day1($y,$firstday,$week1ofyear) }; |
| 753 |
if ($y0==$y && $m==1 && $d<$d0) { |
| 754 |
return($y-1,$self->weeks_in_year($y-1)); |
| 755 |
} |
| 756 |
|
| 757 |
# Otherwise, we'll figure out how many days are between the two and |
| 758 |
# divide by 7 to figure out how many weeks in it is. |
| 759 |
|
| 760 |
my $n = $self->day_of_year([$y,$m,$d]); |
| 761 |
if ($y0<$y) { |
| 762 |
$n += (32-$d0); |
| 763 |
} else { |
| 764 |
$n -= ($d0-1); |
| 765 |
} |
| 766 |
my $w = 1+int(($n-1)/7); |
| 767 |
|
| 768 |
# Make sure we're not into the first week of next year. |
| 769 |
|
| 770 |
if ($w>$self->weeks_in_year($y)) { |
| 771 |
return($y+1,1); |
| 772 |
} |
| 773 |
return($y,$w); |
| 774 |
} |
| 775 |
|
| 776 |
############################################################################### |
| 777 |
# CALC METHODS |
| 778 |
############################################################################### |
| 779 |
|
| 780 |
sub calc_date_date { |
| 781 |
my($self,$date0,$date1) = @_; |
| 782 |
|
| 783 |
# Order them so date0 < date1 |
| 784 |
# If $minus = 1, then the delta is negative |
| 785 |
|
| 786 |
my $minus = 0; |
| 787 |
my $cmp = $self->cmp($date0,$date1); |
| 788 |
|
| 789 |
if ($cmp == 0) { |
| 790 |
return [0,0,0]; |
| 791 |
|
| 792 |
} elsif ($cmp == 1) { |
| 793 |
$minus = 1; |
| 794 |
my $tmp = $date1; |
| 795 |
$date1 = $date0; |
| 796 |
$date0 = $tmp; |
| 797 |
} |
| 798 |
|
| 799 |
my($y0,$m0,$d0,$h0,$mn0,$s0) = @$date0; |
| 800 |
my($y1,$m1,$d1,$h1,$mn1,$s1) = @$date1; |
| 801 |
|
| 802 |
my $sameday = ($y0 == $y1 && $m0 == $m1 && $d0 == $d1 ? 1 : 0); |
| 803 |
|
| 804 |
# Handle the various cases. |
| 805 |
|
| 806 |
my($dh,$dm,$ds); |
| 807 |
if ($sameday) { |
| 808 |
($dh,$dm,$ds) = @{ $self->_calc_hms_hms([$h0,$mn0,$s0],[$h1,$mn1,$s1]) }; |
| 809 |
|
| 810 |
} else { |
| 811 |
# y0-m0-d0 h0:mn0:s0 -> y0-m0-d0 24:00:00 |
| 812 |
# y1-m1-d1 h1:mn1:s1 -> y1-m1-d1 00:00:00 |
| 813 |
|
| 814 |
my $t1 = $self->_calc_hms_hms([$h0,$mn0,$s0],[24,0,0]); |
| 815 |
my $t2 = $self->_calc_hms_hms([0,0,0],[$h1,$mn1,$s1]); |
| 816 |
($dh,$dm,$ds) = @{ $self->calc_time_time($t1,$t2) }; |
| 817 |
|
| 818 |
my $dd0 = $self->days_since_1BC([$y0,$m0,$d0]); |
| 819 |
$dd0++; |
| 820 |
my $dd1 = $self->days_since_1BC([$y1,$m1,$d1]); |
| 821 |
$dh += ($dd1-$dd0)*24; |
| 822 |
} |
| 823 |
|
| 824 |
if ($minus) { |
| 825 |
$dh *= -1; |
| 826 |
$dm *= -1; |
| 827 |
$ds *= -1; |
| 828 |
} |
| 829 |
return [$dh,$dm,$ds]; |
| 830 |
} |
| 831 |
|
| 832 |
sub calc_date_days { |
| 833 |
my($self,$date,$n,$subtract) = @_; |
| 834 |
my($y,$m,$d,$h,$mn,$s) = @$date; |
| 835 |
my($ymdonly) = (defined $h ? 0 : 1); |
| 836 |
|
| 837 |
$n *= -1 if ($subtract); |
| 838 |
my $d1bc = $self->days_since_1BC([$y,$m,$d]); |
| 839 |
$d1bc += $n; |
| 840 |
my $ymd = $self->days_since_1BC($d1bc); |
| 841 |
|
| 842 |
if ($ymdonly) { |
| 843 |
return $ymd; |
| 844 |
} else { |
| 845 |
return [@$ymd,$h*1,$mn*1,$s*1]; |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
sub calc_date_delta { |
| 850 |
my($self,$date,$delta,$subtract) = @_; |
| 851 |
my($y,$m,$d,$h,$mn,$s) = @$date; |
| 852 |
my($dy,$dm,$dw,$dd,$dh,$dmn,$ds) = @$delta; |
| 853 |
|
| 854 |
($y,$m,$d) = |
| 855 |
@{ $self->_calc_date_ymwd([$y,$m,$d], [$dy,$dm,$dw,$dd], $subtract) }; |
| 856 |
|
| 857 |
return $self->calc_date_time([$y,$m,$d,$h,$mn,$s],[$dh,$dmn,$ds],$subtract); |
| 858 |
} |
| 859 |
|
| 860 |
sub calc_date_time { |
| 861 |
my($self,$date,$time,$subtract) = @_; |
| 862 |
my($y,$m,$d,$h,$mn,$s) = @$date; |
| 863 |
my($dh,$dmn,$ds) = @$time; |
| 864 |
|
| 865 |
if ($ds > 59 || $ds < -59) { |
| 866 |
$dmn += int($ds/60); |
| 867 |
$ds = $ds % 60; |
| 868 |
} |
| 869 |
if ($dmn > 59 || $dmn < -59) { |
| 870 |
$dh += int($dmn/60); |
| 871 |
$dmn = $dmn % 60; |
| 872 |
} |
| 873 |
my $dd = 0; |
| 874 |
if ($dh > 23 || $dh < -23) { |
| 875 |
$dd = int($dh/24); |
| 876 |
$dh = $dh % 24; |
| 877 |
} |
| 878 |
|
| 879 |
# Handle subtraction |
| 880 |
if ($subtract) { |
| 881 |
$dh *= -1; |
| 882 |
$dmn *= -1; |
| 883 |
$ds *= -1; |
| 884 |
$dd *= -1; |
| 885 |
} |
| 886 |
|
| 887 |
if ($dd == 0) { |
| 888 |
$y *= 1; |
| 889 |
$m *= 1; |
| 890 |
$d *= 1; |
| 891 |
} else { |
| 892 |
($y,$m,$d) = @{ $self->calc_date_days([$y,$m,$d],$dd) }; |
| 893 |
} |
| 894 |
|
| 895 |
$self->_mod_add(60,$ds,\$s,\$mn); |
| 896 |
$self->_mod_add(60,$dmn,\$mn,\$h); |
| 897 |
$self->_mod_add(24,$dh,\$h,\$d); |
| 898 |
|
| 899 |
if ($d<1) { |
| 900 |
$m--; |
| 901 |
$y--, $m=12 if ($m<1); |
| 902 |
my $day_in_mon = $self->days_in_month($y,$m); |
| 903 |
$d += $day_in_mon; |
| 904 |
} else { |
| 905 |
my $day_in_mon = $self->days_in_month($y,$m); |
| 906 |
if ($d>$day_in_mon) { |
| 907 |
$d -= $day_in_mon; |
| 908 |
$m++; |
| 909 |
$y++, $m=1 if ($m>12); |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
return [$y,$m,$d,$h,$mn,$s]; |
| 914 |
} |
| 915 |
|
| 916 |
sub _calc_date_time_strings { |
| 917 |
my($self,$date,$time,$subtract) = @_; |
| 918 |
my @date = @{ $self->split('date',$date) }; |
| 919 |
return '' if (! @date); |
| 920 |
my @time = @{ $self->split('time',$time) }; |
| 921 |
|
| 922 |
my @date2 = @{ $self->calc_date_time(\@date,\@time,$subtract) }; |
| 923 |
|
| 924 |
return $self->join('date',\@date2); |
| 925 |
} |
| 926 |
|
| 927 |
sub _calc_date_ymwd { |
| 928 |
my($self,$date,$ymwd,$subtract) = @_; |
| 929 |
my($y,$m,$d,$h,$mn,$s) = @$date; |
| 930 |
my($dy,$dm,$dw,$dd) = @$ymwd; |
| 931 |
my($ymdonly) = (defined $h ? 0 : 1); |
| 932 |
|
| 933 |
$dd += $dw*7; |
| 934 |
|
| 935 |
if ($subtract) { |
| 936 |
$y -= $dy; |
| 937 |
$self->_mod_add(-12,-1*$dm,\$m,\$y); |
| 938 |
$dd *= -1; |
| 939 |
|
| 940 |
} else { |
| 941 |
$y += $dy; |
| 942 |
$self->_mod_add(-12,$dm,\$m,\$y); |
| 943 |
} |
| 944 |
|
| 945 |
my $dim = $self->days_in_month($y,$m); |
| 946 |
$d = $dim if ($d > $dim); |
| 947 |
|
| 948 |
my $ymd; |
| 949 |
if ($dd == 0) { |
| 950 |
$ymd = [$y,$m,$d]; |
| 951 |
} else { |
| 952 |
$ymd = $self->calc_date_days([$y,$m,$d],$dd); |
| 953 |
} |
| 954 |
|
| 955 |
if ($ymdonly) { |
| 956 |
return $ymd; |
| 957 |
} else { |
| 958 |
return [@$ymd,$h,$mn,$s]; |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
sub _calc_hms_hms { |
| 963 |
my($self,$hms0,$hms1) = @_; |
| 964 |
my($h0,$m0,$s0,$h1,$m1,$s1) = (@$hms0,@$hms1); |
| 965 |
|
| 966 |
my($s) = ($h1-$h0)*3600 + ($m1-$m0)*60 + $s1-$s0; |
| 967 |
my($m) = int($s/60); |
| 968 |
$s -= $m*60; |
| 969 |
my($h) = int($m/60); |
| 970 |
$m -= $h*60; |
| 971 |
return [$h,$m,$s]; |
| 972 |
} |
| 973 |
|
| 974 |
sub calc_time_time { |
| 975 |
my($self,$time0,$time1,$subtract) = @_; |
| 976 |
my($h0,$m0,$s0,$h1,$m1,$s1) = (@$time0,@$time1); |
| 977 |
|
| 978 |
if ($subtract) { |
| 979 |
$h1 *= -1; |
| 980 |
$m1 *= -1; |
| 981 |
$s1 *= -1; |
| 982 |
} |
| 983 |
my($s) = (($h0+$h1)*60 + ($m0+$m1))*60 + $s0+$s1; |
| 984 |
my($m) = int($s/60); |
| 985 |
$s -= $m*60; |
| 986 |
my($h) = int($m/60); |
| 987 |
$m -= $h*60; |
| 988 |
|
| 989 |
return [$h,$m,$s]; |
| 990 |
} |
| 991 |
|
| 992 |
############################################################################### |
| 993 |
|
| 994 |
# Returns -1 if date0 is before date1, 0 if date0 is the same as date1, and |
| 995 |
# 1 if date0 is after date1. |
| 996 |
# |
| 997 |
sub cmp { |
| 998 |
my($self,$date0,$date1) = @_; |
| 999 |
return ($$date0[0] <=> $$date1[0] || |
| 1000 |
$$date0[1] <=> $$date1[1] || |
| 1001 |
$$date0[2] <=> $$date1[2] || |
| 1002 |
$$date0[3] <=> $$date1[3] || |
| 1003 |
$$date0[4] <=> $$date1[4] || |
| 1004 |
$$date0[5] <=> $$date1[5]); |
| 1005 |
} |
| 1006 |
|
| 1007 |
############################################################################### |
| 1008 |
# This determines the OS. |
| 1009 |
|
| 1010 |
sub _os { |
| 1011 |
my($self) = @_; |
| 1012 |
|
| 1013 |
my $os = ''; |
| 1014 |
|
| 1015 |
if ($^O =~ /MSWin32/io || |
| 1016 |
$^O =~ /Windows_95/io || |
| 1017 |
$^O =~ /Windows_NT/io |
| 1018 |
) { |
| 1019 |
$os = 'Windows'; |
| 1020 |
|
| 1021 |
} elsif ($^O =~ /MacOS/io || |
| 1022 |
$^O =~ /MPE/io || |
| 1023 |
$^O =~ /OS2/io || |
| 1024 |
$^O =~ /NetWare/io |
| 1025 |
) { |
| 1026 |
$os = 'Other'; |
| 1027 |
|
| 1028 |
} elsif ($^O =~ /VMS/io) { |
| 1029 |
$os = 'VMS'; |
| 1030 |
|
| 1031 |
} else { |
| 1032 |
$os = 'Unix'; |
| 1033 |
} |
| 1034 |
|
| 1035 |
return $os; |
| 1036 |
} |
| 1037 |
|
| 1038 |
############################################################################### |
| 1039 |
# Config variable functions |
| 1040 |
|
| 1041 |
# $self->config(SECT); |
| 1042 |
# Creates a new section (if it doesn't already exist). |
| 1043 |
# |
| 1044 |
# $self->config(SECT,'_vars'); |
| 1045 |
# Returns a list of (VAR VAL VAR VAL ...) |
| 1046 |
# |
| 1047 |
# $self->config(SECT,VAR,VAL); |
| 1048 |
# Adds (VAR,VAL) to the list. |
| 1049 |
# |
| 1050 |
sub _section { |
| 1051 |
my($self,$sect,$var,$val) = @_; |
| 1052 |
$sect = lc($sect); |
| 1053 |
|
| 1054 |
# |
| 1055 |
# $self->_section(SECT) creates a new section |
| 1056 |
# |
| 1057 |
|
| 1058 |
if (! defined $var && |
| 1059 |
! exists $$self{'data'}{'sections'}{$sect}) { |
| 1060 |
if ($sect eq 'conf') { |
| 1061 |
$$self{'data'}{'sections'}{$sect} = {}; |
| 1062 |
} else { |
| 1063 |
$$self{'data'}{'sections'}{$sect} = []; |
| 1064 |
} |
| 1065 |
return ''; |
| 1066 |
} |
| 1067 |
|
| 1068 |
if ($var eq '_vars') { |
| 1069 |
return @{ $$self{'data'}{'sections'}{$sect} }; |
| 1070 |
} |
| 1071 |
|
| 1072 |
push @{ $$self{'data'}{'sections'}{$sect} },($var,$val); |
| 1073 |
return; |
| 1074 |
} |
| 1075 |
|
| 1076 |
# This sets a config variable. It also performs all side effects from |
| 1077 |
# setting that variable. |
| 1078 |
# |
| 1079 |
sub _config_var_base { |
| 1080 |
my($self,$var,$val) = @_; |
| 1081 |
|
| 1082 |
if ($var eq 'defaults') { |
| 1083 |
# Reset the configuration if desired. |
| 1084 |
$self->_init_config(1); |
| 1085 |
return; |
| 1086 |
|
| 1087 |
} elsif ($var eq 'eraseholidays') { |
| 1088 |
$self->_init_holidays(1); |
| 1089 |
return; |
| 1090 |
|
| 1091 |
} elsif ($var eq 'eraseevents') { |
| 1092 |
$self->_init_events(1); |
| 1093 |
return; |
| 1094 |
|
| 1095 |
} elsif ($var eq 'configfile') { |
| 1096 |
$self->_config_file($val); |
| 1097 |
return; |
| 1098 |
|
| 1099 |
} elsif ($var eq 'encoding') { |
| 1100 |
my $err = $self->_config_var_encoding($val); |
| 1101 |
return if ($err); |
| 1102 |
|
| 1103 |
} elsif ($var eq 'language') { |
| 1104 |
my $err = $self->_language($val); |
| 1105 |
return if ($err); |
| 1106 |
$err = $self->_config_var_encoding(); |
| 1107 |
return if ($err); |
| 1108 |
|
| 1109 |
} elsif ($var eq 'yytoyyyy') { |
| 1110 |
$val = lc($val); |
| 1111 |
if ($val ne 'c' && |
| 1112 |
$val !~ /^c\d\d$/o && |
| 1113 |
$val !~ /^c\d\d\d\d$/o && |
| 1114 |
$val !~ /^\d+$/o) { |
| 1115 |
carp "ERROR: [config_var] invalid: YYtoYYYY: $val"; |
| 1116 |
return; |
| 1117 |
} |
| 1118 |
|
| 1119 |
} elsif ($var eq 'workweekbeg') { |
| 1120 |
my $err = $self->_config_var_workweekbeg($val); |
| 1121 |
return if ($err); |
| 1122 |
|
| 1123 |
} elsif ($var eq 'workweekend') { |
| 1124 |
my $err = $self->_config_var_workweekend($val); |
| 1125 |
return if ($err); |
| 1126 |
|
| 1127 |
} elsif ($var eq 'workday24hr') { |
| 1128 |
my $err = $self->_config_var_workday24hr($val); |
| 1129 |
return if ($err); |
| 1130 |
|
| 1131 |
} elsif ($var eq 'workdaybeg') { |
| 1132 |
my $err = $self->_config_var_workdaybegend(\$val,'WorkDayBeg'); |
| 1133 |
return if ($err); |
| 1134 |
|
| 1135 |
} elsif ($var eq 'workdayend') { |
| 1136 |
my $err = $self->_config_var_workdaybegend(\$val,'WorkDayEnd'); |
| 1137 |
return if ($err); |
| 1138 |
|
| 1139 |
} elsif ($var eq 'firstday') { |
| 1140 |
my $err = $self->_config_var_firstday($val); |
| 1141 |
return if ($err); |
| 1142 |
|
| 1143 |
} elsif ($var eq 'week1ofyear') { |
| 1144 |
my $err = $self->_config_var_week1ofyear($val); |
| 1145 |
return if ($err); |
| 1146 |
|
| 1147 |
} elsif ($var eq 'tz' || |
| 1148 |
$var eq 'forcedate' || |
| 1149 |
$var eq 'setdate') { |
| 1150 |
# These can only be used if the Date::Manip::TZ module has been loaded |
| 1151 |
carp "ERROR: [config_var] $var config variable requires TZ module"; |
| 1152 |
return; |
| 1153 |
|
| 1154 |
} elsif ($var eq 'recurrange') { |
| 1155 |
my $err = $self->_config_var_recurrange($val); |
| 1156 |
return if ($err); |
| 1157 |
|
| 1158 |
} elsif ($var eq 'defaulttime') { |
| 1159 |
my $err = $self->_config_var_defaulttime($val); |
| 1160 |
return if ($err); |
| 1161 |
|
| 1162 |
} elsif ($var eq 'periodtimesep') { |
| 1163 |
# We have to redo the time regexp |
| 1164 |
delete $$self{'data'}{'rx'}{'time'}; |
| 1165 |
|
| 1166 |
} elsif ($var eq 'format_mmmyyyy') { |
| 1167 |
my $err = $self->_config_var_format_mmmyyyy($val); |
| 1168 |
return if ($err); |
| 1169 |
|
| 1170 |
} elsif ($var eq 'dateformat' || |
| 1171 |
$var eq 'printable' || |
| 1172 |
$var eq 'tomorrowfirst' || |
| 1173 |
$var eq 'use_posix_printf' || |
| 1174 |
$var eq 'maxrecurattempts') { |
| 1175 |
# do nothing |
| 1176 |
|
| 1177 |
} elsif ($var eq 'jan1week1') { |
| 1178 |
carp "WARNING: the jan1week1 Date::Manip config variable is deprecated\n" . |
| 1179 |
" and will be removed in version 7.00. Please use\n" . |
| 1180 |
" the Week1ofYear config variable instead.\n"; |
| 1181 |
if ($val) { |
| 1182 |
$self->_config_var_base('week1ofyear','jan1'); |
| 1183 |
} else { |
| 1184 |
$self->_config_var_base('week1ofyear','jan4'); |
| 1185 |
} |
| 1186 |
return; |
| 1187 |
|
| 1188 |
} else { |
| 1189 |
carp "ERROR: [config_var] invalid config variable: $var"; |
| 1190 |
return ''; |
| 1191 |
} |
| 1192 |
|
| 1193 |
$$self{'data'}{'sections'}{'conf'}{$var} = $val; |
| 1194 |
return; |
| 1195 |
} |
| 1196 |
|
| 1197 |
############################################################################### |
| 1198 |
# Specific config variable functions |
| 1199 |
|
| 1200 |
sub _config_var_encoding { |
| 1201 |
my($self,$val) = @_; |
| 1202 |
|
| 1203 |
if (! $val) { |
| 1204 |
$$self{'data'}{'calc'}{'enc_in'} = [ @{ $$self{'data'}{'enc'} } ]; |
| 1205 |
$$self{'data'}{'calc'}{'enc_out'} = 'UTF-8'; |
| 1206 |
|
| 1207 |
} elsif ($val =~ /^(.*),(.*)$/o) { |
| 1208 |
my($in,$out) = ($1,$2); |
| 1209 |
if ($in) { |
| 1210 |
my $o = find_encoding($in); |
| 1211 |
if (! $o) { |
| 1212 |
carp "ERROR: [config_var] invalid: Encoding: $in"; |
| 1213 |
return 1; |
| 1214 |
} |
| 1215 |
} |
| 1216 |
if ($out) { |
| 1217 |
my $o = find_encoding($out); |
| 1218 |
if (! $o) { |
| 1219 |
carp "ERROR: [config_var] invalid: Encoding: $out"; |
| 1220 |
return 1; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
if ($in && $out) { |
| 1225 |
$$self{'data'}{'calc'}{'enc_in'} = [ $in ]; |
| 1226 |
$$self{'data'}{'calc'}{'enc_out'} = $out; |
| 1227 |
|
| 1228 |
} elsif ($in) { |
| 1229 |
$$self{'data'}{'calc'}{'enc_in'} = [ $in ]; |
| 1230 |
$$self{'data'}{'calc'}{'enc_out'} = 'UTF-8'; |
| 1231 |
|
| 1232 |
} elsif ($out) { |
| 1233 |
$$self{'data'}{'calc'}{'enc_in'} = [ @{ $$self{'data'}{'enc'} } ]; |
| 1234 |
$$self{'data'}{'calc'}{'enc_out'} = $out; |
| 1235 |
|
| 1236 |
} else { |
| 1237 |
$$self{'data'}{'calc'}{'enc_in'} = [ @{ $$self{'data'}{'enc'} } ]; |
| 1238 |
$$self{'data'}{'calc'}{'enc_out'} = 'UTF-8'; |
| 1239 |
} |
| 1240 |
|
| 1241 |
} else { |
| 1242 |
my $o = find_encoding($val); |
| 1243 |
if (! $o) { |
| 1244 |
carp "ERROR: [config_var] invalid: Encoding: $val"; |
| 1245 |
return 1; |
| 1246 |
} |
| 1247 |
$$self{'data'}{'calc'}{'enc_in'} = [ $val ]; |
| 1248 |
$$self{'data'}{'calc'}{'enc_out'} = $val; |
| 1249 |
} |
| 1250 |
|
| 1251 |
if (! @{ $$self{'data'}{'calc'}{'enc_in'} }) { |
| 1252 |
$$self{'data'}{'calc'}{'enc_in'} = [ qw(utf-8 perl) ]; |
| 1253 |
} |
| 1254 |
|
| 1255 |
return 0; |
| 1256 |
} |
| 1257 |
|
| 1258 |
sub _config_var_recurrange { |
| 1259 |
my($self,$val) = @_; |
| 1260 |
|
| 1261 |
$val = lc($val); |
| 1262 |
if ($val =~ /^(none|year|month|week|day|all)$/o) { |
| 1263 |
return 0; |
| 1264 |
} |
| 1265 |
|
| 1266 |
carp "ERROR: [config_var] invalid: RecurRange: $val"; |
| 1267 |
return 1; |
| 1268 |
} |
| 1269 |
|
| 1270 |
sub _config_var_workweekbeg { |
| 1271 |
my($self,$val) = @_; |
| 1272 |
|
| 1273 |
if (! $self->_is_int($val,1,7)) { |
| 1274 |
carp "ERROR: [config_var] invalid: WorkWeekBeg: $val"; |
| 1275 |
return 1; |
| 1276 |
} |
| 1277 |
if ($val >= $self->_config('workweekend')) { |
| 1278 |
carp "ERROR: [config_var] WorkWeekBeg must be before WorkWeekEnd"; |
| 1279 |
return 1; |
| 1280 |
} |
| 1281 |
|
| 1282 |
$self->_calc_workweek($val,''); |
| 1283 |
$self->_init_business_length(); |
| 1284 |
return 0; |
| 1285 |
} |
| 1286 |
|
| 1287 |
sub _config_var_workweekend { |
| 1288 |
my($self,$val) = @_; |
| 1289 |
|
| 1290 |
if (! $self->_is_int($val,1,7)) { |
| 1291 |
carp "ERROR: [config_var] invalid: WorkWeekBeg: $val"; |
| 1292 |
return 1; |
| 1293 |
} |
| 1294 |
if ($val <= $self->_config('workweekbeg')) { |
| 1295 |
carp "ERROR: [config_var] WorkWeekEnd must be after WorkWeekBeg"; |
| 1296 |
return 1; |
| 1297 |
} |
| 1298 |
|
| 1299 |
$self->_calc_workweek('',$val); |
| 1300 |
$self->_init_business_length(); |
| 1301 |
return 0; |
| 1302 |
} |
| 1303 |
|
| 1304 |
sub _config_var_workday24hr { |
| 1305 |
my($self,$val) = @_; |
| 1306 |
|
| 1307 |
if ($val) { |
| 1308 |
$$self{'data'}{'sections'}{'conf'}{'workdaybeg'} = '00:00:00'; |
| 1309 |
$$self{'data'}{'sections'}{'conf'}{'workdayend'} = '24:00:00'; |
| 1310 |
$$self{'data'}{'calc'}{'workdaybeg'} = [0,0,0]; |
| 1311 |
$$self{'data'}{'calc'}{'workdayend'} = [24,0,0]; |
| 1312 |
|
| 1313 |
$self->_calc_bdlength(); |
| 1314 |
$self->_init_business_length(); |
| 1315 |
} |
| 1316 |
|
| 1317 |
return 0; |
| 1318 |
} |
| 1319 |
|
| 1320 |
sub _config_var_workdaybegend { |
| 1321 |
my($self,$val,$conf) = @_; |
| 1322 |
|
| 1323 |
# Must be a valid time. Entered as H, H:M, or H:M:S |
| 1324 |
|
| 1325 |
my $tmp = $self->split('hms',$$val); |
| 1326 |
if (! defined $tmp) { |
| 1327 |
carp "ERROR: [config_var] invalid: $conf: $$val"; |
| 1328 |
return 1; |
| 1329 |
} |
| 1330 |
$$self{'data'}{'calc'}{lc($conf)} = $tmp; |
| 1331 |
$$val = $self->join('hms',$tmp); |
| 1332 |
|
| 1333 |
# workdaybeg < workdayend |
| 1334 |
|
| 1335 |
my @beg = @{ $$self{'data'}{'calc'}{'workdaybeg'} }; |
| 1336 |
my @end = @{ $$self{'data'}{'calc'}{'workdayend'} }; |
| 1337 |
my $beg = $beg[0]*3600 + $beg[1]*60 + $beg[2]; |
| 1338 |
my $end = $end[0]*3600 + $end[1]*60 + $end[2]; |
| 1339 |
|
| 1340 |
if ($beg > $end) { |
| 1341 |
carp "ERROR: [config_var] WorkDayBeg not before WorkDayEnd"; |
| 1342 |
return 1; |
| 1343 |
} |
| 1344 |
|
| 1345 |
# Calculate bdlength |
| 1346 |
|
| 1347 |
$$self{'data'}{'sections'}{'conf'}{'workday24hr'} = 0; |
| 1348 |
|
| 1349 |
$self->_calc_bdlength(); |
| 1350 |
$self->_init_business_length(); |
| 1351 |
|
| 1352 |
return 0; |
| 1353 |
} |
| 1354 |
|
| 1355 |
sub _config_var_firstday { |
| 1356 |
my($self,$val) = @_; |
| 1357 |
|
| 1358 |
if (! $self->_is_int($val,1,7)) { |
| 1359 |
carp "ERROR: [config_var] invalid: FirstDay: $val"; |
| 1360 |
return 1; |
| 1361 |
} |
| 1362 |
|
| 1363 |
return 0; |
| 1364 |
} |
| 1365 |
|
| 1366 |
sub _config_var_week1ofyear { |
| 1367 |
my($self,$val) = @_; |
| 1368 |
$val = lc($val); |
| 1369 |
|
| 1370 |
if ($val =~ /^jan[1-7]$/ || |
| 1371 |
$val =~ /^dow[1-7]$/ || |
| 1372 |
$val eq 'firstday') { |
| 1373 |
return 0; |
| 1374 |
} |
| 1375 |
carp "ERROR: [config_var] invalid: Week1ofYear: $val"; |
| 1376 |
return 1; |
| 1377 |
} |
| 1378 |
|
| 1379 |
sub _config_var_defaulttime { |
| 1380 |
my($self,$val) = @_; |
| 1381 |
|
| 1382 |
if (lc($val) eq 'midnight' || |
| 1383 |
lc($val) eq 'curr') { |
| 1384 |
return 0; |
| 1385 |
} |
| 1386 |
carp "ERROR: [config_var] invalid: DefaultTime: $val"; |
| 1387 |
return 1; |
| 1388 |
} |
| 1389 |
|
| 1390 |
sub _config_var_format_mmmyyyy { |
| 1391 |
my($self,$val) = @_; |
| 1392 |
|
| 1393 |
if (lc($val) eq 'first' || |
| 1394 |
lc($val) eq 'last' || |
| 1395 |
lc($val) eq '') { |
| 1396 |
return 0; |
| 1397 |
} |
| 1398 |
carp "ERROR: [config_var] invalid: Format_MMMYYYY: $val"; |
| 1399 |
return 1; |
| 1400 |
} |
| 1401 |
|
| 1402 |
############################################################################### |
| 1403 |
# Language functions |
| 1404 |
|
| 1405 |
# This reads in a langauge module and sets regular expressions |
| 1406 |
# and word lists based on it. |
| 1407 |
# |
| 1408 |
no strict 'refs'; |
| 1409 |
sub _language { |
| 1410 |
my($self,$lang) = @_; |
| 1411 |
$lang = lc($lang); |
| 1412 |
|
| 1413 |
if (! exists $Date::Manip::Lang::index::Lang{$lang}) { |
| 1414 |
carp "ERROR: [language] invalid: $lang"; |
| 1415 |
return 1; |
| 1416 |
} |
| 1417 |
|
| 1418 |
return 0 if (exists $$self{'data'}{'sections'}{'conf'} && |
| 1419 |
$$self{'data'}{'sections'}{'conf'} eq $lang); |
| 1420 |
$self->_init_language(1); |
| 1421 |
|
| 1422 |
my $mod = $Date::Manip::Lang::index::Lang{$lang}; |
| 1423 |
eval "require Date::Manip::Lang::${mod}"; |
| 1424 |
if ($@) { |
| 1425 |
croak "ERROR: failed to load Date::Manip::Lang::${mod}: $@\n"; |
| 1426 |
} |
| 1427 |
|
| 1428 |
no warnings 'once'; |
| 1429 |
$$self{'data'}{'lang'} = ${ "Date::Manip::Lang::${mod}::Language" }; |
| 1430 |
$$self{'data'}{'enc'} = [ @{ "Date::Manip::Lang::${mod}::Encodings" } ]; |
| 1431 |
|
| 1432 |
# Common words |
| 1433 |
$self->_rx_wordlist('at'); |
| 1434 |
$self->_rx_wordlist('each'); |
| 1435 |
$self->_rx_wordlist('last'); |
| 1436 |
$self->_rx_wordlist('of'); |
| 1437 |
$self->_rx_wordlist('on'); |
| 1438 |
$self->_rx_wordlists('when'); |
| 1439 |
|
| 1440 |
# Next/prev |
| 1441 |
$self->_rx_wordlists('nextprev'); |
| 1442 |
|
| 1443 |
# Field names (years, year, yr, ...) |
| 1444 |
$self->_rx_wordlists('fields'); |
| 1445 |
|
| 1446 |
# Numbers (first, 1st) |
| 1447 |
$self->_rx_wordlists('nth'); |
| 1448 |
$self->_rx_wordlists('nth','nth_dom',31); # 1-31 |
| 1449 |
$self->_rx_wordlists('nth','nth_wom',5); # 1-5 |
| 1450 |
|
| 1451 |
# Calendar names (Mon, Tue and Jan, Feb) |
| 1452 |
$self->_rx_wordlists('day_abb'); |
| 1453 |
$self->_rx_wordlists('day_char'); |
| 1454 |
$self->_rx_wordlists('day_name'); |
| 1455 |
$self->_rx_wordlists('month_abb'); |
| 1456 |
$self->_rx_wordlists('month_name'); |
| 1457 |
|
| 1458 |
# H:M:S separators |
| 1459 |
$self->_rx_simple('sephm'); |
| 1460 |
$self->_rx_simple('sepms'); |
| 1461 |
$self->_rx_simple('sepfr'); |
| 1462 |
|
| 1463 |
# Time replacement strings |
| 1464 |
$self->_rx_replace('times'); |
| 1465 |
|
| 1466 |
# Some offset strings |
| 1467 |
$self->_rx_replace('offset_date'); |
| 1468 |
$self->_rx_replace('offset_time'); |
| 1469 |
|
| 1470 |
# AM/PM strings |
| 1471 |
$self->_rx_wordlists('ampm'); |
| 1472 |
|
| 1473 |
# Business/non-business mode |
| 1474 |
$self->_rx_wordlists('mode'); |
| 1475 |
|
| 1476 |
return 0; |
| 1477 |
} |
| 1478 |
use strict 'refs'; |
| 1479 |
|
| 1480 |
# This takes a string or strings from the language file which is a |
| 1481 |
# regular expression and copies it to the regular expression cache. |
| 1482 |
# |
| 1483 |
# If the language file contains a list of strings, a list of strings |
| 1484 |
# is stored in the regexp cache. |
| 1485 |
# |
| 1486 |
sub _rx_simple { |
| 1487 |
my($self,$ele) = @_; |
| 1488 |
|
| 1489 |
if (exists $$self{'data'}{'lang'}{$ele}) { |
| 1490 |
if (ref($$self{'data'}{'lang'}{$ele})) { |
| 1491 |
@{ $$self{'data'}{'rx'}{$ele} } = @{ $$self{'data'}{'lang'}{$ele} }; |
| 1492 |
} else { |
| 1493 |
$$self{'data'}{'rx'}{$ele} = $$self{'data'}{'lang'}{$ele}; |
| 1494 |
} |
| 1495 |
} else { |
| 1496 |
$$self{'data'}{'rx'}{$ele} = undef; |
| 1497 |
} |
| 1498 |
|
| 1499 |
return; |
| 1500 |
} |
| 1501 |
|
| 1502 |
# We need to quote strings that will be used in regexps, but we don't |
| 1503 |
# want to quote UTF-8 characters. |
| 1504 |
# |
| 1505 |
sub _qe_quote { |
| 1506 |
my($string) = @_; |
| 1507 |
$string =~ s/([-.+*?])/\\$1/g; |
| 1508 |
return $string; |
| 1509 |
} |
| 1510 |
|
| 1511 |
# This takes a list of words and creates a simple regexp which matches |
| 1512 |
# any of them. |
| 1513 |
# |
| 1514 |
# The first word in the list is the default way to express the word using |
| 1515 |
# a normal ASCII character set. |
| 1516 |
# |
| 1517 |
# The second word in the list is the default way to express the word using |
| 1518 |
# a locale character set. If it isn't defined, it defaults to the first word. |
| 1519 |
# |
| 1520 |
sub _rx_wordlist { |
| 1521 |
my($self,$ele) = @_; |
| 1522 |
|
| 1523 |
if (exists $$self{'data'}{'lang'}{$ele}) { |
| 1524 |
my @tmp = @{ $$self{'data'}{'lang'}{$ele} }; |
| 1525 |
|
| 1526 |
$$self{'data'}{'wordlist'}{$ele} = $tmp[0]; |
| 1527 |
|
| 1528 |
my @tmp2; |
| 1529 |
foreach my $tmp (@tmp) { |
| 1530 |
push(@tmp2,_qe_quote($tmp)) if ($tmp); |
| 1531 |
} |
| 1532 |
@tmp2 = sort _sortByLength(@tmp2); |
| 1533 |
|
| 1534 |
$$self{'data'}{'rx'}{$ele} = join('|',@tmp2); |
| 1535 |
|
| 1536 |
} else { |
| 1537 |
$$self{'data'}{'rx'}{$ele} = undef; |
| 1538 |
} |
| 1539 |
|
| 1540 |
return; |
| 1541 |
} |
| 1542 |
|
| 1543 |
no strict 'vars'; |
| 1544 |
sub _sortByLength { |
| 1545 |
return (length $b <=> length $a); |
| 1546 |
} |
| 1547 |
use strict 'vars'; |
| 1548 |
|
| 1549 |
# This takes a hash of the form: |
| 1550 |
# word => string |
| 1551 |
# and creates a regular expression to match word (which must be surrounded |
| 1552 |
# by word boundaries). |
| 1553 |
# |
| 1554 |
sub _rx_replace { |
| 1555 |
my($self,$ele) = @_; |
| 1556 |
|
| 1557 |
if (! exists $$self{'data'}{'lang'}{$ele}) { |
| 1558 |
$$self{'data'}{'rx'}{$ele} = []; |
| 1559 |
return; |
| 1560 |
} |
| 1561 |
|
| 1562 |
my(@key) = keys %{ $$self{'data'}{'lang'}{$ele} }; |
| 1563 |
my $i = 1; |
| 1564 |
foreach my $key (sort(@key)) { |
| 1565 |
my $val = $$self{'data'}{'lang'}{$ele}{$key}; |
| 1566 |
my $k = _qe_quote($key); |
| 1567 |
$$self{'data'}{'rx'}{$ele}[$i++] = qr/(?:^|\b)($k)(?:\b|$)/i; |
| 1568 |
$$self{'data'}{'wordmatch'}{$ele}{lc($key)} = $val; |
| 1569 |
} |
| 1570 |
|
| 1571 |
@key = sort _sortByLength(@key); |
| 1572 |
@key = map { _qe_quote($_) } @key; |
| 1573 |
my $rx = join('|',@key); |
| 1574 |
|
| 1575 |
$$self{'data'}{'rx'}{$ele}[0] = qr/(?:^|\b)(?:$rx)(?:\b|$)/i; |
| 1576 |
|
| 1577 |
return; |
| 1578 |
} |
| 1579 |
|
| 1580 |
# This takes a list of values, each of which can be expressed in multiple |
| 1581 |
# ways, and gets a regular expression which matches any of them, a default |
| 1582 |
# way to express each value, and a hash which matches a matched string to |
| 1583 |
# a value (the value is 1..N where N is the number of values). |
| 1584 |
# |
| 1585 |
sub _rx_wordlists { |
| 1586 |
my($self,$ele,$subset,$max) = @_; |
| 1587 |
$subset = $ele if (! $subset); |
| 1588 |
|
| 1589 |
if (exists $$self{'data'}{'lang'}{$ele}) { |
| 1590 |
my @vallist = @{ $$self{'data'}{'lang'}{$ele} }; |
| 1591 |
$max = $#vallist+1 if (! $max || $max > $#vallist+1); |
| 1592 |
my (@all); |
| 1593 |
|
| 1594 |
for (my $i=1; $i<=$max; $i++) { |
| 1595 |
my @tmp = @{ $$self{'data'}{'lang'}{$ele}[$i-1] }; |
| 1596 |
$$self{'data'}{'wordlist'}{$subset}[$i-1] = $tmp[0]; |
| 1597 |
|
| 1598 |
my @str; |
| 1599 |
foreach my $str (@tmp) { |
| 1600 |
next if (! $str); |
| 1601 |
$$self{'data'}{'wordmatch'}{$subset}{lc($str)} = $i; |
| 1602 |
push(@str,_qe_quote($str)); |
| 1603 |
} |
| 1604 |
push(@all,@str); |
| 1605 |
|
| 1606 |
@str = sort _sortByLength(@str); |
| 1607 |
$$self{'data'}{'rx'}{$subset}[$i] = join('|',@str); |
| 1608 |
} |
| 1609 |
|
| 1610 |
@all = sort _sortByLength(@all); |
| 1611 |
$$self{'data'}{'rx'}{$subset}[0] = join('|',@all); |
| 1612 |
|
| 1613 |
} else { |
| 1614 |
$$self{'data'}{'rx'}{$subset} = undef; |
| 1615 |
} |
| 1616 |
|
| 1617 |
return; |
| 1618 |
} |
| 1619 |
|
| 1620 |
############################################################################### |
| 1621 |
# Year functions |
| 1622 |
# |
| 1623 |
# $self->_method(METHOD) use METHOD as the method for YY->YYYY |
| 1624 |
# conversions |
| 1625 |
# |
| 1626 |
# YEAR = _fix_year(YR) converts a 2-digit to 4-digit year |
| 1627 |
# _fix_year is in TZ_Base |
| 1628 |
|
| 1629 |
sub _method { |
| 1630 |
my($self,$method) = @_; |
| 1631 |
$self->_config('yytoyyyy',$method); |
| 1632 |
|
| 1633 |
return; |
| 1634 |
} |
| 1635 |
|
| 1636 |
############################################################################### |
| 1637 |
# $self->_mod_add($N,$add,\$val,\$rem); |
| 1638 |
# This calculates $val=$val+$add and forces $val to be in a certain |
| 1639 |
# range. This is useful for adding numbers for which only a certain |
| 1640 |
# range is allowed (for example, minutes can be between 0 and 59 or |
| 1641 |
# months can be between 1 and 12). The absolute value of $N determines |
| 1642 |
# the range and the sign of $N determines whether the range is 0 to N-1 |
| 1643 |
# (if N>0) or 1 to N (N<0). $rem is adjusted to force $val into the |
| 1644 |
# appropriate range. |
| 1645 |
# Example: |
| 1646 |
# To add 2 hours together (with the excess returned in days) use: |
| 1647 |
# $self->_mod_add(-24,$h1,\$h,\$day); |
| 1648 |
# To add 2 minutes together (with the excess returned in hours): |
| 1649 |
# $self->_mod_add(60,$mn1,\$mn,\$hr); |
| 1650 |
sub _mod_add { |
| 1651 |
my($self,$N,$add,$val,$rem)=@_; |
| 1652 |
return if ($N==0); |
| 1653 |
$$val+=$add; |
| 1654 |
if ($N<0) { |
| 1655 |
# 1 to N |
| 1656 |
$N = -$N; |
| 1657 |
if ($$val>$N) { |
| 1658 |
$$rem+= int(($$val-1)/$N); |
| 1659 |
$$val = ($$val-1)%$N +1; |
| 1660 |
} elsif ($$val<1) { |
| 1661 |
$$rem-= int(-$$val/$N)+1; |
| 1662 |
$$val = $N-(-$$val % $N); |
| 1663 |
} |
| 1664 |
|
| 1665 |
} else { |
| 1666 |
# 0 to N-1 |
| 1667 |
if ($$val>($N-1)) { |
| 1668 |
$$rem+= int($$val/$N); |
| 1669 |
$$val = $$val%$N; |
| 1670 |
} elsif ($$val<0) { |
| 1671 |
$$rem-= int(-($$val+1)/$N)+1; |
| 1672 |
$$val = ($N-1)-(-($$val+1)%$N); |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
return; |
| 1677 |
} |
| 1678 |
|
| 1679 |
# $flag = $self->_is_int($string [,$low, $high]); |
| 1680 |
# Returns 1 if $string is a valid integer, 0 otherwise. If $low is |
| 1681 |
# entered, $string must be >= $low. If $high is entered, $string must |
| 1682 |
# be <= $high. It is valid to check only one of the bounds. |
| 1683 |
sub _is_int { |
| 1684 |
my($self,$N,$low,$high)=@_; |
| 1685 |
return 0 if (! defined $N or |
| 1686 |
$N !~ /^\s*[-+]?\d+\s*$/o or |
| 1687 |
defined $low && $N<$low or |
| 1688 |
defined $high && $N>$high); |
| 1689 |
return 1; |
| 1690 |
} |
| 1691 |
|
| 1692 |
# $flag = $self->_is_num($string [,$low, $high]); |
| 1693 |
# Returns 1 if $string is a valid number (integer or real), 0 otherwise. |
| 1694 |
# If $low is entered, $string must be >= $low. If $high is entered, |
| 1695 |
# $string must be <= $high. It is valid to check only one of the bounds. |
| 1696 |
sub _is_num { |
| 1697 |
my($self,$N,$low,$high)=@_; |
| 1698 |
return 0 if (! defined $N or |
| 1699 |
($N !~ /^\s*[-+]?\d+(\.\d*)?\s*$/o && |
| 1700 |
$N !~ /^\s*[-+]?\.\d+\s*$/o) or |
| 1701 |
defined $low && $N<$low or |
| 1702 |
defined $high && $N>$high); |
| 1703 |
return 1; |
| 1704 |
} |
| 1705 |
|
| 1706 |
############################################################################### |
| 1707 |
# Split/Join functions |
| 1708 |
|
| 1709 |
sub split { |
| 1710 |
my($self,$op,$string,$arg) = @_; |
| 1711 |
|
| 1712 |
my %opts; |
| 1713 |
if (ref($arg) eq 'HASH') { |
| 1714 |
%opts = %{ $arg }; |
| 1715 |
} elsif ($arg) { |
| 1716 |
# ***DEPRECATED 7.0*** |
| 1717 |
%opts = ('nonorm' => 1); |
| 1718 |
} |
| 1719 |
|
| 1720 |
# ***DEPRECATED 7.0*** |
| 1721 |
if ($op eq 'delta') { |
| 1722 |
$opts{'mode'} = 'standard'; |
| 1723 |
} elsif ($op eq 'business') { |
| 1724 |
$opts{'mode'} = 'business'; |
| 1725 |
$op = 'delta'; |
| 1726 |
} |
| 1727 |
|
| 1728 |
if ($op eq 'date') { |
| 1729 |
|
| 1730 |
if ($string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/o || |
| 1731 |
$string =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)\-(\d\d):(\d\d):(\d\d)$/o || |
| 1732 |
$string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/o) { |
| 1733 |
my($y,$m,$d,$h,$mn,$s) = ($1+0,$2+0,$3+0,$4+0,$5+0,$6+0); |
| 1734 |
return [$y,$m,$d,$h,$mn,$s]; |
| 1735 |
} else { |
| 1736 |
return undef; |
| 1737 |
} |
| 1738 |
|
| 1739 |
} elsif ($op eq 'hms') { |
| 1740 |
if ($string =~ /^(\d\d)(\d\d)(\d\d)$/o || |
| 1741 |
$string =~ /^(\d\d)(\d\d)()$/o || |
| 1742 |
$string =~ /^(\d\d?):(\d\d):(\d\d)$/o || |
| 1743 |
$string =~ /^(\d\d?):(\d\d)()$/o || |
| 1744 |
$string =~ /^(\d\d?)()()$/o) { |
| 1745 |
my($err,$h,$mn,$s) = $self->_hms_fields( { 'out' => 'list' },[$1,$2,$3]); |
| 1746 |
return undef if ($err); |
| 1747 |
return [$h,$mn,$s]; |
| 1748 |
} else { |
| 1749 |
return undef; |
| 1750 |
} |
| 1751 |
|
| 1752 |
} elsif ($op eq 'offset') { |
| 1753 |
if ($string =~ /^([-+]?\d\d)(\d\d)(\d\d)$/o || |
| 1754 |
$string =~ /^([-+]?\d\d)(\d\d)()$/o || |
| 1755 |
$string =~ /^([-+]?\d\d?):(\d\d?):(\d\d?)$/o || |
| 1756 |
$string =~ /^([-+]?\d\d?):(\d\d?)()$/o || |
| 1757 |
$string =~ /^([-+]?\d\d?)()()$/o) { |
| 1758 |
my($err,$h,$mn,$s) = $self->_offset_fields( { 'source' => 'string', |
| 1759 |
'out' => 'list'}, |
| 1760 |
[$1,$2,$3]); |
| 1761 |
return undef if ($err); |
| 1762 |
return [$h,$mn,$s]; |
| 1763 |
} else { |
| 1764 |
return undef; |
| 1765 |
} |
| 1766 |
|
| 1767 |
} elsif ($op eq 'time') { |
| 1768 |
if ($string =~ /^[-+]?\d+(:[-+]?\d+){0,2}$/o) { |
| 1769 |
my($err,$dh,$dmn,$ds) = |
| 1770 |
$self->_time_fields( { 'nonorm' => |
| 1771 |
(exists($opts{'nonorm'}) ? $opts{'nonorm'} : 0), |
| 1772 |
'source' => 'string', |
| 1773 |
'sign' => -1, |
| 1774 |
}, [split(/:/,$string)]); |
| 1775 |
return undef if ($err); |
| 1776 |
return [$dh,$dmn,$ds]; |
| 1777 |
} else { |
| 1778 |
return undef; |
| 1779 |
} |
| 1780 |
|
| 1781 |
} elsif ($op eq 'delta') { |
| 1782 |
my($err,@delta) = $self->_split_delta($string); |
| 1783 |
return undef if ($err); |
| 1784 |
|
| 1785 |
($err,@delta) = |
| 1786 |
$self->_delta_fields( { 'mode' => $opts{'mode'}, |
| 1787 |
'nonorm' => (exists($opts{'nonorm'}) ? |
| 1788 |
$opts{'nonorm'} : 0), |
| 1789 |
'source' => 'string', |
| 1790 |
'sign' => -1, |
| 1791 |
}, [@delta]); |
| 1792 |
|
| 1793 |
return undef if ($err); |
| 1794 |
return [@delta]; |
| 1795 |
} |
| 1796 |
} |
| 1797 |
|
| 1798 |
sub join{ |
| 1799 |
my($self,$op,$data,$arg) = @_; |
| 1800 |
|
| 1801 |
my %opts; |
| 1802 |
if (ref($arg) eq 'HASH') { |
| 1803 |
%opts = %{ $arg }; |
| 1804 |
} elsif ($arg) { |
| 1805 |
# ***DEPRECATED 7.0*** |
| 1806 |
%opts = ('nonorm' => 1); |
| 1807 |
} |
| 1808 |
|
| 1809 |
# ***DEPRECATED 7.0*** |
| 1810 |
if ($op eq 'delta') { |
| 1811 |
$opts{'mode'} = 'standard'; |
| 1812 |
} elsif ($op eq 'business') { |
| 1813 |
$opts{'mode'} = 'business'; |
| 1814 |
$op = 'delta'; |
| 1815 |
} |
| 1816 |
|
| 1817 |
my @data = @$data; |
| 1818 |
|
| 1819 |
if ($op eq 'date') { |
| 1820 |
|
| 1821 |
my($err,$y,$m,$d,$h,$mn,$s) = $self->_date_fields(@data); |
| 1822 |
return undef if ($err); |
| 1823 |
my $form = $self->_config('printable'); |
| 1824 |
if ($form == 1) { |
| 1825 |
return "$y$m$d$h$mn$s"; |
| 1826 |
} elsif ($form == 2) { |
| 1827 |
return "$y-$m-$d-$h:$mn:$s"; |
| 1828 |
} else { |
| 1829 |
return "$y$m$d$h:$mn:$s"; |
| 1830 |
} |
| 1831 |
|
| 1832 |
} elsif ($op eq 'offset') { |
| 1833 |
my($err,$h,$mn,$s) = $self->_offset_fields( { 'source' => 'list', |
| 1834 |
'out' => 'string'}, |
| 1835 |
[@data]); |
| 1836 |
return undef if ($err); |
| 1837 |
return "$h:$mn:$s"; |
| 1838 |
|
| 1839 |
} elsif ($op eq 'hms') { |
| 1840 |
my($err,$h,$mn,$s) = $self->_hms_fields( { 'out' => 'string' },[@data]); |
| 1841 |
return undef if ($err); |
| 1842 |
return "$h:$mn:$s"; |
| 1843 |
|
| 1844 |
} elsif ($op eq 'time') { |
| 1845 |
my($err,$dh,$dmn,$ds) = |
| 1846 |
$self->_time_fields( { 'nonorm' => |
| 1847 |
(exists($opts{'nonorm'}) ? $opts{'nonorm'} : 0), |
| 1848 |
'source' => 'list', |
| 1849 |
'sign' => 0, |
| 1850 |
}, [@data]); |
| 1851 |
return undef if ($err); |
| 1852 |
return "$dh:$dmn:$ds"; |
| 1853 |
|
| 1854 |
} elsif ($op eq 'delta') { |
| 1855 |
my ($err,@delta) = |
| 1856 |
$self->_delta_fields( { 'mode' => $opts{'mode'}, |
| 1857 |
'nonorm' => (exists($opts{'nonorm'}) ? |
| 1858 |
$opts{'nonorm'} : 0), |
| 1859 |
'source' => 'list', |
| 1860 |
'sign' => 0, |
| 1861 |
}, [@data]); |
| 1862 |
return undef if ($err); |
| 1863 |
return join(':',@delta); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
|
| 1867 |
sub _split_delta { |
| 1868 |
my($self,$string) = @_; |
| 1869 |
|
| 1870 |
my $sign = '[-+]?'; |
| 1871 |
my $num = '(?:\d+(?:\.\d*)?|\.\d+)'; |
| 1872 |
my $f = "(?:$sign$num)?"; |
| 1873 |
|
| 1874 |
if ($string =~ /^$f(:$f){0,6}$/o) { |
| 1875 |
$string =~ s/::/:0:/go; |
| 1876 |
$string =~ s/^:/0:/o; |
| 1877 |
$string =~ s/:$/:0/o; |
| 1878 |
my(@delta) = split(/:/,$string); |
| 1879 |
return(0,@delta); |
| 1880 |
} else { |
| 1881 |
return(1); |
| 1882 |
} |
| 1883 |
} |
| 1884 |
|
| 1885 |
# Check that type is not inconsistent with @delta. |
| 1886 |
# |
| 1887 |
# An exact delta cannot have semi-exact or approximate fields set. |
| 1888 |
# A semi-exact delta cannot have approximate fields set. |
| 1889 |
# An exact, semi-exact, or approximate delta cannot have non-integer values. |
| 1890 |
# |
| 1891 |
# If the type was not explicitly specified, guess what it is. |
| 1892 |
# |
| 1893 |
# Returns ($err,$type,$type_from) |
| 1894 |
# |
| 1895 |
sub _check_delta_type { |
| 1896 |
my($self,$mode,$type,$type_from,@delta) = @_; |
| 1897 |
|
| 1898 |
my $est = 0; |
| 1899 |
foreach my $f (@delta) { |
| 1900 |
if (! $self->_is_int($f)) { |
| 1901 |
$est = 1; |
| 1902 |
last; |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
my $approx = 0; |
| 1907 |
if (! $est) { |
| 1908 |
$approx = 1 if ($delta[0] || $delta[1]); |
| 1909 |
} |
| 1910 |
|
| 1911 |
my $semi = 0; |
| 1912 |
if (! $est && ! $approx) { |
| 1913 |
if ($mode eq 'business') { |
| 1914 |
$semi = 1 if ($delta[2]); |
| 1915 |
} else { |
| 1916 |
$semi = 1 if ($delta[2] || $delta[3]); |
| 1917 |
} |
| 1918 |
} |
| 1919 |
|
| 1920 |
if ($est) { |
| 1921 |
# If some of the fields are non-integer, then type must be estimated. |
| 1922 |
|
| 1923 |
if ($type ne 'estimated') { |
| 1924 |
if ($type_from eq 'opt') { |
| 1925 |
return ("Type must be estimated for non-integers"); |
| 1926 |
} |
| 1927 |
$type = 'estimated'; |
| 1928 |
$type_from = 'det'; |
| 1929 |
} |
| 1930 |
|
| 1931 |
} elsif ($approx) { |
| 1932 |
# If some of the approximate fields are set, then type must be |
| 1933 |
# approx or estimated. |
| 1934 |
|
| 1935 |
if ($type ne 'approx' && $type ne 'estimated') { |
| 1936 |
if ($type_from eq 'opt') { |
| 1937 |
return("Type must be approx/estimated"); |
| 1938 |
} |
| 1939 |
$type = 'approx'; |
| 1940 |
$type_from = 'det'; |
| 1941 |
} |
| 1942 |
|
| 1943 |
} elsif ($semi) { |
| 1944 |
# If some of the semi-exact fields are set, then type must be |
| 1945 |
# semi, approx, or estimated |
| 1946 |
|
| 1947 |
if ($type ne 'semi' && $type ne 'approx' && $type ne 'estimated') { |
| 1948 |
if ($type_from eq 'opt') { |
| 1949 |
return("Type must be semi/approx/estimated"); |
| 1950 |
} |
| 1951 |
$type = 'semi'; |
| 1952 |
$type_from = 'det'; |
| 1953 |
} |
| 1954 |
|
| 1955 |
} else { |
| 1956 |
|
| 1957 |
if (! $type) { |
| 1958 |
$type = 'exact'; |
| 1959 |
$type_from = 'det'; |
| 1960 |
} |
| 1961 |
} |
| 1962 |
|
| 1963 |
return ('',$type,$type_from); |
| 1964 |
} |
| 1965 |
|
| 1966 |
# This function returns the fields in a delta in the desired format. |
| 1967 |
# |
| 1968 |
# $opts = { mode => standard/business |
| 1969 |
# type => exact/semi/approx/estimated |
| 1970 |
# nonorm => 0/1, |
| 1971 |
# source => string, list, delta |
| 1972 |
# sign => 0/1/-1 |
| 1973 |
# } |
| 1974 |
# $fields = [Y,M,W,D,H,Mn,S] |
| 1975 |
# |
| 1976 |
# If the business option is 1, treat it as a business delta. |
| 1977 |
# |
| 1978 |
# If the nonorm option is 1, fields are NOT normalized. By default, |
| 1979 |
# they are normalized. |
| 1980 |
# |
| 1981 |
# If source is 'string', then the source of the fields is a string |
| 1982 |
# that has been split, so we need to handle carrying the signs. If |
| 1983 |
# the option is 'list', then the source is a valid delta, so each |
| 1984 |
# field is correctly signed already. In both cases, the type of |
| 1985 |
# delta will need to be determined. If the source is 'delta', then |
| 1986 |
# it comes from a Date::Manip::Delta object. In this case the type |
| 1987 |
# must be specified. If type is not passed in, it will be set. |
| 1988 |
# |
| 1989 |
# If the sign option is 1, a sign is added to every field. If the |
| 1990 |
# sign option is -1, all negative fields are signed. If the sign |
| 1991 |
# option is 0, the minimum number of signs (for fields who's sign is |
| 1992 |
# different from the next higher field) will be added. |
| 1993 |
# |
| 1994 |
# It returns ($err,@fields) |
| 1995 |
# |
| 1996 |
sub _delta_fields { |
| 1997 |
my($self,$opts,$fields) = @_; |
| 1998 |
my @fields = @$fields; |
| 1999 |
no integer; |
| 2000 |
|
| 2001 |
# |
| 2002 |
# Make sure that all fields are defined, numerical, and that there |
| 2003 |
# are 7 of them. |
| 2004 |
# |
| 2005 |
|
| 2006 |
foreach my $f (@fields) { |
| 2007 |
$f=0 if (! defined($f)); |
| 2008 |
return ("Non-numerical field") if (! $self->_is_num($f)); |
| 2009 |
} |
| 2010 |
return ("Delta may contain only 7 fields") if (@fields > 7); |
| 2011 |
while (@fields < 7) { |
| 2012 |
unshift(@fields,0); |
| 2013 |
} |
| 2014 |
|
| 2015 |
# |
| 2016 |
# Make sure each field is the correct sign so that the math will |
| 2017 |
# work correctly. Get rid of all positive signs and leading 0's. |
| 2018 |
# |
| 2019 |
|
| 2020 |
my $mode = $$opts{'mode'}; |
| 2021 |
my $source = $$opts{'source'}; |
| 2022 |
@fields = $self->_sign_source($source,@fields); |
| 2023 |
|
| 2024 |
# |
| 2025 |
# Figure out the type of delta. When called from Date::Manip::Base, it'll |
| 2026 |
# be determined from the data. When called from Date::Manip::Delta, it'll |
| 2027 |
# be specified. |
| 2028 |
# |
| 2029 |
|
| 2030 |
my ($type,$type_from); |
| 2031 |
if (defined $source && $source eq 'delta') { |
| 2032 |
if (! exists $$opts{'type'}) { |
| 2033 |
return ("Type must be specified"); |
| 2034 |
} |
| 2035 |
$type = $$opts{'type'}; |
| 2036 |
|
| 2037 |
} else { |
| 2038 |
my $err; |
| 2039 |
($err,$type,$type_from) = $self->_check_delta_type($mode,'','init',@fields); |
| 2040 |
$$opts{'type'} = $type; |
| 2041 |
$$opts{'type_from'} = $type_from; |
| 2042 |
return($err) if ($err); |
| 2043 |
} |
| 2044 |
|
| 2045 |
# |
| 2046 |
# Normalize values, if desired. |
| 2047 |
# |
| 2048 |
|
| 2049 |
my $norm = 1-$$opts{'nonorm'}; |
| 2050 |
if ($norm) { |
| 2051 |
if ($mode eq 'business') { |
| 2052 |
|
| 2053 |
if ($type eq 'estimated') { |
| 2054 |
@fields = $self->_normalize_bus_est(@fields); |
| 2055 |
|
| 2056 |
} elsif ($type eq 'approx' || |
| 2057 |
$type eq 'semi') { |
| 2058 |
@fields = $self->_normalize_bus_approx(@fields); |
| 2059 |
|
| 2060 |
} else { |
| 2061 |
@fields = $self->_normalize_bus_exact(@fields); |
| 2062 |
} |
| 2063 |
|
| 2064 |
} else { |
| 2065 |
|
| 2066 |
if ($type eq 'estimated') { |
| 2067 |
@fields = $self->_normalize_est(@fields); |
| 2068 |
|
| 2069 |
} elsif ($type eq 'approx' || |
| 2070 |
$type eq 'semi') { |
| 2071 |
@fields = $self->_normalize_approx(@fields); |
| 2072 |
|
| 2073 |
} else { |
| 2074 |
@fields = $self->_normalize_exact(@fields); |
| 2075 |
} |
| 2076 |
|
| 2077 |
} |
| 2078 |
} |
| 2079 |
|
| 2080 |
# |
| 2081 |
# Now make sure that the signs are included as appropriate. |
| 2082 |
# |
| 2083 |
|
| 2084 |
@fields = $self->_sign_fields($$opts{'sign'},@fields); |
| 2085 |
|
| 2086 |
return (0,@fields); |
| 2087 |
} |
| 2088 |
|
| 2089 |
# If a set of fields came from splitting a string, not all of the fields |
| 2090 |
# are signed. If it comes from a list, we just want to remove extra '+' |
| 2091 |
# signs. |
| 2092 |
# |
| 2093 |
sub _sign_source { |
| 2094 |
my($self,$source,@fields) = @_; |
| 2095 |
|
| 2096 |
# Needed to handle fractional fields |
| 2097 |
no integer; |
| 2098 |
if ($source eq 'string') { |
| 2099 |
|
| 2100 |
# if the source is splitting a delta, not all fields are signed, |
| 2101 |
# so we need to carry the negative signs. |
| 2102 |
|
| 2103 |
my $sign = '+'; |
| 2104 |
foreach my $f (@fields) { |
| 2105 |
if ($f =~ /^([-+])/o) { |
| 2106 |
$sign = $1; |
| 2107 |
} else { |
| 2108 |
$f = "$sign$f"; |
| 2109 |
} |
| 2110 |
$f *= 1; |
| 2111 |
} |
| 2112 |
|
| 2113 |
} else { |
| 2114 |
foreach my $f (@fields) { |
| 2115 |
$f *= 1; |
| 2116 |
} |
| 2117 |
} |
| 2118 |
|
| 2119 |
return @fields; |
| 2120 |
} |
| 2121 |
|
| 2122 |
# This applies the correct sign to each field based on the $sign option: |
| 2123 |
# |
| 2124 |
# 1 : all fields signed |
| 2125 |
# -1 : all negative fields signed |
| 2126 |
# 0 : minimum number of signs for a joined set of fields |
| 2127 |
# |
| 2128 |
sub _sign_fields { |
| 2129 |
my($self,$sign,@fields) = @_; |
| 2130 |
$sign = 0 if (! defined $sign); |
| 2131 |
|
| 2132 |
if ($sign == 1) { |
| 2133 |
# All fields signed |
| 2134 |
foreach my $f (@fields) { |
| 2135 |
$f = "+$f" if ($f > 0); |
| 2136 |
} |
| 2137 |
|
| 2138 |
} elsif ($sign == 0) { |
| 2139 |
# Minimum number of signs |
| 2140 |
my $s = ($fields[0] < 0 ? '-' : '+'); |
| 2141 |
foreach my $f (@fields[1..$#fields]) { |
| 2142 |
if ($f > 0 && $s eq '-') { |
| 2143 |
$f = "+$f"; |
| 2144 |
$s = '+'; |
| 2145 |
} elsif ($f < 0) { |
| 2146 |
if ($s eq '-') { |
| 2147 |
$f *= -1; |
| 2148 |
} else { |
| 2149 |
$s = '-'; |
| 2150 |
} |
| 2151 |
} |
| 2152 |
} |
| 2153 |
} |
| 2154 |
|
| 2155 |
return @fields; |
| 2156 |
} |
| 2157 |
|
| 2158 |
# $opts = { nonorm => 0/1, |
| 2159 |
# source => string, list |
| 2160 |
# sign => 0/1/-1 |
| 2161 |
# } |
| 2162 |
# $fields = [H,M,S] |
| 2163 |
# |
| 2164 |
# This function formats the fields in an amount of time measured in |
| 2165 |
# hours, minutes, and seconds. |
| 2166 |
# |
| 2167 |
# It is similar to how _delta_fields (above) works. |
| 2168 |
# |
| 2169 |
sub _time_fields { |
| 2170 |
my($self,$opts,$fields) = @_; |
| 2171 |
my @fields = @$fields; |
| 2172 |
|
| 2173 |
# |
| 2174 |
# Make sure that all fields are defined, numerical, and that there |
| 2175 |
# are 3 of them. |
| 2176 |
# |
| 2177 |
|
| 2178 |
foreach my $f (@fields) { |
| 2179 |
$f=0 if (! defined($f)); |
| 2180 |
return (1) if (! $self->_is_int($f)); |
| 2181 |
} |
| 2182 |
return (1) if (@fields > 3); |
| 2183 |
while (@fields < 3) { |
| 2184 |
unshift(@fields,0); |
| 2185 |
} |
| 2186 |
|
| 2187 |
# |
| 2188 |
# Make sure each field is the correct sign so that the math will |
| 2189 |
# work correctly. Get rid of all positive signs and leading 0's. |
| 2190 |
# |
| 2191 |
|
| 2192 |
my $source = $$opts{'source'}; |
| 2193 |
@fields = $self->_sign_source($source,@fields); |
| 2194 |
|
| 2195 |
# |
| 2196 |
# Normalize them. Values will be signed only if they are |
| 2197 |
# negative. |
| 2198 |
# |
| 2199 |
|
| 2200 |
my $norm = 1-$$opts{'nonorm'}; |
| 2201 |
if ($norm) { |
| 2202 |
my($h,$mn,$s) = @fields; |
| 2203 |
$s += $h*3600 + $mn*60; |
| 2204 |
@fields = __normalize_hms($h,$mn,$s); |
| 2205 |
} |
| 2206 |
|
| 2207 |
# |
| 2208 |
# Now make sure that the signs are included as appropriate. |
| 2209 |
# |
| 2210 |
|
| 2211 |
@fields = $self->_sign_fields($$opts{'sign'},@fields); |
| 2212 |
|
| 2213 |
return (0,@fields); |
| 2214 |
} |
| 2215 |
|
| 2216 |
# $opts = { out => string, list |
| 2217 |
# } |
| 2218 |
# $fields = [H,M,S] |
| 2219 |
# |
| 2220 |
# This function formats the fields in an HMS. |
| 2221 |
# |
| 2222 |
# If the out options is string, it prepares the fields to be joined (i.e. |
| 2223 |
# they are all 2 digits long). Otherwise, they are just numerical values |
| 2224 |
# (not necessarily 2 digits long). |
| 2225 |
# |
| 2226 |
# HH:MN:SS is always between 00:00:00 and 24:00:00. |
| 2227 |
# |
| 2228 |
# It returns ($err,@fields) |
| 2229 |
# |
| 2230 |
sub _hms_fields { |
| 2231 |
my($self,$opts,$fields) = @_; |
| 2232 |
my @fields = @$fields; |
| 2233 |
|
| 2234 |
# |
| 2235 |
# Make sure that all fields are defined, numerical (with no sign), |
| 2236 |
# and that there are 3 of them. |
| 2237 |
# |
| 2238 |
|
| 2239 |
foreach my $f (@fields) { |
| 2240 |
$f=0 if (! $f); |
| 2241 |
return (1) if (! $self->_is_int($f,0)); |
| 2242 |
} |
| 2243 |
return (1) if (@fields > 3); |
| 2244 |
while (@fields < 3) { |
| 2245 |
push(@fields,0); |
| 2246 |
} |
| 2247 |
|
| 2248 |
# |
| 2249 |
# Check validity. |
| 2250 |
# |
| 2251 |
|
| 2252 |
my ($h,$m,$s) = @fields; |
| 2253 |
return (1) if ($h > 24 || $m > 59 || $s > 59 || |
| 2254 |
($h==24 && ($m > 0 || $s > 0))); |
| 2255 |
|
| 2256 |
# |
| 2257 |
# Format |
| 2258 |
# |
| 2259 |
|
| 2260 |
if ($$opts{'out'} eq 'list') { |
| 2261 |
foreach my $f ($h,$m,$s) { |
| 2262 |
$f *= 1; |
| 2263 |
} |
| 2264 |
|
| 2265 |
} else { |
| 2266 |
foreach my $f ($h,$m,$s) { |
| 2267 |
$f = "0$f" if (length($f)<2); |
| 2268 |
} |
| 2269 |
} |
| 2270 |
|
| 2271 |
return (0,$h,$m,$s); |
| 2272 |
} |
| 2273 |
|
| 2274 |
# $opts = { source => string, list |
| 2275 |
# out => string, list |
| 2276 |
# } |
| 2277 |
# $fields = [H,M,S] |
| 2278 |
# |
| 2279 |
# This function formats the fields in a timezone offset measured in |
| 2280 |
# hours, minutes, and seconds. |
| 2281 |
# |
| 2282 |
# All offsets must be -23:59:59 <= offset <= 23:59:59 . |
| 2283 |
# |
| 2284 |
# The data comes from an offset in string or list format, and is |
| 2285 |
# formatted so that it can be used to create a string or list format |
| 2286 |
# output. |
| 2287 |
# |
| 2288 |
sub _offset_fields { |
| 2289 |
my($self,$opts,$fields) = @_; |
| 2290 |
my @fields = @$fields; |
| 2291 |
|
| 2292 |
# |
| 2293 |
# Make sure that all fields are defined, numerical, and that there |
| 2294 |
# are 3 of them. |
| 2295 |
# |
| 2296 |
|
| 2297 |
foreach my $f (@fields) { |
| 2298 |
$f=0 if (! defined $f || $f eq ''); |
| 2299 |
return (1) if (! $self->_is_int($f)); |
| 2300 |
} |
| 2301 |
return (1) if (@fields > 3); |
| 2302 |
while (@fields < 3) { |
| 2303 |
push(@fields,0); |
| 2304 |
} |
| 2305 |
|
| 2306 |
# |
| 2307 |
# Check validity. |
| 2308 |
# |
| 2309 |
|
| 2310 |
my ($h,$m,$s) = @fields; |
| 2311 |
if ($$opts{'source'} eq 'string') { |
| 2312 |
# Values = -23 59 59 to +23 59 59 |
| 2313 |
return (1) if ($h < -23 || $h > 23 || |
| 2314 |
$m < 0 || $m > 59 || |
| 2315 |
$s < 0 || $s > 59); |
| 2316 |
} else { |
| 2317 |
# Values (-23,-59,-59) to (23,59,59) |
| 2318 |
# Non-zero values must have the same sign |
| 2319 |
if ($h >0) { |
| 2320 |
return (1) if ( $h > 23 || |
| 2321 |
$m < 0 || $m > 59 || |
| 2322 |
$s < 0 || $s > 59); |
| 2323 |
} elsif ($h < 0) { |
| 2324 |
return (1) if ($h < -23 || |
| 2325 |
$m < -59 || $m > 0 || |
| 2326 |
$s < -59 || $s > 0); |
| 2327 |
} elsif ($m > 0) { |
| 2328 |
return (1) if ( $m > 59 || |
| 2329 |
$s < 0 || $s > 59); |
| 2330 |
} elsif ($m < 0) { |
| 2331 |
return (1) if ($m < -59 || |
| 2332 |
$s < -59 || $s > 0); |
| 2333 |
} else { |
| 2334 |
return (1) if ($s < -59 || $s > 59); |
| 2335 |
} |
| 2336 |
} |
| 2337 |
|
| 2338 |
# |
| 2339 |
# Make sure each field is the correct sign so that the math will |
| 2340 |
# work correctly. Get rid of all positive signs and leading 0's. |
| 2341 |
# |
| 2342 |
|
| 2343 |
if ($$opts{'source'} eq 'string') { |
| 2344 |
|
| 2345 |
# In a string offset, only the first field is signed, so we need |
| 2346 |
# to carry negative signs. |
| 2347 |
|
| 2348 |
if ($h =~ /^\-/) { |
| 2349 |
$h *= 1; |
| 2350 |
$m *= -1; |
| 2351 |
$s *= -1; |
| 2352 |
} elsif ($m =~ /^\-/) { |
| 2353 |
$h *= 1; |
| 2354 |
$m *= 1; |
| 2355 |
$s *= -1; |
| 2356 |
} else { |
| 2357 |
$h *= 1; |
| 2358 |
$m *= 1; |
| 2359 |
$s *= 1; |
| 2360 |
} |
| 2361 |
|
| 2362 |
} else { |
| 2363 |
foreach my $f (@fields) { |
| 2364 |
$f *= 1; |
| 2365 |
} |
| 2366 |
} |
| 2367 |
|
| 2368 |
# |
| 2369 |
# Format them. They're already done for 'list' output. |
| 2370 |
# |
| 2371 |
|
| 2372 |
if ($$opts{'out'} eq 'string') { |
| 2373 |
my $sign; |
| 2374 |
if ($h<0 || $m<0 || $s<0) { |
| 2375 |
$h = abs($h); |
| 2376 |
$m = abs($m); |
| 2377 |
$s = abs($s); |
| 2378 |
$sign = '-'; |
| 2379 |
} else { |
| 2380 |
$sign = '+'; |
| 2381 |
} |
| 2382 |
|
| 2383 |
$h = "0$h" if (length($h) < 2); |
| 2384 |
$m = "0$m" if (length($m) < 2); |
| 2385 |
$s = "0$s" if (length($s) < 2); |
| 2386 |
$h = "$sign$h"; |
| 2387 |
} |
| 2388 |
|
| 2389 |
return (0,$h,$m,$s); |
| 2390 |
} |
| 2391 |
|
| 2392 |
# ($err,$y,$m,$d,$h,$mn,$s) = $self->_date_fields($y,$m,$d,$h,$mn,$s); |
| 2393 |
# |
| 2394 |
# Makes sure the fields are the right length. |
| 2395 |
# |
| 2396 |
sub _date_fields { |
| 2397 |
my($self,@fields) = @_; |
| 2398 |
return (1) if (@fields != 6); |
| 2399 |
|
| 2400 |
my($y,$m,$d,$h,$mn,$s) = @fields; |
| 2401 |
|
| 2402 |
$y = "0$y" while (length($y) < 4); |
| 2403 |
$m = "0$m" if (length($m)==1); |
| 2404 |
$d = "0$d" if (length($d)==1); |
| 2405 |
$h = "0$h" if (length($h)==1); |
| 2406 |
$mn = "0$mn" if (length($mn)==1); |
| 2407 |
$s = "0$s" if (length($s)==1); |
| 2408 |
|
| 2409 |
if (wantarray) { |
| 2410 |
return (0,$y,$m,$d,$h,$mn,$s); |
| 2411 |
} else { |
| 2412 |
return "$y$m$d$h:$mn:$s"; |
| 2413 |
} |
| 2414 |
} |
| 2415 |
|
| 2416 |
# $self->_delta_convert(FORMAT,DELTA) |
| 2417 |
# This converts delta into the given format. Returns '' if invalid. |
| 2418 |
# |
| 2419 |
sub _delta_convert { |
| 2420 |
my($self,$format,$delta)=@_; |
| 2421 |
my $fields = $self->split($format,$delta); |
| 2422 |
return undef if (! defined $fields); |
| 2423 |
return $self->join($format,$fields); |
| 2424 |
} |
| 2425 |
|
| 2426 |
############################################################################### |
| 2427 |
# Normalize the different types of deltas |
| 2428 |
|
| 2429 |
sub __normalize_ym { |
| 2430 |
my($y,$m,$s,$mon) = @_; |
| 2431 |
no integer; |
| 2432 |
|
| 2433 |
if (defined($s)) { |
| 2434 |
$m = int($s/$mon); |
| 2435 |
$s -= int(sprintf('%f',$m*$mon)); |
| 2436 |
$y = int($m/12); |
| 2437 |
$m -= $y*12; |
| 2438 |
|
| 2439 |
return($y,$m,$s); |
| 2440 |
} else { |
| 2441 |
$m += $y*12; |
| 2442 |
$y = int($m/12); |
| 2443 |
$m -= $y*12; |
| 2444 |
|
| 2445 |
return($y,$m); |
| 2446 |
} |
| 2447 |
} |
| 2448 |
sub __normalize_wd { |
| 2449 |
my($w,$d,$s,$wk,$day) = @_; |
| 2450 |
no integer; |
| 2451 |
|
| 2452 |
$d = int($s/$day); |
| 2453 |
$s -= int($d*$day); |
| 2454 |
$w = int($d/$wk); |
| 2455 |
$d -= $w*$wk; |
| 2456 |
|
| 2457 |
return($w,$d,$s); |
| 2458 |
} |
| 2459 |
sub __normalize_hms { |
| 2460 |
my($h,$mn,$s) = @_; |
| 2461 |
no integer; |
| 2462 |
|
| 2463 |
$h = int($s/3600); |
| 2464 |
$s -= $h*3600; |
| 2465 |
$mn = int($s/60); |
| 2466 |
$s -= $mn*60; |
| 2467 |
$s = int($s); |
| 2468 |
|
| 2469 |
return($h,$mn,$s); |
| 2470 |
} |
| 2471 |
|
| 2472 |
sub _normalize_est { |
| 2473 |
my($self,$y,$m,$w,$d,$h,$mn,$s) = @_; |
| 2474 |
no integer; |
| 2475 |
|
| 2476 |
# Figure out how many seconds there are in the estimated delta |
| 2477 |
# |
| 2478 |
# 365.2425/12 days/month * 24 hours/day * 3600 sec/hour = 2629746 sec/month |
| 2479 |
|
| 2480 |
my $mon = 2629746; |
| 2481 |
my $day = 86400; |
| 2482 |
my $wk = 7; |
| 2483 |
$s += ($y*12+$m)*$mon + ($w*$wk + $d)*$day + |
| 2484 |
$h*3600 + $mn*60; |
| 2485 |
|
| 2486 |
($y,$m,$s) = __normalize_ym($y,$m,$s,$mon); |
| 2487 |
($w,$d,$s) = __normalize_wd($w,$d,$s,$wk,$day); |
| 2488 |
($h,$mn,$s) = __normalize_hms($h,$mn,$s); |
| 2489 |
|
| 2490 |
return ($y,$m,$w,$d,$h,$mn,$s); |
| 2491 |
} |
| 2492 |
sub _normalize_bus_est { |
| 2493 |
my($self,$y,$m,$w,$d,$h,$mn,$s) = @_; |
| 2494 |
no integer; |
| 2495 |
|
| 2496 |
# Figure out how many seconds there are in the estimated delta |
| 2497 |
# |
| 2498 |
# 365.2425/12 * wk_len/7 days/month * day sec/day = X sec/month |
| 2499 |
|
| 2500 |
my $day = $$self{'data'}{'len'}{'bdlength'}; |
| 2501 |
my $wk = $$self{'data'}{'len'}{'workweek'}; |
| 2502 |
my $mon = 365.2425/12 * $wk/7 * $day; |
| 2503 |
|
| 2504 |
$s += ($y*12+$m)*$mon + ($w*$wk + $d)*$day + |
| 2505 |
$h*3600 + $mn*60; |
| 2506 |
|
| 2507 |
($y,$m,$s) = __normalize_ym($y,$m,$s,$mon); |
| 2508 |
($w,$d,$s) = __normalize_wd($w,$d,$s,$wk,$day); |
| 2509 |
($h,$mn,$s) = __normalize_hms($h,$mn,$s); |
| 2510 |
|
| 2511 |
return ($y,$m,$w,$d,$h,$mn,$s); |
| 2512 |
} |
| 2513 |
|
| 2514 |
sub _normalize_approx { |
| 2515 |
my($self,$y,$m,$w,$d,$h,$mn,$s) = @_; |
| 2516 |
no integer; |
| 2517 |
|
| 2518 |
my $wk = 7; |
| 2519 |
my $day = 86400; |
| 2520 |
$s += ($w*$wk + $d)*$day + $h*3600 + $mn*60; |
| 2521 |
|
| 2522 |
($y,$m) = __normalize_ym($y,$m); |
| 2523 |
($w,$d,$s) = __normalize_wd($w,$d,$s,$wk,$day); |
| 2524 |
($h,$mn,$s) = __normalize_hms($h,$mn,$s); |
| 2525 |
|
| 2526 |
return ($y,$m,$w,$d,$h,$mn,$s); |
| 2527 |
} |
| 2528 |
sub _normalize_bus_approx { |
| 2529 |
my($self,$y,$m,$w,$d,$h,$mn,$s) = @_; |
| 2530 |
no integer; |
| 2531 |
|
| 2532 |
my $wk = $$self{'data'}{'len'}{'workweek'}; |
| 2533 |
my $day = $$self{'data'}{'len'}{'bdlength'}; |
| 2534 |
$s += ($w*$wk + $d)*$day + $h*3600 + $mn*60; |
| 2535 |
|
| 2536 |
($y,$m) = __normalize_ym($y,$m); |
| 2537 |
($w,$d,$s) = __normalize_wd($w,$d,$s,$wk,$day); |
| 2538 |
($h,$mn,$s) = __normalize_hms($h,$mn,$s); |
| 2539 |
|
| 2540 |
return ($y,$m,$w,$d,$h,$mn,$s); |
| 2541 |
} |
| 2542 |
|
| 2543 |
sub _normalize_exact { |
| 2544 |
my($self,$y,$m,$w,$d,$h,$mn,$s) = @_; |
| 2545 |
no integer; |
| 2546 |
|
| 2547 |
$s += $h*3600 + $mn*60; |
| 2548 |
|
| 2549 |
($h,$mn,$s) = __normalize_hms($h,$mn,$s); |
| 2550 |
|
| 2551 |
return ($y,$m,$w,$d,$h,$mn,$s); |
| 2552 |
} |
| 2553 |
sub _normalize_bus_exact { |
| 2554 |
my($self,$y,$m,$w,$d,$h,$mn,$s) = @_; |
| 2555 |
no integer; |
| 2556 |
|
| 2557 |
my $day = $$self{'data'}{'len'}{'bdlength'}; |
| 2558 |
|
| 2559 |
$s += $d*$day + $h*3600 + $mn*60; |
| 2560 |
|
| 2561 |
# Calculate d |
| 2562 |
|
| 2563 |
$d = int($s/$day); |
| 2564 |
$s -= $d*$day; |
| 2565 |
|
| 2566 |
($h,$mn,$s) = __normalize_hms($h,$mn,$s); |
| 2567 |
|
| 2568 |
return ($y,$m,$w,$d,$h,$mn,$s); |
| 2569 |
} |
| 2570 |
|
| 2571 |
############################################################################### |
| 2572 |
# Timezone critical dates |
| 2573 |
|
| 2574 |
# NOTE: Although I would prefer to stick this routine in the |
| 2575 |
# Date::Manip::TZ module where it would be more appropriate, it must |
| 2576 |
# appear here as it will be used to generate the data that will be |
| 2577 |
# used by the Date::Manip::TZ module. |
| 2578 |
# |
| 2579 |
# This calculates a critical date based on timezone information. The |
| 2580 |
# critical date is the date (usually in the current time) at which |
| 2581 |
# the current timezone period ENDS. |
| 2582 |
# |
| 2583 |
# Input is: |
| 2584 |
# $year,$mon,$flag,$num,$dow |
| 2585 |
# This is information from the appropriate Rule line from the |
| 2586 |
# zoneinfo files. These are used to determine the date (Y/M/D) |
| 2587 |
# when the timezone period will end. |
| 2588 |
# $isdst |
| 2589 |
# Whether or not the next timezone period is a Daylight Saving |
| 2590 |
# Time period. |
| 2591 |
# $time,$timetype |
| 2592 |
# The time of day when the change occurs. The timetype can be |
| 2593 |
# 'w' (wallclock time in the current period), 's' (standard |
| 2594 |
# time which will match wallclock time in a non-DST period, or |
| 2595 |
# be off an hour in a DST period), and 'u' (universal time). |
| 2596 |
# |
| 2597 |
# Output is: |
| 2598 |
# $endUT, $endLT, $begUT, $begLT |
| 2599 |
# endUT is the actual last second of the current timezone |
| 2600 |
# period. endLT is the same time expressed in local time. |
| 2601 |
# begUT is the start (in UT) of the next time period. Note that |
| 2602 |
# the begUT date is the one which actually corresponds to the |
| 2603 |
# date/time specified in the input. begLT is the time in the new |
| 2604 |
# local time. The endUT/endLT are the time one second earlier. |
| 2605 |
# |
| 2606 |
sub _critical_date { |
| 2607 |
my($self,$year,$mon,$flag,$num,$dow, |
| 2608 |
$isdst,$time,$timetype,$stdoff,$dstoff) = @_; |
| 2609 |
|
| 2610 |
# |
| 2611 |
# Get the predicted Y/M/D |
| 2612 |
# |
| 2613 |
|
| 2614 |
my($y,$m,$d) = ($year+0,$mon+0,1); |
| 2615 |
|
| 2616 |
if ($flag eq 'dom') { |
| 2617 |
$d = $num; |
| 2618 |
|
| 2619 |
} elsif ($flag eq 'last') { |
| 2620 |
my $ymd = $self->nth_day_of_week($year,-1,$dow,$mon); |
| 2621 |
$d = $$ymd[2]; |
| 2622 |
|
| 2623 |
} elsif ($flag eq 'ge') { |
| 2624 |
my $ymd = $self->nth_day_of_week($year,1,$dow,$mon); |
| 2625 |
$d = $$ymd[2]; |
| 2626 |
while ($d < $num) { |
| 2627 |
$d += 7; |
| 2628 |
} |
| 2629 |
|
| 2630 |
} elsif ($flag eq 'le') { |
| 2631 |
my $ymd = $self->nth_day_of_week($year,-1,$dow,$mon); |
| 2632 |
$d = $$ymd[2]; |
| 2633 |
while ($d > $num) { |
| 2634 |
$d -= 7; |
| 2635 |
} |
| 2636 |
} |
| 2637 |
|
| 2638 |
# |
| 2639 |
# Get the predicted time and the date (not yet taking into |
| 2640 |
# account time type). |
| 2641 |
# |
| 2642 |
|
| 2643 |
my($h,$mn,$s) = @{ $self->split('hms',$time) }; |
| 2644 |
my $date = [ $y,$m,$d,$h,$mn,$s ]; |
| 2645 |
|
| 2646 |
# |
| 2647 |
# Calculate all the relevant dates. |
| 2648 |
# |
| 2649 |
|
| 2650 |
my($endUT,$endLT,$begUT,$begLT,$offset); |
| 2651 |
$stdoff = $self->split('offset',$stdoff); |
| 2652 |
$dstoff = $self->split('offset',$dstoff); |
| 2653 |
|
| 2654 |
if ($timetype eq 'w') { |
| 2655 |
$begUT = $self->calc_date_time($date,($isdst ? $stdoff : $dstoff), 1); |
| 2656 |
} elsif ($timetype eq 'u') { |
| 2657 |
$begUT = $date; |
| 2658 |
} else { |
| 2659 |
$begUT = $self->calc_date_time($date,$stdoff, 1); |
| 2660 |
} |
| 2661 |
|
| 2662 |
$endUT = $self->calc_date_time($begUT,[0,0,-1]); |
| 2663 |
$endLT = $self->calc_date_time($endUT,($isdst ? $stdoff : $dstoff)); |
| 2664 |
$begLT = $self->calc_date_time($begUT,($isdst ? $dstoff : $stdoff)); |
| 2665 |
|
| 2666 |
return ($endUT,$endLT,$begUT,$begLT); |
| 2667 |
} |
| 2668 |
|
| 2669 |
############################################################################### |
| 2670 |
# Get a list of strings to try to parse. |
| 2671 |
|
| 2672 |
sub _encoding { |
| 2673 |
my($self,$string) = @_; |
| 2674 |
my @ret; |
| 2675 |
|
| 2676 |
foreach my $enc (@{ $$self{'data'}{'calc'}{'enc_in'} }) { |
| 2677 |
if (lc($enc) eq 'utf-8') { |
| 2678 |
_utf8_on($string); |
| 2679 |
push(@ret,$string) if is_utf8($string, 1); |
| 2680 |
} elsif (lc($enc) eq 'perl') { |
| 2681 |
push(@ret,encode_utf8($string)); |
| 2682 |
} else { |
| 2683 |
my $tmp = $string; |
| 2684 |
_utf8_off($tmp); |
| 2685 |
$tmp = encode_utf8(decode($enc, $tmp)); |
| 2686 |
_utf8_on($tmp); |
| 2687 |
push(@ret,$tmp) if is_utf8($tmp, 1);; |
| 2688 |
} |
| 2689 |
} |
| 2690 |
|
| 2691 |
return @ret; |
| 2692 |
} |
| 2693 |
|
| 2694 |
1; |
| 2695 |
# Local Variables: |
| 2696 |
# mode: cperl |
| 2697 |
# indent-tabs-mode: nil |
| 2698 |
# cperl-indent-level: 3 |
| 2699 |
# cperl-continued-statement-offset: 2 |
| 2700 |
# cperl-continued-brace-offset: 0 |
| 2701 |
# cperl-brace-offset: 0 |
| 2702 |
# cperl-brace-imaginary-offset: 0 |
| 2703 |
# cperl-label-offset: 0 |
| 2704 |
# End: |