1Date::Calendar::ProfileUss(e3r)Contributed Perl DocumentDaattieo:n:Calendar::Profiles(3)
2
3
4
6 Date::Calendar::Profiles - Some sample profiles for Date::Calendar and
7 Date::Calendar::Year
8
10 use Date::Calendar::Profiles qw( $Profiles );
11 use Date::Calendar;
12
13 $cal_US_AK = Date::Calendar->new( $Profiles->{'US-AK'} [,LANG[,WEEKEND]] );
14 $cal_DE_BY = Date::Calendar->new( $Profiles->{'DE-BY'} [,LANG[,WEEKEND]] );
15
16 or
17
18 use Date::Calendar::Profiles qw( $Profiles );
19 use Date::Calendar::Year;
20
21 $year_2000_US_FL = Date::Calendar::Year->new( 2000, $Profiles->{'US-FL'} [,LANG[,WEEKEND]] );
22 $year_2001_DE_NW = Date::Calendar::Year->new( 2001, $Profiles->{'DE-NW'} [,LANG[,WEEKEND]] );
23
24 and also
25
26 use Date::Calendar::Profiles
27 qw(
28 &Previous_Friday
29 &Next_Monday
30 &Next_Monday_or_Tuesday
31 &Nearest_Workday
32 &Sunday_to_Monday
33 &Advent1
34 &Advent2
35 &Advent3
36 &Advent4
37 &Advent
38 );
39
41 This module provides some sample profiles (i.e., holiday schemes) for
42 use with the Date::Calendar(3) and Date::Calendar::Year(3) module.
43
44 You are not required to use these, you can always roll your own (this
45 is very easy). See the section "HOW TO ROLL YOUR OWN" below for more
46 instructions on how to do this, and take the profiles from this module
47 as examples.
48
49 I intend not to make any fixes to any of the calendar profiles in this
50 module anymore unless there are VERY compelling reasons to do so. These
51 profiles are merely meant as examples.
52
53 The suggested way of using these profiles is to copy them to your own
54 code and then to modify them as needed. Otherwise many people could be
55 negatively affected if I made any changes to a profile someone has been
56 using for years.
57
58 Any improvements are therefore left as an exercise to the inclined
59 reader.
60
62 The method "init()" in module Date::Calendar::Year(3) is responsible
63 for parsing the calendar schemes contained here in the
64 Date::Calendar::Profiles module.
65
66 This method offers a "mini-language" which allows to specify common
67 date formulas, like for instance a simple fixed date (in various
68 different formats, e.g. american or european), or things like "the
69 second Sunday of May" (Mother's Day), or "Easter Sunday minus 46 days"
70 (Ash Wednesday), to cite just a few.
71
72 See the section "DATE FORMULA SYNTAX" below for more details.
73
74 There are some more complicated formulas, however, which cannot be
75 expressed in such simple terms.
76
77 The rule that if a holiday falls on a weekend, it will be substituted
78 by either the adjacent Friday or Monday (whichever lies closer), is an
79 example of this.
80
81 In order to be able to deal with such formulas, and in order to be as
82 flexible as possible, the "init()" method offers the possibility of
83 using callback functions to deal with such dates and formulas.
84
85 See the section "CALLBACK INTERFACE" below for more details on this
86 topic.
87
88 In order to assist you with more common cases of odd formulas, the
89 module Date::Calendar::Profiles exports the following utility
90 subroutines (which are meant to be used as "filters" in callback
91 functions of your own):
92
93 • "($year,$month,$day[,ANYTHING]) =
94 Previous_Friday($year,$month,$day[,ANYTHING]);"
95
96 If the given date falls on a Saturday or Sunday, this function
97 changes the date to the adjacent Friday before that, and returns this
98 new date.
99
100 Otherwise the given date is returned unchanged.
101
102 The rest of the input parameters, if any, are simply copied to the
103 output.
104
105 • "($year,$month,$day[,ANYTHING]) =
106 Next_Monday($year,$month,$day[,ANYTHING]);"
107
108 If the given date falls on a Saturday or Sunday, this function
109 changes the date to the adjacent Monday after that, and returns this
110 new date.
111
112 Otherwise the given date is returned unchanged.
113
114 The rest of the input parameters, if any, are simply copied to the
115 output.
116
117 • "($year,$month,$day[,ANYTHING]) =
118 Next_Monday_or_Tuesday($year,$month,$day[,ANYTHING]);"
119
120 If the given date falls on a Saturday, the date of the next Monday
121 (after that weekend) is returned.
122
123 If the given date falls on a Sunday, the date of the next Tuesday
124 (after that weekend) is returned.
125
126 If the given date falls on a Monday, the date of the next Tuesday
127 (the day after the Monday) is returned.
128
129 Otherwise the given date is returned unchanged.
130
131 The rest of the input parameters, if any, are simply copied to the
132 output.
133
134 This function is used for the second of two adjacent holidays, where
135 the first holiday obeys the "Next Monday" rule (see the description
136 of the function immediately above).
137
138 Examples of this are Christmas and Boxing Day, among others.
139
140 When the first holiday falls on Friday, the second one falls on
141 Saturday and is substituted by Monday.
142
143 When the first holiday falls on a Saturday, the second one falls on
144 Sunday, so the first holiday is substituted by Monday and the second
145 one by Tuesday.
146
147 When the first holiday falls on a Sunday, the second one falls on a
148 Monday. Therefore the first holiday is substituted by Monday, and
149 consequently the second holiday must be substituted by Tuesday.
150
151 Or, in other terms:
152
153 Fri Sat => Fri Mon
154 Sat Sun => Mon Tue
155 Sun Mon => Mon Tue
156
157 Note that there is no filter subroutine yet for the second of two
158 adjacent holidays when the first holiday obeys the "Nearest Workday"
159 rule (see the function described immediately below), i.e.,
160
161 Fri Sat => Fri Mon
162 Sat Sun => Fri Mon
163 Sun Mon => Mon Tue
164
165 This is left as an excercise to the inclined reader. ":-)"
166
167 • "($year,$month,$day[,ANYTHING]) =
168 Nearest_Workday($year,$month,$day[,ANYTHING]);"
169
170 If the given date falls on a Saturday, this function returns the date
171 of the Friday on the day before.
172
173 If the given date falls on a Sunday, this function returns the date
174 of the Monday on the day after.
175
176 Otherwise the given date is returned unchanged.
177
178 The rest of the input parameters, if any, are simply copied to the
179 output.
180
181 • "($year,$month,$day[,ANYTHING]) =
182 Sunday_to_Monday($year,$month,$day[,ANYTHING]);"
183
184 If the given date falls on a Sunday, this function returns the date
185 of the Monday on the day after.
186
187 Otherwise the given date is returned unchanged.
188
189 The rest of the input parameters, if any, are simply copied to the
190 output.
191
192 The typical use of these filter subroutines is in a "return" statement
193 at the end of callback functions of your own, when you already have
194 calculated the holiday in question and only need to adjust it according
195 to the rule implemented by the filter subroutine in question.
196
197 See also the implementation of the Date::Calendar::Profiles module for
198 examples of how to use these functions.
199
201 - Fixed dates:
202
203 "Christmas" => "24.12", # European format (day, month)
204 "Christmas" => "24.12.",
205
206 "Christmas" => "24Dec",
207 "Christmas" => "24.Dec",
208 "Christmas" => "24Dec.",
209 "Christmas" => "24.Dec.",
210
211 "Christmas" => "24-12",
212 "Christmas" => "24-12-",
213
214 "Christmas" => "24-Dec",
215 "Christmas" => "24-Dec-",
216
217 "Christmas" => "12/25", # American format (month, day)
218 "Christmas" => "Dec25",
219 "Christmas" => "Dec/25",
220
221 - Dates relative to Easter Sunday:
222
223 "Ladies' Carnival" => "-52",
224 "Carnival Monday" => "-48",
225 "Mardi Gras" => "-47",
226 "Ash Wednesday" => "-46",
227 "Palm Sunday" => "-7",
228 "Maundy Thursday" => "-3",
229 "Good Friday" => "-2",
230 "Easter Sunday" => "+0",
231 "Easter Monday" => "+1",
232 "Ascension" => "+39",
233 "Whitsunday" => "+49",
234 "Whitmonday" => "+50",
235 "Corpus Christi" => "+60",
236
237 - The 1st, 2nd, 3rd, 4th or last day of week:
238
239 "Thanksgiving" => "4Thu11",
240 "Thanksgiving" => "4/Thu/Nov",
241 "Columbus Day" => "2/Mon/Oct",
242 "Columbus Day" => "2/Mon/10",
243 "Columbus Day" => "2/1/Oct",
244 "Columbus Day" => "2/1/10",
245 "Memorial Day" => "5/Mon/May", # LAST Monday of May
246
247 - Half holidays, commemorative days:
248
249 "Christmas" => ":24.12.", # only half a day off
250 "Valentine's Day" => "#Feb/14", # not an official holiday
251
253 The interface of the callback functions to use with the "init()" method
254 of the Date::Calendar::Year(3) module is very simple:
255
256 The callback function receives two arguments when called, first the
257 year number for which the holiday is to be calculated, and second the
258 name (the "label") of the holiday in question (which serves as key in
259 the hash of a holiday scheme).
260
261 This second parameter allows you to use the same callback function for
262 different holidays, which might be more practical (than separate
263 callback functions) if for instance you have a set of similar holidays
264 to calculate, like for instance the four Sundays before Christmas
265 ("Advent").
266
267 The callback function "Advent()" (exported by the
268 Date::Calendar::Profiles module) exemplifies this technique.
269
270 The callback function is expected to return a list
271 ""($year,$month,$day)"" with the exact date of the holiday (the year
272 number in the output must of course match the year number passed as
273 parameter).
274
275 A fatal error occurs if the returned list does not constitute a valid
276 date, in the requested year.
277
278 Optionally, the callback function may return a fourth value (after the
279 date) containing a string, which may be either "#" or ":".
280
281 The string "#" signifies that the date in question is a purely
282 commemorative date, i.e., that you don't get a day off from work on
283 that day.
284
285 The string ":" means that the date in question is a "half" holiday,
286 i.e., a day on which you get half a day off from work.
287
288 In case the holiday in question was not observed or did not exist in
289 the requested year, the callback function may also return an empty
290 list. This will cause the "init()" method to simply drop this holiday
291 for that year.
292
293 The module Date::Calendar::Profiles exports the sample callback
294 functions "Advent1()", "Advent2()", "Advent3()", "Advent4()" and
295 "Advent()", which might assist you in rolling your own profiles.
296
298 Every calendar profile (holiday scheme) is a hash.
299
300 The name of the holiday (like "Christmas", for instance) serves as the
301 key in this hash and must therefore be unique (unless you want to
302 override a default which was set previously, but see below for more on
303 this).
304
305 The value for each key is either a string, which specifies a simple
306 date formula, or the reference of a callback function.
307
308 See the section "CALLBACK INTERFACE" above for a description of the
309 interface (in and out) of these callback functions.
310
311 See the section "DATE FORMULA SYNTAX" above and the description of the
312 "init()" method in Date::Calendar::Year(3) for the exact syntax of date
313 formula strings.
314
315 BEWARE that if keys are not unique in the source code, later entries
316 will overwrite previous ones! I.e.,
317
318 ...
319 "My special holiday" => "01-11",
320 "My special holiday" => "02-11",
321 ...
322
323 will NOT set two holidays of the same name, one on November first, the
324 other on November second, but only one, on November second!
325
326 Therefore, in order to use sets of defaults and to be able to override
327 some of them, you must FIRST include any hash containing the default
328 definitions, and THEN write down your own definitions (see also the
329 Date::Calendar::Profiles module for examples of this!), like this:
330
331 $defaults =
332 {
333 "Holiday #1" => "01-01",
334 "Holiday #2" => "02-02",
335 "Holiday #3" => "03-03"
336 };
337
338 $variant1 =
339 {
340 %$defaults,
341 "Holiday #2" => "09-02",
342 "Holiday #4" => "04-04"
343 };
344
345 This is because of the way hashes work in Perl.
346
347 Now let's suppose that you want to write a profile containing all your
348 relatives' and friends' birthdays or anniversaries.
349
350 Simply go ahead and list them in your program, in any order you like,
351 as follows (for example):
352
353 $Birthdays =
354 {
355 "Spouse 1971" => "30.12.",
356 "Wedding Day 1992" => "01.09.",
357 "Valentine's Day" => "14.02.",
358 "Son Richard 1996" => "11.05.",
359 "Daughter Irene 1994" => "17.01.",
360 "Mom 1939" => "19.08.",
361 "Dad 1937" => "23.04.",
362 "Brother Timothy 1969" => "24.04.",
363 "Sister Catherine 1973" => "21.10.",
364 "Cousin Paul 1970" => "16.10.",
365 "Aunt Marjorie 1944" => "09.06.",
366 "Uncle George 1941" => "02.08.",
367 "Friend Alexander 1968" => "12.06.",
368 };
369
370 The year numbers after the names are not really necessary, but they
371 allow us to display the person's current age. If this year number is
372 omitted, we simply don't display the age.
373
374 Now in order to query this birthday database, we can use the following
375 little program:
376
377 #!perl -w
378
379 use strict;
380 no strict "vars";
381 use Date::Calc qw(:all);
382 use Date::Calendar;
383
384 $Birthdays =
385 {
386 ... # (see above)
387 };
388
389 @today = Today();
390 $calendar = Date::Calendar->new( $Birthdays );
391 $calendar->year( $today[0] );
392
393 foreach $key (@ARGV)
394 {
395 if (@list = $calendar->search( $key ))
396 {
397 foreach $date (@list)
398 {
399 @labels = $calendar->labels( $date );
400 $dow = shift(@labels);
401 # More than one person might have birthday on the same date:
402 $name = $key;
403 foreach $person (@labels)
404 {
405 if (index(lc($person),lc($key)) >= 0)
406 {
407 $name = $person;
408 last;
409 }
410 }
411 $delta = Delta_Days(@today, $date->date());
412 $age = '';
413 if ($name =~ s!\s*(\d+)\s*$!!)
414 {
415 $age = $today[0] - $1;
416 $age-- if ($delta > 0);
417 $age = sprintf(" (%2d years old)", $age);
418 }
419 printf
420 (
421 "%-20.20s: %+5d days => %3.3s %2d-%3.3s-%4d%s\n",
422 $name,
423 $delta,
424 $dow,
425 $date->day(),
426 Month_to_Text($date->month()),
427 $date->year(),
428 $age
429 );
430 }
431 }
432 else { print "No entry found in birthday list for '$key'!\n" }
433 }
434
435 __END__
436
437 Let us save this program as, say, "birthday.pl".
438
439 Then we can query this birthday database by providing search strings on
440 the command line, like this (note that this is a (case-insensitive)
441 substring search, NOT a regular expression match!):
442
443 > date
444 Wed Oct 3 18:05:45 CEST 2001
445
446 > perl birthday.pl wed spo
447 Wedding Day : -32 days => Sat 1-Sep-2001 ( 9 years old)
448 Spouse : +88 days => Sun 30-Dec-2001 (29 years old)
449
450 > perl birthday.pl son daug
451 Son Richard : -145 days => Fri 11-May-2001 ( 5 years old)
452 Daughter Irene : -259 days => Wed 17-Jan-2001 ( 7 years old)
453
454 > perl birthday.pl broth sist
455 Brother Timothy : -162 days => Tue 24-Apr-2001 (32 years old)
456 Sister Catherine : +18 days => Sun 21-Oct-2001 (27 years old)
457
458 > perl birthday.pl mom dad
459 Mom : -45 days => Sun 19-Aug-2001 (62 years old)
460 Dad : -163 days => Mon 23-Apr-2001 (64 years old)
461
462 > perl birthday.pl uncl aunt
463 Uncle George : -62 days => Thu 2-Aug-2001 (60 years old)
464 Aunt Marjorie : -116 days => Sat 9-Jun-2001 (57 years old)
465
466 > perl birthday.pl alex
467 Friend Alexander : -113 days => Tue 12-Jun-2001 (33 years old)
468
469 In order to get the whole list, we can supply a substring which is
470 contained in every name, which happens to be a blank (" "):
471
472 > perl birthday.pl ' '
473 Daughter Irene : -259 days => Wed 17-Jan-2001 ( 7 years old)
474 Valentine's Day : -231 days => Wed 14-Feb-2001
475 Dad : -163 days => Mon 23-Apr-2001 (64 years old)
476 Brother Timothy : -162 days => Tue 24-Apr-2001 (32 years old)
477 Son Richard : -145 days => Fri 11-May-2001 ( 5 years old)
478 Aunt Marjorie : -116 days => Sat 9-Jun-2001 (57 years old)
479 Friend Alexander : -113 days => Tue 12-Jun-2001 (33 years old)
480 Uncle George : -62 days => Thu 2-Aug-2001 (60 years old)
481 Mom : -45 days => Sun 19-Aug-2001 (62 years old)
482 Wedding Day : -32 days => Sat 1-Sep-2001 ( 9 years old)
483 Cousin Paul : +13 days => Tue 16-Oct-2001 (30 years old)
484 Sister Catherine : +18 days => Sun 21-Oct-2001 (27 years old)
485 Spouse : +88 days => Sun 30-Dec-2001 (29 years old)
486
487 By the way, a similar program is included in the "examples"
488 subdirectory of the Date::Calc distribution, called "anniversaries.pl".
489
490 See also the file "EXAMPLES.txt" in the distribution's main directory
491 for a short description of that little script.
492
494 Date::Calendar(3), Date::Calendar::Year(3), Date::Calc::Object(3),
495 Date::Calc(3), Date::Calc::Util(3).
496
498 The calendar profiles included in this module usually do not take
499 historical irregularities into account (even though some do in order to
500 show how this can be done), they only provide means for calculating
501 regularly recurring events (the profiles should therefore not be relied
502 upon for historical faithfulness).
503
505 The australian calendar profiles are known to contain wrong dates.
506 This is due to the fact that Australia decrees its holidays
507 individually for each year, difficulting the calculation of the
508 holidays by way of a formula. An effort to compare (and to correct) the
509 current implementation with official documents (web pages) by the
510 Australian authorities is under way. This hasn't been finished yet
511 because it is very time-consuming.
512
514 This man page documents "Date::Calendar::Profiles" version 6.4.
515
517 Steffen Beyer
518 mailto:STBEY@cpan.org
519 http://www.engelschall.com/u/sb/download/
520
522 Copyright (c) 2000 - 2015 by Steffen Beyer. All rights reserved.
523
525 This package is free software; you can use, modify and redistribute it
526 under the same terms as Perl itself, i.e., at your option, under the
527 terms either of the "Artistic License" or the "GNU General Public
528 License".
529
530 The C library at the core of the module "Date::Calc::XS" can, at your
531 discretion, also be used, modified and redistributed under the terms of
532 the "GNU Library General Public License".
533
534 Please refer to the files "Artistic.txt", "GNU_GPL.txt" and
535 "GNU_LGPL.txt" in the "license" subdirectory of this distribution for
536 any details!
537
539 This package is distributed in the hope that it will be useful, but
540 WITHOUT ANY WARRANTY; without even the implied warranty of
541 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
542
543 See the "GNU General Public License" for more details.
544
545
546
547perl v5.34.0 2021-07-22 Date::Calendar::Profiles(3)