| 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::Delta - Methods for working with deltas |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
use Date::Manip::Delta; |
| 14 |
$date = new Date::Manip::Delta; |
| 15 |
|
| 16 |
=head1 DESCRIPTION |
| 17 |
|
| 18 |
This module contains functions useful in parsing and manipulating |
| 19 |
deltas. As used in this module, the term delta refers to an amount |
| 20 |
of time elapsed. It includes no information about a starting or |
| 21 |
ending time. |
| 22 |
|
| 23 |
There are several concepts involved in understanding the properties |
| 24 |
of a delta. |
| 25 |
|
| 26 |
=over 4 |
| 27 |
|
| 28 |
=item B<standard and business delta> |
| 29 |
|
| 30 |
There are two different modes for working with deltas: standard and |
| 31 |
business. The mode used depends on how you treat the calendar. |
| 32 |
|
| 33 |
Standard deltas use the full calendar without any modifications. |
| 34 |
|
| 35 |
A business delta uses a calendar in the way a business might. In a |
| 36 |
business calendar, anything outside of a business day is ignored. |
| 37 |
Typically, this includes holidays and weekends. In addition, the part |
| 38 |
of the day outside of business hours is also ignored, so a day may |
| 39 |
only run from 08:00 to 17:00 and everything outside of this is |
| 40 |
ignored. |
| 41 |
|
| 42 |
The length of a work day is usually not 24 hours. It is defined by |
| 43 |
the start and end of the work day and is set using the config |
| 44 |
variables: B<WorkDayBeg> and B<WorkDayEnd> (B<WorkDay24Hr> may be used to |
| 45 |
specify a 24-hour work day). The work week is defined using the |
| 46 |
config variables: B<WorkWeekBeg> and B<WorkWeekEnd>. |
| 47 |
|
| 48 |
Daylight saving time are ignored with business calculations because |
| 49 |
time changes occur at night (usually on the weekends) outside of |
| 50 |
business hours. This may yield unexpected results if the work day is |
| 51 |
defined to be 24-hours and the work week includes a day when a time |
| 52 |
change occurs. |
| 53 |
|
| 54 |
=item B<fields> |
| 55 |
|
| 56 |
A delta consists of 7 fields: years, months, weeks, days, hours, |
| 57 |
minutes, and seconds, usually expressed as a colon-separated string. |
| 58 |
For example: |
| 59 |
|
| 60 |
1:2:3:4:5:6:7 |
| 61 |
|
| 62 |
refers to an elapsed amount of time 1 year, 2 months, 3 weeks, 4 days, |
| 63 |
5 hours, 6 minutes, and 7 seconds long. |
| 64 |
|
| 65 |
=item B<normalized> |
| 66 |
|
| 67 |
A delta can be normalized or not. A normalized delta has values which |
| 68 |
have been simplified based on how a human would think of them. As an |
| 69 |
example, the delta: |
| 70 |
|
| 71 |
0:0:0:0:0:10:70 |
| 72 |
|
| 73 |
is not normalized since 70 seconds is typically thought of as 1 minute |
| 74 |
10 seconds. The normalized form of this delta would be: |
| 75 |
|
| 76 |
0:0:0:0:0:11:10 |
| 77 |
|
| 78 |
By default, deltas are converted to a normalized form in most |
| 79 |
functions that create/modify a delta, but this can be overridden. |
| 80 |
|
| 81 |
=item B<Types of deltas> |
| 82 |
|
| 83 |
There are 4 type of deltas that are available. |
| 84 |
|
| 85 |
=over 8 |
| 86 |
|
| 87 |
=item Exact deltas |
| 88 |
|
| 89 |
The most common type (and the default in most situations) is an exact |
| 90 |
delta. An exact delta is one where only fields which have exactly |
| 91 |
known lengths are allowed to be non-zero. |
| 92 |
|
| 93 |
For standard calculations, there are only three exactly known fields |
| 94 |
(hours, minutes, and seconds). The lengths are defined as: |
| 95 |
|
| 96 |
1 hour = 3600 seconds |
| 97 |
1 minute = 60 seconds |
| 98 |
|
| 99 |
Note that since a day is NOT always 24 hours (due to daylight saving time |
| 100 |
changes), a day is not an exactly known field. |
| 101 |
|
| 102 |
For business calculations, a day IS an exactly known field. Since |
| 103 |
business mode ignores daylight saving time, the length of the day can |
| 104 |
be calculated based on the config variables listed above. So, for |
| 105 |
example, if the work day is 08:00-17:00, the length of the day is 9 |
| 106 |
hours. The length of the week is still unknown since some work weeks |
| 107 |
may have fewer days than others due to holidays. |
| 108 |
|
| 109 |
All fields which are not exactly known will always have zero value. |
| 110 |
|
| 111 |
=item Semi-exact deltas |
| 112 |
|
| 113 |
A semi-exact delta treats the day/week fields as if they were exactly |
| 114 |
known. |
| 115 |
|
| 116 |
For standard calculations, this is done by using the relationships: |
| 117 |
|
| 118 |
1 day = 24 hours |
| 119 |
1 week = 7 days |
| 120 |
|
| 121 |
For business calculations, it is done by treating a week as a constant |
| 122 |
length (determined by the config variables listed above) ignoring |
| 123 |
holidays. So if a typical work week is Mon-Fri, the length of the |
| 124 |
week is 5 days. |
| 125 |
|
| 126 |
For semi-exact deltas, the value of the year/month must be zero. |
| 127 |
|
| 128 |
Although this may yield some values that are not exactly accurate |
| 129 |
around daylight saving time transitions, strictly speaking, they yield |
| 130 |
results that are useful in terms of how humans think of deltas. |
| 131 |
|
| 132 |
=item Approximate deltas |
| 133 |
|
| 134 |
An approximate delta can have non-zero values for all fields. When normalizing |
| 135 |
the fields, the year/month fields are treated as one set using the relationship |
| 136 |
|
| 137 |
1 year = 12 months |
| 138 |
|
| 139 |
The remaining fields are normalized using the semi-exact relationships. |
| 140 |
|
| 141 |
=item Estimated deltas |
| 142 |
|
| 143 |
The final type of delta are estimated deltas. These are deltas where |
| 144 |
an estimated length is applied to all the approximate fields. |
| 145 |
|
| 146 |
For standard deltas, the additional relationship: |
| 147 |
|
| 148 |
1 year = 365.2425 days |
| 149 |
|
| 150 |
is used. For business deltas, the additional relationship: |
| 151 |
|
| 152 |
1 year = X/7 * 365.2425 days |
| 153 |
|
| 154 |
(where X is the number of work days in a week) is used. |
| 155 |
|
| 156 |
Fractional seconds will be discarded (not rounded). |
| 157 |
|
| 158 |
=back |
| 159 |
|
| 160 |
NOTE: it is not possible to look at a delta and determine what type |
| 161 |
it is. For example, a standard delta with a non-zero day value might |
| 162 |
be approximate or semi-exact. The type will need to be explicitly |
| 163 |
selected, or determined by the context of the operation. |
| 164 |
|
| 165 |
=item B<signs> |
| 166 |
|
| 167 |
Each field has a sign associated with it. For example, the |
| 168 |
delta "1 year ago" is written as: |
| 169 |
|
| 170 |
-1:0:0:0:0:0:0 |
| 171 |
|
| 172 |
The sign of any field is optional, and if omitted, it is the |
| 173 |
same as the next higher field. So, the following are identical: |
| 174 |
|
| 175 |
+1:2:3:4:5:6:7 |
| 176 |
+1:+2:+3:+4:+5:+6:+7 |
| 177 |
|
| 178 |
In a normalized delta, all fields in a set will have the same |
| 179 |
sign. So the standard delta: |
| 180 |
|
| 181 |
0:0:+3:-2:0:0:0:0 (3 weeks -2 days) |
| 182 |
|
| 183 |
is not normalized. The normalized version would be: |
| 184 |
|
| 185 |
0:0:+2:5:0:0:0:0 (2 weeks, 5 days) |
| 186 |
|
| 187 |
Since an approximate delta has two sets (the y/m set and the w/d/h/mn/s set), |
| 188 |
these deltas may have two signs. So, the following is a |
| 189 |
fully normalized approximate delta: |
| 190 |
|
| 191 |
+1:0:-3:3:1:0:0 |
| 192 |
|
| 193 |
=item B<fractional values> |
| 194 |
|
| 195 |
Fractional fields are allowed such as: |
| 196 |
|
| 197 |
1.25 days |
| 198 |
1.1 years |
| 199 |
|
| 200 |
but whenever parsing a delta with fractional fields, the delta will be |
| 201 |
normalized using the estimated relationships described above. Fractional |
| 202 |
seconds will be discarded. |
| 203 |
|
| 204 |
=back |
| 205 |
|
| 206 |
=head1 METHODS |
| 207 |
|
| 208 |
=over 4 |
| 209 |
|
| 210 |
=item B<new> |
| 211 |
|
| 212 |
=item B<new_config> |
| 213 |
|
| 214 |
=item B<new_date> |
| 215 |
|
| 216 |
=item B<new_delta> |
| 217 |
|
| 218 |
=item B<new_recur> |
| 219 |
|
| 220 |
=item B<base> |
| 221 |
|
| 222 |
=item B<tz> |
| 223 |
|
| 224 |
=item B<is_date> |
| 225 |
|
| 226 |
=item B<is_delta> |
| 227 |
|
| 228 |
=item B<is_recur> |
| 229 |
|
| 230 |
=item B<config> |
| 231 |
|
| 232 |
=item B<err> |
| 233 |
|
| 234 |
Please refer to the L<Date::Manip::Obj> documentation for these methods. |
| 235 |
|
| 236 |
=item B<parse> |
| 237 |
|
| 238 |
$err = $delta->parse($string, \%opts); |
| 239 |
$err = $delta->parse($string [,$business] [,$no_normalize]); |
| 240 |
|
| 241 |
The second format is supported for backward compatibility, but is |
| 242 |
deprecated and will be removed in Date::Manip 7.00. The second form |
| 243 |
is equivalent to: |
| 244 |
|
| 245 |
$err = $delta->parse($string, { business => $business, |
| 246 |
nonorm => $no_normalize }); |
| 247 |
|
| 248 |
This takes a string and parses it to see if it is a valid delta. If it is, |
| 249 |
an error code of 0 is returned and $delta now contains the value of the |
| 250 |
delta. Otherwise, an error code of 1 is returned and an error condition |
| 251 |
is set in the delta. |
| 252 |
|
| 253 |
Recognized options are: |
| 254 |
|
| 255 |
mode : standard/business |
| 256 |
to specify if it is a business delta or a standard delta |
| 257 |
nonorm : 0/1 |
| 258 |
1 if the delta should not be normalized |
| 259 |
type : exact, semi, approx, estimated |
| 260 |
|
| 261 |
When specifying the type, the delta given must satisfy the requirements |
| 262 |
of the type (i.e. no year field for an exact delta). |
| 263 |
|
| 264 |
A delta string is usually specified in compact notation which consists |
| 265 |
of a colon separated list of numbers (with optional signs): |
| 266 |
|
| 267 |
Examples: |
| 268 |
0:0:0:0:4:3:-2 |
| 269 |
+4:3:-2 |
| 270 |
+4::3 |
| 271 |
|
| 272 |
In compact notation, from 1 to 7 of the fields may be given. For |
| 273 |
example D:H:MN:S may be given to specify only four of the fields. No |
| 274 |
spaces may be present in the string, but it is allowed to omit |
| 275 |
some of the fields. For example 5::3:30 is valid. In this case, |
| 276 |
missing fields default to the value 0. |
| 277 |
|
| 278 |
The delta string may also be specified using common field |
| 279 |
abbreviations. This is described below in the |
| 280 |
L<"ADDITIONAL DELTA NOTATIONS"> section. |
| 281 |
|
| 282 |
=item B<input> |
| 283 |
|
| 284 |
$str = $delta->input(); |
| 285 |
|
| 286 |
This returns the string that was parsed to form the delta. |
| 287 |
|
| 288 |
=item B<set> |
| 289 |
|
| 290 |
$err = $delta->set(\%opts); |
| 291 |
$err = $delta->set($field,$val [,$no_normalize]); |
| 292 |
|
| 293 |
The second format is supported for backward compatibility, but is |
| 294 |
deprecated and will be removed in Date::Manip 7.00. The second form |
| 295 |
is equivalent to: |
| 296 |
|
| 297 |
$err = $delta->set( $field => $val, 'nonorm' => $no_normalize ); |
| 298 |
|
| 299 |
This explicitly sets one or more parts of a delta. C<%opts> is a set of |
| 300 |
key/value pairs: |
| 301 |
|
| 302 |
$key $val |
| 303 |
|
| 304 |
delta [Y,M,W,D,H,MN,S] sets the entire delta |
| 305 |
business [Y,M,W,D,H,MN,S] sets the entire delta |
| 306 |
standard [Y,M,W,D,H,MN,S] sets the entire delta |
| 307 |
y YEAR sets one field |
| 308 |
M MONTH |
| 309 |
w WEEK |
| 310 |
d DAY |
| 311 |
h HOUR |
| 312 |
m MINUTE |
| 313 |
s SECOND |
| 314 |
|
| 315 |
nonorm 0/1 |
| 316 |
mode business, standard |
| 317 |
type exact, semi, estimated, approx |
| 318 |
|
| 319 |
An error is returned if an invalid data is passed in. |
| 320 |
|
| 321 |
C<%opts> can only include a single key that affects each field (i.e. you |
| 322 |
can set B<delta> or B<business> but not both, and you cannot set both |
| 323 |
B<delta> and B<y>, but you CAN set both B<y> and B<w>). |
| 324 |
|
| 325 |
When setting the entire delta with B<business> or B<standard>, it flags |
| 326 |
the delta as a business or standard mode delta respectively. In those |
| 327 |
cases, you are not allowed to set the B<mode> option. Partial deltas |
| 328 |
are allowed (i.e. [H,MN,S]) in which case zeros are added for all fields |
| 329 |
not specified. |
| 330 |
|
| 331 |
When setting the entire delta with B<delta>, the flag is left |
| 332 |
unchanged (unless the B<mode> option is also passed in). |
| 333 |
|
| 334 |
Also, when setting the entire delta, signs are not carried from |
| 335 |
one field to another, so [-1,2,...] is equivalent to [-1,+2,...]. |
| 336 |
|
| 337 |
By default, a delta is normalized, but setting the B<nonorm> key to |
| 338 |
a true value will not do that. |
| 339 |
|
| 340 |
For backwards compatibility, B<normal> can be used in place of B<standard>, |
| 341 |
both as C<$field> or as C<$val>. This is deprecated and will be removed in |
| 342 |
Date::Manip 7.00. |
| 343 |
|
| 344 |
When setting any field in the delta, the type of delta will be determined |
| 345 |
automatically as either B<exact> (if only fields that are exactly known |
| 346 |
are have non-zero fields), B<semi> (if only fields that are semi-exact or |
| 347 |
exact are included), or B<approx> otherwise. If the B<type> option is |
| 348 |
set, it will be used provided it is valid (i.e. you cannot set it to B<exact> |
| 349 |
if fields that are not exactly known are set). |
| 350 |
|
| 351 |
=item B<printf> |
| 352 |
|
| 353 |
$out = $delta->printf($in); |
| 354 |
@out = $delta->printf(@in); |
| 355 |
|
| 356 |
This takes a string or list of strings which may contain any number of |
| 357 |
special formatting directives. These directives are replaced with |
| 358 |
information contained in the delta. Everything else in the string is |
| 359 |
returned unmodified. |
| 360 |
|
| 361 |
A directive always begins with '%'. They are described in the section |
| 362 |
below in the section L</"PRINTF DIRECTIVES">. |
| 363 |
|
| 364 |
=item B<calc> |
| 365 |
|
| 366 |
Please refer to the Date::Manip::Calc documentation for details. |
| 367 |
|
| 368 |
=item B<type> |
| 369 |
|
| 370 |
$flag = $delta->type($op); |
| 371 |
|
| 372 |
This tests to see if a delta is of a certain type. $op can be; |
| 373 |
|
| 374 |
business : returns 1 if it is a business delta |
| 375 |
standard : returns 1 if it is a standard (non-business delta) |
| 376 |
|
| 377 |
exact : returns 1 if it is exact |
| 378 |
semi : returns 1 if it is semi-exact |
| 379 |
approx : returns 1 if it is approximate |
| 380 |
estimated : returns 1 if it is estimated |
| 381 |
|
| 382 |
=item B<value> |
| 383 |
|
| 384 |
$val = $delta->value(); |
| 385 |
@val = $delta->value(); |
| 386 |
|
| 387 |
This returns the value of the delta. In scalar context, it returns |
| 388 |
the printable string (equivalent to the printf directive '%Dt'). In |
| 389 |
list context, it returns a list of fields. |
| 390 |
|
| 391 |
An empty string/list is returned if there is no valid delta stored in $delta. |
| 392 |
|
| 393 |
=item B<convert> |
| 394 |
|
| 395 |
$delta->convert($to); |
| 396 |
|
| 397 |
This converts a delta from one type to another. $to can be 'exact', |
| 398 |
'semi', or 'approx'. The conversion uses the approximate and estimated |
| 399 |
relationships listed above to convert the delta. |
| 400 |
|
| 401 |
For example, if the exact non-business delta $delta contains: |
| 402 |
|
| 403 |
0:0:0:0:44:0:0 |
| 404 |
|
| 405 |
then the following call: |
| 406 |
|
| 407 |
$delta->convert('semi') |
| 408 |
|
| 409 |
would produce the semi-exact delta: |
| 410 |
|
| 411 |
0:0:0:1:20:0:0 |
| 412 |
|
| 413 |
The result will always be normalized. |
| 414 |
|
| 415 |
Converting from one type to another that is less exact (i.e. exact to semi-exact |
| 416 |
or semi-exact to approx) is supported. Converting the other direction is |
| 417 |
supported for backward compatibility, but will be removed in 7.00 because |
| 418 |
that operation is not one that is well defined. |
| 419 |
|
| 420 |
There is currently no support for converting business to non-business |
| 421 |
(or vice-versa). |
| 422 |
|
| 423 |
=item B<cmp> |
| 424 |
|
| 425 |
$flag = $delta1->cmp($delta2); |
| 426 |
|
| 427 |
This compares two deltas (using the approximate relationships listed |
| 428 |
above) and returns -1, 0, or 1 which could be used to sort them by length |
| 429 |
of time. |
| 430 |
|
| 431 |
Both deltas must be valid, and both must be either business or |
| 432 |
non-business deltas. They do not need to be the same out of exact, |
| 433 |
semi-exact, and approximate. |
| 434 |
|
| 435 |
undef will be returned if either delta is invalid, or you try to compare |
| 436 |
a business and non-business delta. |
| 437 |
|
| 438 |
=back |
| 439 |
|
| 440 |
=head1 ADDITIONAL DELTA NOTATIONS |
| 441 |
|
| 442 |
When parsing a delta, the string may be specified with the field spelled |
| 443 |
out, rather than using the colon separated fields. |
| 444 |
|
| 445 |
This expanded notation has the fields spelled out in some language |
| 446 |
specific form: |
| 447 |
|
| 448 |
Examples: |
| 449 |
+4 hours +3mn -2second |
| 450 |
+ 4 hr 3 minutes -2 |
| 451 |
4 hour + 3 min -2 s |
| 452 |
4 hr 2 s |
| 453 |
|
| 454 |
A field in the expanded notation has an optional sign, a number, and a |
| 455 |
string specifying the type of field. If the sign is absent, it |
| 456 |
defaults to the sign of the next larger element. So the following are |
| 457 |
equivalent: |
| 458 |
|
| 459 |
-4 hr 3 min 2 sec |
| 460 |
-4 hr -3 min -2 sec |
| 461 |
|
| 462 |
The valid strings describing each of the fields is contained in "Delta field |
| 463 |
names" section of the appropriate Date::Manip::Lang::<LANGUAGE> document. |
| 464 |
Refer to the L<Date::Manip::Lang> document for a list of languages. |
| 465 |
|
| 466 |
For example, for English, the document is L<Date::Manip::Lang::English> and |
| 467 |
the field names include strings like: |
| 468 |
|
| 469 |
y: y, yr, year, years |
| 470 |
m: m, mon, mons, month, months |
| 471 |
w: w, wk, ws, wks, week, weeks |
| 472 |
d: d, day, days |
| 473 |
h: h, hr, hrs, hour, hours |
| 474 |
mn: mn, min, mins, minute, minutes |
| 475 |
s: s, sec, secs, second, seconds |
| 476 |
|
| 477 |
This list may not be complete. You should refer to the language document |
| 478 |
for the full list. |
| 479 |
|
| 480 |
The "seconds" string may be omitted. The sign, number, and string may |
| 481 |
all be separated from each other by any amount of whitespace. The |
| 482 |
string specifying the unit must be separated from a following number |
| 483 |
by whitespace or a comma, so the following example will NOT work: |
| 484 |
|
| 485 |
4hours3minutes |
| 486 |
|
| 487 |
At minimum, it must be expressed as: |
| 488 |
|
| 489 |
4hours 3minutes |
| 490 |
4 hours, 3 minutes |
| 491 |
|
| 492 |
In the the expanded format, all fields must be given in the order: Y M |
| 493 |
W D H MN S. Any number of them may be omitted provided the rest |
| 494 |
remain in the correct order. Small numbers may be spelled out, so |
| 495 |
|
| 496 |
in two weeks |
| 497 |
in 2 weeks |
| 498 |
|
| 499 |
both work (but do not rely on this to work for large numbers). |
| 500 |
|
| 501 |
Most languages also allow a word to specify whether the delta is an |
| 502 |
amount of time after or before a fixed point. In English, the word "in" |
| 503 |
refers to a time after a fixed point, and "ago" refers to a point before |
| 504 |
a fixed point. So, the following deltas are equivalent: |
| 505 |
|
| 506 |
1:0:0:0:0:0:0 |
| 507 |
in 1 year |
| 508 |
|
| 509 |
and the following are equivalent |
| 510 |
|
| 511 |
-1:0:0:0:0:0:0 |
| 512 |
1 year ago |
| 513 |
|
| 514 |
The word "in" is completely ignored. The word "ago" has the affect of |
| 515 |
reversing all signs that appear in front of the components of the |
| 516 |
delta. In other words, the following two strings are identical: |
| 517 |
|
| 518 |
-12 yr 6 mon ago |
| 519 |
+12 yr +6 mon |
| 520 |
|
| 521 |
(don't forget that there is an implied minus sign in front of the 6 in |
| 522 |
the first string because when no sign is explicitly given, it carries |
| 523 |
the previously entered sign). |
| 524 |
|
| 525 |
The in/ago words only apply to the expanded format, so the following |
| 526 |
is invalid: |
| 527 |
|
| 528 |
1:0:0 ago |
| 529 |
|
| 530 |
A delta may be standard (non-business) or business. By default, a delta |
| 531 |
is treated as a non-business delta, but this can be changed in two |
| 532 |
different ways. |
| 533 |
|
| 534 |
The first way to make a delta be business is to pass in the appropriate |
| 535 |
option. For example: |
| 536 |
|
| 537 |
$delta->parse($string, { 'mode' => 'business' }); |
| 538 |
$delta->parse($string, { 'mode' => 'standard' }); |
| 539 |
|
| 540 |
The second way to specify whether a delta is business or non-business |
| 541 |
is to include a key word in the string that is parsed. If this string |
| 542 |
is included, it should not conflict with the value of a 'mode' option. |
| 543 |
|
| 544 |
Most languages include a word like "business" which can be used to |
| 545 |
specify that the resulting delta is a business delta or a non-business |
| 546 |
delta. Other languages have equivalent words. The placement of the |
| 547 |
word is not important. Also, the "business" word can be included with |
| 548 |
all types of deltas, and in both compact and expanded notation, so the |
| 549 |
following are valid and equivalent: |
| 550 |
|
| 551 |
in 4 hours business |
| 552 |
4:0:0 business |
| 553 |
business 0:0:0:0:4:0:0 |
| 554 |
|
| 555 |
There are also words "exact" or "approximate" which may be included in |
| 556 |
the delta for backward compatibility. However, they will be ignored. |
| 557 |
They will be removed in Date::Manip 7.00. The accuracy of delta |
| 558 |
(exact, semi-exact, approximate) will be determined only by what |
| 559 |
fields are present in the delta and the options passed in. |
| 560 |
When a delta is parsed, it is automatically normalized, unless the |
| 561 |
'nonorm' option is passed in. |
| 562 |
|
| 563 |
=head1 PRINTF DIRECTIVES |
| 564 |
|
| 565 |
The following printf directives are replaced with information |
| 566 |
from the delta. Directives may be replaced by the values of a |
| 567 |
single field in the delta (i.e. the hours or weeks field), |
| 568 |
the value of several fields expressed in terms of one of them |
| 569 |
(i.e. the number of years and months expressed in terms of |
| 570 |
months), or the directive may format either the entire delta, |
| 571 |
or portions of it. |
| 572 |
|
| 573 |
=over 4 |
| 574 |
|
| 575 |
=item B<Simple directives> |
| 576 |
|
| 577 |
These are directives which print simple characters. Currently, the |
| 578 |
only one is: |
| 579 |
|
| 580 |
%% Replaced by a single '%' |
| 581 |
|
| 582 |
As an example: |
| 583 |
|
| 584 |
$delta->printf('|%%|'); |
| 585 |
=> |%| |
| 586 |
|
| 587 |
=item B<Directives to print out a single field> |
| 588 |
|
| 589 |
The following directive is used to print out the value of a single |
| 590 |
field. Spaces are included here for clarity, but are not in the |
| 591 |
actual directive. |
| 592 |
|
| 593 |
% [+] [pad] [width] Xv |
| 594 |
|
| 595 |
Here, X is one of (y,M,w,d,h,m,s). The directive will print out the |
| 596 |
value for that field. |
| 597 |
|
| 598 |
If a '+' is included immediately after the '%', a sign will always be |
| 599 |
included. By default, only negative values will include a sign. |
| 600 |
|
| 601 |
'width' and 'pad' are used to set the width of the string containing |
| 602 |
the field as well as how it is padded. |
| 603 |
|
| 604 |
'width' is any positive integer (without a sign). If 'width' is |
| 605 |
included, it sets the length of the output string (unless the string |
| 606 |
is already longer than that, in which case the 'width' is ignored). |
| 607 |
|
| 608 |
If 'pad' is included, it may be the character '<', '>', or '0'. It |
| 609 |
will be ignored if 'width' is not included, or the string is already |
| 610 |
longer than 'width'. If the formatted delta field is shorter than |
| 611 |
'width', it will be padded with spaces on the left (if 'pad' is '<'), |
| 612 |
or right (if 'pad' is '>'), or it will be padded on the left (after |
| 613 |
any sign) with zeroes (if 'pad' is '0'). |
| 614 |
|
| 615 |
In the following examples, $delta contains the delta: 1:2:3:4:5:6:7 |
| 616 |
|
| 617 |
$delta->printf('|Month: %Mv|'); |
| 618 |
=> |Month: 2| |
| 619 |
|
| 620 |
$delta->printf('|Day: %+05dv|'); |
| 621 |
=> |Day: +0004| |
| 622 |
|
| 623 |
$delta->printf('|Day: %+<5dv|'); |
| 624 |
=> |Day: +4| |
| 625 |
|
| 626 |
$delta->printf('|Day: %>5sv|'); |
| 627 |
=> |Day: 7 | |
| 628 |
|
| 629 |
=item B<Directives to print out several fields in terms of one of them> |
| 630 |
|
| 631 |
The following directive is used to print out the value of several |
| 632 |
different fields, expressed in terms of a single field. |
| 633 |
|
| 634 |
% [+] [pad] [width] [.precision] XYZ |
| 635 |
|
| 636 |
Here, X, Y, and Z are each one of (y,M,w,d,h,m,s). The directive will |
| 637 |
print out the value for fields Y through Z expressed in terms of field X. |
| 638 |
|
| 639 |
Y must come before Z in the sequence (y,M,w,d,h,m,s) or it can be the |
| 640 |
same as Z. |
| 641 |
|
| 642 |
So, to print the day and hour fields in terms of seconds, use the directive: |
| 643 |
|
| 644 |
%sdh |
| 645 |
|
| 646 |
Any time all of X, Y, and Z are from a single set of fields, exact |
| 647 |
relationships are used. |
| 648 |
|
| 649 |
If the X, Y, and Z fields do not all belong to the same set of fields, |
| 650 |
approximate relationships are used. |
| 651 |
|
| 652 |
For non-business deltas, an approximate relationship is needed to link |
| 653 |
the Y/M part of the delta to the W/D part and a semi-approximate |
| 654 |
relationship is needed to link the W/D part with the H/MN/S part. |
| 655 |
These relationships are: |
| 656 |
|
| 657 |
1 day = 24 hours |
| 658 |
1 year = 365.2425 |
| 659 |
|
| 660 |
For business deltas, the approximate and semi-approximate relationships |
| 661 |
used to link the fields together are: |
| 662 |
|
| 663 |
1 week = X (length of business week in days) |
| 664 |
1 year = X/7 * 365.2425 |
| 665 |
|
| 666 |
For business deltas, the length of the day is defined using |
| 667 |
WorkDayStart and WorkDayEnd. For non-business deltas, a day is 24 |
| 668 |
hours long (i.e. daylight saving time is ignored). |
| 669 |
|
| 670 |
If 'precision' is included, it is the number of decimal places to |
| 671 |
print. If it is not included, but 'width' is included, precision will |
| 672 |
be set automatically to display the maximum number of decimal places |
| 673 |
given 'width'. |
| 674 |
|
| 675 |
If 'pad' is included, it may be the character '<', '>', or '0', and is |
| 676 |
used in the same way as printing out a single field. |
| 677 |
|
| 678 |
In the following examples, $delta contains the delta: 1:2:3:4:5:6:7 |
| 679 |
|
| 680 |
$delta->printf('|%.4Myw|'); |
| 681 |
=> |14.6900| |
| 682 |
1 year, 2 months, 3 weeks is approximately |
| 683 |
14.6900 months |
| 684 |
|
| 685 |
=item B<Directives to print out portions of the delta> |
| 686 |
|
| 687 |
The following directives may be used to print out some or all of a delta. |
| 688 |
|
| 689 |
% [+] [pad] [width] Dt |
| 690 |
% [+] [pad] [width] DXY |
| 691 |
|
| 692 |
The first directive will print out the entire delta. |
| 693 |
|
| 694 |
The second will print out the delta from the X to Y fields inclusive |
| 695 |
(where X and Y are each one of (y,M,w,d,h,m,s) and X must come before |
| 696 |
Y in the sequence). |
| 697 |
|
| 698 |
'pad' is optional and can be either '<' or '>' meaning to pad on the |
| 699 |
left or right with spaces. It defaults to '<'. |
| 700 |
|
| 701 |
If a '+' is included immediately following the '%', every field will |
| 702 |
have a sign attached. Otherwise, only the leftmost field in each set |
| 703 |
of fields will include a sign. |
| 704 |
|
| 705 |
$delta->printf('|%Dt|'); |
| 706 |
=> |+1:2:+3:+4:5:6:7| |
| 707 |
|
| 708 |
$delta->printf('|%+Dyd|'); |
| 709 |
=> |+1:+2:+3:+4| |
| 710 |
|
| 711 |
=back |
| 712 |
|
| 713 |
=head1 KNOWN BUGS |
| 714 |
|
| 715 |
None known. |
| 716 |
|
| 717 |
=head1 BUGS AND QUESTIONS |
| 718 |
|
| 719 |
Please refer to the Date::Manip::Problems documentation for |
| 720 |
information on submitting bug reports or questions to the author. |
| 721 |
|
| 722 |
=head1 SEE ALSO |
| 723 |
|
| 724 |
Date::Manip - main module documentation |
| 725 |
|
| 726 |
=head1 LICENSE |
| 727 |
|
| 728 |
This script is free software; you can redistribute it and/or |
| 729 |
modify it under the same terms as Perl itself. |
| 730 |
|
| 731 |
=head1 AUTHOR |
| 732 |
|
| 733 |
Sullivan Beck (https://github.com/SBECK-github/) |
| 734 |
|
| 735 |
=cut |