Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / lib / site_perl / 5.8.0 / sun4-solaris / Date / Calc / Object.pod
CommitLineData
86530b38
AT
1
2=head1 NAME
3
4Date::Calc::Object - Object-oriented add-on for Date::Calc with overloaded operators
5
6=head1 MOTTO
7
8Make frequent things easy and infrequent or hard things possible
9
10=head1 PREFACE
11
12Note that you do B<NOT> need to "C<use Date::Calc qw(...);>" in
13addition to this module.
14
15Simply
16
17 use Date::Calc::Object qw(...);
18
19B<INSTEAD OF>
20
21 use Date::Calc qw(...);
22
23with the same "C<qw(...)>" as you would with the "Date::Calc"
24module, and then forget about "Date::Calc::Object" altogether.
25
26The rest of your existing code doesn't change at all.
27
28Note also that in order to create a new date object, you do not
29need to use
30
31 $date_object = Date::Calc::Object->new(...);
32
33(but you may), and should use
34
35 $date_object = Date::Calc->new(...);
36
37instead (saves you some typing and is a trifle faster).
38
39=head1 SYNOPSIS
40
41=head2 Export tags
42
43 :all - all functions from Date::Calc
44 :aux - auxiliary functions shift_*
45 :ALL - both :all and :aux
46
47=head2 Functions
48
49See L<Date::Calc(3)> for a list of available functions.
50
51 $year = shift_year(\@_);
52 ($year,$mm,$dd) = shift_date(\@_);
53 ($hrs,$min,$sec) = shift_time(\@_);
54 ($year,$mm,$dd,$hrs,$min,$sec) = shift_datetime(\@_);
55
56=head2 Methods
57
58 $old = Date::Calc->accurate_mode([FLAG]);
59 $old = Date::Calc->number_format([NUMBER|CODEREF]);
60 $old = Date::Calc->delta_format([NUMBER|CODEREF]); # global default
61 $old = Date::Calc->date_format([NUMBER|CODEREF]); # global default
62 $old = Date::Calc->language([LANGUAGE]); # global default
63
64 $old = $date->accurate_mode([FLAG]); # is global nevertheless!
65 $old = $date->number_format([NUMBER|CODEREF]); # is global nevertheless!
66 $old = $date->delta_format([NUMBER|CODEREF]); # individual override
67 $old = $date->date_format([NUMBER|CODEREF]); # individual override
68 $old = $date->language([LANGUAGE]); # individual override
69
70 $flag = $date->is_delta();
71 $flag = $date->is_date();
72 $flag = $date->is_short(); # i.e., has no time part
73 $flag = $date->is_long(); # i.e., has time part
74 $flag = $date->is_valid();
75
76 $date = Date::Calc->new([TYPE]);
77 $date = Date::Calc->new([TYPE,]YEAR,MONTH,DAY[,HRS,MIN,SEC]);
78 $date = Date::Calc->new($arrayref);
79 $newdate = $somedate->new([TYPE]);
80 $newdate = $somedate->new([TYPE,]YEAR,MONTH,DAY[,HRS,MIN,SEC]);
81 $newdate = $somedate->new($arrayref);
82
83 $datecopy = $date->clone();
84 $targetdate->copy($sourcedate);
85 $targetdate->copy($arrayref);
86 $targetdate->copy(@list);
87
88 ($year,$month,$day) = $date->date([TYPE]);
89 ($year,$month,$day) = $date->date([TYPE,]YEAR,MONTH,DAY[,HRS,MIN,SEC]);
90 ($year,$month,$day) = $date->date($arrayref);
91 ([$hrs,$min,$sec]) = $date->time([TYPE]);
92 ($hrs,$min,$sec) = $date->time([TYPE,]HRS,MIN,SEC);
93 ([$hrs,$min,$sec]) = $date->time($arrayref);
94
95 ($year,$month,$day,$hrs,$min,$sec) =
96 $date->datetime([TYPE]);
97 ($year,$month,$day,$hrs,$min,$sec) =
98 $date->datetime([TYPE,]YEAR,MONTH,DAY[,HRS,MIN,SEC]);
99
100 $date = Date::Calc->today([FLAG]);
101 $date = Date::Calc->now([FLAG]); # shorthand for --+
102 $date = Date::Calc->today_and_now([FLAG]); # <-----+
103 $date = Date::Calc->gmtime([time]); # UTC/GMT
104 $date = Date::Calc->localtime([time]); # local time
105 $delta = Date::Calc->tzoffset([time]);
106 $date = Date::Calc->time2date([time]); # UTC/GMT
107
108 $date->today([FLAG]); # updates the date part only
109 $date->now([FLAG]); # updates the time part only
110 $date->today_and_now([FLAG]); # updates both date and time
111 $date->gmtime([time]); # updates both date and time (UTC/GMT)
112 $date->localtime([time]); # updates both date and time (local time)
113 $delta->tzoffset([time]); # updates both date and time
114 $date->time2date([time]); # updates both date and time (UTC/GMT)
115
116 $time = Date::Calc->mktime(); # same as "$time = CORE::time();"
117 $time = Date::Calc->date2time(); # same as "$time = CORE::time();"
118
119 $time = $date->mktime(); # converts into Unix time (local time)
120 $time = $date->date2time(); # converts into Unix time (UTC/GMT)
121
122 $year = $date->year([YEAR]);
123 $month = $date->month([MONTH]);
124 $day = $date->day([DAY]);
125 $hours = $date->hours([HRS]);
126 $minutes = $date->minutes([MIN]);
127 $seconds = $date->seconds([SEC]);
128
129 $number = $date->number([NUMBER|CODEREF]);
130 $string = $date->string([NUMBER|CODEREF][,LANGUAGE]);
131
132 $delta->normalize(); # renormalizes a delta vector
133
134=head2 Overloaded Operators
135
136 #####################################################
137 # Scalar operands are always converted into a delta #
138 # vector with that many days, i.e., [1,0,0,SCALAR] #
139 #####################################################
140
141=head2 Comparison Operators:
142
143 if ($date1 < $date2) { # compares date part only
144 if ($date1 <= $date2) { # compares date part only
145 if ($date1 > $date2) { # compares date part only
146 if ($date1 >= $date2) { # compares date part only
147 if ($date1 == $date2) { # compares date part only
148 if ($date1 != $date2) { # compares date part only
149
150 $comp = $date1 <=> $date2; # compares date part only
151
152 if ($date1 lt $date2) { # compares both date and time
153 if ($date1 le $date2) { # compares both date and time
154 if ($date1 gt $date2) { # compares both date and time
155 if ($date1 ge $date2) { # compares both date and time
156 if ($date1 eq $date2) { # compares both date and time
157 if ($date1 ne $date2) { # compares both date and time
158
159 $comp = $date1 cmp $date2; # compares both date and time
160
161Note that you can of course also compare two deltas,
162but not a date and a delta!
163
164 ##################################################
165 # Default TYPE for array refs in comparisons is: #
166 # Same as other operand #
167 ##################################################
168
169 if ([2000,4,1] == $date) {
170 if ($today > [2000,4,1]) {
171
172 if ($now ge [2000,3,26,2,0,0]) {
173
174 if ($delta == [18,0,0]) {
175 if ($delta == -1) {
176
177=head2 Plus:
178
179 $date2 = $date1 + $delta;
180 $date2 = $delta + $date1;
181 $date += $delta;
182 $this = $date++;
183 $next = ++$date;
184
185 $delta3 = $delta1 + $delta2;
186 $delta1 += $delta2;
187 $delta += $date; # beware of implicit type change!
188 $delta++;
189 ++$delta;
190
191 #####################################################
192 # Default TYPE for array refs in '+' operations is: #
193 # Opposite of other operand #
194 #####################################################
195
196 $date2 = [2000,3,26] + $delta;
197 $date2 = $date1 + [+1,0,0];
198 $date2 = [0,0,-1] + $date1;
199 $date2 = $date1 + 1;
200 $date += [0,0,+1];
201 $date += 2;
202
203 $delta3 = [1,+1,0,-1] + $delta2;
204 $delta3 = $delta1 + [1,0,0,+1];
205 $delta3 = $delta1 + 1;
206 $delta += [1,0,+1,0];
207 $delta += [2000,3,26]; # beware of implicit type change!
208 $delta += 7;
209
210=head2 Unary Minus:
211
212 $delta2 = -$delta1;
213
214=head2 Minus:
215
216 $delta = $date2 - $date1;
217 $date2 = $date1 - $delta;
218 $date -= $delta;
219 $date2 -= $date1; # beware of implicit type change!
220 $this = $date--;
221 $prev = --$date;
222
223 $delta3 = $delta2 - $delta1;
224 $delta2 -= $delta1;
225 $delta--;
226 --$delta;
227
228 #####################################################
229 # Default TYPE for array refs in '-' operations is: #
230 # Always a date #
231 #####################################################
232
233 $delta = $today - [2000,3,26];
234 $delta = [2000,4,1] - $date;
235 $date2 = [2000,3,26] - $delta;
236 $date2 = $date1 - [1,0,0,+7];
237 $date2 = $date1 - 7;
238 $date -= [1,0,0,+1]; # better add [0,0,-1] instead!
239 $date2 -= [2000,3,26]; # beware of implicit type change!
240 $date2 -= 1;
241
242 $delta3 = [1,0,+1,0] - $delta1;
243 $delta3 = $delta2 - [1,0,0,-1];
244 $delta -= [1,0,0,+1];
245 $delta -= 7;
246
247=head2 Miscellaneous Operators:
248
249 $string = "$date";
250 $string = "$delta";
251
252 print "$date\n";
253 print "$delta\n";
254
255 if ($date) { # date is valid
256 if ($delta) { # delta is valid
257
258 $days = abs($date);
259 $diff = abs($delta); # can be negative!
260
261 $diff = abs(abs($delta)); # always positive
262
263=head1 DESCRIPTION
264
265=over 2
266
267=item *
268
269FLAG
270
271"FLAG" is either 0 (for "false") or 1 (for "true").
272
273In the case of "C<accurate_mode()>", this switches "accurate mode"
274on and off (see further below for an explanation of what that is).
275
276In the case of "C<today()>", "C<now()>" and "C<today_and_now()>",
277a "true" value indicates "GMT" (Greenwich Mean Time), as opposed
278to local time, which is the default.
279
280=item *
281
282NUMBER
283
284"NUMBER" is a number between 0 and 2 (for "number_format()" and "number()")
285or between 0 and 3 (for "delta_format()", "date_format()" and "string()"),
286indicating which of the three/four predefined formats, respectively,
287should be used for converting a date into numeric representation
288(needed for comparing dates, for instance) or string representation.
289
290Format #0 is the default at startup and the simplest of all (and
291should be fastest to calculate, too).
292
293The string representation of dates in format #0 also has the advantage of
294being sortable in chronological order (and of complying with S<ISO 8601>).
295
296(The numeric formats are (trivially) always sortable in chronological
297order of course.)
298
299The other formats are increasingly more sophisticated (in terms of
300esthetics and computation time) with increasing number:
301
302 Delta number formats (short):
303
304 0 13603
305 1 13603
306 2 13603
307
308 Delta string formats (short):
309
310 0 '+0+0+13603'
311 1 '+0 +0 +13603'
312 2 '+0Y +0M +13603D'
313 3 '+0 Y +0 M +13603 D'
314
315 Date number formats (short):
316
317 0 20010401
318 1 730576
319 2 730576
320
321 Date string formats (short):
322
323 0 '20010401'
324 1 '01-Apr-2001'
325 2 'Sun 1-Apr-2001'
326 3 'Sunday, April 1st 2001'
327
328 Delta number formats (long):
329
330 0 13603.012959
331 1 13603.012959
332 2 13603.0624884259
333
334 Delta string formats (long):
335
336 0 '+0+0+13603+1+29+59'
337 1 '+0 +0 +13603 +1 +29 +59'
338 2 '+0Y +0M +13603D +1h +29m +59s'
339 3 '+0 Y +0 M +13603 D +1 h +29 m +59 s'
340
341 Date number formats (long):
342
343 0 20010401.082959
344 1 730576.082959
345 2 730576.354155093
346
347 Date string formats (long):
348
349 0 '20010401082959'
350 1 '01-Apr-2001 08:29:59'
351 2 'Sun 1-Apr-2001 08:29:59'
352 3 'Sunday, April 1st 2001 08:29:59'
353
354If a number outside of the permitted range is specified, or if the value
355is not a code reference (see also the next section below for more details),
356the default format #0 is used instead.
357
358=item *
359
360CODEREF
361
362"CODEREF" is the reference of a subroutine which can be passed to the
363methods "number_format()", "delta_format()" and "date_format()" in order
364to install a callback function which will be called subsequently whenever
365a date (or delta) object needs to be (implicitly) converted into a number
366or string.
367
368This happens for instance when you compare two date objects, or when you
369put a date object reference in a string between double quotes.
370
371Such a "CODEREF" can also be passed to the methods "number()" and
372"string()" for explicitly converting a date object as desired.
373
374=item *
375
376LANGUAGE
377
378"LANGUAGE" is either a number in the range C<[1..Languages()]>,
379or one of the strings "C<Language_to_Text(1..Languages())>"
380(see also L<Date::Calc(3)>).
381
382=item *
383
384TYPE
385
386"TYPE" is 0 for a regular date and 1 for a delta vector (a list of
387year, month, day and optionally hours, minutes and seconds offsets).
388
389=item *
390
391Storage
392
393"Date::Calc" objects are implemented as two nested arrays.
394
395The "blessed" array (whose reference is the object reference
396you receive when calling the "new()" method) contains an
397anonymous array at position zero and the object's data in
398its remaining fields.
399
400The embedded anonymous array is used for storing the object's
401attributes (flags).
402
403Dates and delta vectors always comprise either 3 or 6 data values:
404Year, month, day plus (optionally) hours, minutes and seconds.
405
406These values are stored in the "blessed" array at positions 1..3
407or 1..6, respectively.
408
409An object without the time values is therefore called "short",
410and an object having time values is called "long" throughout
411this manual.
412
413Hint: Whenever possible, if you do not need the time values, omit
414them, i.e., always use the "short" form of the object if possible,
415this will speed up calculations a little (the short form uses
416different (faster) functions for all calculations internally).
417
418The embedded anonymous array contains various flags:
419
420At position zero, it contains the "TYPE" indicator which determines
421whether the object is a date or a delta vector.
422
423At position 1, the object stores the "NUMBER" of one of the delta
424vector formats, or the reference of a callback function which converts
425the contents of the object into string representation if it's a delta
426vector, or "undef" if the global settings apply.
427
428At position 2, the object stores the "NUMBER" of one of the date formats,
429or the reference of a callback function which converts the contents of
430the object into string representation if it's a date, or "undef" if the
431global settings apply.
432
433At position 3, the object stores the "LANGUAGE" to be used for all
434conversions into strings (where applicable), or "undef" if the global
435language setting applies.
436
437Note that your callback functions (see the section "Callback Functions"
438further below for more details) do not need to pay attention to the
439value at position 3, the language (of the "Date::Calc" module)
440will automatically be set to this value whenever the callback
441functions are called, and automatically reset to its former value
442after the callback.
443
444So if your callback functions use the "*_to_Text*" functions from
445the "Date::Calc" module, they will automatically use the correct
446language.
447
448Be reminded though that you should B<NEVER> access the object's
449internal data directly, i.e., through their positional numbers,
450but B<ALWAYS> through their respective accessor methods, e.g.:
451
452 year()
453 month()
454 day()
455 hours()
456 minutes()
457 seconds()
458 date()
459 time()
460 datetime()
461 is_delta()
462 is_date()
463 is_short()
464 is_long()
465 delta_format()
466 date_format()
467 language()
468
469And although position 4 and onward in the embedded anonymous array is
470currently unused, it might not stay so in future releases of this module.
471
472Therefore, in case you need more attributes in a subclass of the
473"Date::Calc[::Object]" class, I suggest using values starting at
474positions a bit further up, e.g. 6, 8 or 10.
475
476=item *
477
478Invalid Dates
479
480Only "new()" allows to create objects containing possibly invalid
481dates (needed for reading in and evaluating user input, for example).
482
483=item *
484
485Usage
486
487The methods
488
489 accurate_mode()
490 number_format()
491 delta_format()
492 date_format()
493 language()
494 date()
495 time()
496 datetime()
497 year()
498 month()
499 day()
500 hours()
501 minutes()
502 seconds()
503
504are used for reading as well as for setting attributes. They simply
505return the values in question if they are called without parameters.
506
507The methods
508
509 accurate_mode()
510 number_format()
511 delta_format()
512 date_format()
513 language()
514
515always return the previous value if a new value is set. This allows
516you to change these values temporarily and to restore their old value
517afterwards more easily (but you can also override the "format" and
518"language" settings directly when calling the "number()" or "string()"
519method).
520
521The methods
522
523 date()
524 time()
525 datetime()
526 year()
527 month()
528 day()
529 hours()
530 minutes()
531 seconds()
532
533always return the new values when the corresponding values have
534been changed.
535
536The method "date()" NEVER returns the time values (hours, minutes,
537seconds) even if they have just been set using this method (which
538the method optionally allows). Otherwise it would be very hard to
539predict the exact number of values it returns, which might lead
540to errors (wrong number of parameters) elsewhere in your program.
541
542The method "datetime()" ALWAYS returns the time values (hours,
543minutes, seconds) even if the object in question lacks a time
544part. In that case, zeros are returned for hours, minutes and
545seconds instead (but the stored time part is left unchanged,
546whether it exists or not).
547
548If you do not provide values for hours, minutes and seconds when
549using the method "date()" to set the values for year, month and
550day, the time part will not be changed (whether it exists or not).
551
552If you do not provide values for hours, minutes and seconds when
553using the method "datetime()" to set the values for year, month
554and day, the time part will be filled with zeros (the time part
555will be created if necessary).
556
557If the object is short, i.e., if it does not have any time values,
558the method "time()" returns an empty list.
559
560If the object is short and the methods "hours()", "minutes()" or
561"seconds()" are used to set any of these time values, the object
562is automatically promoted to the "long" form, and the other two
563time values are filled with zeros.
564
565The following methods can also return "undef" under certain
566circumstances:
567
568 delta_format()
569 date_format()
570 language()
571 is_delta()
572 is_date()
573 is_short()
574 is_long()
575 is_valid()
576 hours()
577 minutes()
578 seconds()
579 number()
580 string()
581
582The methods "delta_format()", "date_format()" and "language()"
583return "undef" when they are called as object methods and no
584individual override has been defined for the object in question.
585
586The "is_*()" predicate methods return "undef" if the object in
587question does not have the expected internal structure. This can
588happen for instance when you create an empty object with "new()".
589
590When called without parameters, the methods "hours()", "minutes()"
591and "seconds()" return "undef" if the object in question does not
592have a time part.
593
594The methods "number()" and "string()" return "undef" if the object
595in question is not valid (i.e., if "is_valid()" returns "undef" or
596false).
597
598And finally, the methods
599
600 copy()
601 today()
602 now()
603 today_and_now()
604 gmtime()
605 localtime()
606 tzoffset()
607 time2date()
608 normalize()
609
610return the object reference of the (target) object in question
611for convenience.
612
613=item *
614
615Import/Export
616
617Note that you can import and export Unix "time" values using the
618methods "gmtime()", "localtime()", "mktime()", "date2time()" and
619"time2date()", both as local time or as UTC/GMT.
620
621=item *
622
623Accurate Mode
624
625The method "accurate_mode()" controls the internal flag which
626determines which of two modes of operation is used.
627
628When set to true (the default at startup), delta vectors are
629calculated to give the exact difference in days between two
630dates. The "year" and "month" entries in the resulting delta
631vector are always zero in that case.
632
633If "accurate mode" is switched off (when the corresponding
634flag is set to false), delta vectors are calculated with
635year and month differences.
636
637E.g., the difference between C<[1999,12,6]> and C<[2000,6,24]>
638is C<[+0 +0 +201]> (plus 201 days) in accurate mode and
639C<[+1 -6 +18]> (plus one year, minus 6 months, plus 18 days)
640when accurate mode is switched off.
641
642(The delta vector is calculated by simply taking the difference
643in years, the difference in months and the difference in days.)
644
645Because years and months have varying lengths in terms of days,
646the latter is less accurate than the former because it depends
647on the context of the two dates of which it represents the
648difference. Added to a different date, the latter delta vector
649may yield a different offset in terms of days.
650
651Beware also that - for the same reason - the absolute value
652("C<abs()>") of a delta vector returns a fictitious number
653of days if the delta vector contains non-zero values for
654"year" and/or "month" (see also next section below for
655more details).
656
657Example:
658
659The difference between C<[2000,1,1]> and C<[2000,3,1]> is
660C<[+0 +0 +60]> in accurate mode and C<[+0 +2 +0]> else (one
661could also call this "year-month-day mode" or "YMD mode" for
662short).
663
664When added to the date C<[2000,4,1]>, the "accurate" delta
665vector yields the date C<[2000,5,31]>, whereas the other delta
666vector yields the date C<[2000,6,1]>.
667
668Moreover, when added to the date C<[1999,1,1]>, the "accurate"
669delta vector yields the date C<[1999,3,2]>, whereas the "inaccurate"
670delta vector yields the date C<[1999,3,1]>.
671
672Depending on what you want, the one or the other mode may suit
673you better.
674
675=item *
676
677Absolute Value
678
679Note that "C<abs($date)>" and "C<abs($delta)>" are just shorthands
680for "C<$date-E<gt>number()>" and "C<$delta-E<gt>number()>".
681
682The operator "C<abs()>", when applied to a date or delta vector,
683returns the corresponding number of days (see below for an exception
684to this), with the time part (if available) represented by a fraction
685after the decimal point.
686
687In the case of dates, the absolute value (to the left of the
688decimal point) is the number of days since the 1st of January
689S<1 A.D.> (by extrapolating the Gregorian calendar back beyond
690its "natural" limit of 1582 A.D.) B<PLUS ONE>.
691
692(I.e., the absolute value of the 1st of January 1 A.D. is 1.)
693
694Exception:
695
696If the "NUMBER" or "number_format()" is set to 0 (the default
697setting), the absolute value of a date to the left of the decimal
698point is "yyyymmdd", i.e., the number in which the uppermost four
699digits correspond to the year, the next lower two digits to the
700month and the lowermost two digits to the day.
701
702In the case of delta vectors, the absolute value (to the left
703of the decimal point) is simply the difference in days (but
704see also below).
705
706Note that the absolute value of a delta vector can be negative!
707
708If you want a positive value in all cases, apply the "C<abs()>"
709operator again, i.e., "C<$posdiff = abs(abs($delta));>".
710
711If the delta vector contains non-zero values for "year" and/or
712"month" (see also the discussion of "Accurate Mode" in the section
713above), an exact representation in days cannot be calculated,
714because years and months do not have fixed equivalents in days.
715
716If nevertheless you attempt to calculate the absolute value of
717such a delta vector, a fictitious value is returned, which is
718calculated by simply multiplying the year difference with 12,
719adding the month difference, multiplying this sum with 31 and
720finally adding the day difference.
721
722Beware that because of this, the absolute values of delta
723vectors are not necessarily contiguous.
724
725Moreover, since there is more than one way to express the
726difference between two dates, comparisons of delta vectors
727may not always yield the expected result.
728
729Example:
730
731The difference between the two dates C<[2000,4,30]> and
732C<[2001,5,1]> can be expressed as C<[+1 +1 -29]>, or as
733C<[+1 +0 +1]>.
734
735The first delta vector has an absolute value of 374,
736whereas the latter delta vector has an absolute value
737of only 373 (while the true difference in days between
738the two dates is 366).
739
740If the date or delta vector has a time part, the time is returned
741as a fraction of a full day after the decimal point as follows:
742
743If the "NUMBER" or "number_format()" is set to 0 (the default
744setting) or 1, this fraction is simply ".hhmmss", i.e., the
745two digits after the decimal point represent the hours, the
746next two digits the minutes and the last two digits the seconds.
747
748Note that you cannot simply add and subtract these values to
749yield meaningful dates or deltas again, you can only use them
750for comparisons (equal, not equal, less than, greater than,
751etc.). If you want to add/subtract, read on:
752
753Only when the "NUMBER" or "number_format()" is set to 2, this
754fraction will be the equivalent number of seconds (i.e.,
755C<(((hours * 60) + minutes) * 60) + seconds>) divided by the
756number of seconds in a full day (i.e., C<24*60*60 = 86400>),
757or C<0/86400>, C<1/86400>, ... , C<86399/86400>.
758
759In other words, the (mathematically correct) fraction of a day.
760
761You can safely perform arithmetics with these values as far
762as the internal precision of your vendor's implementation
763of the C run-time library (on which Perl depends) will permit.
764
765=item *
766
767Renormalizing Delta Vectors
768
769When adding or subtracting delta vectors to/from one another,
770the addition or subtraction takes place component by component.
771
772Example:
773
774 [+0 +0 +0 +3 +29 +50] + [+0 +0 +0 +0 +55 +5] = [+0 +0 +0 +3 +84 +55]
775 [+0 +0 +0 +3 +29 +50] - [+0 +0 +0 +0 +55 +5] = [+0 +0 +0 +3 -26 +45]
776
777This may result in time values outside the usual ranges (C<[-23..+23]>
778for hours and C<[-59..+59]> for minutes and seconds).
779
780Note that even though the delta value for days will often become quite large,
781it is impossible to renormalize this value because there is no constant
782conversion factor from days to months (should it be 28, 29, 30 or 31?).
783
784If accurate mode (see further above for what that is) is switched off,
785delta vectors can also contain non-zero values for years and months. If
786you add or subtract these, the value for months can lie outside the
787range C<[-11..11]>, which isn't wrong, but may seem funny.
788
789Therefore, the "normalize()" method will also renormalize the "months"
790value, if and only if accurate mode has been switched off. (!)
791
792(Hence, switch accurate mode B<ON> temporarily if you B<DON'T> want
793the renormalization of the "months" value to happen.)
794
795If you want to force the time values from the example above back into
796their proper ranges, use the "normalize()" method as follows:
797
798 print "[$delta]\n";
799 $delta->normalize();
800 print "[$delta]\n";
801
802This will print
803
804 [+0 +0 +0 +3 +84 +55]
805 [+0 +0 +0 +4 +24 +55]
806
807for the first and
808
809 [+0 +0 +0 +3 -26 +45]
810 [+0 +0 +0 +2 +34 +45]
811
812for the second delta vector from the example further above.
813
814Note that the values for days, hours, minutes and seconds are
815guaranteed to have the same sign after the renormalization.
816
817Under "normal" circumstances, i.e., when accurate mode is on (the
818default), this method only has an effect on the time part of the
819delta vector.
820
821If the delta vector in question does not have a time part, nothing
822is done.
823
824If accurate mode is off, the "months" value is also normalized,
825i.e., if it lies outside of the range C<[-11..11]>, integer
826multiples of 12 are added to the "years" value and subtracted
827from the "months" value. Moreover, the "months" value is
828guaranteed to have the same sign as the values for days,
829hours, minutes and seconds, unless the "months" value is zero
830or the values for days, hours, minutes and seconds are all zero.
831
832If the object in question is a date and if warnings are enabled,
833the message "normalizing a date is a no-op" will be printed to
834STDERR.
835
836If the object in question is not a valid "Date::Calc" object,
837nothing is done.
838
839The method returns its object's reference, which allows chaining
840of method calls, as in the following example:
841
842 @time = $delta->normalize()->time();
843
844=item *
845
846Callback Functions
847
848Note that you are not restricted to the built-in formats
849(numbered from 0 to 2 for "number_format()" and "number()"
850and from 0 to 3 for "delta_format()", "date_format()" and
851"string()") for converting a date or delta object into a
852number or string.
853
854You can also provide your own function(s) for doing so, in
855order to suit your own taste or needs, by passing a subroutine
856reference to the appropriate method, i.e., "number_format()",
857"number()", "delta_format()", "date_format()" and "string()".
858
859You can pass a handler to only one or more of these methods,
860or to all of them, as you like. You can use different callback
861functions, or the same for all.
862
863In order to facilitate the latter, and in order to make the
864decoding of the various cases easier for you, the callback
865function receives a uniquely identifying function code as
866its second parameter:
867
868 0 = TO_NUMBER | IS_DATE | IS_SHORT (number[_format])
869 1 = TO_NUMBER | IS_DATE | IS_LONG (number[_format])
870 2 = TO_NUMBER | IS_DELTA | IS_SHORT (number[_format])
871 3 = TO_NUMBER | IS_DELTA | IS_LONG (number[_format])
872 4 = TO_STRING | IS_DATE | IS_SHORT (string|date_format)
873 5 = TO_STRING | IS_DATE | IS_LONG (string|date_format)
874 6 = TO_STRING | IS_DELTA | IS_SHORT (string|delta_format)
875 7 = TO_STRING | IS_DELTA | IS_LONG (string|delta_format)
876
877The first parameter of the callback function is of course the
878handle of the object in question itself (therefore, the callback
879function can actually be an object method - but not a class method,
880for obvious reasons).
881
882The handler should return the resulting number or string, as
883requested.
884
885BEWARE that you should NEVER rely upon any knowledge of the
886object's internal structure, as this may be subject to change!
887
888ALWAYS use the test and access methods provided by this module!
889
890Example:
891
892 sub handler
893 {
894 my($self,$code) = @_;
895
896 if ($code == 0) # TO_NUMBER | IS_DATE | IS_SHORT
897 {
898 return Date_to_Days( $self->date() );
899 }
900 elsif ($code == 1) # TO_NUMBER | IS_DATE | IS_LONG
901 {
902 return Date_to_Days( $self->date() ) +
903 ( ( $self->hours() * 60 +
904 $self->minutes() ) * 60 +
905 $self->seconds() ) / 86400;
906 }
907 elsif ($code == 2) # TO_NUMBER | IS_DELTA | IS_SHORT
908 {
909 return ( $self->year() * 12 +
910 $self->month() ) * 31 +
911 $self->day();
912 }
913 elsif ($code == 3) # TO_NUMBER | IS_DELTA | IS_LONG
914 {
915 return ( $self->year() * 12 +
916 $self->month() ) * 31 +
917 $self->day() +
918 ( ( $self->hours() * 60 +
919 $self->minutes() ) * 60 +
920 $self->seconds() ) / 86400;
921 }
922 elsif ($code == 4) # TO_STRING | IS_DATE | IS_SHORT
923 {
924 return join( "/", $self->date() );
925 }
926 elsif ($code == 5) # TO_STRING | IS_DATE | IS_LONG
927 {
928 return join( "/", $self->date() ) . " " .
929 join( ":", $self->time() );
930 }
931 elsif ($code == 6) # TO_STRING | IS_DELTA | IS_SHORT
932 {
933 return join( "|", $self->date() );
934 }
935 elsif ($code == 7) # TO_STRING | IS_DELTA | IS_LONG
936 {
937 return join( "|", $self->datetime() );
938 }
939 else
940 {
941 die "internal error";
942 }
943 }
944
945 Date::Calc->number_format(\&handler);
946 Date::Calc->delta_format(\&handler);
947 Date::Calc->date_format(\&handler);
948
949This sets our handler to take care of all automatic conversions,
950such as needed when comparing dates or when interpolating a string
951in double quotes which contains a date object.
952
953To deactivate a handler, simply pass a valid format number to the
954method in question, e.g.:
955
956 Date::Calc->number_format(0);
957 Date::Calc->delta_format(2);
958 Date::Calc->date_format(3);
959
960When calling the "number()" or "string()" method explicitly, you can
961pass a different format number (than the global setting), like this:
962
963 $number = $date->number(2);
964 $string = $date->string(1);
965
966You can also pass a handler's reference, like so:
967
968 $number = $date->number(\&handler);
969 $string = $date->string(\&handler);
970
971This overrides the global setting for the duration of the call of
972"number()" or "string()" (but doesn't change the global setting
973itself).
974
975Moreover, you can also define individual overrides for the date and
976the delta vector formats (but not the number format) for individual
977objects, e.g.:
978
979 $date->delta_format(1);
980 $date->date_format(2);
981
982 $date->delta_format(\&handler);
983 $date->date_format(\&handler);
984
985In order to deactivate an individual handler for an object, and/or
986in order to deactivate any override altogether (so that the global
987settings apply again), you have to pass "undef" explicitly to the
988method in question:
989
990 $date->delta_format(undef);
991 $date->date_format(undef);
992
993You can also define a language for individual objects (see the
994next section immediately below for more details).
995
996If such an individual language override has been set, and if your
997callback handlers only use the "*_to_Text*" functions from the
998"Date::Calc" module to produce any text, the text produced will
999automatically be in the desired language.
1000
1001This is because the language is set to the value determined by
1002the individual override before the callback handler is executed,
1003and reset to its previous value afterwards.
1004
1005=item *
1006
1007Languages
1008
1009Note that this module is completely transparent to the setting
1010of a language in "Date::Calc". This means that you can choose a
1011language in "Date::Calc" (with the "Language()" function) and all
1012dates subsequently printed by this module will automatically be
1013in that language - provided that you use the built-in formats of
1014this module, or that you use the "*to_Text*" functions from the
1015"Date::Calc" module in your formatting handler (callback function).
1016
1017However, this global language setting can be overridden for
1018individual date (or delta) objects by using the B<OBJECT> method
1019
1020 $oldlang = $date->language($newlang);
1021
1022(The global setting is not altered by this in any way.)
1023
1024In order to deactivate such an individual language setting
1025(so that the global setting applies again), simply pass the
1026value "undef" explicitly to the "language()" object method:
1027
1028 $date->language(undef);
1029
1030The B<CLASS> method
1031
1032 $oldlang = Date::Calc->language($newlang);
1033
1034is just a convenient wrapper around the "Language()" function,
1035which allows you to enter language numbers (as returned by the
1036"Decode_Language()" function) or strings (as returned by the
1037"Language_to_Text()" function), at your option.
1038
1039The "language()" method (both class and object) always returns
1040the B<NAME> (one of "C<Language_to_Text(1..Languages())>") of
1041the current setting (and never its number).
1042
1043=item *
1044
1045Exported Functions
1046
1047The "Date::Calc::Object" package imports ":all" functions exported
1048by the "Date::Calc" module and re-exports them, for conveniency.
1049
1050This allows you to write
1051
1052 use Date::Calc::Object qw(...);
1053
1054instead of
1055
1056 use Date::Calc qw(...);
1057
1058but with exactly the same semantics. The difference is that
1059the object-oriented frontend is loaded additionally in the
1060first case.
1061
1062As with "Date::Calc" you can use the ":all" tag to import all
1063of "Date::Calc"'s functions:
1064
1065 use Date::Calc::Object qw(:all);
1066
1067In addition to the functions exported by "Date::Calc", the
1068"Date::Calc::Object" package offers some utility functions
1069of its own for export:
1070
1071 $year = shift_year(\@_);
1072 ($year,$mm,$dd) = shift_date(\@_);
1073 ($hrs,$min,$sec) = shift_time(\@_);
1074 ($year,$mm,$dd,$hrs,$min,$sec) = shift_datetime(\@_);
1075
1076These functions enable your subroutines or methods to accept
1077a "Date::Calc" (or subclass) date object, an (anonymous) array
1078or a list (containing the necessary values) as parameters
1079B<INTERCHANGEABLY>.
1080
1081You can import all of these auxiliary functions by using an
1082":aux" tag:
1083
1084 use Date::Calc::Object qw(:aux);
1085
1086If you want to import both all of the "Date::Calc" functions
1087as well as all these auxiliary functions, use the ":ALL" tag:
1088
1089 use Date::Calc::Object qw(:ALL);
1090
1091=item *
1092
1093Subclassing
1094
1095In case you want to subclass "Date::Calc" objects and to add
1096new attributes of your own, it is recommended that you proceed
1097as follows (the following will be considered as a part of the
1098module's "contract of use" - which might be subject to change
1099in the future, however):
1100
1101Define a constant for the index of each attribute you want to
1102add, currently starting no lower than "4", at the top of your
1103subclass:
1104
1105 use constant ATTRIB1 => 4;
1106 use constant ATTRIB2 => 5;
1107 use constant ATTRIB3 => 6;
1108 ...
1109
1110It is recommended that you use constants (which are easy to
1111change), because I someday might want to require the element
1112with index "4" for a new attribute of my own... C<:-)>
1113
1114Then access your attributes like so (e.g. after calling
1115"C<$self = SUPER-E<gt>new();>" in your constructor method):
1116
1117 $self->[0][ATTRIB1] = 'value1';
1118 $self->[0][ATTRIB2] = 'value2';
1119 $self->[0][ATTRIB3] = 'value3';
1120 ...
1121
1122Beware that if you put anything other than numbers or strings
1123into your attributes, the methods "clone()" and "copy()" might
1124not work as expected anymore!
1125
1126Especially if your attributes contain references to other data
1127structures, only the references will be copied, but not the data
1128structures themselves.
1129
1130This may not be what you want.
1131
1132(You will have to override these two methods and write some
1133of your own if not.)
1134
1135In order for the overloaded operators and the "shift_*()"
1136auxiliary functions from the "Date::Calc::Object" package
1137to work properly (the latter of which are heavily used in
1138the "Date::Calendar[::Year]" modules, for instance), the
1139package name of your subclass (= the one your objects will
1140be blessed into) is B<REQUIRED> to contain a "::".
1141
1142Note that you should B<ONLY> subclass "Date::Calc", B<NEVER>
1143"Date::Calc::Object", since subclassing the latter is less
1144efficient (because "Date::Calc::Object" is just an empty class
1145which inherits from "Date::Calc" - subclassing "Date::Calc::Object"
1146would thus just introduce an additional name space layer to search
1147during Perl's runtime method binding process).
1148
1149If you give your subclass a package name below/inside the
1150"Date::" namespace, you will also benefit from the fact that
1151all error messages produced by the "Date::Calc[::Object]" module
1152(and also the "Date::Calendar[::Year]" modules, by the way)
1153will appear to have originated from the place outside of all
1154"C</^Date::/>" modules (including yours) where one of the "Date::"
1155modules was first called - i.e., all errors are always blamed
1156on the user, no matter how deeply nested inside the "Date::"
1157modules they occur, and do not usually refer to places inside
1158any of the "Date::" modules (this assumes that there are no
1159bugs in the "Date::" modules, and that all errors are always
1160the user's fault C<:-)>).
1161
1162Moreover, your module's own error messages will behave in the
1163same way if you "C<use Carp::Clan qw(^Date::);>" at the top of
1164your module and if you produce all error messages using "carp()"
1165and "croak()" (instead of "warn()" and "die()", respectively).
1166
1167=back
1168
1169=head1 EXAMPLES
1170
1171=over 3
1172
1173=item 1)
1174
1175 # Switch to summer time:
1176 $now = Date::Calc->now();
1177 if (($now ge [2000,3,26,2,0,0]) and
1178 ($now lt [2000,3,26,3,0,0]))
1179 {
1180 $now += [0,0,0,1,0,0];
1181 }
1182
1183=item 2)
1184
1185 use Date::Calc::Object qw(:all);
1186
1187 Date::Calc->date_format(3);
1188
1189 $date = 0;
1190 while (!$date)
1191 {
1192 print "Please enter the date of your birthday (day-month-year): ";
1193 $date = Date::Calc->new( Decode_Date_EU( scalar(<STDIN>) ) );
1194 if ($date)
1195 {
1196 $resp = 0;
1197 while ($resp !~ /^\s*[YyNn]/)
1198 {
1199 print "Your birthday is: $date\n";
1200 print "Is that correct? (yes/no) ";
1201 $resp = <STDIN>;
1202 }
1203 $date = 0 unless ($resp =~ /^\s*[Yy]/)
1204 }
1205 else
1206 {
1207 print "Unable to parse your birthday. Please try again.\n";
1208 }
1209 }
1210
1211 if ($date + [18,0,0] <= [Today()])
1212 { print "Ok, you are over 18.\n"; }
1213 else
1214 { print "Sorry, you are under 18!\n"; }
1215
1216=back
1217
1218For more examples, see the "examples" subdirectory in this distribution,
1219and their descriptions in the file "EXAMPLES.txt".
1220
1221=head1 SEE ALSO
1222
1223Date::Calc(3), Date::Calendar(3),
1224Date::Calendar::Year(3), Date::Calendar::Profiles(3).
1225
1226=head1 VERSION
1227
1228This man page documents "Date::Calc::Object" version 5.3.
1229
1230=head1 AUTHOR
1231
1232 Steffen Beyer
1233 mailto:sb@engelschall.com
1234 http://www.engelschall.com/u/sb/download/
1235
1236=head1 COPYRIGHT
1237
1238Copyright (c) 2000 - 2002 by Steffen Beyer. All rights reserved.
1239
1240=head1 LICENSE
1241
1242This package is free software; you can redistribute it and/or
1243modify it under the same terms as Perl itself, i.e., under the
1244terms of the "Artistic License" or the "GNU General Public License".
1245
1246Please refer to the files "Artistic.txt" and "GNU_GPL.txt"
1247in this distribution for details!
1248
1249=head1 DISCLAIMER
1250
1251This package is distributed in the hope that it will be useful,
1252but WITHOUT ANY WARRANTY; without even the implied warranty of
1253MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1254
1255See the "GNU General Public License" for more details.
1256