1Date::Manip::Delta(3) User Contributed Perl DocumentationDate::Manip::Delta(3)
2
3
4
6 Date::Manip::Delta - Methods for working with deltas
7
9 use Date::Manip::Delta;
10 $date = new Date::Manip::Delta;
11
13 This module contains functions useful in parsing and manipulating
14 deltas. As used in this module, a delta refers only to the amount of
15 time elapsed. It includes no information about a starting or ending
16 time.
17
18 There are several concepts involved in understanding the properties of
19 a delta.
20
21 fields
22 A delta consists of 7 fields: years, months, weeks, days, hours,
23 minutes, and seconds, usually expressed as a colon-separated
24 string. For example:
25
26 1:2:3:4:5:6:7
27
28 refers to an elapsed amount of time 1 year, 2 months, 3 weeks, 4
29 days, 5 hours, 6 minutes, and 7 seconds long.
30
31 normalized
32 A delta can be normalized or not. A normalized delta has values
33 which have been made consistent with the type of data they
34 represent. For example, a delta of:
35
36 0:0:0:0:0:10:70
37
38 is not normalized since 70 seconds is better expressed as 1 minute
39 10 seconds. The normalized form of this delta would be:
40
41 0:0:0:0:0:11:10
42
43 Deltas are automatically converted to a normalized form in almost
44 all functions.
45
46 sets of fields
47 When normalizing a delta, fields are grouped together in sets where
48 the exact relationship is known between all fields in the set.
49
50 For example, in a normal delta, it is known that there are exactly
51 60 seconds in a minute, exactly 60 minutes in an hour, etc.
52
53 It is NOT known how many weeks are in a month however. So, the year
54 and month fields form one set, and the weeks, days, hours, minutes,
55 and seconds form a second set.
56
57 When normalizing a delta, no data from one set will ever be mixed
58 with data from another set.
59
60 As a result, the following delta is normalized:
61
62 0:3:8:0:0:0:0
63
64 Although 8 weeks is clearly more than 1 month, we don't know the
65 relationship between the two, so they don't mix.
66
67 exact deltas
68 An exact delta is one which does not include any fields which
69 cannot be exactly written in terms of seconds. For example, a delta
70 which includes a year or month field can never be exact since there
71 is no exact length for either.
72
73 So, the delta:
74
75 0:3:8:0:0:0:0
76
77 is not exact, but the delta:
78
79 0:0:0:12:30:0:0
80
81 is exact.
82
83 business delta
84 Deltas can refer to changes in either the full calendar, or they
85 can refer to a business calendar.
86
87 Business deltas have the added complexity that there is no definite
88 relationship between the number of work days in a work week (there
89 may be a holiday during the week). As a result, there are three
90 sets of fields: year/month, week, day/hour/minute/second. An exact
91 business delta will not have a year, month, or week field.
92
93 There IS a definite relationship between hours and days, but it is
94 probably not 24 hours = 1 day. Common definitions of a work day
95 include 8 hours long (09:00-17:00) or 9 hours long (08:00-17:00),
96 and any other definition may be included may be defined as long as
97 the start time is earlier in the day than the end time. The config
98 variables WorkDayBeg, WorkDayEnd, and WorkDay24Hr can be used to
99 defined the length of the work day.
100
101 signs
102 Each set of fields has a sign associated with it. For example, the
103 delta "1 year ago" is written as:
104
105 -1:0:0:0:0:0:0
106
107 Since there is no mixing of data between sets of fields, you can
108 end up with a delta with two (or three in the case of business
109 deltas) signs. So, the following is a fully normalized business
110 delta:
111
112 +1:0:-3:+3:0:0:0
113
114 Note that for a fully normalized delta, the leading field in each
115 set of fields will always have a sign, even when it is redundant or
116 unnecessary.
117
118 For example:
119
120 +2:1:+2:6:23:51:30
121 +0:0:+0:0:0:0:10
122
123 In a normalized delta, all fields in a set will have the same sign.
124
126 new
127 new_config
128 new_date
129 new_delta
130 new_recur
131 base
132 tz
133 is_date
134 is_delta
135 is_recur
136 config
137 err Please refer to the Date::Manip::Obj documentation for these
138 methods.
139
140 parse
141 $err = $delta->parse($string [,$business]);
142
143 This takes a string and parses it to see if it is a valid delta. If
144 it is, an error code of 0 is returned and $delta now contains the
145 value of the delta. Otherwise, an error code of 1 is returned.
146
147 A valid delta is in one of two forms: colon or expanded.
148
149 The colon format is:
150
151 +Y:+M:+W:+D:+H:+MN:+S
152 examples:
153 0:0:0:0:4:3:-2
154 +4:3:-2
155 +4::3
156
157 In the colon format, from 1 to 7 of the fields may be given. For
158 example +D:+H:+MN:+S may be given to specify only four of the
159 fields. No spaces may be present in the colon format. It is
160 allowed to omit some of the fields. For example 5::3:30 is valid.
161 In this case, missing fields default to the value 0.
162
163 The expanded format is:
164
165 +Yy +Mm +Ww +Dd +Hh +MNmn +Ss
166 examples:
167 +4 hours +3mn -2second
168 + 4 hr 3 minutes -2
169 4 hour + 3 min -2 s
170 4 hr 2 s (note that minutes are omitted)
171
172 A field in the expanded format (+Yy) is a sign, a number, and a
173 string specifying the type of field. The sign is "+", "-", or
174 absent (defaults to the next larger element). The valid strings
175 (in English) specifying the field type are:
176
177 y: y, yr, year, years
178 m: m, mon, month, months
179 w: w, wk, ws, wks, week, weeks
180 d: d, day, days
181 h: h, hr, hour, hours
182 mn: mn, min, minute, minutes
183 s: s, sec, second, seconds
184
185 Other languages have similar abbreviations.
186
187 Also, the "seconds" string may be omitted. The sign, number, and
188 string may all be separated from each other by any number of
189 whitespace. The string specifying the unit must be separated from a
190 following number by whitespace or a comma, so the following example
191 will NOT work:
192
193 4hours3minutes
194
195 At minimum, it must be expressed as:
196
197 4hours 3minutes
198 4 hours, 3 minutes
199
200 In the the expanded format, all fields must be given in the order:
201 Y M W D H MN S. Any number of them may be omitted provided the
202 rest remain in the correct order.
203
204 Most languages also allow a word to specify whether the delta is an
205 amount of time after or before a fixed point. In English, the word
206 "in" refers to a time after a fixed point, and "ago" refers to a
207 point before a fixed point. So, the following deltas are
208 equivalent:
209
210 1:0:0:0:0:0:0
211 in 1 year
212
213 and the following are equivalent
214
215 -1:0:0:0:0:0:0
216 1 year ago
217
218 The word "in" is completely ignored. The word "ago" has the affect
219 of reversing all signs that appear in front of the components of
220 the delta. In other words, the following two strings are
221 identical:
222
223 -12 yr 6 mon ago
224 +12 yr +6 mon
225
226 (don't forget that there is an implied minus sign in front of the 6
227 in the first string because when no sign is explicitly given, it
228 carries the previously entered sign).
229
230 The in/ago words only apply to the expanded format, so the
231 following is invalid:
232
233 1:0:0 ago
234
235 A delta may be business mode, or non-business mode. By default, a
236 delta is treated as a non-business mode delta, but this can be
237 changed in two different ways.
238
239 The first way to make a delta be business mode is to pass in the
240 2nd argument to the function that is non-zero. If this is done, the
241 delta will be a business delta by default.
242
243 The second way to specify whether a delta is business or non-
244 business is to include a key word in the string that is parsed.
245 When this is done, these strings override any value of the
246 $business argument.
247
248 Most languages include a word like "business" which can be used to
249 specify that the resulting delta is a business mode delta or a non-
250 business delta. Other languages have equivalent words. The
251 placement of the word is not important. Also, the "business" word
252 can be included with both types of deltas, so the following are
253 valid and equivalent:
254
255 in 4 hours business
256 4:0:0 business
257 business 0:0:0:0:4:0:0
258
259 There are also words "exact" or "approximate" which serve to force
260 the delta to be non-business mode. For backward compatibility, both
261 are available and serve the same purpose (they no longer determine
262 whether the delta is exact or not... that is determined only by the
263 fields that are included as described above).
264
265 set
266 $err = $delta->set($field,$val);
267
268 This explicitly sets one or more fields in a delta.
269
270 $field can be any of the following:
271
272 $field $val
273
274 delta [Y,M,W,D,H,MN,S] sets the entire delta
275 business [Y,M,W,D,H,MN,S] sets the entire delta
276 normal [Y,M,W,D,H,MN,S] sets the entire delta
277 y YEAR sets one field
278 M MONTH
279 w WEEK
280 d DAY
281 h HOUR
282 m MINUTE
283 s SECOND
284
285 mode business, normal
286
287 An error is returned if an invalid value is passed in.
288
289 When setting the entire delta with "business" or "normal", it flags
290 the delta as a business or non-business delta respectively. When
291 setting the entire delta with "delta", the flag is left unchanged.
292
293 printf
294 $out = $delta->printf($in);
295
296 This takes a string ($in) which may contain any number of special
297 formatting directives. These directives are replaced with
298 information contained in the delta. Everything else in the string
299 is returned unmodified.
300
301 A directive always begins with '%'. They are described in the
302 section below in the section PRINTF DIRECTIVES.
303
304 calc
305 $date2 = $delta->calc($date1 [,$subtract]);
306 $delta3 = $delta1->calc($delta2 [,$subtract]);
307
308 Please refer to the Date::Manip::Calc documentation for details.
309
310 type
311 $flag = $delta->type($op);
312
313 This tests to see if a delta is of a certain type. $op can be;
314
315 business : returns 1 if it is a business delta
316 exact : returns 1 if it is exact
317
318 value
319 $val = $delta->value();
320 @val = $delta->value();
321
322 This returns the value of the delta. In scalar context, it returns
323 the printable string (equivalent to the printf directive '%Dt'). In
324 list context, it returns a list of fields.
325
326 undef is returned if there is no valid delta stored in $delta.
327
329 The following printf directives are replaced with information from the
330 delta. Directives may be replaced by the values of a single field in
331 the delta (i.e. the hours or weeks field), the value of several fields
332 expressed in terms of one of them (i.e. the number of years and months
333 expressed in terms of months), or the directive may format either the
334 entire delta, or portions of it.
335
336 Simple directives
337 These are directives which print simple characters. Currently, the
338 only one is:
339
340 %% Replaced by a single '%'
341
342 As an example:
343
344 $delta->printf('|A %% B|');
345 => |A % B|
346
347 Directives to print out a single field
348 The following directive is used to print out the value of a single
349 field. Spaces are included here for clarity, but are not in the
350 actual directive.
351
352 % [+] [pad] [width] Xv
353
354 Here, X is one of (y,M,w,d,h,m,s). The directive will print out the
355 value for that field (in the normalized delta).
356
357 If a '+' is included immediately after the '%', a sign will always
358 be included. By default, only negative values will include a sign.
359
360 'width' is any positive integer (without a sign). If 'width' is
361 included, it sets the length of the output string (unless the
362 string is already longer than that, in which case the 'width' is
363 ignored).
364
365 If 'pad' is included, it may be the character '<', '>', or '0'. It
366 will be ignored unless 'width' is included. If the formatted delta
367 field is shorter than 'width', it will be padded with spaces on the
368 left (if 'pad' is '<'), or right (if 'pad' is '>'), or it will be
369 padded on the left (after any sign) with zeroes (if 'pad' is '0').
370
371 In the following examples, $delta contains the delta: 1:2:3:4:5:6:7
372
373 $delta->printf('|Month: %Mv|');
374 => |Month: 2|
375
376 $delta->printf('|Day: %+05dv|');
377 => |Day: +0004|
378
379 $delta->printf('|Day: %+<5dv|');
380 => |Day: +4|
381
382 $delta->printf('|Day: %>5sv|');
383 => |Day: 7 |
384
385 Directives to print out several fields in terms of one of them
386 The following directive is used to print out the value of several
387 different fields, expressed in terms of a single field.
388
389 % [+] [pad] [width] [.precision] XYZ
390
391 Here, X, Y, and Z are each one of (y,M,w,d,h,m,s). The directive
392 will print out the value for fields Y through Z expressed in terms
393 of field X.
394
395 Y must come before Z in the sequence (y,M,w,d,h,m,s) or it can be
396 the same as Z.
397
398 So, to print the day and hour fields in terms of seconds, use the
399 directive:
400
401 %sdh
402
403 Any time all of X, Y, and Z are from a single set of fields, exact
404 relationships are used.
405
406 If the X, Y, and Z fields do not all belong to the same set of
407 fields, approximate relationships are used.
408
409 For non-business deltas, an approximate relationship is needed to
410 link the Y/M part of the delta to the W/D/H/Mn/S part. The
411 relationship used is that a year is assigned a length of 365.2425
412 days.
413
414 For business deltas, the relationship between weeks and days is set
415 to be the length of the business week (as defined using the
416 WorkWeekBeg and WorkWeekEnd config variables). Also, a factor of
417 X/7 * 365.2425 (where X is the number of days in a work week) is
418 used to determine the number of work days in a year.
419
420 If 'precision' is included, it is the number of decimal places to
421 print. If it is not included, but 'width' is included, precision
422 will be set automatically to display the maximum number of decimal
423 places given 'width'.
424
425 If 'pad' is included, it may be the character '<', '>', or '0', and
426 is used in the same way as printing out a single field.
427
428 In the following examples, $delta contains the delta: 1:2:3:4:5:6:7
429
430 $delta->printf('|%.4Myw|');
431 => |14.6900|
432 1 year, 2 months, 3 weeks is approximately
433 14.6900 months
434
435 Directives to print out portions of the delta
436 The following directives may be used to print out some or all of a
437 delta.
438
439 % [+] [pad] [width] Dt
440 % [+] [pad] [width] DXY
441
442 The first directive will print out the entire delta.
443
444 The second will print out the delta from the X to Y fields
445 inclusive (where X and Y are each one of (y,M,w,d,h,m,s) and X must
446 come before Y in the sequence).
447
448 'pad' is optional and can be either '<' or '>' meaning to pad on
449 the left or right with spaces. It defaults to '<'.
450
451 If a '+' is included immediately following the '%', every field
452 will have a sign attached. Otherwise, only the leftmost field in
453 each set of fields will include a sign.
454
455 $delta->printf('|%Dt|');
456 => |+1:2:+3:+4:5:6:7|
457
458 $delta->printf('|%+Dyd|');
459 => |+1:+2:+3:+4|
460
462 None known.
463
465 Please refer to the Date::Manip::Problems documentation for information
466 on submitting bug reports or questions to the author.
467
469 Date::Manip - main module documentation
470
472 This script is free software; you can redistribute it and/or modify it
473 under the same terms as Perl itself.
474
476 Sullivan Beck (sbeck@cpan.org)
477
478
479
480perl v5.12.0 2010-04-27 Date::Manip::Delta(3)