![]() |
![]()
| ![]() |
![]()
NAMEDate::Manip::Examples - examples of how to use Date::Manip DESCRIPTIONThis document includes a number of examples on how to do common Date::Manip operations. I will be happy to add new examples over time, and welcome suggestions and examples to include. In most cases, an example will include two different ways of getting the answer. The first way will be using the new (as of 6.00) OO modules. The second will be using the old-style functional interface. It should be noted that any time you want to work with alternate time zones, the OO interface is STRONGLY recommended since the functional interface does not preserve time zone information with the date, and may therefore give incorrect results in some cases. However, working in the time zone of the system should give correct results. It should be noted that, in the examples below, it appears that the OO method often requires more lines of code than the functional interface. There are a number of ways to shorten the OO method, but for the examples, I wanted to include all the steps explicitly. PARSING A DATEDates can be parsed in practically any form in common usage:
The Date::Manip::Date manual has a list of all valid formats. PARSING AN AMOUNT OF TIMEAmounts of time (referred to as deltas) can also be parsed:
TO CALCULATE THE AMOUNT OF TIME BETWEEN TWO DATES$datestr1 = "Jan 30 1999 13:00 EST"; $datestr2 = "2/Mar/1999 15:30:00 +0500";
The Date::Manip::Calc manual has information about these, and other types of calculations. TO ADD AN AMOUNT OF TIME TO A DATETo find a second date a given amount of time before or after a first date, use the following: $datestr = "Jan 30 1999 13:00 EST"; $deltastr = "12 hours ago"; $deltastr = "in 3 business days";
If the delta is a business delta, it will do a business mode calculation. The Date::Manip::Calc manual has information about these, and other types of calculations. COMPARE TWO DATESTo take two different dates and see which is earlier, do the following: $datestr1 = "Jan 30 1999 13:00 EST"; $datestr2 = "2/Mar/1999 15:30:00 +0500";
TO EXTRACT INFORMATION ABOUT A DATE OR DELTAIf you have a date or a delta, you can extract information about them as follows: $datestr = "1:24:08 PM EST Feb 3, 1996"; $deltastr = "12 hours ago";
The Date::Manip::Date manual contains all of the format codes that can be used to extract information from a date. The Date::Manip::Delta manual contains the codes for a delta. WORKING WITH EPOCHDate::Manip can easily be used to work with the number of seconds since the epoch (Jan 1, 1970 00:00:00 UTC). If you have a date, and you want to find out how many seconds it is after the epoch, you can do it in the following ways: $datestr = "1999-04-30-15:30:00 EDT"; $secs = 1234567;
Note that Date::Manip will work with both positive seconds (for dates that have come since the epoch) and negative seconds (for dates that occurred before the epoch). RECURRING EVENTSTo find a list of dates where a recurring event happens (even very complex recurrences), do the following:
The Date::Manip::Recur manual contains information about recurring events. WORKING WITH DATES IN ANOTHER LANGUAGEIf you want to work with dates in a language other than English (but you are only working with a single language), do the following:
The Date::Manip::Config manual has a list of all supported languages (in the section on the Language config variable). The meaning of the DateFormat config variable is also included. WORKING WITH TWO DIFFERENT LANGUAGESIf you want to work with dates in two (or more) languages, it is STRONGLY recommended that you use the OO interface. The functional interface will be much slower since it has to re-initialize a lot of language-specific stuff every time you switch back and forth between languages.
BUGS AND QUESTIONSPlease refer to the Date::Manip::Problems documentation for information on submitting bug reports or questions to the author. SEE ALSODate::Manip - main module documentation LICENSEThis script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. AUTHORSullivan Beck (sbeck@cpan.org)
|