| 1 |
# Copyright (c) 1995-2026 Sullivan Beck. All rights reserved. |
| 2 |
# This program is free software; you can redistribute it and/or modify it |
| 3 |
# under the same terms as Perl itself. |
| 4 |
|
| 5 |
=pod |
| 6 |
|
| 7 |
=head1 NAME |
| 8 |
|
| 9 |
Date::Manip::Base - Base methods for date manipulation |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
use Date::Manip::Base; |
| 14 |
$dmb = new Date::Manip::Base; |
| 15 |
|
| 16 |
=head1 DESCRIPTION |
| 17 |
|
| 18 |
The Date::Manip package of modules consists of several modules for |
| 19 |
doing high level date operations with full error checking and a lot of |
| 20 |
flexibility. |
| 21 |
|
| 22 |
The high level operations, though intended to be used in most |
| 23 |
situations, have a lot of overhead associated with them. As such, a |
| 24 |
number of the most useful low level routines (which the high level |
| 25 |
routines use to do much of the real work) are included in this module |
| 26 |
and are available directly to users. |
| 27 |
|
| 28 |
These low level routines are powerful enough that they can be used |
| 29 |
independent of the high level routines and perform useful (though much |
| 30 |
simpler) operations. They are also significantly faster than the high |
| 31 |
level routines. |
| 32 |
|
| 33 |
These routines do little error checking on input. Invalid data will |
| 34 |
result in meaningless results. If you need error checking, you must |
| 35 |
call the higher level Date::Manip routines instead of these. |
| 36 |
|
| 37 |
These routines also ignore all effects of time zones and daylight |
| 38 |
saving time. One way to think of these routines is working with times |
| 39 |
and dates in the GMT time zone. |
| 40 |
|
| 41 |
=head1 BASE METHODS |
| 42 |
|
| 43 |
This class inherits several base methods from the L<Date::Manip::Obj> |
| 44 |
class. Please refer to the documentation for that class for a |
| 45 |
description of those methods. |
| 46 |
|
| 47 |
=over 4 |
| 48 |
|
| 49 |
=item B<err> |
| 50 |
|
| 51 |
=item B<new> |
| 52 |
|
| 53 |
=item B<new_config> |
| 54 |
|
| 55 |
Please refer to the L<Date::Manip::Obj> documentation for these methods. |
| 56 |
|
| 57 |
=item B<config> |
| 58 |
|
| 59 |
$dmb->config($var1,$val1,$var2,$val2,...); |
| 60 |
|
| 61 |
This will set the value of any configuration variable. Please refer to the |
| 62 |
L<Date::Manip::Config> manual for a list of all configuration variables and their |
| 63 |
description. |
| 64 |
|
| 65 |
=back |
| 66 |
|
| 67 |
=head1 DATE METHODS |
| 68 |
|
| 69 |
In all of the following method descriptions, the following variables |
| 70 |
are used: |
| 71 |
|
| 72 |
=over 4 |
| 73 |
|
| 74 |
=item B<$date> |
| 75 |
|
| 76 |
This is a list reference containing a full date and time: |
| 77 |
|
| 78 |
[$y, $m, $d, $h, $mn, $s] |
| 79 |
|
| 80 |
=item B<$ymd> |
| 81 |
|
| 82 |
A list reference containing only the date portion: |
| 83 |
|
| 84 |
[$y, $m, $d] |
| 85 |
|
| 86 |
=item B<$hms> |
| 87 |
|
| 88 |
A list reference containing only the time portion: |
| 89 |
|
| 90 |
[$h, $mn, $s] |
| 91 |
|
| 92 |
=item B<$delta> |
| 93 |
|
| 94 |
A list containing a full delta (an amount of time elapsed, or a duration): |
| 95 |
|
| 96 |
[$dy, $dm, $dw, $dd, $dh, $dmn, $ds] |
| 97 |
|
| 98 |
=item B<$time> |
| 99 |
|
| 100 |
A list reference containing the hour/minute/second portion of a delta: |
| 101 |
|
| 102 |
[$dh, $dmn, $ds] |
| 103 |
|
| 104 |
=item B<$offset> |
| 105 |
|
| 106 |
A list containing a time zone expressed as an offset: |
| 107 |
|
| 108 |
[ $offh, $offm, $offs ] |
| 109 |
|
| 110 |
Although this module does not make use of timezone information, a few of |
| 111 |
the functions perform operations on time zone offsets, primarily because |
| 112 |
these operations are needed in the higher level modules. |
| 113 |
|
| 114 |
=back |
| 115 |
|
| 116 |
The elements C<($y, $m, $d, $h, $mn, $s)> are all numeric. In most of |
| 117 |
the routines described below, no error checking is done on the input. |
| 118 |
C<$y> should be between 1 and 9999, C<$m> between 1 and 12, C<$d> |
| 119 |
between 1 and 31, C<$h> should be between 0 and 23, C<$mn> and C<$s> |
| 120 |
between 0 and 59. |
| 121 |
|
| 122 |
C<$hms> can be between 00:00:00 and 24:00:00, but an C<$offset> must be |
| 123 |
between -23:59:59 and +23:59:59. |
| 124 |
|
| 125 |
Years are not translated to 4 digit years, so passing in a year of |
| 126 |
"04" will be equivalent to "0004", NOT "2004". |
| 127 |
|
| 128 |
The elements C<($dy, $dm, $dw, $dd, $dh, $dmn, $ds)> are all numeric, |
| 129 |
but can be positive or negative. They represent an elapsed amount |
| 130 |
of time measured in years, months, weeks, etc. |
| 131 |
|
| 132 |
Since no error checking is done, passing in C<($y,$m,$d) = (2004,2,31)> |
| 133 |
will NOT trigger an error, even though February does not have 31 days. |
| 134 |
Instead, some meaningless result will be returned. |
| 135 |
|
| 136 |
=over 4 |
| 137 |
|
| 138 |
=item B<calc_date_date> |
| 139 |
|
| 140 |
=item B<calc_date_days> |
| 141 |
|
| 142 |
=item B<calc_date_delta> |
| 143 |
|
| 144 |
=item B<calc_date_time> |
| 145 |
|
| 146 |
=item B<calc_time_time> |
| 147 |
|
| 148 |
These are all routines for doing simple date and time calculations. |
| 149 |
As mentioned above, they ignore all affects of time zones and daylight |
| 150 |
saving time. |
| 151 |
|
| 152 |
The following methods are available: |
| 153 |
|
| 154 |
$time = $dmb->calc_date_date($date1,$date2); |
| 155 |
|
| 156 |
This take two dates and determine the amount of time between them. |
| 157 |
|
| 158 |
$date = $dmb->calc_date_days($date,$n [,$subtract]); |
| 159 |
$ymd = $dmb->calc_date_days($ymd,$n [,$subtract]); |
| 160 |
|
| 161 |
This returns a date C<$n> days later (if C<$nE<gt>0>) or earlier (if |
| 162 |
C<$nE<lt>0>) than the date passed in. If C<$subtract> is passed in, |
| 163 |
the sign of C<$n> is reversed. |
| 164 |
|
| 165 |
$date = $dmb->calc_date_delta($date,$delta [,$subtract]); |
| 166 |
|
| 167 |
This take a date and add the given delta to it (or subtract the delta |
| 168 |
if C<$subtract> is non-zero). |
| 169 |
|
| 170 |
$date = $dmb->calc_date_time($date,$time [,$subtract]); |
| 171 |
|
| 172 |
This take a date and add the given time to it (or subtract the time |
| 173 |
if C<$subtract> is non-zero). |
| 174 |
|
| 175 |
$time = $dmb->calc_time_time(@time1,@time2 [,$subtract]); |
| 176 |
|
| 177 |
This take two times and add them together (or subtract the second from |
| 178 |
the first if C<$subtract> is non-zero). |
| 179 |
|
| 180 |
=item B<check> |
| 181 |
|
| 182 |
=item B<check_time> |
| 183 |
|
| 184 |
$valid = $dmb->check($date); |
| 185 |
$valid = $dmb->check_time($hms); |
| 186 |
|
| 187 |
This tests a list of values to see if they form a valid date or time |
| 188 |
ignoring all time zone affects. The date/time would be valid in GMT, |
| 189 |
but perhaps not in all time zones. |
| 190 |
|
| 191 |
1 is returned if the the fields are valid, 0 otherwise. |
| 192 |
|
| 193 |
C<$hms> is in the range 00:00:00 to 24:00:00. |
| 194 |
|
| 195 |
=item B<cmp> |
| 196 |
|
| 197 |
$flag = $dmb->cmp($date1,$date2); |
| 198 |
|
| 199 |
Returns -1, 0, or 1 if date1 is before, the same as, or after date2. |
| 200 |
|
| 201 |
=item B<day_of_week> |
| 202 |
|
| 203 |
$day = $dmb->day_of_week($date); |
| 204 |
$day = $dmb->day_of_week($ymd); |
| 205 |
|
| 206 |
Returns the day of the week (1 for Monday, 7 for Sunday). |
| 207 |
|
| 208 |
=item B<day_of_year> |
| 209 |
|
| 210 |
$day = $dmb->day_of_year($ymd); |
| 211 |
$day = $dmb->day_of_year($date); |
| 212 |
|
| 213 |
In the first case, returns the day of the year (1 to 366) for C<($y, |
| 214 |
$m, $d)>. In the second case, it returns a fractional day (1.0 <= |
| 215 |
C<$day> < 367.0). For example, day 1.5 falls on Jan 1, at noon. The |
| 216 |
somewhat non-intuitive answer (1.5 instead of 0.5) is to make the two |
| 217 |
forms return numerically equivalent answers for times of 00:00:00 |
| 218 |
. You can look at the integer part of the number as being the day of |
| 219 |
the year, and the fractional part of the number as the fraction of the |
| 220 |
day that has passed at the given time. |
| 221 |
|
| 222 |
The inverse operations can also be done: |
| 223 |
|
| 224 |
$ymd = $dmb->day_of_year($y,$day); |
| 225 |
$date = $dmb->day_of_year($y,$day); |
| 226 |
|
| 227 |
If C<$day> is an integer, the year, month, and day is returned. If C<$day> |
| 228 |
is a floating point number, it returns the year, month, day, hour, |
| 229 |
minutes, and decimal seconds. |
| 230 |
|
| 231 |
C<$day> must be greater than or equal to 1 and less than 366 on non-leap years |
| 232 |
or 367 on leap years. |
| 233 |
|
| 234 |
=item B<days_in_month> |
| 235 |
|
| 236 |
$days = $dmb->days_in_month($y,$m); |
| 237 |
|
| 238 |
Returns the number of days in the month. |
| 239 |
|
| 240 |
@days = $dmb->days_in_month($y,0); |
| 241 |
|
| 242 |
Returns a list of 12 elements with the days in each month of the year. |
| 243 |
|
| 244 |
=item B<days_in_year> |
| 245 |
|
| 246 |
$days = $dmb->days_in_year($y); |
| 247 |
|
| 248 |
Returns the number of days in the year (365 or 366) |
| 249 |
|
| 250 |
=item B<days_since_1BC> |
| 251 |
|
| 252 |
$days = $dmb->days_since_1BC($date); |
| 253 |
$days = $dmb->days_since_1BC($ymd); |
| 254 |
|
| 255 |
Returns the number of days since Dec 31, 1BC. Since the calendar has |
| 256 |
changed a number of times, the number returned is based on the current |
| 257 |
calendar projected backwards in time, and in no way reflects a true |
| 258 |
number of days since then. As such, the result is largely meaningless, |
| 259 |
except when called twice as a means of determining the number of days |
| 260 |
separating two dates. |
| 261 |
|
| 262 |
The inverse operation is also available: |
| 263 |
|
| 264 |
$ymd = $dmb->days_since_1BC($days); |
| 265 |
|
| 266 |
Returns the date C<$days> since Dec 31, 1BC. So day 1 is Jan 1, 0001. |
| 267 |
|
| 268 |
=item B<leapyear> |
| 269 |
|
| 270 |
$flag = $dmb->leapyear($y); |
| 271 |
|
| 272 |
Returns 1 if the argument is a leap year. |
| 273 |
|
| 274 |
=item B<nth_day_of_week> |
| 275 |
|
| 276 |
$ymd = $dmb->nth_day_of_week($y,$n,$dow); |
| 277 |
|
| 278 |
Returns the C<$n>th occurrence of C<$dow> (1 for Monday, 7 for Sunday) in the |
| 279 |
year. C<$n> must be between 1 and 53 or -1 through -53. |
| 280 |
|
| 281 |
$ymd = $dmb->nth_day_of_week($y,$n,$dow,$m); |
| 282 |
|
| 283 |
Returns the C<$n>th occurrence of C<$dow> in the given month. C<$n> |
| 284 |
must be between 1 and 5 or it can be -1 through -5. |
| 285 |
|
| 286 |
In all cases, nothing is returned if C<$n> is beyond the last actual |
| 287 |
result (i.e. the 5th Sunday in a month with only four Sundays). |
| 288 |
|
| 289 |
=item B<secs_since_1970> |
| 290 |
|
| 291 |
$secs = $dmb->secs_since_1970($date); |
| 292 |
|
| 293 |
Returns the number of seconds since Jan 1, 1970 00:00:00 (negative if date is |
| 294 |
earlier). |
| 295 |
|
| 296 |
$date = $dmb->secs_since_1970($secs); |
| 297 |
|
| 298 |
Translates number of seconds into a date. |
| 299 |
|
| 300 |
=item B<split> |
| 301 |
|
| 302 |
=item B<join> |
| 303 |
|
| 304 |
The split and join functions are used to take a string containing a common |
| 305 |
type of time data and split it into a list of fields. The join function takes |
| 306 |
the list and forms it into a string. |
| 307 |
|
| 308 |
The general format for these is: |
| 309 |
|
| 310 |
$obj = $dmb->split($type,$string,\%opts); |
| 311 |
$string = $dmb->join($type,$obj,\%opts); |
| 312 |
|
| 313 |
An older format is also supported: |
| 314 |
|
| 315 |
$obj = $dmb->split($type,$string,[$no_normalize]); |
| 316 |
$string = $dmb->join($type,$obj,[$no_normalize]); |
| 317 |
|
| 318 |
but this is deprecated and will be removed in Date::Manip 7.00. These |
| 319 |
are equivalent to: |
| 320 |
|
| 321 |
$obj = $dmb->split($type,$string,{ 'nonorm' => $no_normalize }); |
| 322 |
$string = $dmb->join($type,$obj,{ 'nonorm' => $no_normalize }); |
| 323 |
|
| 324 |
The value of C<$type> determines what type of join/split operation occurs. |
| 325 |
|
| 326 |
Rudimentary error checking is performed with both of these functions |
| 327 |
and undef is returned in the case of any error. No error checking is done |
| 328 |
on the specific values. |
| 329 |
|
| 330 |
=over 8 |
| 331 |
|
| 332 |
=item B<$type = 'date'> |
| 333 |
|
| 334 |
$date = $dmb->split("date",$string); |
| 335 |
$string = $dmb->join("date",$date); |
| 336 |
|
| 337 |
This splits a string containing a date or creates one from a list reference. |
| 338 |
The string split must be of one of the forms: |
| 339 |
|
| 340 |
YYYYMMDDHH:MN:SS |
| 341 |
YYYYMMDDHHMNSS |
| 342 |
YYYY-MM-DD-HH:MN:SS |
| 343 |
|
| 344 |
The string formed by join is one of the above, depending on the value of |
| 345 |
the Printable config variable. The default format is YYYYMMDDHH:MN:SS, |
| 346 |
but if Printable is set to 1, YYYYMMDDHHMNSS is produced, and if Printable |
| 347 |
is set to 2, the YYYY-MM-DD-HH:MN:SS form is produced. |
| 348 |
|
| 349 |
=item B<$type = 'hms'> |
| 350 |
|
| 351 |
$hms = $dmb->split("hms",$string); |
| 352 |
$string = $dmb->join("hms",$hms); |
| 353 |
|
| 354 |
This works with the hours, minutes, and seconds portion of a date. |
| 355 |
|
| 356 |
When splitting a string, the string can be of any of the forms: |
| 357 |
|
| 358 |
H |
| 359 |
H:MN |
| 360 |
H:MN:SS |
| 361 |
HH |
| 362 |
HHMN |
| 363 |
HHMNSS |
| 364 |
|
| 365 |
Here, H is a 1 or 2 digit representation of the hours but HH (and all |
| 366 |
other fields) are two digit representations. |
| 367 |
|
| 368 |
The string formed by the join function will always be of the form HH:MN:SS. |
| 369 |
|
| 370 |
The time must be between 00:00:00 and 24:00:00. |
| 371 |
|
| 372 |
=item B<$type = 'offset'> |
| 373 |
|
| 374 |
$offset = $dmb->split("offset",$string); |
| 375 |
$string = $dmb->join("offset",$offset); |
| 376 |
|
| 377 |
An offset string should have a sign (though it is optional if it is |
| 378 |
positive) and is any of the forms: |
| 379 |
|
| 380 |
+H |
| 381 |
+H:MN |
| 382 |
+H:MN:SS |
| 383 |
+HH |
| 384 |
+HHMN |
| 385 |
+HHMNSS |
| 386 |
|
| 387 |
Here, H is a 1 or 2 digit representation of the hours. All other fields are |
| 388 |
two digit representations. |
| 389 |
|
| 390 |
The string formed by the join function will always be of the form |
| 391 |
+HH:MN:SS. |
| 392 |
|
| 393 |
The offset must be between -23:59:59 and +23:59:59 . |
| 394 |
|
| 395 |
=item B<$type = 'time'> |
| 396 |
|
| 397 |
$time = $dmb->split("time",$string,\%opts]); |
| 398 |
$string = $dmb->join("time",$time,\%opts); |
| 399 |
|
| 400 |
The only option supported is: |
| 401 |
|
| 402 |
'nonorm' 0/1 |
| 403 |
|
| 404 |
This works with an amount of time in hours, minutes, and seconds. The |
| 405 |
string is of the format: |
| 406 |
|
| 407 |
+H:MN:S |
| 408 |
|
| 409 |
where all signs are optional. The returned value (whether a list reference |
| 410 |
from the split function, or a string from the join function) will have |
| 411 |
all fields normalized unless C<no_norm> is true. |
| 412 |
|
| 413 |
=item B<$type = 'delta'> |
| 414 |
|
| 415 |
$delta = $dmb->split("delta",$string,\%opts); |
| 416 |
$string = $dmb->join("delta",$delta,\%opts); |
| 417 |
|
| 418 |
Options recognized are: |
| 419 |
|
| 420 |
mode : standard/business |
| 421 |
nonorm : 0/1 |
| 422 |
type : exact/semi/approx/estimated |
| 423 |
|
| 424 |
A second format is also supported, but is deprecated and will be removed in |
| 425 |
Date::Manip 7.0. |
| 426 |
|
| 427 |
$delta = $dmb->split("business",$string,\%opts); |
| 428 |
$string = $dmb->join("business",$delta,\%opts); |
| 429 |
|
| 430 |
These are equivalent to using 'delta' with an option of C<'mode' => 'business'>. |
| 431 |
|
| 432 |
These split a string containing a delta, or create a string |
| 433 |
containing one using the rules described in the L<Date::Manip::Delta> |
| 434 |
documentation. |
| 435 |
|
| 436 |
The string that can be split is of the form: |
| 437 |
|
| 438 |
Y:M:W:D:H:MN:S |
| 439 |
|
| 440 |
Any field may have a sign, but they are optional. |
| 441 |
|
| 442 |
Fields may be omitted entirely. For example: |
| 443 |
|
| 444 |
D:H:MN:S |
| 445 |
D:::S |
| 446 |
|
| 447 |
are both valid. |
| 448 |
|
| 449 |
The string or list output is normalized unless the B<nonorm> option is passed in. |
| 450 |
|
| 451 |
The type of the delta (which determines how it will be normalized) will be |
| 452 |
automatically determined if not specified. The type will default to the |
| 453 |
value given in the table below based on the FIRST condition that is true. |
| 454 |
|
| 455 |
default_type condition |
| 456 |
|
| 457 |
estimated any field is a non-integer |
| 458 |
approx any of the approximate fields are non-zero |
| 459 |
semi any of the semi-exact fields are non-zero |
| 460 |
exact only the exact fields are non-zero |
| 461 |
|
| 462 |
=back |
| 463 |
|
| 464 |
=item B<week1_day1> |
| 465 |
|
| 466 |
$ymd = $dmb->week1_day1($y); |
| 467 |
|
| 468 |
This returns the date of the 1st day of the 1st week in the given year. |
| 469 |
|
| 470 |
Note that this uses the config variable Week1ofYear (and possibly |
| 471 |
FirstDay) to define when the first week begins, so refer to them for |
| 472 |
more information. Also note that it depending on the value of these two |
| 473 |
variables, the date of the 1st day of the 1st week might actually be |
| 474 |
in the previous year. |
| 475 |
|
| 476 |
=item B<weeks_in_year> |
| 477 |
|
| 478 |
$w = $dmb->weeks_in_year($y); |
| 479 |
|
| 480 |
This returns the number of ISO 8601 weeks in the year. It will always be |
| 481 |
52 or 53. |
| 482 |
|
| 483 |
=item B<week_of_year> |
| 484 |
|
| 485 |
($y,$w) = $dmb->week_of_year($date); |
| 486 |
($y,$w) = $dmb->week_of_year($ymd); |
| 487 |
|
| 488 |
This returns the week number (1-53) of the given date and the year |
| 489 |
that it falls in. Since the ISO 8601 definition of a week is used, the |
| 490 |
year returned is not necessarily the one passed in (it may differ for |
| 491 |
the first or last week of the year). |
| 492 |
|
| 493 |
The inverse operation is also available: |
| 494 |
|
| 495 |
$ymd = $dmb->week_of_year($y,$w); |
| 496 |
|
| 497 |
which returns the first day of the given week. |
| 498 |
|
| 499 |
Note that this uses the config variable Week1ofYear (and possibly |
| 500 |
FirstDay) to define when the first week begins, so refer to them for |
| 501 |
more information. |
| 502 |
|
| 503 |
=back |
| 504 |
|
| 505 |
=head1 KNOWN BUGS |
| 506 |
|
| 507 |
None known. |
| 508 |
|
| 509 |
=head1 BUGS AND QUESTIONS |
| 510 |
|
| 511 |
Please refer to the L<Date::Manip::Problems> documentation for |
| 512 |
information on submitting bug reports or questions to the author. |
| 513 |
|
| 514 |
=head1 SEE ALSO |
| 515 |
|
| 516 |
L<Date::Manip> - main module documentation |
| 517 |
|
| 518 |
=head1 LICENSE |
| 519 |
|
| 520 |
This script is free software; you can redistribute it and/or |
| 521 |
modify it under the same terms as Perl itself. |
| 522 |
|
| 523 |
=head1 AUTHOR |
| 524 |
|
| 525 |
Sullivan Beck (https://github.com/SBECK-github/) |
| 526 |
|
| 527 |
=cut |