| 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::Calc - describes date calculations |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
Two objects (both of which are either L<Date::Manip::Date> or |
| 14 |
L<Date::Manip::Delta> objects) may be used to creates a third object |
| 15 |
based on those two. |
| 16 |
|
| 17 |
$delta = $date->calc($date2 [,$subtract] [,$mode]); |
| 18 |
|
| 19 |
$date2 = $date->calc($delta [,$subtract]); |
| 20 |
$date2 = $delta->calc($date1 [,$subtract]); |
| 21 |
|
| 22 |
$delta3 = $delta1->calc($delta2 [,$subtract] [,$no_normalize]); |
| 23 |
|
| 24 |
=head1 DESCRIPTION |
| 25 |
|
| 26 |
This document describes the different types of calculations that can be |
| 27 |
done using dates and deltas. Date calculations are much more complicated |
| 28 |
than they initially appear, so this document is fairly large. |
| 29 |
|
| 30 |
The complication in date calculations is due to the fact that it is |
| 31 |
impossible to express some parts of a delta as an exact length. Some |
| 32 |
examples will illustrate this: |
| 33 |
|
| 34 |
As an example, let's take two dates and determine how much time elapsed |
| 35 |
between them: |
| 36 |
|
| 37 |
Nov 3 2016 11:00:00 |
| 38 |
Dec 5 2016 12:00:00 |
| 39 |
|
| 40 |
Elapsed time: 770 hours |
| 41 |
|
| 42 |
There are several ways to describe the time that elapsed. The first way |
| 43 |
is to give the difference exactly. This is the exact delta. |
| 44 |
|
| 45 |
An exact delta is always described in terms of hours, minutes, and seconds. |
| 46 |
|
| 47 |
The problem with this is that we don't think in terms of exact deltas. |
| 48 |
We think in terms which cannot be expressed exactly. |
| 49 |
|
| 50 |
For example, most people would look at those two dates and think: |
| 51 |
|
| 52 |
Perceived: 1 month, 2 days, 1 hour |
| 53 |
|
| 54 |
But the two dates: |
| 55 |
|
| 56 |
Feb 3 2016 11:00:00 |
| 57 |
Mar 5 2016 12:00:00 |
| 58 |
|
| 59 |
Elapsed time: 745 hours |
| 60 |
Perceived: 1 month, 2 days, 1 hour |
| 61 |
|
| 62 |
Some fields in a delta do not have an exact length. A year is usually |
| 63 |
365 days long, but sometimes it is 366. A month might be 28, 29, 30, |
| 64 |
or 31 days long. |
| 65 |
|
| 66 |
Perhaps the most unexpected difficulty is that days are not of |
| 67 |
constant length. Most people would define a day as 24 hours, but when |
| 68 |
you take daylight saving time into account that definition produces |
| 69 |
unexpected results. The following calculation illustrates this: |
| 70 |
|
| 71 |
Nov 5, 2011 02:30 EDT |
| 72 |
+ 24 hour |
| 73 |
|
| 74 |
Result: Nov 6, 2011 01:30 EST |
| 75 |
|
| 76 |
This immediately causes most people to redefine a day as the amount of time |
| 77 |
between the same wall clock time. For example, the amount of time between noon |
| 78 |
one day and noon the next (regardless of daylight saving time changes). |
| 79 |
|
| 80 |
This definition doesn't work either. For example: |
| 81 |
|
| 82 |
Mar 12, 2011 02:30 EST |
| 83 |
+ 1 day (same time next day) |
| 84 |
|
| 85 |
Result: Mar 13 02:30 EST |
| 86 |
|
| 87 |
But that date does not exist! Neither does: |
| 88 |
|
| 89 |
Result: Mar 13 02:30 EDT |
| 90 |
|
| 91 |
An alternate calculation could be: |
| 92 |
|
| 93 |
Nov 5, 2011 01:30 EDT |
| 94 |
+ 1 day (same time next day) |
| 95 |
|
| 96 |
Result: Nov 6, 01:30 EDT |
| 97 |
Result: Nov 6, 01:30 EST |
| 98 |
|
| 99 |
Both of those results exist. Which result did you mean? The first |
| 100 |
one is probably correct (since it is 24 hours later), but an hour |
| 101 |
later, you will have the same clock time again. |
| 102 |
|
| 103 |
So, the same time next day definition doesn't work at all for some |
| 104 |
dates (during a 'spring forward' type daylight saving time transition) |
| 105 |
and is ambiguous for others (during a 'fall back' type daylight saving |
| 106 |
time transition). |
| 107 |
|
| 108 |
Calculations involving exact deltas are unambiguous in all cases. |
| 109 |
|
| 110 |
A second class of delta is called a semi-exact delta, and these add |
| 111 |
days (and weeks) to the delta, and treats days as a "same time next day" |
| 112 |
at all times except the two cases where the resulting date falls in the |
| 113 |
period where a daylight saving time transition is occurring. Then it |
| 114 |
falls back to the 24 hour definition. |
| 115 |
|
| 116 |
A final class of delta is an approximate delta which includes all of |
| 117 |
the fields (years and months). This allows Date::Manip to handle |
| 118 |
deltas in a way that is consistent with how most people perceive the |
| 119 |
elapsed time. It should be noted that there is some uncertaintly |
| 120 |
there as not everyone's definition of how a delta is perceived is the |
| 121 |
same, but in general, they should be closer to what most people think |
| 122 |
of. |
| 123 |
|
| 124 |
=head1 TYPES OF CALCULATIONS |
| 125 |
|
| 126 |
This document describes the different types of calculations. |
| 127 |
Calculations involve two types of Date::Manip objects: dates and |
| 128 |
deltas. These are described in the L<Date::Manip::Date> and |
| 129 |
L<Date::Manip::Delta> manuals respectively. |
| 130 |
|
| 131 |
Two objects (two dates, two deltas, or one of each) are used. In all |
| 132 |
cases, if a second object is not passed in, undef is returned. |
| 133 |
|
| 134 |
There are 3 types of calculations: |
| 135 |
|
| 136 |
=over 4 |
| 137 |
|
| 138 |
=item B<Date/Date calculations> |
| 139 |
|
| 140 |
A calculation involving 2 dates is used to determine the amount of time (the |
| 141 |
delta) between them. |
| 142 |
|
| 143 |
$delta = $date1->calc($date2 [,$subtract] [,$mode]); |
| 144 |
|
| 145 |
Two dates can be worked with and a delta will be produced which is |
| 146 |
the amount of time between the two dates. |
| 147 |
|
| 148 |
C<$date1> and C<$date2> are L<Date::Manip::Date> objects with valid dates. |
| 149 |
The L<Date::Manip::Delta> object returned is the amount of time between |
| 150 |
them. If C<$subtract> is not passed in (or is 0), the delta produced |
| 151 |
is: |
| 152 |
|
| 153 |
DELTA = DATE2 - DATE1 |
| 154 |
|
| 155 |
If C<$subtract> is non-zero, the delta produced is: |
| 156 |
|
| 157 |
DELTA = DATE1 - DATE2 |
| 158 |
|
| 159 |
The C<$subtract> argument has special importance when doing approximate |
| 160 |
calculations, and this is described below. |
| 161 |
|
| 162 |
If either date is invalid, a delta object will be returned which |
| 163 |
has an error associated with it. |
| 164 |
|
| 165 |
The C<$mode> argument describes the type of delta that is produced and |
| 166 |
is described below in L</"MODE">. |
| 167 |
|
| 168 |
=item B<Date/Delta calculations> |
| 169 |
|
| 170 |
Date/delta calculations can be performed using either a L<Date::Manip::Date> |
| 171 |
or L<Date::Manip::Delta> object as the primary object: |
| 172 |
|
| 173 |
$date2 = $date1->calc($delta [,$subtract]); |
| 174 |
$date2 = $delta->calc($date1 [,$subtract]); |
| 175 |
|
| 176 |
A date and delta can be combined to yield a date that is the |
| 177 |
given amount of time before or after it. |
| 178 |
|
| 179 |
C<$date1> and C<$delta> are L<Date::Manip::Date> and L<Date::Manip::Delta> |
| 180 |
objects respectively. A new L<Date::Manip::Date> object is produced. |
| 181 |
If either C<$date1> or C<$delta> are invalid, the new date object will have |
| 182 |
an error associated with it. |
| 183 |
|
| 184 |
Both of the calls above perform the same function and produce exactly the |
| 185 |
same results. |
| 186 |
|
| 187 |
If C<$subtract> is not passed in, or is 0, the resulting date is |
| 188 |
formed as: |
| 189 |
|
| 190 |
DATE2 = DATE1 + DELTA |
| 191 |
|
| 192 |
If C<$subtract> is non-zero, the resulting date is: |
| 193 |
|
| 194 |
DATE2 = DATE1 - DELTA |
| 195 |
|
| 196 |
The C<$subtract> argument has special importance when doing approximate |
| 197 |
calculations, and this is described below in L</"SUBTRACTION">. |
| 198 |
|
| 199 |
=item B<Delta/Delta calculations> |
| 200 |
|
| 201 |
Delta/delta calculations can be performed to add two amounts of time |
| 202 |
together, or subtract them. |
| 203 |
|
| 204 |
$delta3 = $delta1->calc($delta2 [,$subtract] [,$no_normalize]); |
| 205 |
|
| 206 |
If C<$subtract> is not passed in, or is 0, the resulting delta formed |
| 207 |
is: |
| 208 |
|
| 209 |
DELTA3 = DELTA1 + DELTA2 |
| 210 |
|
| 211 |
If C<$subtract> is non-zero, then the resulting delta is: |
| 212 |
|
| 213 |
DELTA3 = DELTA1 - DELTA2 |
| 214 |
|
| 215 |
C<$delta1> and C<$delta2> are valid L<Date::Manip::Delta> objects, and a new |
| 216 |
L<Date::Manip::Delta> object is produced. |
| 217 |
|
| 218 |
C<$no_normalize> can be the string 'nonormalize' or a non-zero value (in |
| 219 |
which case $subtract MUST be entered, even if it is 0). |
| 220 |
|
| 221 |
=back |
| 222 |
|
| 223 |
=head1 MODE |
| 224 |
|
| 225 |
Date::Manip calculations can be divided into two different categories: |
| 226 |
business and non-business; and within those are three sub-categories: |
| 227 |
exact, semi-exact, and approximate. |
| 228 |
|
| 229 |
=over 4 |
| 230 |
|
| 231 |
=item B<Business and non-business calculations> |
| 232 |
|
| 233 |
A business calculation is one where the length of the day is |
| 234 |
determined by the length of the work day, and only business days |
| 235 |
(i.e. days in which business is conducted) count. Holidays and |
| 236 |
weekends are omitted (though there is some flexibility in defining |
| 237 |
what exactly constitutes the work week as described in the |
| 238 |
L<Date::Manip::Config> manual). This is described in more detail below |
| 239 |
in L</"BUSINESS MODE CONSIDERATIONS">. |
| 240 |
|
| 241 |
A non-business mode calculation is the normal type of calculation |
| 242 |
where no days are ignored, and all days are full length. |
| 243 |
|
| 244 |
=item B<Exact, semi-exact, and approximate calculations> |
| 245 |
|
| 246 |
An exact calculation is one in which the delta used (or produced) is |
| 247 |
an exact delta. An exact delta is described in more detail in the |
| 248 |
L<Date::Manip::Delta> manual, but the short explanation is that it is |
| 249 |
a delta which only involves fields of an exactly known length (hours, |
| 250 |
minutes, and seconds). Business deltas also include days in the exact |
| 251 |
part. The value of all other fields in the delta will be zero. |
| 252 |
|
| 253 |
A semi-exact calculation is one in which the deltas used (or produced) |
| 254 |
is a semi-exact delta. This is also described in the |
| 255 |
L<Date::Manip::Delta> manual, but the short explanation is that it |
| 256 |
includes days and weeks (for standard calculations) or weeks (for |
| 257 |
business calculations) in addition to the exact fields. A semi-exact |
| 258 |
day is defined as the same clock time on two successive days. So noon |
| 259 |
to noon is 1 day (even though it may not be exactly 24 hours due to a |
| 260 |
daylight saving time transition). A week is defined as 7 days. This |
| 261 |
is described in more detail below. |
| 262 |
|
| 263 |
An approximate calculation is one in which the deltas used (or |
| 264 |
produced) are approximate, and may include any of the fields. |
| 265 |
|
| 266 |
=back |
| 267 |
|
| 268 |
In date-delta and delta-delta calculations, the mode of the |
| 269 |
calculation will be determined automatically by the delta. In the case |
| 270 |
of date-date calculations, the mode is supplied as an argument. |
| 271 |
|
| 272 |
=over 4 |
| 273 |
|
| 274 |
=item B<Mode in date-date calculations> |
| 275 |
|
| 276 |
When doing a date-date calculation, the following call is used: |
| 277 |
|
| 278 |
$delta = $date1->calc($date2 [,$subtract] [,$mode]); |
| 279 |
|
| 280 |
C<$mode> defaults to "exact". The delta produced will be be either a |
| 281 |
business or non-business delta; exact, semi-exact, or approximate, as |
| 282 |
specified by C<$mode>. |
| 283 |
|
| 284 |
Currently, the possible values that $mode can have are: |
| 285 |
|
| 286 |
exact : an exact, non-business calculation |
| 287 |
semi : a semi-exact, non-business calculation |
| 288 |
approx : an approximate, non-business calculation |
| 289 |
|
| 290 |
business : an exact, business calculation |
| 291 |
bsemi : a semi-exact, business calculation |
| 292 |
bapprox : an approximate, business calculation |
| 293 |
|
| 294 |
=item B<Mode in date-delta calculations> |
| 295 |
|
| 296 |
When doing calculations of a date and a delta: |
| 297 |
|
| 298 |
$date2 = $date1->calc($delta [,$subtract]); |
| 299 |
$date2 = $delta->calc($date1 [,$subtract]); |
| 300 |
|
| 301 |
the mode is not passed in. It is determined exclusively by the |
| 302 |
delta. If C<$delta> is a business delta, A business calculation is |
| 303 |
done. If C<$delta> is a non-business delta, a non-business calculation |
| 304 |
will be done. |
| 305 |
|
| 306 |
The C<$delta> will also be classified as exact, semi-exact, or approximate |
| 307 |
based on which fields are non-zero. |
| 308 |
|
| 309 |
=item B<Mode in delta-delta calculations> |
| 310 |
|
| 311 |
When doing calculations with two deltas: |
| 312 |
|
| 313 |
$delta3 = $delta1->calc($delta2 [,$subtract]); |
| 314 |
|
| 315 |
the mode is not passed in. It is determined by the two deltas. |
| 316 |
|
| 317 |
If both deltas are business mode, or both are non-business mode, a |
| 318 |
new delta will be produced of the same type. |
| 319 |
|
| 320 |
It one of the deltas is a business mode and the other is not, the |
| 321 |
resulting delta will have an error condition since there is no |
| 322 |
direct correlation between the two types of deltas. Even though |
| 323 |
it would be easy to add the two together, it would be impossible |
| 324 |
to come up with a result that is meaningful. |
| 325 |
|
| 326 |
If both deltas are exact, semi-exact, or approximate, the resulting |
| 327 |
delta is the same. If one delta is approximate and one is not, then |
| 328 |
the resulting delta is approximate. It is NOT treated as an error. |
| 329 |
Likewise, if one is semi-exact and the other exact, a semi-exact delta |
| 330 |
is produced. |
| 331 |
|
| 332 |
=back |
| 333 |
|
| 334 |
=head1 TIMEZONE CONSIDERATIONS |
| 335 |
|
| 336 |
=over 4 |
| 337 |
|
| 338 |
=item B<date-date calculations> |
| 339 |
|
| 340 |
When doing a business calculation, both dates must be in the same time |
| 341 |
zone or an error is produced. |
| 342 |
|
| 343 |
For non-business calculations, when calculating the difference between |
| 344 |
two dates in different time zones, C<$date2> will be converted to the |
| 345 |
same timezone as C<$date1> and the returned date will be in this |
| 346 |
timezone. |
| 347 |
|
| 348 |
=item B<date-delta calculations> |
| 349 |
|
| 350 |
When adding a delta to a date, the resulting date will be in the same |
| 351 |
time zone as the original date. |
| 352 |
|
| 353 |
=item B<delta-delta calculations> |
| 354 |
|
| 355 |
No timezone information applies. |
| 356 |
|
| 357 |
=back |
| 358 |
|
| 359 |
It should also be noted that daylight saving time considerations are |
| 360 |
currently ignored when doing business calculations. In common usage, |
| 361 |
daylight saving time changes occurs outside of the business day, so |
| 362 |
the business day length is constant. As a result, daylight saving |
| 363 |
time is ignored. |
| 364 |
|
| 365 |
=head1 BUSINESS MODE CONSIDERATIONS |
| 366 |
|
| 367 |
In order to correctly do business mode calculations, a config file |
| 368 |
should exist which contains the section defining holidays (otherwise, |
| 369 |
weekends will be ignored, but all other days will be counted as |
| 370 |
business days). This is documented below, and in the |
| 371 |
L<Date::Manip::Config> section of the documentation. Some config |
| 372 |
variables (namely WorkWeekBeg, WorkWeekEnd, WorkDayBeg, WorkDayEnd, |
| 373 |
and WorkDay24Hr) defined the length of the work week and work day. |
| 374 |
|
| 375 |
If the workday is defined as 08:00 to 18:00, a work week consisting of |
| 376 |
Mon-Sat, and the standard (American) holidays, then from Tuesday at |
| 377 |
12:00 to the following Monday at 14:00 is 5 days and 2 hours. If the |
| 378 |
"end" of the day is reached in a calculation, it automatically |
| 379 |
switches to the next day. So, Tuesday at 12:00 plus 6 hours is |
| 380 |
Wednesday at 08:00 (provided Wed is not a holiday). Also, a date that |
| 381 |
is not during a workday automatically becomes the start of the next |
| 382 |
workday. So, Sunday 12:00 and Monday at 03:00 both automatically |
| 383 |
becomes Monday at 08:00 (provided Monday is not a holiday). |
| 384 |
|
| 385 |
Note that a business week is treated the same as an exact week |
| 386 |
(i.e. from Tuesday to Tuesday, regardless of holidays). Because this |
| 387 |
means that the relationship between days and weeks is NOT unambiguous, |
| 388 |
when a semi-exact delta is produced from two dates, it will be in |
| 389 |
terms of d/h/mn/s (i.e. no week field). |
| 390 |
|
| 391 |
Anyone using business mode is going to notice a few quirks about it |
| 392 |
which should be explained. When I designed business mode, I had in |
| 393 |
mind what a business which promises 1 business day turnaround really |
| 394 |
means. |
| 395 |
|
| 396 |
If you do a business calculation (with the workday set to 9:00-17:00), |
| 397 |
you will get the following: |
| 398 |
|
| 399 |
Saturday at noon + 1 business day = Tuesday at 9:00 |
| 400 |
Saturday at noon - 1 business day = Friday at 9:00 |
| 401 |
|
| 402 |
What does this mean? |
| 403 |
|
| 404 |
As an example, say I use a business that works 9-5 and they have a |
| 405 |
drop box so I can drop things off over the weekend and they promise 1 |
| 406 |
business day turnaround. If I drop something off Friday night, |
| 407 |
Saturday, or Sunday, it doesn't matter. They're going to get started |
| 408 |
on it Monday morning. It'll be 1 business day to finish the job, so |
| 409 |
the earliest I can expect it to be done is around 17:00 Monday or 9:00 |
| 410 |
Tuesday morning. Unfortunately, there is some ambiguity as to what |
| 411 |
day 17:00 really falls on, similar to the ambiguity that occurs when |
| 412 |
you ask what day midnight falls on. Although it's not the only |
| 413 |
answer, Date::Manip treats midnight as the beginning of a day rather |
| 414 |
than the end of one. In the same way, 17:00 is equivalent to 9:00 the |
| 415 |
next day and any time the date calculations encounter 17:00, it |
| 416 |
automatically switch to 9:00 the next day. Although this introduces |
| 417 |
some quirks, I think this is justified. I also think that it is the |
| 418 |
way most people think of it. If I drop something off first thing |
| 419 |
Monday morning, I would expect to pick it up first thing Tuesday if |
| 420 |
there is 1 business day turnaround. |
| 421 |
|
| 422 |
Equivalently, if I want a job to be finished on Saturday (despite the fact |
| 423 |
that I cannot pick it up since the business is closed), I have to drop it |
| 424 |
off no later than Friday at 9:00. That gives them a full business day to |
| 425 |
finish it off. Of course, I could just as easily drop it off at 17:00 |
| 426 |
Thursday, or any time between then and 9:00 Friday. Again, it's a matter |
| 427 |
of treating 17:00 as ambiguous. |
| 428 |
|
| 429 |
So Saturday + 1 business day = Tuesday at 9:00 (which means anything |
| 430 |
from Monday 17:00 to Tuesday 9:00), but Monday at 9:01 + 1 business |
| 431 |
day = Tuesday at 9:01 which is unambiguous. |
| 432 |
|
| 433 |
It should be noted that when adding years, months, and weeks, |
| 434 |
the business day is ignored. Once they've been added, the resulting |
| 435 |
date is forced to be a business time (i.e. it moves to the start of |
| 436 |
the next business day if it wasn't one already) before proceeding with |
| 437 |
the days, hours, minutes, and seconds part. |
| 438 |
|
| 439 |
=head1 EXACT, SEMI-EXACT, AND APPROXIMATE DATE/DELTA CALCULATIONS |
| 440 |
|
| 441 |
This section contains more details about exactly how exact, semi-exact, |
| 442 |
and approximate calculations are performed for date/delta calculations. |
| 443 |
|
| 444 |
All calculations make use of some exact quantities, including: |
| 445 |
|
| 446 |
1 year = 12 months |
| 447 |
1 week = 7 days |
| 448 |
1 hour = 60 minutes |
| 449 |
1 minute = 60 seconds |
| 450 |
|
| 451 |
This leaves two relationships which are not exact: |
| 452 |
|
| 453 |
1 month = ? days |
| 454 |
1 day = ? hours |
| 455 |
|
| 456 |
For non-business calculations, a day is usually 24 hours long. Due to |
| 457 |
daylight saving time transitions which might make a day be 23 or 25 hours |
| 458 |
long (or in some cases, some other length), the relation is not exact. |
| 459 |
Whenever possible, a day is actually measured as the same time on |
| 460 |
two days (i.e. Tuesday at noon to Wednesday at noon) even if that |
| 461 |
period is not precisely 24 hours. For business calculations, a days |
| 462 |
length is determined by the length of the work day and is known |
| 463 |
exactly. |
| 464 |
|
| 465 |
Exact calculations involve ONLY quantities of time with a known length, |
| 466 |
so there is no ambiguity in them. |
| 467 |
|
| 468 |
Approximate and semi-exact calculations involve variable length fields, |
| 469 |
and so they must be treated specially. |
| 470 |
|
| 471 |
In order to do an approximate or semi-exact calculation, the delta is |
| 472 |
added to a date in pieces, where the fields in each piece have an |
| 473 |
exact and known relationship. |
| 474 |
|
| 475 |
For a non-business calculation, a calculation occurs in the following |
| 476 |
steps: |
| 477 |
|
| 478 |
year/month fields added |
| 479 |
week/day fields added |
| 480 |
hour/minute/second fields added |
| 481 |
|
| 482 |
For a business calculation, the steps are: |
| 483 |
|
| 484 |
year/month fields added |
| 485 |
week field added |
| 486 |
day field added |
| 487 |
hour/minute/second fields added |
| 488 |
|
| 489 |
After each step, a valid date must be present, or it will be adjusted |
| 490 |
before proceeding to the next step. Note however that for business |
| 491 |
calculations, the first step must produce a valid date, but not |
| 492 |
necessarily a business date. The second step will produce a valid |
| 493 |
business date. |
| 494 |
|
| 495 |
A series of examples will illustrate this. |
| 496 |
|
| 497 |
=over 4 |
| 498 |
|
| 499 |
=item B<A date and non-business approximate delta> |
| 500 |
|
| 501 |
date = Mar 31 2001 at 12:00:00 |
| 502 |
delta = 1 year, 1 month, 1 day, 1 hour |
| 503 |
|
| 504 |
First, the year/month fields are added without modifying any other field. |
| 505 |
This would produce: |
| 506 |
|
| 507 |
Apr 31, 2002 at 12:00 |
| 508 |
|
| 509 |
which is not valid. Any time the year/month fields produce a day past |
| 510 |
the end of the month, the result is 'truncated' to the last day of the |
| 511 |
month, so this produces: |
| 512 |
|
| 513 |
Apr 30, 2002 at 12:00 |
| 514 |
|
| 515 |
Next the week/day fields are added producing: |
| 516 |
|
| 517 |
May 1, 2002 at 12:00 |
| 518 |
|
| 519 |
and finally, the exact fields (hour/minute/second) are added to produce: |
| 520 |
|
| 521 |
May 1, 2002 at 13:00 |
| 522 |
|
| 523 |
=item B<A simple business calculation> |
| 524 |
|
| 525 |
Assuming a normal Monday-Friday work week from 8:00 - 17:00: |
| 526 |
|
| 527 |
date = Wed, Nov 23, 2011 at 12:00 |
| 528 |
delta = 1 week, 1 day, 1 hour |
| 529 |
|
| 530 |
First, the week field is added: |
| 531 |
|
| 532 |
Wed, Nov 30, 2011 at 12:00 |
| 533 |
|
| 534 |
Then the day field is added: |
| 535 |
|
| 536 |
Thu, Dec 1, 2011 at 12:00 |
| 537 |
|
| 538 |
Then the exact fields are added: |
| 539 |
|
| 540 |
Thu, Dec 1, 2011 at 13:00 |
| 541 |
|
| 542 |
=item B<A business example where a holiday impacts it> |
| 543 |
|
| 544 |
In America, Jul 4 is a holiday, so Mon, Jul 4, 2011 is not a work day. |
| 545 |
|
| 546 |
date = Mon, Jun 27, 2011 at 12:00 |
| 547 |
delta = 1 week, 1 day, 1 hour |
| 548 |
|
| 549 |
First, the week field is added: |
| 550 |
|
| 551 |
Mon, Jul 4, 2011 at 12:00 |
| 552 |
|
| 553 |
Since that is not a work day, it immediately becomes: |
| 554 |
|
| 555 |
Tue, Jul 5, 2011 at 8:00 |
| 556 |
|
| 557 |
Then the day field is added: |
| 558 |
|
| 559 |
Wed, Jul 6, 2011 at 8:00 |
| 560 |
|
| 561 |
and finally the remaining fields: |
| 562 |
|
| 563 |
Wed, Jul 6, 2011 at 9:00 |
| 564 |
|
| 565 |
=item B<Calculation where daylight savings time impacts it (fall example)> |
| 566 |
|
| 567 |
In the America/New_York timezone (Eastern time), on November 6, 2011, |
| 568 |
the following time change occurred: |
| 569 |
|
| 570 |
2011-11-06 02:00 EDT => 2011-11-06 01:00 EST |
| 571 |
|
| 572 |
Three simple calculations illustrate how this is handled: |
| 573 |
|
| 574 |
date = 2011-11-05 02:30 EDT |
| 575 |
delta = 1 day |
| 576 |
|
| 577 |
Adding the day produces: |
| 578 |
|
| 579 |
2011-11-06 02:30 EDT |
| 580 |
|
| 581 |
which is valid, so that is the result. |
| 582 |
|
| 583 |
Similarly: |
| 584 |
|
| 585 |
date = 2011-11-07 02:30 EST |
| 586 |
delta = -1 day |
| 587 |
|
| 588 |
produces: |
| 589 |
|
| 590 |
2011-11-06 02:30 EST |
| 591 |
|
| 592 |
which is valid. |
| 593 |
|
| 594 |
Finally: |
| 595 |
|
| 596 |
date = 2011-11-05 02:30 EDT |
| 597 |
delta = 2 days |
| 598 |
|
| 599 |
produces: |
| 600 |
|
| 601 |
2011-11-07 02:30 EST |
| 602 |
|
| 603 |
The calculation will preserve the savings time where possible so the |
| 604 |
resulting day will have the same offset from UTC. If that is not |
| 605 |
possible, but the resulting day is valid in the other offset, that |
| 606 |
will be used instead. |
| 607 |
|
| 608 |
=item B<Calculation where daylight savings time impacts it (spring example)> |
| 609 |
|
| 610 |
In the America/New_York timezone (Eastern time), on March 13, |
| 611 |
the following time change occurred: |
| 612 |
|
| 613 |
2011-03-13 02:00 EST => 2011-03-13 03:00 EDT |
| 614 |
|
| 615 |
In this case, a calculation may produce an invalid date. |
| 616 |
|
| 617 |
date = 2011-03-12 02:30 EST |
| 618 |
delta = 1 day |
| 619 |
|
| 620 |
produces: |
| 621 |
|
| 622 |
2011-03-13 02:30 EST |
| 623 |
|
| 624 |
This is not valid. Neither is: |
| 625 |
|
| 626 |
2011-03-13 02:30 EDT |
| 627 |
|
| 628 |
In this case, the calculation will be redone converting days to 24-hour |
| 629 |
periods, so the calculation becomes: |
| 630 |
|
| 631 |
date = 2011-03-12 02:30 EST |
| 632 |
delta = 24 hours |
| 633 |
|
| 634 |
which will produce a valid date: |
| 635 |
|
| 636 |
2011-03-13 03:30 EDT |
| 637 |
|
| 638 |
=back |
| 639 |
|
| 640 |
=head1 EXACT, SEMI-EXACT, AND APPROXIMATE DATE/DATE CALCULATIONS |
| 641 |
|
| 642 |
This section contains more details about exactly how exact, semi-exact, |
| 643 |
and approximate calculations are performed for date/date calculations. |
| 644 |
|
| 645 |
When calculating the delta between two dates, the delta may take |
| 646 |
different forms depending on the mode passed in. An exact calculation |
| 647 |
will produce a delta which included only exact fields. A semi-exact calculation |
| 648 |
may produce a semi-exact delta, and an approximate calculation may produce |
| 649 |
an approximate delta. Note that if the two dates are close enough together, |
| 650 |
an exact delta will be produced (even if the mode is semi-exact or approximate), |
| 651 |
or it may produce a semi-exact delta in approximate mode. |
| 652 |
|
| 653 |
For example, the two dates "Mar 12 1995 12:00" and "Apr 13 1995 12:00" |
| 654 |
would have an exact delta of "744 hours", and a semi-exact delta of |
| 655 |
"31 days". It would have an approximate delta of "1 month 1 day". |
| 656 |
|
| 657 |
Two dates, "Mar 31 12:00" and "Apr 30 12:00" would have deltas "720 |
| 658 |
hours" (exact), "30 days" (semi-exact) or "1 month" (approximate). |
| 659 |
|
| 660 |
Approximate mode is a more human way of looking at things (you'd say 1 |
| 661 |
month and 2 days more often then 33 days), but it is less meaningful |
| 662 |
in terms of absolute time. |
| 663 |
|
| 664 |
One thing to remember is that an exact delta is exactly the amount of |
| 665 |
time that has passed, including all effects of daylight saving |
| 666 |
time. Semi-exact and approximate deltas usually ignore the affects of |
| 667 |
daylight saving time. |
| 668 |
|
| 669 |
=head1 SUBTRACTION |
| 670 |
|
| 671 |
In exact and semi-exact calculations, and in delta-delta calculations, |
| 672 |
the the C<$subtract> argument is easy to understand. When working |
| 673 |
with an approximate delta however (either when adding an approximate |
| 674 |
delta to a date, or when taking two dates to get an approximate |
| 675 |
delta), there is a degree of uncertainty in how the calculation is |
| 676 |
done, and the $subtract argument is used to specify exactly how the |
| 677 |
approximate delta is to be use. An example illustrates this quite |
| 678 |
well. |
| 679 |
|
| 680 |
If you take the date Jan 4, 2000 and subtract a delta of "1 month 1 |
| 681 |
week" from it, you end up with Nov 27, 1999 (Jan 4, 2000 minus 1 month |
| 682 |
is Dec 4, 1999; minus 1 week is Nov 27, 1999). But Nov 27, 1999 plus a |
| 683 |
delta of "1 month 1 week" is Jan 3, 2000 (Nov 27, 1999 plus 1 month is |
| 684 |
Dec 27, 1999; plus 1 week is Jan 3, 2000). |
| 685 |
|
| 686 |
In other words the approximate delta (but NOT the exact or semi-exact |
| 687 |
delta) is different depending on whether you move from earlier date to |
| 688 |
the later date, or vice versa. And depending on what you are |
| 689 |
calculating, both are useful. |
| 690 |
|
| 691 |
In order to resolve this, the C<$subtract> argument can take on the values |
| 692 |
0, 1, or 2, and have different meanings. |
| 693 |
|
| 694 |
=over 4 |
| 695 |
|
| 696 |
=item B<$subtract in approximate date-date calculations> |
| 697 |
|
| 698 |
In the call: |
| 699 |
|
| 700 |
$delta = $date1->calc($date2,$subtract,"approx"); |
| 701 |
|
| 702 |
if C<$subtract> is 0, the resulting delta can be added to C<$date1> to get |
| 703 |
C<$date2>. Obviously C<$delta> may still be negative (if C<$date2> comes before |
| 704 |
C<$date1>). |
| 705 |
|
| 706 |
If C<$subtract> is 1, the resulting delta can be subtracted from C<$date1> |
| 707 |
to get C<$date2> (the deltas from these two are identical except for having |
| 708 |
an opposite sign). |
| 709 |
|
| 710 |
If C<$subtract> is 2, the resulting delta can be added to C<$date2> to get |
| 711 |
C<$date1>. In other words, the following are identical: |
| 712 |
|
| 713 |
$delta = $date1->calc($date2,2,"approx"); |
| 714 |
$delta = $date2->calc($date1,"approx"); |
| 715 |
|
| 716 |
=item B<$subtract in approximate date-delta calculations> |
| 717 |
|
| 718 |
In the call: |
| 719 |
|
| 720 |
$date2 = $date1->calc($delta,$subtract); |
| 721 |
|
| 722 |
If C<$subtract> is 0, the resulting date is determined by adding C<$delta> to |
| 723 |
C<$date1>. |
| 724 |
|
| 725 |
If C<$subtract> is 1, the resulting date is determined by subtracting C<$delta> |
| 726 |
from C<$date1>. |
| 727 |
|
| 728 |
If C<$subtract> is 2, the resulting date is the date which C<$delta> |
| 729 |
can be added to to get C<$date1>. |
| 730 |
|
| 731 |
For business mode calculations, C<$date1> will first be adjusted to be a valid |
| 732 |
work day (if it isn't already), so this may lead to non-intuitive results. |
| 733 |
|
| 734 |
In some cases, it is impossible to do a calculation with C<$subtract> = 2. |
| 735 |
As an example, if the date is "Dec 31" and the delta is "1 month", there |
| 736 |
is no date which you can add "1 month" to to get "Dec 31". When this occurs, |
| 737 |
the date returned has an error flag. |
| 738 |
|
| 739 |
=back |
| 740 |
|
| 741 |
=head1 APPROXIMATE DATE/DATE CALCULATION |
| 742 |
|
| 743 |
There are two different ways to look at the approximate delta between |
| 744 |
two dates. |
| 745 |
|
| 746 |
In Date::Manip 5.xx, the approximate delta between the two dates: |
| 747 |
|
| 748 |
Jan 10 1996 noon |
| 749 |
Jan 7 1998 noon |
| 750 |
|
| 751 |
was 1:11:4:0:0:0:0 (or 1 year, 11 months, 4 weeks). In calculating |
| 752 |
this, the first date was adjusted as far as it could go towards the |
| 753 |
second date without going past it with each unit starting with the |
| 754 |
years and ending with the seconds. |
| 755 |
|
| 756 |
This gave a strictly positive or negative delta, but it isn't |
| 757 |
actually how most people would think of the delta. |
| 758 |
|
| 759 |
As of Date::Manip 6.0, the delta is 2:0:0:-3:0:0:0 (or 2 years minus |
| 760 |
3 days). Although this leads to mixed-sign deltas, it is actually how |
| 761 |
more people would think about the delta. It has the additional |
| 762 |
advantage of being easier to calculate. |
| 763 |
|
| 764 |
For non-business mode calculations, the year/month part of the |
| 765 |
approximate delta will move a date from the year/month of the first |
| 766 |
date into the year/month of the second date. The remainder of the |
| 767 |
delta will adjust the days/hours/minutes/seconds as appropriate. |
| 768 |
|
| 769 |
For approximate business mode calculations, the year, date, and week |
| 770 |
parts will be done approximately, and the remainder will be done |
| 771 |
exactly. |
| 772 |
|
| 773 |
=head1 KNOWN BUGS |
| 774 |
|
| 775 |
None known. |
| 776 |
|
| 777 |
=head1 BUGS AND QUESTIONS |
| 778 |
|
| 779 |
Please refer to the L<Date::Manip::Problems> documentation for |
| 780 |
information on submitting bug reports or questions to the author. |
| 781 |
|
| 782 |
=head1 SEE ALSO |
| 783 |
|
| 784 |
L<Date::Manip> - main module documentation |
| 785 |
|
| 786 |
=head1 LICENSE |
| 787 |
|
| 788 |
This script is free software; you can redistribute it and/or |
| 789 |
modify it under the same terms as Perl itself. |
| 790 |
|
| 791 |
=head1 AUTHOR |
| 792 |
|
| 793 |
Sullivan Beck ([email protected]) |
| 794 |
|
| 795 |
=cut |