home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _49370a1d11ea126b4d66c5ff9c20b13c < prev    next >
Text File  |  2000-03-23  |  77KB  |  1,802 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Date::Calc - Gregorian calendar date calculations</TITLE>
  5. <LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> Date::Calc - Gregorian calendar date calculations</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#preface">PREFACE</A></LI>
  24.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  25.     <LI><A HREF="#important notes">IMPORTANT NOTES</A></LI>
  26.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  27.     <LI><A HREF="#recipes">RECIPES</A></LI>
  28.     <LI><A HREF="#see also">SEE ALSO</A></LI>
  29.     <LI><A HREF="#limitations">LIMITATIONS</A></LI>
  30.     <LI><A HREF="#version">VERSION</A></LI>
  31.     <LI><A HREF="#author">AUTHOR</A></LI>
  32.     <LI><A HREF="#copyright">COPYRIGHT</A></LI>
  33.     <LI><A HREF="#license">LICENSE</A></LI>
  34.     <LI><A HREF="#disclaimer">DISCLAIMER</A></LI>
  35. </UL>
  36. <!-- INDEX END -->
  37.  
  38. <HR>
  39. <P>
  40. <H1><A NAME="name">NAME</A></H1>
  41. <P>Date::Calc - Gregorian calendar date calculations</P>
  42. <P>
  43. <HR>
  44. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  45. <UL>
  46. <LI>Linux</LI>
  47. <LI>Solaris</LI>
  48. <LI>Windows</LI>
  49. </UL>
  50. <HR>
  51. <H1><A NAME="preface">PREFACE</A></H1>
  52. <P>This package consists of a C library and a Perl module (which uses
  53. the C library, internally) for all kinds of date calculations based
  54. on the Gregorian calendar (the one used in all western countries today),
  55. thereby complying with all relevant norms and standards: ISO/R 2015-1971,
  56. DIN 1355 and, to some extent, ISO 8601 (where applicable).</P>
  57. <P>(See also <A HREF="http://www.engelschall.com/u/sb/download/Date-Calc/DIN1355/">http://www.engelschall.com/u/sb/download/Date-Calc/DIN1355/</A>
  58. for a scan of part of the ``DIN 1355'' document (in German)).</P>
  59. <P>The module of course handles year numbers of 2000 and above correctly
  60. (``Year 2000'' or ``Y2K'' compliance) -- actually all year numbers from 1
  61. to the largest positive integer representable on your system (which
  62. is at least 32767) can be dealt with.</P>
  63. <P>Note that this package projects the Gregorian calendar back until the
  64. year 1 A.D. -- even though the Gregorian calendar was only adopted
  65. in 1582 by most (not all) European countries, in obedience to the
  66. corresponding decree of catholic pope Gregor I in that year.</P>
  67. <P>Some (mainly protestant) countries continued to use the Julian calendar
  68. (used until then) until as late as the beginning of the 20th century.</P>
  69. <P>Finally, note that this package is not intended to do everything you could
  70. ever imagine automagically for you; it is rather intended to serve as a
  71. toolbox (in the best of UNIX spirit and traditions) which should, however,
  72. always get you where you want to go.</P>
  73. <P>See the section ``RECIPES'' at the bottom of this document for solutions
  74. to common problems!</P>
  75. <P>If nevertheless you can't figure out how to solve a particular problem,
  76. please let me know! (See e-mail address at the end of this document.)</P>
  77. <P>
  78. <HR>
  79. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  80. <PRE>
  81.   use Date::Calc qw(
  82.       Days_in_Year
  83.       Days_in_Month
  84.       Weeks_in_Year
  85.       leap_year
  86.       check_date
  87.       check_business_date
  88.       Day_of_Year
  89.       Date_to_Days
  90.       Day_of_Week
  91.       Week_Number
  92.       Week_of_Year
  93.       Monday_of_Week
  94.       Nth_Weekday_of_Month_Year
  95.       Standard_to_Business
  96.       Business_to_Standard
  97.       Delta_Days
  98.       Delta_DHMS
  99.       Add_Delta_Days
  100.       Add_Delta_DHMS
  101.       Add_Delta_YMD
  102.       System_Clock
  103.       Today
  104.       Now
  105.       Today_and_Now
  106.       Easter_Sunday
  107.       Decode_Month
  108.       Decode_Day_of_Week
  109.       Decode_Language
  110.       Decode_Date_EU
  111.       Decode_Date_US
  112.       Compress
  113.       Uncompress
  114.       check_compressed
  115.       Compressed_to_Text
  116.       Date_to_Text
  117.       Date_to_Text_Long
  118.       English_Ordinal
  119.       Calendar
  120.       Month_to_Text
  121.       Day_of_Week_to_Text
  122.       Day_of_Week_Abbreviation
  123.       Language_to_Text
  124.       Language
  125.       Languages
  126.       Decode_Date_EU2
  127.       Decode_Date_US2
  128.       Parse_Date
  129.   );</PRE>
  130. <PRE>
  131.   use Date::Calc qw(:all);</PRE>
  132. <PRE>
  133.   Days_in_Year
  134.       $days = Days_in_Year($year,$month);</PRE>
  135. <PRE>
  136.   Days_in_Month
  137.       $days = Days_in_Month($year,$month);</PRE>
  138. <PRE>
  139.   Weeks_in_Year
  140.       $weeks = Weeks_in_Year($year);</PRE>
  141. <PRE>
  142.   leap_year
  143.       if (leap_year($year))</PRE>
  144. <PRE>
  145.   check_date
  146.       if (check_date($year,$month,$day))</PRE>
  147. <PRE>
  148.   check_business_date
  149.       if (check_business_date($year,$week,$dow))</PRE>
  150. <PRE>
  151.   Day_of_Year
  152.       $doy = Day_of_Year($year,$month,$day);</PRE>
  153. <PRE>
  154.   Date_to_Days
  155.       $days = Date_to_Days($year,$month,$day);</PRE>
  156. <PRE>
  157.   Day_of_Week
  158.       $dow = Day_of_Week($year,$month,$day);</PRE>
  159. <PRE>
  160.   Week_Number
  161.       $week = Week_Number($year,$month,$day);</PRE>
  162. <PRE>
  163.   Week_of_Year
  164.       ($week,$year) = Week_of_Year($year,$month,$day);</PRE>
  165. <PRE>
  166.   Monday_of_Week
  167.       ($year,$month,$day) = Monday_of_Week($week,$year);</PRE>
  168. <PRE>
  169.   Nth_Weekday_of_Month_Year
  170.       if (($year,$month,$day) =
  171.       Nth_Weekday_of_Month_Year($year,$month,$dow,$n))</PRE>
  172. <PRE>
  173.   Standard_to_Business
  174.       ($year,$week,$dow) =
  175.       Standard_to_Business($year,$month,$day);</PRE>
  176. <PRE>
  177.   Business_to_Standard
  178.       ($year,$month,$day) =
  179.       Business_to_Standard($year,$week,$dow);</PRE>
  180. <PRE>
  181.   Delta_Days
  182.       $Dd = Delta_Days($year1,$month1,$day1,
  183.                        $year2,$month2,$day2);</PRE>
  184. <PRE>
  185.   Delta_DHMS
  186.       ($Dd,$Dh,$Dm,$Ds) =
  187.       Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1,
  188.                  $year2,$month2,$day2, $hour2,$min2,$sec2);</PRE>
  189. <PRE>
  190.   Add_Delta_Days
  191.       ($year,$month,$day) =
  192.       Add_Delta_Days($year,$month,$day,
  193.                      $Dd);</PRE>
  194. <PRE>
  195.   Add_Delta_DHMS
  196.       ($year,$month,$day, $hour,$min,$sec) =
  197.       Add_Delta_DHMS($year,$month,$day, $hour,$min,$sec,
  198.                      $Dd,$Dh,$Dm,$Ds);</PRE>
  199. <PRE>
  200.   Add_Delta_YMD
  201.       ($year,$month,$day) =
  202.       Add_Delta_YMD($year,$month,$day,
  203.                     $Dy,$Dm,$Dd);</PRE>
  204. <PRE>
  205.   System_Clock
  206.       ($year,$month,$day, $hour,$min,$sec, $doy,$dow,$dst) =
  207.       System_Clock();</PRE>
  208. <PRE>
  209.   Today
  210.       ($year,$month,$day) = Today();</PRE>
  211. <PRE>
  212.   Now
  213.       ($hour,$min,$sec) = Now();</PRE>
  214. <PRE>
  215.   Today_and_Now
  216.       ($year,$month,$day, $hour,$min,$sec) = Today_and_Now();</PRE>
  217. <PRE>
  218.   Easter_Sunday
  219.       ($year,$month,$day) = Easter_Sunday($year);</PRE>
  220. <PRE>
  221.   Decode_Month
  222.       if ($month = Decode_Month($string))</PRE>
  223. <PRE>
  224.   Decode_Day_of_Week
  225.       if ($dow = Decode_Day_of_Week($string))</PRE>
  226. <PRE>
  227.   Decode_Language
  228.       if ($lang = Decode_Language($string))</PRE>
  229. <PRE>
  230.   Decode_Date_EU
  231.       if (($year,$month,$day) = Decode_Date_EU($string))</PRE>
  232. <PRE>
  233.   Decode_Date_US
  234.       if (($year,$month,$day) = Decode_Date_US($string))</PRE>
  235. <PRE>
  236.   Compress
  237.       $date = Compress($year,$month,$day);</PRE>
  238. <PRE>
  239.   Uncompress
  240.       if (($century,$year,$month,$day) = Uncompress($date))</PRE>
  241. <PRE>
  242.   check_compressed
  243.       if (check_compressed($date))</PRE>
  244. <PRE>
  245.   Compressed_to_Text
  246.       $string = Compressed_to_Text($date);</PRE>
  247. <PRE>
  248.   Date_to_Text
  249.       $string = Date_to_Text($year,$month,$day);</PRE>
  250. <PRE>
  251.   Date_to_Text_Long
  252.       $string = Date_to_Text_Long($year,$month,$day);</PRE>
  253. <PRE>
  254.   English_Ordinal
  255.       $string = English_Ordinal($number);</PRE>
  256. <PRE>
  257.   Calendar
  258.       $string = Calendar($year,$month);</PRE>
  259. <PRE>
  260.   Month_to_Text
  261.       $string = Month_to_Text($month);</PRE>
  262. <PRE>
  263.   Day_of_Week_to_Text
  264.       $string = Day_of_Week_to_Text($dow);</PRE>
  265. <PRE>
  266.   Day_of_Week_Abbreviation
  267.       $string = Day_of_Week_Abbreviation($dow);</PRE>
  268. <PRE>
  269.   Language_to_Text
  270.       $string = Language_to_Text($lang);</PRE>
  271. <PRE>
  272.   Language
  273.       $lang = Language();
  274.       Language($lang);
  275.       $oldlang = Language($newlang);</PRE>
  276. <PRE>
  277.   Languages
  278.       $max_lang = Languages();</PRE>
  279. <PRE>
  280.   Decode_Date_EU2
  281.       if (($year,$month,$day) = Decode_Date_EU2($string))</PRE>
  282. <PRE>
  283.   Decode_Date_US2
  284.       if (($year,$month,$day) = Decode_Date_US2($string))</PRE>
  285. <PRE>
  286.   Parse_Date
  287.       if (($year,$month,$day) = Parse_Date($string))</PRE>
  288. <PRE>
  289.   Version
  290.       $string = Date::Calc::Version();</PRE>
  291. <P>
  292. <HR>
  293. <H1><A NAME="important notes">IMPORTANT NOTES</A></H1>
  294. <P>(See the section ``RECIPES'' at the bottom of this document for
  295. solutions to common problems!)</P>
  296. <UL>
  297. <LI>
  298. ``Year 2000'' (``Y2K'') compliance
  299. <P>The upper limit for any year number in this module is only given
  300. by the size of the largest positive integer that can be represented
  301. in a variable of the C type ``int'' on your system, which is at least
  302. 32767, according to the ANSI C standard (exceptions see below).</P>
  303. <P>In order to simplify calculations, this module projects the gregorian
  304. calendar back until the year 1 A.D. -- i.e., back <STRONG>BEYOND</STRONG> the
  305. year 1582 when this calendar was first decreed by the catholic pope
  306. Gregor I!</P>
  307. <P>Therefore, <STRONG>BE SURE TO ALWAYS SPECIFY ``1998'' WHEN YOU MEAN ``1998''</STRONG>,
  308. for instance, and <STRONG>DO NOT WRITE ``98'' INSTEAD</STRONG>, because this will
  309. in fact perform a calculation based on the year ``98'' A.D. and
  310. <STRONG>NOT</STRONG> ``1998''!</P>
  311. <P>The only exceptions from this rule are the functions which contain
  312. the word ``compress'' in their names (which only handle years between
  313. 1970 and 2069 and also accept the abbreviations ``00'' to ``99''), and
  314. the functions whose names begin with ``Decode_Date_'' (which map year
  315. numbers below 100 to the range 1970 - 2069, using a technique known
  316. as ``windowing'').</P>
  317. <P></P>
  318. <LI>
  319. First index
  320. <P><STRONG>ALL</STRONG> ranges in this module start with ``<CODE>1</CODE>'', <STRONG>NOT</STRONG> ``<CODE>0</CODE>''!</P>
  321. <P>I.e., the day of month, day of week, day of year, month of year,
  322. week of year, first valid year number and language <STRONG>ALL</STRONG> start
  323. counting at one, <STRONG>NOT</STRONG> zero!</P>
  324. <P>The only exception is the function ``<CODE>Week_Number()</CODE>'', which may
  325. in fact return ``<CODE>0</CODE>'' when the given date actually lies in the
  326. last week of the <STRONG>PREVIOUS</STRONG> year.</P>
  327. <P></P>
  328. <LI>
  329. Function naming conventions
  330. <P>Function names completely in lower case indicate a boolean return value.</P>
  331. <P></P>
  332. <LI>
  333. Boolean values
  334. <P>Boolean values in this module are always a numeric zero (``<CODE>0</CODE>'') for
  335. ``false'' and a numeric one (``<CODE>1</CODE>'') for ``true''.</P>
  336. <P></P>
  337. <LI>
  338. Exception handling
  339. <P>The functions in this module will usually die with a corresponding error
  340. message if their input parameters, intermediate results or output values
  341. are out of range.</P>
  342. <P>The following functions handle errors differently:</P>
  343. <PRE>
  344.   -  check_date()
  345.   -  check_business_date()
  346.   -  check_compressed()</PRE>
  347. <P>(which return a ``false'' return value when the given input does not represent
  348. a valid date),</P>
  349. <PRE>
  350.   -  Nth_Weekday_of_Month_Year()</PRE>
  351. <P>(which returns an empty list if the requested 5th day of week does not exist),</P>
  352. <PRE>
  353.   -  Decode_Month()
  354.   -  Decode_Day_of_Week()
  355.   -  Decode_Language()
  356.   -  Compress()</PRE>
  357. <P>(which return ``<CODE>0</CODE>'' upon failure or invalid input), and</P>
  358. <PRE>
  359.   -  Decode_Date_EU()
  360.   -  Decode_Date_US()
  361.   -  Decode_Date_EU2()
  362.   -  Decode_Date_US2()
  363.   -  Parse_Date()
  364.   -  Uncompress()</PRE>
  365. <P>(which return an empty list upon failure or invalid input).</P>
  366. <P>Note that you can always catch an exception thrown by any of the functions
  367. in this module and handle it yourself by enclosing the function call in an
  368. ``<A HREF="../../../lib/Pod/perlfunc.html#item_eval"><CODE>eval</CODE></A>'' with curly brackets and checking the special variable ``<CODE>$@</CODE>''
  369. (see <EM>perlfunc(1)/eval</EM> for details).</P>
  370. <P></P></UL>
  371. <P>
  372. <HR>
  373. <H1><A NAME="description">DESCRIPTION</A></H1>
  374. <UL>
  375. <LI>
  376. <A HREF="../../../lib/Pod/perlfunc.html#item_qw"><CODE>use Date::Calc qw( Days_in_Year Days_in_Month ... );</CODE></A>
  377. <P></P>
  378. <LI>
  379. <A HREF="../../../lib/Pod/perlfunc.html#item_qw"><CODE>use Date::Calc qw(:all);</CODE></A>
  380. <P>You can either specify the functions you want to import explicitly by
  381. enumerating them between the parentheses of the ``<A HREF="../../../lib/Pod/perlfunc.html#item_qw"><CODE>qw()</CODE></A>'' operator, or
  382. you can use the ``<CODE>:all</CODE>'' tag instead to import <STRONG>ALL</STRONG> available functions.</P>
  383. <P></P>
  384. <LI>
  385. <CODE>$days = Days_in_Year($year,$month);</CODE>
  386. <P>This function returns the sum of the number of days in the months starting
  387. with January up to and including ``<CODE>$month</CODE>'' in the given year ``<CODE>$year</CODE>''.</P>
  388. <P>I.e., ``<CODE>Days_in_Year(1998,1)</CODE>'' returns ``<CODE>31</CODE>'', ``<CODE>Days_in_Year(1998,2)</CODE>''
  389. returns ``<CODE>59</CODE>'', ``<CODE>Days_in_Year(1998,3)</CODE>'' returns ``<CODE>90</CODE>'', and so on.</P>
  390. <P>Note that ``<CODE>Days_in_Year($year,12)</CODE>'' returns the number of days in the
  391. given year ``<CODE>$year</CODE>'', i.e., either ``<CODE>365</CODE>'' or ``<CODE>366</CODE>''.</P>
  392. <P></P>
  393. <LI>
  394. <CODE>$days = Days_in_Month($year,$month);</CODE>
  395. <P>This function returns the number of days in the given month ``<CODE>$month</CODE>'' of
  396. the given year ``<CODE>$year</CODE>''.</P>
  397. <P>The year must always be supplied, even though it is only needed when the
  398. month is February, in order to determine wether it is a leap year or not.</P>
  399. <P>I.e., ``<CODE>Days_in_Month(1998,1)</CODE>'' returns ``<CODE>31</CODE>'', ``<CODE>Days_in_Month(1998,2)</CODE>''
  400. returns ``<CODE>28</CODE>'', ``<CODE>Days_in_Month(2000,2)</CODE>'' returns ``<CODE>29</CODE>'',
  401. ``<CODE>Days_in_Month(1998,3)</CODE>'' returns ``<CODE>31</CODE>'', and so on.</P>
  402. <P></P>
  403. <LI>
  404. <CODE>$weeks = Weeks_in_Year($year);</CODE>
  405. <P>This function returns the number of weeks in the given year ``<CODE>$year</CODE>'',
  406. i.e., either ``<CODE>52</CODE>'' or ``<CODE>53</CODE>''.</P>
  407. <P></P>
  408. <LI>
  409. <CODE>if (leap_year($year))</CODE>
  410. <P>This function returns ``true'' (``<CODE>1</CODE>'') if the given year ``<CODE>$year</CODE>'' is
  411. a leap year and ``false'' (``<CODE>0</CODE>'') otherwise.</P>
  412. <P></P>
  413. <LI>
  414. <CODE>if (check_date($year,$month,$day))</CODE>
  415. <P>This function returns ``true'' (``<CODE>1</CODE>'') if the given three numerical
  416. values ``<CODE>$year</CODE>'', ``<CODE>$month</CODE>'' and ``<CODE>$day</CODE>'' constitute a valid date,
  417. and ``false'' (``<CODE>0</CODE>'') otherwise.</P>
  418. <P></P>
  419. <LI>
  420. <CODE>if (check_business_date($year,$week,$dow))</CODE>
  421. <P>This function returns ``true'' (``<CODE>1</CODE>'') if the given three numerical
  422. values ``<CODE>$year</CODE>'', ``<CODE>$week</CODE>'' and ``<CODE>$dow</CODE>'' constitute a valid date
  423. in business format, and ``false'' (``<CODE>0</CODE>'') otherwise.</P>
  424. <P><STRONG>Beware</STRONG> that this function does <STRONG>NOT</STRONG> compute wether a given date
  425. is a business day (i.e., Monday to Friday)!</P>
  426. <P>To do so, use ``<CODE>(Day_of_Week($year,$month,$day) < 6)</CODE>'' instead.</P>
  427. <P></P>
  428. <LI>
  429. <CODE>$doy = Day_of_Year($year,$month,$day);</CODE>
  430. <P>This function returns the (relative) number of the day of the given date
  431. in the given year.</P>
  432. <P>E.g., ``<CODE>Day_of_Year($year,1,1)</CODE>'' returns ``<CODE>1</CODE>'',
  433. ``<CODE>Day_of_Year($year,2,1)</CODE>'' returns ``<CODE>32</CODE>'', and
  434. ``<CODE>Day_of_Year($year,12,31)</CODE>'' returns either ``<CODE>365</CODE>'' or ``<CODE>366</CODE>''.</P>
  435. <P></P>
  436. <LI>
  437. <CODE>$days = Date_to_Days($year,$month,$day);</CODE>
  438. <P>This function returns the (absolute) number of the day of the given date,
  439. where counting starts at the 1st of January of the year 1 A.D.</P>
  440. <P>I.e., ``<CODE>Date_to_Days(1,1,1)</CODE>'' returns ``<CODE>1</CODE>'', ``<CODE>Date_to_Days(1,12,31)</CODE>''
  441. returns ``<CODE>365</CODE>'', ``<CODE>Date_to_Days(2,1,1)</CODE>'' returns ``<CODE>366</CODE>'',
  442. ``<CODE>Date_to_Days(1998,5,1)</CODE>'' returns ``<CODE>729510</CODE>'', and so on.</P>
  443. <P></P>
  444. <LI>
  445. <CODE>$dow = Day_of_Week($year,$month,$day);</CODE>
  446. <P>This function returns the number of the day of week of the given date.</P>
  447. <P>The function returns ``<CODE>1</CODE>'' for Monday, ``<CODE>2</CODE>'' for Tuesday and so on
  448. until ``<CODE>7</CODE>'' for Sunday.</P>
  449. <P>Note that in the Hebrew calendar (on which the Christian calendar is based),
  450. the week starts with Sunday and ends with the Sabbath or Saturday (where
  451. according to the Genesis (as described in the Bible) the Lord rested from
  452. creating the world).</P>
  453. <P>In medieval times, catholic popes have decreed the Sunday to be the official
  454. day of rest, in order to dissociate the Christian from the Hebrew belief.</P>
  455. <P>Nowadays, the Sunday <STRONG>AND</STRONG> the Saturday are commonly considered (and
  456. used as) days of rest, usually referred to as the ``week-end''.</P>
  457. <P>Consistent with this practice, current norms and standards (such as
  458. ISO/R 2015-1971, DIN 1355 and ISO 8601) define the Monday
  459. as the first day of the week.</P>
  460. <P></P>
  461. <LI>
  462. <CODE>$week = Week_Number($year,$month,$day);</CODE>
  463. <P>This function returns the number of the week the given date lies in.</P>
  464. <P>If the given date lies in the <STRONG>LAST</STRONG> week of the <STRONG>PREVIOUS</STRONG> year,
  465. ``<CODE>0</CODE>'' is returned.</P>
  466. <P>If the given date lies in the <STRONG>FIRST</STRONG> week of the <STRONG>NEXT</STRONG> year,
  467. ``<CODE>Weeks_in_Year($year) + 1</CODE>'' is returned.</P>
  468. <P></P>
  469. <LI>
  470. <CODE>($week,$year) = Week_of_Year($year,$month,$day);</CODE>
  471. <P>This function returns the number of the week the given date lies in,
  472. as well as the year that week belongs to.</P>
  473. <P>I.e., if the given date lies in the <STRONG>LAST</STRONG> week of the <STRONG>PREVIOUS</STRONG> year,
  474. ``<CODE>(Weeks_in_Year($year-1), $year-1)</CODE>'' is returned.</P>
  475. <P>If the given date lies in the <STRONG>FIRST</STRONG> week of the <STRONG>NEXT</STRONG> year,
  476. ``<CODE>(1, $year+1)</CODE>'' is returned.</P>
  477. <P>Otherwise, ``<CODE>(Week_Number($year,$month,$day), $year)</CODE>'' is returned.</P>
  478. <P></P>
  479. <LI>
  480. <CODE>($year,$month,$day) = Monday_of_Week($week,$year);</CODE>
  481. <P>This function returns the date of the first day of the given week, i.e.,
  482. the Monday.</P>
  483. <P>``<CODE>$year</CODE>'' must be greater than or equal to ``<CODE>1</CODE>'', and ``<CODE>$week</CODE>'' must
  484. lie in the range ``<CODE>1</CODE>'' to ``<CODE>Weeks_in_Year($year)</CODE>''.</P>
  485. <P>Note that you can write
  486. ``<CODE>($year,$month,$day) = Monday_of_Week(Week_of_Year($year,$month,$day));</CODE>''
  487. in order to calculate the date of the Monday of the same week as the
  488. given date.</P>
  489. <P></P>
  490. <LI>
  491. <CODE>if (($year,$month,$day) = Nth_Weekday_of_Month_Year($year,$month,$dow,$n))</CODE>
  492. <P>This function calculates the date of the ``<CODE>$n</CODE>''th day of week ``<CODE>$dow</CODE>''
  493. in the given month ``<CODE>$month</CODE>'' and year ``<CODE>$year</CODE>''; such as, for example,
  494. the 3rd Thursday of a given month and year.</P>
  495. <P>This can be used to send a notification mail to the members of a group
  496. which meets regularly on every 3rd Thursday of a month, for instance.</P>
  497. <P>(See the section ``RECIPES'' near the end of this document for a code
  498. snippet to actually do so.)</P>
  499. <P>``<CODE>$year</CODE>'' must be greater than or equal to ``<CODE>1</CODE>'', ``<CODE>$month</CODE>'' must lie
  500. in the range ``<CODE>1</CODE>'' to ``<CODE>12</CODE>'', ``<CODE>$dow</CODE>'' must lie in the range ``<CODE>1</CODE>''
  501. to ``<CODE>7</CODE>'' and ``<CODE>$n</CODE>'' must lie in the range ``<CODE>1</CODE>'' to ``<CODE>5</CODE>'', or a fatal
  502. error (with appropriate error message) occurs.</P>
  503. <P>The function returns an empty list when the 5th of a given day of week
  504. does not exist in the given month and year.</P>
  505. <P></P>
  506. <LI>
  507. <CODE>($year,$week,$dow) = Standard_to_Business($year,$month,$day);</CODE>
  508. <P>This function converts a given date from standard notation (year,
  509. month, day (of month)) to business notation (year, week, day of week).</P>
  510. <P></P>
  511. <LI>
  512. <CODE>($year,$month,$day) = Business_to_Standard($year,$week,$dow);</CODE>
  513. <P>This function converts a given date from business notation (year,
  514. week, day of week) to standard notation (year, month, day (of month)).</P>
  515. <P></P>
  516. <LI>
  517. <CODE>$Dd = Delta_Days($year1,$month1,$day1, $year2,$month2,$day2);</CODE>
  518. <P>This function returns the difference in days between the two given
  519. dates.</P>
  520. <P>The result is positive if the two dates are in chronological order,
  521. i.e., if date #1 comes chronologically <STRONG>BEFORE</STRONG> date #2, and negative
  522. if the order of the two dates is reversed.</P>
  523. <P>The result is zero if the two dates are identical.</P>
  524. <P></P>
  525. <LI>
  526. <CODE>($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2);</CODE>
  527. <P>This function returns the difference in days, hours, minutes and seconds
  528. between the two given dates with times.</P>
  529. <P>All four return values will be positive if the two dates are in chronological
  530. order, i.e., if date #1 comes chronologically <STRONG>BEFORE</STRONG> date #2, and negative
  531. (in all four return values!) if the order of the two dates is reversed.</P>
  532. <P>This is so that the two functions ``<CODE>Delta_DHMS()</CODE>'' and ``<CODE>Add_Delta_DHMS()</CODE>''
  533. (description see further below) are complementary, i.e., mutually inverse:</P>
  534. <PRE>
  535.   Add_Delta_DHMS(@date1,@time1, Delta_DHMS(@date1,@time1, @date2,@time2))</PRE>
  536. <P>yields ``<CODE>(@date2,@time2)</CODE>'' again, whereas</P>
  537. <PRE>
  538.   Add_Delta_DHMS(@date2,@time2,
  539.       map(-$_, Delta_DHMS(@date1,@time1, @date2,@time2)))</PRE>
  540. <P>yields ``<CODE>(@date1,@time1)</CODE>'', and</P>
  541. <PRE>
  542.   Delta_DHMS(@date1,@time1, Add_Delta_DHMS(@date1,@time1, @delta))</PRE>
  543. <P>yields ``<CODE>@delta</CODE>'' again.</P>
  544. <P>The result is zero (in all four return values) if the two dates and times
  545. are identical.</P>
  546. <P></P>
  547. <LI>
  548. <CODE>($year,$month,$day) = Add_Delta_Days($year,$month,$day, $Dd);</CODE>
  549. <P>This function has two principal uses:</P>
  550. <P>First, it can be used to calculate a new date, given an initial date and
  551. an offset (which may be positive or negative) in days, in order to answer
  552. questions like ``today plus 90 days -- which date gives that?''.</P>
  553. <P>(In order to add a weeks offset, simply multiply the weeks offset with
  554. ``<CODE>7</CODE>'' and use that as your days offset.)</P>
  555. <P>Second, it can be used to convert the canonical representation of a date,
  556. i.e., the number of that day (where counting starts at the 1st of January
  557. in 1 A.D.), back into a date given as year, month and day.</P>
  558. <P>Because counting starts at ``<CODE>1</CODE>'', you will actually have to subtract ``<CODE>1</CODE>''
  559. from the canonical date in order to get back the original date:</P>
  560. <PRE>
  561.   $canonical = Date_to_Days($year,$month,$day);</PRE>
  562. <PRE>
  563.   ($year,$month,$day) = Add_Delta_Days(1,1,1, $canonical - 1);</PRE>
  564. <P>Moreover, this function is the inverse of the function ``<CODE>Delta_Days()</CODE>'':</P>
  565. <PRE>
  566.   Add_Delta_Days(@date1, Delta_Days(@date1, @date2))</PRE>
  567. <P>yields ``<CODE>@date2</CODE>'' again, whereas</P>
  568. <PRE>
  569.   Add_Delta_Days(@date2, -Delta_Days(@date1, @date2))</PRE>
  570. <P>yields ``<CODE>@date1</CODE>'', and</P>
  571. <PRE>
  572.   Delta_Days(@date1, Add_Delta_Days(@date1, $delta))</PRE>
  573. <P>yields ``<CODE>$delta</CODE>'' again.</P>
  574. <P></P>
  575. <LI>
  576. <CODE>($year,$month,$day, $hour,$min,$sec) = Add_Delta_DHMS($year,$month,$day, $hour,$min,$sec, $Dd,$Dh,$Dm,$Ds);</CODE>
  577. <P>This function serves to add a days, hours, minutes and seconds offset to a
  578. given date and time, in order to answer questions like ``today and now plus
  579. 7 days but minus 5 hours and then plus 30 minutes, what date and time gives
  580. that?'':</P>
  581. <PRE>
  582.   ($y,$m,$d,$H,$M,$S) = Add_Delta_DHMS(Today_and_Now(), +7,-5,+30,0);</PRE>
  583. <P></P>
  584. <LI>
  585. <CODE>($year,$month,$day) = Add_Delta_YMD($year,$month,$day, $Dy,$Dm,$Dd);</CODE>
  586. <P>This function serves to add a years, months and days offset to a given date.</P>
  587. <P>(In order to add a weeks offset, simply multiply the weeks offset with ``<CODE>7</CODE>''
  588. and add this number to your days offset.)</P>
  589. <P>Note that the three offsets for years, months and days are applied separately
  590. from each other, in reverse order.</P>
  591. <P>(This also allows them to have opposite signs.)</P>
  592. <P>In other words, first the days offset is applied (using the function
  593. ``<CODE>Add_Delta_Days()</CODE>'', internally), then the months offset, and finally
  594. the years offset.</P>
  595. <P>If the resulting date happens to fall on a day beyond the end of the
  596. resulting month, like the 31st of April or the 29th of February (in
  597. non-leap years), then the day is replaced by the last valid day of
  598. that month in that year (e.g., the 30th of April or 28th of February).</P>
  599. <P><STRONG>BEWARE</STRONG> that this behaviour differs from that of previous versions
  600. of this module!</P>
  601. <P>(Formerly, only the 29th of February in non-leap years was checked for
  602. (which - in contrast to the current version - was replaced by the 1st
  603. of March). Other possible invalid dates were not checked (and returned
  604. unwittingly), constituting a severe bug of previous versions.)</P>
  605. <P><STRONG>BEWARE</STRONG> also that because of this replacement, but even more because
  606. a year and a month offset is not equivalent to a fixed number of days,
  607. the transformation performed by this function is <STRONG>NOT REVERSIBLE</STRONG>!</P>
  608. <P>This is in contrast to the functions ``<CODE>Add_Delta_Days()</CODE>'' and
  609. ``<CODE>Add_Delta_DHMS()</CODE>'', which for this very reason have inverse functions
  610. (namely ``<CODE>Delta_Days()</CODE>'' and ``<CODE>Delta_DHMS()</CODE>''), whereas there exists no
  611. inverse for this function.</P>
  612. <P>Note that for this same reason, even</P>
  613. <PRE>
  614.   @date = Add_Delta_YMD(
  615.           Add_Delta_YMD(@date, $Dy,$Dm,$Dd), -$Dy,-$Dm,-$Dd);</PRE>
  616. <P>will (in general!) <STRONG>NOT</STRONG> return the initial date ``<CODE>@date</CODE>''!</P>
  617. <P>(This might work in some cases, though.)</P>
  618. <P>Note that this is <STRONG>NOT</STRONG> a program bug but <STRONG>NECESSARILY</STRONG> so because of
  619. the varying lengths of years and months!</P>
  620. <P></P>
  621. <LI>
  622. <CODE>($year,$month,$day, $hour,$min,$sec, $doy,$dow,$dst) = System_Clock();</CODE>
  623. <P>If your operating system supports the corresponding system calls
  624. (``<A HREF="../../../lib/Pod/perlfunc.html#item_time"><CODE>time()</CODE></A>'' and ``<A HREF="../../../lib/Pod/perlfunc.html#item_localtime"><CODE>localtime()</CODE></A>''), this function will return
  625. the information provided by your system clock, i.e., the current
  626. date and time, the number of the day of year, the number of the
  627. day of week and a flag signaling wether daylight savings time
  628. is currently in effect or not.</P>
  629. <P>The ranges of values returned (and their meanings) are as follows:</P>
  630. <PRE>
  631.                 $year   :   should at least cover 1900..2038
  632.                 $month  :   1..12
  633.                 $day    :   1..31
  634.                 $hour   :   0..23
  635.                 $min    :   0..59
  636.                 $sec    :   0..59    (0..61 on some systems)
  637.                 $doy    :   1..366
  638.                 $dow    :   1..7
  639.                 $dst    :  -1..1</PRE>
  640. <P>The day of week (``<CODE>$dow</CODE>'') will be ``<CODE>1</CODE>'' for Monday, ``<CODE>2</CODE>'' for
  641. Tuesday and so on until ``<CODE>7</CODE>'' for Sunday.</P>
  642. <P>The daylight savings time flag (``<CODE>$dst</CODE>'') will be ``<CODE>-1</CODE>'' if this
  643. information is not available on your system, ``<CODE>0</CODE>'' for no daylight
  644. savings time (i.e., normal time) and ``<CODE>1</CODE>'' when daylight savings
  645. time is in effect.</P>
  646. <P>If your operating system does not provide the necessary system calls,
  647. calling this function will result in a fatal ``not available on this
  648. system'' error message.</P>
  649. <P>If you want to handle this exception yourself, use ``<A HREF="../../../lib/Pod/perlfunc.html#item_eval"><CODE>eval</CODE></A>'' as follows:</P>
  650. <PRE>
  651.   eval { ($year,$month,$day, $hour,$min,$sec, $doy,$dow,$dst) =
  652.     System_Clock(); };</PRE>
  653. <PRE>
  654.   if ($@)
  655.   {
  656.       # Handle missing system clock
  657.       # (For instance, ask user to enter this information manually)
  658.   }</PRE>
  659. <P>Note that curlies (``{'' and ``}'') are used here to delimit the statement to
  660. be ``eval''ed (which is the way to catch exceptions in Perl), and not quotes
  661. (which is a way to evaluate Perl expressions at runtime).</P>
  662. <P></P>
  663. <LI>
  664. <CODE>($year,$month,$day) = Today();</CODE>
  665. <P>This function returns a subset of the values returned by the function
  666. ``<CODE>System_Clock()</CODE>'' (see above for details), namely the current year,
  667. month and day.</P>
  668. <P>A fatal ``not available on this system'' error message will appear if the
  669. corresponding system calls are not supported by your current operating
  670. system.</P>
  671. <P></P>
  672. <LI>
  673. <CODE>($hour,$min,$sec) = Now();</CODE>
  674. <P>This function returns a subset of the values returned by the function
  675. ``<CODE>System_Clock()</CODE>'' (see above for details), namely the current time
  676. (hours, minutes and full seconds).</P>
  677. <P>A fatal ``not available on this system'' error message will appear if the
  678. corresponding system calls are not supported by your current operating
  679. system.</P>
  680. <P></P>
  681. <LI>
  682. <CODE>($year,$month,$day, $hour,$min,$sec) = Today_and_Now();</CODE>
  683. <P>This function returns a subset of the values returned by the function
  684. ``<CODE>System_Clock()</CODE>'' (see above for details), namely the current date
  685. (year, month, day) and time (hours, minutes and full seconds).</P>
  686. <P>A fatal ``not available on this system'' error message will appear if the
  687. corresponding system calls are not supported by your current operating
  688. system.</P>
  689. <P></P>
  690. <LI>
  691. <CODE>($year,$month,$day) = Easter_Sunday($year);</CODE>
  692. <P>This function calculates the date of easter sunday for all years in the
  693. range from 1583 to 2299 (all other year numbers will result in a fatal
  694. ``year out of range'' error message) using the method known as the ``Gaussian
  695. Rule''.</P>
  696. <P>Some related christian feast days which depend on the date of easter sunday:</P>
  697. <PRE>
  698.   Carnival Monday / Rosenmontag / Veille du Mardi Gras   =  -48 days
  699.   Mardi Gras / Karnevalsdienstag / Mardi Gras            =  -47 days
  700.   Ash Wednesday / Aschermittwoch / Mercredi des Cendres  =  -46 days
  701.   Palm Sunday / Palmsonntag / Dimanche des Rameaux       =   -7 days
  702.   Easter Friday / Karfreitag / Vendredi Saint            =   -2 days
  703.   Easter Saturday / Ostersamstag / Samedi de Paques      =   -1 day
  704.   Easter Monday / Ostermontag / Lundi de Paques          =   +1 day
  705.   Ascension of Christ / Christi Himmelfahrt / Ascension  =  +39 days
  706.   Whitsunday / Pfingstsonntag / Dimanche de Pentecote    =  +49 days
  707.   Whitmonday / Pfingstmontag / Lundi de Pentecote        =  +50 days
  708.   Feast of Corpus Christi / Fronleichnam / Fete-Dieu     =  +60 days</PRE>
  709. <P>Use the offsets shown above to calculate the date of the corresponding
  710. feast day as follows:</P>
  711. <PRE>
  712.   ($year,$month,$day) = Add_Delta_Days(Easter_Sunday($year), $offset));</PRE>
  713. <P></P>
  714. <LI>
  715. <CODE>if ($month = Decode_Month($string))</CODE>
  716. <P>This function takes a string as its argument, which should contain the
  717. name of a month <STRONG>IN THE CURRENTLY SELECTED LANGUAGE</STRONG> (see further below
  718. for details about the multi-language support of this package), or any uniquely
  719. identifying abbreviation of a month's name (i.e., the first few letters),
  720. and returns the corresponding number (1..12) upon a successful match, or
  721. ``<CODE>0</CODE>'' otherwise (therefore, the return value can also be used as the
  722. conditional expression in an ``if'' statement).</P>
  723. <P>Note that the input string may not contain any other characters which do not
  724. pertain to the month's name, especially no leading or trailing whitespace.</P>
  725. <P>Note also that matching is performed in a case-insensitive manner (this may
  726. depend on the ``locale'' setting on your current system, though!)</P>
  727. <P>With ``English'' as the currently selected language (which is the default),
  728. the following examples will all return the value ``<CODE>9</CODE>'':</P>
  729. <PRE>
  730.   $month = Decode_Month("s");
  731.   $month = Decode_Month("Sep");
  732.   $month = Decode_Month("septemb");
  733.   $month = Decode_Month("September");</PRE>
  734. <P></P>
  735. <LI>
  736. <CODE>if ($dow = Decode_Day_of_Week($string))</CODE>
  737. <P>This function takes a string as its argument, which should contain the
  738. name of a day of week <STRONG>IN THE CURRENTLY SELECTED LANGUAGE</STRONG> (see further
  739. below for details about the multi-language support of this package), or any
  740. uniquely identifying abbreviation of the name of a day of week (i.e., the
  741. first few letters), and returns the corresponding number (1..7) upon a
  742. successful match, or ``<CODE>0</CODE>'' otherwise (therefore, the return value can
  743. also be used as the conditional expression in an ``if'' statement).</P>
  744. <P>Note that the input string may not contain any other characters which
  745. do not pertain to the name of the day of week, especially no leading
  746. or trailing whitespace.</P>
  747. <P>Note also that matching is performed in a case-insensitive manner (this may
  748. depend on the ``locale'' setting on your current system, though!)</P>
  749. <P>With ``English'' as the currently selected language (which is the default),
  750. the following examples will all return the value ``<CODE>3</CODE>'':</P>
  751. <PRE>
  752.   $dow = Decode_Day_of_Week("w");
  753.   $dow = Decode_Day_of_Week("Wed");
  754.   $dow = Decode_Day_of_Week("wednes");
  755.   $dow = Decode_Day_of_Week("Wednesday");</PRE>
  756. <P></P>
  757. <LI>
  758. <CODE>if ($lang = Decode_Language($string))</CODE>
  759. <P>This function takes a string as its argument, which should contain the
  760. name of one of the languages supported by this package (<STRONG>IN THIS VERY
  761. LANGUAGE ITSELF</STRONG>), or any uniquely identifying abbreviation of the name
  762. of a language (i.e., the first few letters), and returns its corresponding
  763. internal number (1..7 in the original distribution) upon a successful match,
  764. or ``<CODE>0</CODE>'' otherwise (therefore, the return value can also be used as the
  765. conditional expression in an ``if'' statement).</P>
  766. <P>Note that the input string may not contain any other characters which do
  767. not pertain to the name of a language, especially no leading or trailing
  768. whitespace.</P>
  769. <P>Note also that matching is performed in a case-insensitive manner (this may
  770. depend on the ``locale'' setting on your current system, though!)</P>
  771. <P>The original distribution supports the following seven languages:</P>
  772. <PRE>
  773.             English                    ==>   1    (default)
  774.             Franτais    (French)       ==>   2
  775.             Deutsch     (German)       ==>   3
  776.             Espa±ol     (Spanish)      ==>   4
  777.             PortuguΩs   (Portuguese)   ==>   5
  778.             Nederlands  (Dutch)        ==>   6
  779.             Italiano    (Italian)      ==>   7</PRE>
  780. <P>See the section ``How to install additional languages'' in the file
  781. ``INSTALL.txt'' in this distribution for how to add more languages
  782. to this package.</P>
  783. <P>In the original distribution (no other languages installed),
  784. the following examples will all return the value ``<CODE>3</CODE>'':</P>
  785. <PRE>
  786.   $lang = Decode_Language("d");
  787.   $lang = Decode_Language("de");
  788.   $lang = Decode_Language("Deutsch");</PRE>
  789. <P>Note that you may not be able to enter the special international characters
  790. in some of the languages' names over the keyboard directly on some systems.</P>
  791. <P>This should never be a problem, though; just enter an abbreviation of the
  792. name of the language consisting of the first few letters up to the character
  793. before the first special international character.</P>
  794. <P></P>
  795. <LI>
  796. <CODE>if (($year,$month,$day) = Decode_Date_EU($string))</CODE>
  797. <P>This function scans a given string and tries to parse any date
  798. which might be embedded in it.</P>
  799. <P>The function returns an empty list if it can't successfully
  800. extract a valid date from its input string, or else it returns
  801. the date found.</P>
  802. <P>The function accepts almost any format, as long as the date is
  803. given in the european order (hence its name) day-month-year.</P>
  804. <P>Thereby, zero or more <STRONG>NON-NUMERIC</STRONG> characters may <STRONG>PRECEDE</STRONG>
  805. the day and <STRONG>FOLLOW</STRONG> the year.</P>
  806. <P>Moreover, zero or more <STRONG>NON-ALPHANUMERIC</STRONG> characters are permitted
  807. <STRONG>BETWEEN</STRONG> these three items (i.e., between day and month and between
  808. month and year).</P>
  809. <P>The month may be given either numerically (i.e., a number from ``<CODE>1</CODE>''
  810. to ``<CODE>12</CODE>''), or alphanumerically, i.e., as the name of the month <STRONG>IN
  811. THE CURRENTLY SELECTED LANGUAGE</STRONG>, or any uniquely identifying abbreviation
  812. thereof.</P>
  813. <P>(See further below for details about the multi-language support of this
  814. package!)</P>
  815. <P>If the year is given as one or two digits only (i.e., if the year is less
  816. than 100), it is mapped to the window ``<CODE>1970 - 2069</CODE>'' as follows:</P>
  817. <PRE>
  818.    0 E<lt>= $year E<lt>  70  ==>  $year += 2000;
  819.   70 E<lt>= $year E<lt> 100  ==>  $year += 1900;</PRE>
  820. <P>If the day, month and year are all given numerically but <STRONG>WITHOUT</STRONG> any
  821. delimiting characters between them, this string of digits will be mapped
  822. to the day, month and year as follows:</P>
  823. <PRE>
  824.                 Length:        Mapping:
  825.                   3              dmy
  826.                   4              dmyy
  827.                   5              dmmyy
  828.                   6              ddmmyy
  829.                   7              dmmyyyy
  830.                   8              ddmmyyyy</PRE>
  831. <P>(Where ``d'' stands for ``day'', ``m'' stands for ``month'' and ``y'' stands for
  832. ``year''.)</P>
  833. <P>All other strings consisting purely of digits (without any intervening
  834. delimiters) are rejected, i.e., not recognized.</P>
  835. <P>Examples:</P>
  836. <PRE>
  837.   "3.1.64"
  838.   "3 1 64"
  839.   "03.01.64"
  840.   "03/01/64"
  841.   "3. Jan 1964"
  842.   "Birthday: 3. Jan '64 in Backnang/Germany"
  843.   "03-Jan-64"
  844.   "3.Jan1964"
  845.   "3Jan64"
  846.   "030164"
  847.   "3ja64"
  848.   "3164"</PRE>
  849. <P>Experiment! (See the corresponding example applications in the
  850. ``examples'' subdirectory of this distribution in order to do so.)</P>
  851. <P></P>
  852. <LI>
  853. <CODE>if (($year,$month,$day) = Decode_Date_US($string))</CODE>
  854. <P>This function scans a given string and tries to parse any date
  855. which might be embedded in it.</P>
  856. <P>The function returns an empty list if it can't successfully
  857. extract a valid date from its input string, or else it returns
  858. the date found.</P>
  859. <P>The function accepts almost any format, as long as the date is
  860. given in the U.S. american order (hence its name) month-day-year.</P>
  861. <P>Thereby, zero or more <STRONG>NON-ALPHANUMERIC</STRONG> characters may <STRONG>PRECEDE</STRONG>
  862. and <STRONG>FOLLOW</STRONG> the month (i.e., precede the month and separate it from
  863. the day which follows behind).</P>
  864. <P>Moreover, zero or more <STRONG>NON-NUMERIC</STRONG> characters are permitted
  865. <STRONG>BETWEEN</STRONG> the day and the year, as well as <STRONG>AFTER</STRONG> the year.</P>
  866. <P>The month may be given either numerically (i.e., a number from ``<CODE>1</CODE>''
  867. to ``<CODE>12</CODE>''), or alphanumerically, i.e., as the name of the month <STRONG>IN
  868. THE CURRENTLY SELECTED LANGUAGE</STRONG>, or any uniquely identifying abbreviation
  869. thereof.</P>
  870. <P>(See further below for details about the multi-language support of this
  871. package!)</P>
  872. <P>If the year is given as one or two digits only (i.e., if the year is less
  873. than 100), it is mapped to the window ``<CODE>1970 - 2069</CODE>'' as follows:</P>
  874. <PRE>
  875.    0 E<lt>= $year E<lt>  70  ==>  $year += 2000;
  876.   70 E<lt>= $year E<lt> 100  ==>  $year += 1900;</PRE>
  877. <P>If the month, day and year are all given numerically but <STRONG>WITHOUT</STRONG> any
  878. delimiting characters between them, this string of digits will be mapped
  879. to the month, day and year as follows:</P>
  880. <PRE>
  881.                 Length:        Mapping:
  882.                   3              mdy
  883.                   4              mdyy
  884.                   5              mddyy
  885.                   6              mmddyy
  886.                   7              mddyyyy
  887.                   8              mmddyyyy</PRE>
  888. <P>(Where ``m'' stands for ``month'', ``d'' stands for ``day'' and ``y'' stands for
  889. ``year''.)</P>
  890. <P>All other strings consisting purely of digits (without any intervening
  891. delimiters) are rejected, i.e., not recognized.</P>
  892. <P>If only the day and the year form a contiguous string of digits, they
  893. will be mapped as follows:</P>
  894. <PRE>
  895.                 Length:        Mapping:
  896.                   2              dy
  897.                   3              dyy
  898.                   4              ddyy
  899.                   5              dyyyy
  900.                   6              ddyyyy</PRE>
  901. <P>(Where ``d'' stands for ``day'' and ``y'' stands for ``year''.)</P>
  902. <P>Examples:</P>
  903. <PRE>
  904.   "1 3 64"
  905.   "01/03/64"
  906.   "Jan 3 '64"
  907.   "Jan 3 1964"
  908.   "===> January 3rd 1964 (birthday)"
  909.   "Jan31964"
  910.   "Jan364"
  911.   "ja364"
  912.   "1364"</PRE>
  913. <P>Experiment! (See the corresponding example applications in the
  914. ``examples'' subdirectory of this distribution in order to do so.)</P>
  915. <P></P>
  916. <LI>
  917. <CODE>$date = Compress($year,$month,$day);</CODE>
  918. <P>This function encodes a date in 16 bits, which is the value being returned.</P>
  919. <P>The encoding scheme is as follows:</P>
  920. <PRE>
  921.             Bit number:    FEDCBA9 8765 43210
  922.             Contents:      yyyyyyy mmmm ddddd</PRE>
  923. <P>(Where the ``yyyyyyy'' contain the number of the year, ``mmmm'' the number of
  924. the month and ``ddddd'' the number of the day.)</P>
  925. <P>The function returns ``<CODE>0</CODE>'' if the given input values do not represent a
  926. valid date. Therefore, the return value of this function can also be used
  927. as the conditional expression in an ``if'' statement, in order to check
  928. wether the given input values constitute a valid date).</P>
  929. <P>Through this special encoding scheme, it is possible to <STRONG>COMPARE</STRONG>
  930. compressed dates for equality and order (less than/greater than)
  931. <STRONG>WITHOUT</STRONG> any previous <STRONG>DECODING</STRONG>!</P>
  932. <P>Note however that contiguous dates do <STRONG>NOT</STRONG> necessarily have contiguous
  933. compressed representations!</P>
  934. <P>I.e., incrementing the compressed representation of a date <STRONG>MAY OR MAY NOT</STRONG>
  935. yield a valid new date!</P>
  936. <P>Note also that this function can only handle dates within one century.</P>
  937. <P>This century can be chosen at random by defining a base century and year
  938. (also called the ``epoch''). In the original distribution of this package,
  939. the base century is set to ``1900'' and the base year to ``70'' (which is
  940. standard on UNIX systems).</P>
  941. <P>This allows this function to handle dates from ``1970'' up to ``2069''.</P>
  942. <P>If the given year is equal to, say, ``95'', this package will automatically
  943. assume that you really mean ``1995'' instead. However, if you specify a year
  944. number which is <STRONG>SMALLER</STRONG> than 70, like ``64'', for instance, this package
  945. will assume that you really mean ``2064''.</P>
  946. <P>You are not confined to two-digit (abbreviated) year numbers, though.</P>
  947. <P>The function also accepts ``full-length'' year numbers, provided that they
  948. lie in the supported range (i.e., from ``1970'' to ``2069'', in the original
  949. configuration of this package).</P>
  950. <P>Note that this function is maintained mainly for backward compatibility,
  951. and that its use is not recommended.</P>
  952. <P></P>
  953. <LI>
  954. <CODE>if (($century,$year,$month,$day) = Uncompress($date))</CODE>
  955. <P>This function decodes dates that were encoded previously using the function
  956. ``<CODE>Compress()</CODE>''.</P>
  957. <P>It returns the century, year, month and day of the date encoded in ``<CODE>$date</CODE>''
  958. if ``<CODE>$date</CODE>'' represents a valid date, or an empty list otherwise.</P>
  959. <P>The year returned in ``<CODE>$year</CODE>'' is actually a two-digit year number
  960. (i.e., the year number taken modulo 100), and only the expression
  961. ``<CODE>$century + $year</CODE>'' yields the ``full-length'' year number
  962. (for example, <CODE>1900 + 95 = 1995</CODE>).</P>
  963. <P>Note that this function is maintained mainly for backward compatibility,
  964. and that its use is not recommended.</P>
  965. <P></P>
  966. <LI>
  967. <CODE>if (check_compressed($date))</CODE>
  968. <P>This function returns ``true'' (``<CODE>1</CODE>'') if the given input value
  969. constitutes a valid compressed date, and ``false'' (``<CODE>0</CODE>'') otherwise.</P>
  970. <P>Note that this function is maintained mainly for backward compatibility,
  971. and that its use is not recommended.</P>
  972. <P></P>
  973. <LI>
  974. <CODE>$string = Compressed_to_Text($date);</CODE>
  975. <P>This function returns a string of fixed length (always 9 characters long)
  976. containing a textual representation of the compressed date encoded in
  977. ``<CODE>$date</CODE>''.</P>
  978. <P>This string has the form ``dd-Mmm-yy'', where ``dd'' is the two-digit number
  979. of the day, ``Mmm'' are the first three letters of the name of the month
  980. in the currently selected language (see further below for details about
  981. the multi-language support of this package), and ``yy'' is the two-digit
  982. year number (i.e., the year number taken modulo 100).</P>
  983. <P>If ``<CODE>$date</CODE>'' does not represent a valid date, the string ``??-???-??'' is
  984. returned instead.</P>
  985. <P>Note that this function is maintained mainly for backward compatibility,
  986. and that its use is not recommended.</P>
  987. <P></P>
  988. <LI>
  989. <CODE>$string = Date_to_Text($year,$month,$day);</CODE>
  990. <P>This function returns a string containing a textual representation of the
  991. given date of the form ``www dd-Mmm-yyyy'', where ``www'' are the first three
  992. letters of the name of the day of week in the currently selected language,
  993. or a special abbreviation, if special abbreviations have been defined for
  994. the currently selected language (see further below for details about the
  995. multi-language support of this package), ``dd'' is the day (one or two digits),
  996. ``Mmm'' are the first three letters of the name of the month in the currently
  997. selected language, and ``yyyy'' is the number of the year in full length.</P>
  998. <P>If the given input values do not constitute a valid date, a fatal ``not a
  999. valid date'' error occurs.</P>
  1000. <P>(See the section ``RECIPES'' near the end of this document for a code snippet
  1001. for how to print dates in any format you like.)</P>
  1002. <P></P>
  1003. <LI>
  1004. <CODE>$string = Date_to_Text_Long($year,$month,$day);</CODE>
  1005. <P>This function returns a string containing a textual representation of the
  1006. given date roughly of the form ``Wwwwww, dd Mmmmmm yyyy'', where ``Wwwwww''
  1007. is the name of the day of week in the currently selected language (see
  1008. further below for details about the multi-language support of this package),
  1009. ``dd'' is the day (one or two digits), ``Mmmmmm'' is the name of the month
  1010. in the currently selected language, and ``yyyy'' is the number of the year
  1011. in full length.</P>
  1012. <P>The exact format of the output string depends on the currently selected
  1013. language. In the original distribution of this package, these formats are
  1014. defined as follows:</P>
  1015. <PRE>
  1016.   1  English    :  "Wwwwww, Mmmmmm ddth yyyy"
  1017.   2  French     :  "Wwwwww, le dd Mmmmmm yyyy"
  1018.   3  German     :  "Wwwwww, den dd. Mmmmmm yyyy"
  1019.   4  Spanish    :  "Wwwwww, dd de Mmmmmm de yyyy"
  1020.   5  Portuguese :  "Wwwwww, dia dd de Mmmmmm de yyyy"
  1021.   6  Dutch      :  "Wwwwww, dd. Mmmmmm yyyy"
  1022.   7  Italian    :  "Wwwwww, dd Mmmmmm yyyy"</PRE>
  1023. <P>(You can change these formats in the file ``DateCalc.c'' before
  1024. building this module in order to suit your personal preferences.)</P>
  1025. <P>If the given input values do not constitute a valid date, a fatal
  1026. ``not a valid date'' error occurs.</P>
  1027. <P>(See the section ``RECIPES'' near the end of this document for
  1028. an example on how to print dates in any format you like.)</P>
  1029. <P></P>
  1030. <LI>
  1031. <CODE>$string = English_Ordinal($number);</CODE>
  1032. <P>This function returns a string containing the (english) abbreviation
  1033. of the ordinal number for the given (cardinal) number ``<CODE>$number</CODE>''.</P>
  1034. <P>I.e.,</P>
  1035. <PRE>
  1036.     0  =>  '0th'    10  =>  '10th'    20  =>  '20th'
  1037.     1  =>  '1st'    11  =>  '11th'    21  =>  '21st'
  1038.     2  =>  '2nd'    12  =>  '12th'    22  =>  '22nd'
  1039.     3  =>  '3rd'    13  =>  '13th'    23  =>  '23rd'
  1040.     4  =>  '4th'    14  =>  '14th'    24  =>  '24th'
  1041.     5  =>  '5th'    15  =>  '15th'    25  =>  '25th'
  1042.     6  =>  '6th'    16  =>  '16th'    26  =>  '26th'
  1043.     7  =>  '7th'    17  =>  '17th'    27  =>  '27th'
  1044.     8  =>  '8th'    18  =>  '18th'    28  =>  '28th'
  1045.     9  =>  '9th'    19  =>  '19th'    29  =>  '29th'</PRE>
  1046. <P>etc.</P>
  1047. <P></P>
  1048. <LI>
  1049. <CODE>$string = Calendar($year,$month);</CODE>
  1050. <P>This function returns a calendar of the given month in the given year
  1051. (somewhat similar to the UNIX ``cal'' command), <STRONG>IN THE CURRENTLY SELECTED
  1052. LANGUAGE</STRONG> (see further below for details about the multi-language support
  1053. of this package).</P>
  1054. <P>Example:</P>
  1055. <PRE>
  1056.   print Calendar(1998,5);</PRE>
  1057. <P>This will print:</P>
  1058. <PRE>
  1059.            May 1998
  1060.   Mon Tue Wed Thu Fri Sat Sun
  1061.                     1   2   3
  1062.     4   5   6   7   8   9  10
  1063.    11  12  13  14  15  16  17
  1064.    18  19  20  21  22  23  24
  1065.    25  26  27  28  29  30  31</PRE>
  1066. <P></P>
  1067. <LI>
  1068. <CODE>$string = Month_to_Text($month);</CODE>
  1069. <P>This function returns the name of the given month in the currently selected
  1070. language (see further below for details about the multi-language support of
  1071. this package).</P>
  1072. <P>If the given month lies outside of the valid range from ``<CODE>1</CODE>'' to ``<CODE>12</CODE>'',
  1073. a fatal ``month out of range'' error will occur.</P>
  1074. <P></P>
  1075. <LI>
  1076. <CODE>$string = Day_of_Week_to_Text($dow);</CODE>
  1077. <P>This function returns the name of the given day of week in the currently
  1078. selected language (see further below for details about the multi-language
  1079. support of this package).</P>
  1080. <P>If the given day of week lies outside of the valid range from ``<CODE>1</CODE>'' to ``<CODE>7</CODE>'',
  1081. a fatal ``day of week out of range'' error will occur.</P>
  1082. <P></P>
  1083. <LI>
  1084. <CODE>$string = Day_of_Week_Abbreviation($dow);</CODE>
  1085. <P>This function returns the special abbreviation of the name of the given
  1086. day of week, <STRONG>IF</STRONG> such special abbreviations have been defined for the
  1087. currently selected language (see further below for details about the
  1088. multi-language support of this package).</P>
  1089. <P>(In the original distribution of this package, this is only true for
  1090. Portuguese.)</P>
  1091. <P>If not, the first three letters of the name of the day of week in the
  1092. currently selected language are returned instead.</P>
  1093. <P>If the given day of week lies outside of the valid range from ``<CODE>1</CODE>''
  1094. to ``<CODE>7</CODE>'', a fatal ``day of week out of range'' error will occur.</P>
  1095. <P>Currently, this table of special abbreviations is only used by the
  1096. functions ``<CODE>Date_to_Text()</CODE>'' and ``<CODE>Calendar()</CODE>'', internally.</P>
  1097. <P></P>
  1098. <LI>
  1099. <CODE>$string = Language_to_Text($lang);</CODE>
  1100. <P>This function returns the name of any language supported by this package
  1101. when the internal number representing that language is given as input.</P>
  1102. <P>The original distribution supports the following seven languages:</P>
  1103. <PRE>
  1104.             1   ==>   English                     (default)
  1105.             2   ==>   Franτais    (French)
  1106.             3   ==>   Deutsch     (German)
  1107.             4   ==>   Espa±ol     (Spanish)
  1108.             5   ==>   PortuguΩs   (Portuguese)
  1109.             6   ==>   Nederlands  (Dutch)
  1110.             7   ==>   Italiano    (Italian)</PRE>
  1111. <P>See the section ``How to install additional languages'' in the file
  1112. ``INSTALL.txt'' in this distribution for how to add more languages
  1113. to this package.</P>
  1114. <P>See the description of the function ``<CODE>Languages()</CODE>'' further below
  1115. to determine how many languages are actually available in a given
  1116. installation of this package.</P>
  1117. <P></P>
  1118. <LI>
  1119. <CODE>$lang = Language();</CODE>
  1120. <P></P>
  1121. <LI>
  1122. <CODE>Language($lang);</CODE>
  1123. <P></P>
  1124. <LI>
  1125. <CODE>$oldlang = Language($newlang);</CODE>
  1126. <P>This function can be used to determine which language is currently selected,
  1127. and to change the selected language.</P>
  1128. <P>Thereby, each language has a unique internal number.</P>
  1129. <P>The original distribution contains the following seven languages:</P>
  1130. <PRE>
  1131.             1   ==>   English                     (default)
  1132.             2   ==>   Franτais    (French)
  1133.             3   ==>   Deutsch     (German)
  1134.             4   ==>   Espa±ol     (Spanish)
  1135.             5   ==>   PortuguΩs   (Portuguese)
  1136.             6   ==>   Nederlands  (Dutch)
  1137.             7   ==>   Italiano    (Italian)</PRE>
  1138. <P>See the section ``How to install additional languages'' in the file
  1139. ``INSTALL.txt'' in this distribution for how to add more languages
  1140. to this package.</P>
  1141. <P>See the description of the function ``<CODE>Languages()</CODE>'' further below
  1142. to determine how many languages are actually available in a given
  1143. installation of this package.</P>
  1144. <P><STRONG>BEWARE</STRONG> that in order for your programs to be portable, you should <STRONG>NEVER</STRONG>
  1145. actually use the internal number of a language in this package <STRONG>EXPLICITLY</STRONG>,
  1146. because the same number could mean different languages on different systems,
  1147. depending on what languages have been added to any given installation of this
  1148. package.</P>
  1149. <P>Therefore, you should always use a statement such as</P>
  1150. <PRE>
  1151.   Language(Decode_Language("Name_of_Language"));</PRE>
  1152. <P>to select the desired language, and</P>
  1153. <PRE>
  1154.   $language = Language_to_Text(Language());</PRE>
  1155. <P>or</P>
  1156. <PRE>
  1157.   $old_language = Language_to_Text(Language("Name_of_new_Language"));</PRE>
  1158. <P>to determine the (previously) selected language.</P>
  1159. <P>If the so chosen language is not available in the current installation,
  1160. this will result in an appropriate error message, instead of silently
  1161. using the wrong (a random) language (which just happens to have the
  1162. same internal number in the other installation).</P>
  1163. <P>Note that in the current implementation of this package, the selected
  1164. language is a global setting valid for <STRONG>ALL</STRONG> functions that use the names
  1165. of months, days of week or languages internally, valid for <STRONG>ALL PROCESSES</STRONG>
  1166. using the same copy of the ``Date::Calc'' shared library in memory!</P>
  1167. <P>This may have surprising side-effects in a multi-user environment, and even
  1168. more so when Perl will be capable of multi-threading in some future release.</P>
  1169. <P></P>
  1170. <LI>
  1171. <CODE>$max_lang = Languages();</CODE>
  1172. <P>This function returns the (maximum) number of languages which are
  1173. currently available in your installation of this package.</P>
  1174. <P>(This may vary from installation to installation.)</P>
  1175. <P>See the section ``How to install additional languages'' in the file
  1176. ``INSTALL.txt'' in this distribution for how to add more languages
  1177. to this package.</P>
  1178. <P>In the original distribution of this package there are seven built-in
  1179. languages, therefore the value returned by this function will be ``<CODE>7</CODE>''
  1180. if no other languages have been added to your particular installation.</P>
  1181. <P></P>
  1182. <LI>
  1183. <CODE>if (($year,$month,$day) = Decode_Date_EU2($string))</CODE>
  1184. <P>This function is the Perl equivalent of the function ``<CODE>Decode_Date_EU()</CODE>''
  1185. (implemented in C), included here merely as an example to demonstrate how
  1186. easy it is to write your own routine in Perl (using regular expressions)
  1187. adapted to your own special needs, should the necessity arise, and intended
  1188. primarily as a basis for your own development.</P>
  1189. <P>In one particular case this Perl version is actually slightly more permissive
  1190. than its C equivalent, as far as the class of permitted intervening (i.e.,
  1191. delimiting) characters is concerned.</P>
  1192. <P>(Can you tell the subtle, almost insignificant difference by looking at
  1193. the code? Or by experimenting? Hint: Try the string ``a3b1c64d'' with both
  1194. functions.)</P>
  1195. <P></P>
  1196. <LI>
  1197. <CODE>if (($year,$month,$day) = Decode_Date_US2($string))</CODE>
  1198. <P>This function is the Perl equivalent of the function ``<CODE>Decode_Date_US()</CODE>''
  1199. (implemented in C), included here merely as an example to demonstrate how
  1200. easy it is to write your own routine in Perl (using regular expressions)
  1201. adapted to your own special needs, should the necessity arise, and intended
  1202. primarily as a basis for your own development.</P>
  1203. <P>In one particular case this Perl version is actually slightly more permissive
  1204. than its C equivalent.</P>
  1205. <P>(Hint: This is the same difference as with the ``<CODE>Decode_Date_EU()</CODE>'' and
  1206. ``<CODE>Decode_Date_EU2()</CODE>'' pair of functions.)</P>
  1207. <P>In a different case, the C version is a little bit more permissive than its
  1208. Perl equivalent.</P>
  1209. <P>(Can you tell the difference by looking at the code? Or by experimenting?
  1210. Hint: Try the string ``(1/364)'' with both functions.)</P>
  1211. <P></P>
  1212. <LI>
  1213. <CODE>if (($year,$month,$day) = Parse_Date($string))</CODE>
  1214. <P>This function is useful for parsing dates as returned by the UNIX ``<CODE>date</CODE>''
  1215. command or as found in the headers of e-mail (in order to determine the
  1216. date at which some e-mail has been sent or received, for instance).</P>
  1217. <P>Example #1:</P>
  1218. <PRE>
  1219.   ($year,$month,$day) = Parse_Date(`/bin/date`);</PRE>
  1220. <P>Example #2:</P>
  1221. <PRE>
  1222.   while (<MAIL>)
  1223.   {
  1224.       if (/^From \S/)
  1225.       {
  1226.           ($year,$month,$day) = Parse_Date($_);
  1227.           ...
  1228.       }
  1229.       ...
  1230.   }</PRE>
  1231. <P>The function returns an empty list if it can't extract a valid date from
  1232. the input string.</P>
  1233. <P></P>
  1234. <LI>
  1235. <CODE>$string = Date::Calc::Version();</CODE>
  1236. <P>This function returns a string with the (numeric) version number of the
  1237. C library (``DateCalc.c'') at the core of this package (which is also
  1238. (automatically) the version number of the ``Calc.xs'' file).</P>
  1239. <P>Note that under all normal circumstances, this version number should be
  1240. identical with the one found in the Perl variable ``<CODE>$Date::Calc::VERSION</CODE>''
  1241. (the version number of the ``Calc.pm'' file).</P>
  1242. <P>Since this function is not exported, you always have to qualify it explicitly,
  1243. i.e., ``<CODE>Date::Calc::Version()</CODE>''.</P>
  1244. <P>This is to avoid possible name space conflicts with version functions from
  1245. other modules.</P>
  1246. <P></P></UL>
  1247. <P>
  1248. <HR>
  1249. <H1><A NAME="recipes">RECIPES</A></H1>
  1250. <OL>
  1251. <LI><STRONG><A NAME="item_%29">)</A></STRONG><BR>
  1252.  
  1253. How do I compare two dates?
  1254. <P>Solution #1:</P>
  1255. <PRE>
  1256.   use Date::Calc qw( Date_to_Days );</PRE>
  1257. <PRE>
  1258.   if (Date_to_Days($year1,$month1,$day1)  <
  1259.       Date_to_Days($year2,$month2,$day2))</PRE>
  1260. <PRE>
  1261.   if (Date_to_Days($year1,$month1,$day1)  <=
  1262.       Date_to_Days($year2,$month2,$day2))</PRE>
  1263. <PRE>
  1264.   if (Date_to_Days($year1,$month1,$day1)  >
  1265.       Date_to_Days($year2,$month2,$day2))</PRE>
  1266. <PRE>
  1267.   if (Date_to_Days($year1,$month1,$day1)  >=
  1268.       Date_to_Days($year2,$month2,$day2))</PRE>
  1269. <PRE>
  1270.   if (Date_to_Days($year1,$month1,$day1)  ==
  1271.       Date_to_Days($year2,$month2,$day2))</PRE>
  1272. <PRE>
  1273.   if (Date_to_Days($year1,$month1,$day1)  !=
  1274.       Date_to_Days($year2,$month2,$day2))</PRE>
  1275. <PRE>
  1276.   $cmp = (Date_to_Days($year1,$month1,$day1)  <=>
  1277.           Date_to_Days($year2,$month2,$day2));</PRE>
  1278. <P>Solution #2:</P>
  1279. <PRE>
  1280.   use Date::Calc qw( Delta_Days );</PRE>
  1281. <PRE>
  1282.   if (Delta_Days($year1,$month1,$day1,
  1283.                  $year2,$month2,$day2) > 0)</PRE>
  1284. <PRE>
  1285.   if (Delta_Days($year1,$month1,$day1,
  1286.                  $year2,$month2,$day2) >= 0)</PRE>
  1287. <PRE>
  1288.   if (Delta_Days($year1,$month1,$day1,
  1289.                  $year2,$month2,$day2) < 0)</PRE>
  1290. <PRE>
  1291.   if (Delta_Days($year1,$month1,$day1,
  1292.                  $year2,$month2,$day2) <= 0)</PRE>
  1293. <PRE>
  1294.   if (Delta_Days($year1,$month1,$day1,
  1295.                  $year2,$month2,$day2) == 0)</PRE>
  1296. <PRE>
  1297.   if (Delta_Days($year1,$month1,$day1,
  1298.                  $year2,$month2,$day2) != 0)</PRE>
  1299. <P></P>
  1300. <LI><STRONG>)</STRONG><BR>
  1301.  
  1302. How do I check wether a given date lies within a certain range of dates?
  1303. <PRE>
  1304.   use Date::Calc qw( Date_to_Days );</PRE>
  1305. <PRE>
  1306.   $lower = Date_to_Days($year1,$month1,$day1);
  1307.   $upper = Date_to_Days($year2,$month2,$day2);</PRE>
  1308. <PRE>
  1309.   $date = Date_to_Days($year,$month,$day);</PRE>
  1310. <PRE>
  1311.   if (($date >= $lower) && ($date <= $upper))
  1312.   {
  1313.       # ok
  1314.   }
  1315.   else
  1316.   {
  1317.       # not ok
  1318.   }</PRE>
  1319. <P></P>
  1320. <LI><STRONG>)</STRONG><BR>
  1321.  
  1322. How do I verify wether someone has a certain age?
  1323. <PRE>
  1324.   use Date::Calc qw( Decode_Date_EU Today leap_year Delta_Days );</PRE>
  1325. <PRE>
  1326.   $date = <STDIN>; # get birthday</PRE>
  1327. <PRE>
  1328.   ($year1,$month1,$day1) = Decode_Date_EU($date);</PRE>
  1329. <PRE>
  1330.   ($year2,$month2,$day2) = Today();</PRE>
  1331. <PRE>
  1332.   if (($day1 == 29) && ($month1 == 2) && !leap_year($year2))
  1333.       { $day1--; }</PRE>
  1334. <PRE>
  1335.   if ( (($year2 - $year1) >  18) ||
  1336.      ( (($year2 - $year1) == 18) &&
  1337.      (Delta_Days($year2,$month1,$day1, $year2,$month2,$day2) >= 0) ) )
  1338.   {
  1339.       print "Ok - you are over 18.\n";
  1340.   }
  1341.   else
  1342.   {
  1343.       print "Sorry - you aren't 18 yet!\n";
  1344.   }</PRE>
  1345. <P></P>
  1346. <LI><STRONG>)</STRONG><BR>
  1347.  
  1348. How do I calculate the number of the week of month
  1349. the current date lies in?
  1350. <P>For example:</P>
  1351. <PRE>
  1352.             April 1998
  1353.     Mon Tue Wed Thu Fri Sat Sun
  1354.               1   2   3   4   5  =  week #1
  1355.       6   7   8   9  10  11  12  =  week #2
  1356.      13  14  15  16  17  18  19  =  week #3
  1357.      20  21  22  23  24  25  26  =  week #4
  1358.      27  28  29  30              =  week #5</PRE>
  1359. <P>Solution:</P>
  1360. <PRE>
  1361.   use Date::Calc qw( Today Day_of_Week );</PRE>
  1362. <PRE>
  1363.   ($year,$month,$day) = Today();</PRE>
  1364. <PRE>
  1365.   $week = int(($day + Day_of_Week($year,$month,1) - 2) / 7) + 1;</PRE>
  1366. <P></P>
  1367. <LI><STRONG>)</STRONG><BR>
  1368.  
  1369. How do I calculate wether a given date is the 1st, 2nd, 3rd, 4th or 5th
  1370. of that day of week in the given month?
  1371. <P>For example:</P>
  1372. <PRE>
  1373.            October 2000
  1374.     Mon Tue Wed Thu Fri Sat Sun
  1375.                               1
  1376.       2   3   4   5   6   7   8
  1377.       9  10  11  12  13  14  15
  1378.      16  17  18  19  20  21  22
  1379.      23  24  25  26  27  28  29
  1380.      30  31</PRE>
  1381. <P>Is Sunday, the 15th of October 2000, the 1st, 2nd, 3rd, 4th or 5th
  1382. Sunday of that month?</P>
  1383. <P>Solution:</P>
  1384. <PRE>
  1385.   use Date::Calc qw( Day_of_Week Delta_Days
  1386.                      Nth_Weekday_of_Month_Year
  1387.                      Date_to_Text_Long English_Ordinal
  1388.                      Day_of_Week_to_Text Month_to_Text );</PRE>
  1389. <PRE>
  1390.   ($year,$month,$day) = (2000,10,15);</PRE>
  1391. <PRE>
  1392.   $dow = Day_of_Week($year,$month,$day);</PRE>
  1393. <PRE>
  1394.   $n = int( Delta_Days(
  1395.             Nth_Weekday_of_Month_Year($year,$month,$dow,1),
  1396.             $year,$month,$day)
  1397.             / 7) + 1;</PRE>
  1398. <PRE>
  1399.   printf("%s is the %s %s in %s %d.\n",
  1400.       Date_to_Text_Long($year,$month,$day),
  1401.       English_Ordinal($n),
  1402.       Day_of_Week_to_Text($dow),
  1403.       Month_to_Text($month),
  1404.       $year);</PRE>
  1405. <P>This prints:</P>
  1406. <PRE>
  1407.   Sunday, October 15th 2000 is the 3rd Sunday in October 2000.</PRE>
  1408. <P></P>
  1409. <LI><STRONG>)</STRONG><BR>
  1410.  
  1411. How do I calculate the date of the Wednesday of the same week as
  1412. the current date?
  1413. <P>Solution #1:</P>
  1414. <PRE>
  1415.   use Date::Calc qw( Today Day_of_Week Add_Delta_Days );</PRE>
  1416. <PRE>
  1417.   $searching_dow = 3; # 3 = Wednesday</PRE>
  1418. <PRE>
  1419.   @today = Today();</PRE>
  1420. <PRE>
  1421.   $current_dow = Day_of_Week(@today);</PRE>
  1422. <PRE>
  1423.   @date = Add_Delta_Days(@today, $searching_dow - $current_dow);</PRE>
  1424. <P>Solution #2:</P>
  1425. <PRE>
  1426.   use Date::Calc qw( Today Add_Delta_Days
  1427.                      Monday_of_Week Week_of_Year );</PRE>
  1428. <PRE>
  1429.   $searching_dow = 3; # 3 = Wednesday</PRE>
  1430. <PRE>
  1431.   @today = Today();</PRE>
  1432. <PRE>
  1433.   @date = Add_Delta_Days( Monday_of_Week( Week_of_Year(@today) ),
  1434.                           $searching_dow - 1 );</PRE>
  1435. <P>Solution #3:</P>
  1436. <PRE>
  1437.   use Date::Calc qw( Standard_to_Business Today
  1438.                      Business_to_Standard );</PRE>
  1439. <PRE>
  1440.   @business = Standard_to_Business(Today());</PRE>
  1441. <PRE>
  1442.   $business[2] = 3; # 3 = Wednesday</PRE>
  1443. <PRE>
  1444.   @date = Business_to_Standard(@business);</PRE>
  1445. <P></P>
  1446. <LI><STRONG>)</STRONG><BR>
  1447.  
  1448. How can I add a week offset to a business date (including across
  1449. year boundaries)?
  1450. <PRE>
  1451.   use Date::Calc qw( Business_to_Standard Add_Delta_Days
  1452.                      Standard_to_Business );</PRE>
  1453. <PRE>
  1454.   @temp = Business_to_Standard($year,$week,$dow);</PRE>
  1455. <PRE>
  1456.   @temp = Add_Delta_Days(@temp, $week_offset * 7);</PRE>
  1457. <PRE>
  1458.   ($year,$week,$dow) = Standard_to_Business(@temp);</PRE>
  1459. <P></P>
  1460. <LI><STRONG>)</STRONG><BR>
  1461.  
  1462. How do I calculate the last and the next Saturday for any
  1463. given date?
  1464. <PRE>
  1465.   use Date::Calc qw( Today Day_of_Week Add_Delta_Days
  1466.                      Day_of_Week_to_Text Date_to_Text );</PRE>
  1467. <PRE>
  1468.   $searching_dow = 6; # 6 = Saturday</PRE>
  1469. <PRE>
  1470.   @today = Today();</PRE>
  1471. <PRE>
  1472.   $current_dow = Day_of_Week(@today);</PRE>
  1473. <PRE>
  1474.   if ($searching_dow == $current_dow)
  1475.   {
  1476.       @prev = Add_Delta_Days(@today,-7);
  1477.       @next = Add_Delta_Days(@today,+7);
  1478.   }
  1479.   else
  1480.   {
  1481.       if ($searching_dow > $current_dow)
  1482.       {
  1483.           @next = Add_Delta_Days(@today,
  1484.                     $searching_dow - $current_dow);
  1485.           @prev = Add_Delta_Days(@next,-7);
  1486.       }
  1487.       else
  1488.       {
  1489.           @prev = Add_Delta_Days(@today,
  1490.                     $searching_dow - $current_dow);
  1491.           @next = Add_Delta_Days(@prev,+7);
  1492.       }
  1493.   }</PRE>
  1494. <PRE>
  1495.   $dow = Day_of_Week_to_Text($searching_dow);</PRE>
  1496. <PRE>
  1497.   print "Today is:      ", ' ' x length($dow),
  1498.                                Date_to_Text(@today), "\n";
  1499.   print "Last $dow was:     ", Date_to_Text(@prev),  "\n";
  1500.   print "Next $dow will be: ", Date_to_Text(@next),  "\n";</PRE>
  1501. <P>This will print something like:</P>
  1502. <PRE>
  1503.   Today is:              Sun 12-Apr-1998
  1504.   Last Saturday was:     Sat 11-Apr-1998
  1505.   Next Saturday will be: Sat 18-Apr-1998</PRE>
  1506. <P></P>
  1507. <LI><STRONG>)</STRONG><BR>
  1508.  
  1509. How can I calculate the last business day (payday!) of a month?
  1510. <P>Solution #1 (holidays <STRONG>NOT</STRONG> taken into account):</P>
  1511. <PRE>
  1512.   use Date::Calc qw( Days_in_Month Day_of_Week Add_Delta_Days );</PRE>
  1513. <PRE>
  1514.   $day = Days_in_Month($year,$month);
  1515.   $dow = Day_of_Week($year,$month,$day);
  1516.   if ($dow > 5)
  1517.   {
  1518.       ($year,$month,$day) =
  1519.           Add_Delta_Days($year,$month,$day, 5-$dow);
  1520.   }</PRE>
  1521. <P>Solution #2 (holidays taken into account):</P>
  1522. <P>This solution expects a multi-dimensional array ``<CODE>@holiday</CODE>'', which
  1523. contains all holidays, as follows: ``<CODE>$holiday[$year][$month][$day] = 1;</CODE>''.</P>
  1524. <P>(See the description of the function ``<CODE>Easter_Sunday()</CODE>'' further above for
  1525. how to calculate the moving (variable) christian feast days!)</P>
  1526. <P>Days which are not holidays remain undefined or should have a value of zero
  1527. in this array.</P>
  1528. <PRE>
  1529.   use Date::Calc qw( Days_in_Month Add_Delta_Days Day_of_Week );</PRE>
  1530. <PRE>
  1531.   $day = Days_in_Month($year,$month);
  1532.   while (1)
  1533.   {
  1534.       while ($holiday[$year][$month][$day])
  1535.       {
  1536.           ($year,$month,$day) =
  1537.               Add_Delta_Days($year,$month,$day, -1);
  1538.       }
  1539.       $dow = Day_of_Week($year,$month,$day);
  1540.       if ($dow > 5)
  1541.       {
  1542.           ($year,$month,$day) =
  1543.               Add_Delta_Days($year,$month,$day, 5-$dow);
  1544.       }
  1545.       else { last; }
  1546.   }</PRE>
  1547. <P></P>
  1548. <LI><STRONG>)</STRONG><BR>
  1549.  
  1550. How do I convert a MS Visual Basic ``DATETIME'' value into its date
  1551. and time constituents?
  1552. <PRE>
  1553.   use Date::Calc qw( Add_Delta_DHMS Date_to_Text );</PRE>
  1554. <PRE>
  1555.   $datetime = "35883.121653";</PRE>
  1556. <PRE>
  1557.   ($Dd,$Dh,$Dm,$Ds) = ($datetime =~ /^(\d+)\.(\d\d)(\d\d)(\d\d)$/);</PRE>
  1558. <PRE>
  1559.   ($year,$month,$day, $hour,$min,$sec) =
  1560.       Add_Delta_DHMS(1900,1,1, 0,0,0, $Dd,$Dh,$Dm,$Ds);</PRE>
  1561. <PRE>
  1562.   printf("The given date is %s %02d:%02d:%02d\n",
  1563.       Date_to_Text($year,$month,$day), $hour, $min, $sec);</PRE>
  1564. <P>This prints:</P>
  1565. <PRE>
  1566.   The given date is Tue 31-Mar-1998 12:16:53</PRE>
  1567. <P></P>
  1568. <LI><STRONG>)</STRONG><BR>
  1569.  
  1570. How can I send a reminder to members of a group on the day
  1571. before a meeting which occurs every first Friday of a month?
  1572. <PRE>
  1573.   use Date::Calc qw( Today Date_to_Days Add_Delta_YMD
  1574.                      Nth_Weekday_of_Month_Year );</PRE>
  1575. <PRE>
  1576.   ($year,$month,$day) = Today();</PRE>
  1577. <PRE>
  1578.   $tomorrow = Date_to_Days($year,$month,$day) + 1;</PRE>
  1579. <PRE>
  1580.   $dow = 5; # 5 = Friday
  1581.   $n   = 1; # 1 = First of that day of week</PRE>
  1582. <PRE>
  1583.   $meeting_this_month = Date_to_Days(
  1584.       Nth_Weekday_of_Month_Year($year,$month,$dow,$n) );</PRE>
  1585. <PRE>
  1586.   ($year,$month,$day) = Add_Delta_YMD($year,$month,$day, 0,1,0);</PRE>
  1587. <PRE>
  1588.   $meeting_next_month = Date_to_Days(
  1589.       Nth_Weekday_of_Month_Year($year,$month,$dow,$n) );</PRE>
  1590. <PRE>
  1591.   if (($tomorrow == $meeting_this_month) ||
  1592.       ($tomorrow == $meeting_next_month))
  1593.   {
  1594.       # Send reminder e-mail!
  1595.   }</PRE>
  1596. <P></P>
  1597. <LI><STRONG>)</STRONG><BR>
  1598.  
  1599. How can I print a date in a different format than provided by
  1600. the functions ``<CODE>Date_to_Text()</CODE>'', ``<CODE>Date_to_Text_Long()</CODE>'' or
  1601. ``<CODE>Compressed_to_Text()</CODE>''?
  1602. <PRE>
  1603.   use Date::Calc qw( Today Day_of_Week_to_Text
  1604.                      Day_of_Week Month_to_Text
  1605.                      English_Ordinal );</PRE>
  1606. <PRE>
  1607.   ($year,$month,$day) = Today();</PRE>
  1608. <P>For example with leading zeros for the day: ``Fri 03-Jan-1964''</P>
  1609. <PRE>
  1610.   printf("%.3s %02d-%.3s-%d\n",
  1611.       Day_of_Week_to_Text(Day_of_Week($year,$month,$day)),
  1612.       $day,
  1613.       Month_to_Text($month),
  1614.       $year);</PRE>
  1615. <P>For example in U.S. american format: ``April 12th, 1998''</P>
  1616. <PRE>
  1617.   $string = sprintf("%s %s, %d",
  1618.                 Month_to_Text($month),
  1619.                 English_Ordinal($day),
  1620.                 $year);</PRE>
  1621. <P>(See also <EM>perlfunc(1)/printf</EM> and/or <EM>perlfunc(1)/sprintf</EM>!)</P>
  1622. <P></P>
  1623. <LI><STRONG>)</STRONG><BR>
  1624.  
  1625. How can I iterate through a range of dates?
  1626. <PRE>
  1627.   use Date::Calc qw( Delta_Days Add_Delta_Days );</PRE>
  1628. <PRE>
  1629.   @start = (1999,5,27);
  1630.   @stop  = (1999,6,1);</PRE>
  1631. <PRE>
  1632.   $j = Delta_Days(@start,@stop);</PRE>
  1633. <PRE>
  1634.   for ( $i = 0; $i <= $j; $i++ )
  1635.   {
  1636.       @date = Add_Delta_Days(@start,$i);
  1637.       printf("%4d/%02d/%02d\n", @date);
  1638.   }</PRE>
  1639. <P>Note that the loop can be improved; see also the recipe below.</P>
  1640. <P></P>
  1641. <LI><STRONG>)</STRONG><BR>
  1642.  
  1643. How can I create a (Perl) list of dates in a certain range?
  1644. <PRE>
  1645.   use Date::Calc qw( Delta_Days Add_Delta_Days Date_to_Text );</PRE>
  1646. <PRE>
  1647.   sub date_range
  1648.   {
  1649.       my(@date) = (@_)[0,1,2];
  1650.       my(@list);
  1651.       my($i);</PRE>
  1652. <PRE>
  1653.       $i = Delta_Days(@_);
  1654.       while ($i-- >= 0)
  1655.       {
  1656.           push( @list, [ @date ] );
  1657.           @date = Add_Delta_Days(@date, 1) if ($i >= 0);
  1658.       }
  1659.       return(@list);
  1660.   }</PRE>
  1661. <PRE>
  1662.   @range = &date_range(1999,11,3, 1999,12,24); # in chronological order</PRE>
  1663. <PRE>
  1664.   foreach $date (@range)
  1665.   {
  1666.       print Date_to_Text(@{$date}), "\n";
  1667.   }</PRE>
  1668. <P>Note that you probably shouldn't use this one, because it is much
  1669. more efficient to iterate through all the dates (as shown in the
  1670. recipe immediately above) than to construct such an array and then
  1671. to loop through it. Also, it is much more space-efficient not to
  1672. create this array.</P>
  1673. <P></P>
  1674. <LI><STRONG>)</STRONG><BR>
  1675.  
  1676. How can I calculate the difference in days between dates,
  1677. but without counting Saturdays and Sundays?
  1678. <PRE>
  1679.   sub Delta_Business_Days
  1680.   {
  1681.       my(@date1) = (@_)[0,1,2];
  1682.       my(@date2) = (@_)[3,4,5];
  1683.       my($minus,$result,$dow1,$dow2,$diff,$temp);</PRE>
  1684. <PRE>
  1685.       $minus  = 0;
  1686.       $result = Delta_Days(@date1,@date2);
  1687.       if ($result != 0)
  1688.       {
  1689.           if ($result < 0)
  1690.           {
  1691.               $minus = 1;
  1692.               $result = -$result;
  1693.               $dow1 = Day_of_Week(@date2);
  1694.               $dow2 = Day_of_Week(@date1);
  1695.           }
  1696.           else
  1697.           {
  1698.               $dow1 = Day_of_Week(@date1);
  1699.               $dow2 = Day_of_Week(@date2);
  1700.           }
  1701.           $diff = $dow2 - $dow1;
  1702.           $temp = $result;
  1703.           if ($diff != 0)
  1704.           {
  1705.               if ($diff < 0)
  1706.               {
  1707.                   $diff += 7;
  1708.               }
  1709.               $temp -= $diff;
  1710.               $dow1 += $diff;
  1711.               if ($dow1 > 6)
  1712.               {
  1713.                   $result--;
  1714.                   if ($dow1 > 7)
  1715.                   {
  1716.                       $result--;
  1717.                   }
  1718.               }
  1719.           }
  1720.           if ($temp != 0)
  1721.           {
  1722.               $temp /= 7;
  1723.               $result -= ($temp << 1);
  1724.           }
  1725.       }
  1726.       if ($minus) { return -$result; }
  1727.       else        { return  $result; }
  1728.   }</PRE>
  1729. <P>This solution is probably of little practical value, however,
  1730. because it doesn't take legal holidays into account.</P>
  1731. <P></P></OL>
  1732. <P>
  1733. <HR>
  1734. <H1><A NAME="see also">SEE ALSO</A></H1>
  1735. <P>perl(1), perlfunc(1), perlsub(1), perlmod(1),
  1736. perlxs(1), perlxstut(1), perlguts(1).</P>
  1737. <P>news:news.answers
  1738. ``Calendar FAQ, v. 1.9 (modified 25 Apr 1998) Part 1/3''</P>
  1739. <P>news:news.answers
  1740. ``Calendar FAQ, v. 1.9 (modified 25 Apr 1998) Part 2/3''</P>
  1741. <P>news:news.answers
  1742. ``Calendar FAQ, v. 1.9 (modified 25 Apr 1998) Part 3/3''</P>
  1743. <P><A HREF="http://www.math.uio.no/faq/calendars/faq.html">http://www.math.uio.no/faq/calendars/faq.html</A></P>
  1744. <P><A HREF="http://www.pip.dknet.dk/~pip10160/calendar.html">http://www.pip.dknet.dk/~pip10160/calendar.html</A></P>
  1745. <P>(All authored by Claus Tondering <<A HREF="mailto:c-t@pip.dknet.dk">c-t@pip.dknet.dk</A>>)</P>
  1746. <P>
  1747. <HR>
  1748. <H1><A NAME="limitations">LIMITATIONS</A></H1>
  1749. <P>In the current implementation of this package, the selected language
  1750. is stored in a global variable.</P>
  1751. <P>Therefore, when you are using a threaded Perl, this may cause undesired
  1752. side effects (of one thread always selecting the language for <STRONG>ALL OTHER</STRONG>
  1753. threads as well).</P>
  1754. <P>
  1755. <HR>
  1756. <H1><A NAME="version">VERSION</A></H1>
  1757. <P>This man page documents ``Date::Calc'' version 4.3.</P>
  1758. <P>
  1759. <HR>
  1760. <H1><A NAME="author">AUTHOR</A></H1>
  1761. <PRE>
  1762.   Steffen Beyer
  1763.   Ainmillerstr. 5 / App. 513
  1764.   D-80801 Munich
  1765.   Germany</PRE>
  1766. <PRE>
  1767.   <A HREF="mailto:sb@engelschall.com">mailto:sb@engelschall.com</A>
  1768.   <A HREF="http://www.engelschall.com/u/sb/download/">http://www.engelschall.com/u/sb/download/</A></PRE>
  1769. <P><STRONG>Please contact me by e-mail whenever possible!</STRONG></P>
  1770. <P>
  1771. <HR>
  1772. <H1><A NAME="copyright">COPYRIGHT</A></H1>
  1773. <P>Copyright (c) 1995 - 2000 by Steffen Beyer.
  1774. All rights reserved.</P>
  1775. <P>
  1776. <HR>
  1777. <H1><A NAME="license">LICENSE</A></H1>
  1778. <P>This package is free software; you can redistribute it and/or
  1779. modify it under the same terms as Perl itself, i.e., under the
  1780. terms of the ``Artistic License'' or the ``GNU General Public License''.</P>
  1781. <P>The C library at the core of this Perl module can additionally
  1782. be redistributed and/or modified under the terms of the
  1783. ``GNU Library General Public License''.</P>
  1784. <P>Please refer to the files ``Artistic.txt'', ``GNU_GPL.txt'' and
  1785. ``GNU_LGPL.txt'' in this distribution for details!</P>
  1786. <P>
  1787. <HR>
  1788. <H1><A NAME="disclaimer">DISCLAIMER</A></H1>
  1789. <P>This package is distributed in the hope that it will be useful,
  1790. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1791. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.</P>
  1792. <P>See the ``GNU General Public License'' for more details.</P>
  1793. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  1794. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  1795. <STRONG><P CLASS=block> Date::Calc - Gregorian calendar date calculations</P></STRONG>
  1796. </TD></TR>
  1797. </TABLE>
  1798.  
  1799. </BODY>
  1800.  
  1801. </HTML>
  1802.