| 1 |
# Copyright (c) 1995-2026 Sullivan Beck. All rights reserved. |
| 2 |
# This program is free software; you can redistribute it and/or modify it |
| 3 |
# under the same terms as Perl itself. |
| 4 |
|
| 5 |
=pod |
| 6 |
|
| 7 |
=head1 NAME |
| 8 |
|
| 9 |
Date::Manip::Examples - examples of how to use Date::Manip |
| 10 |
|
| 11 |
=head1 DESCRIPTION |
| 12 |
|
| 13 |
This document includes a number of examples on how to do common |
| 14 |
Date::Manip operations. I will be happy to add new examples over |
| 15 |
time, and welcome suggestions and examples to include. |
| 16 |
|
| 17 |
In most cases, an example will include two different ways of getting |
| 18 |
the answer. The first way will be using the new (as of 6.00) OO |
| 19 |
modules. The second will be using the old-style functional interface. |
| 20 |
|
| 21 |
It should be noted that any time you want to work with alternate |
| 22 |
time zones, the OO interface is STRONGLY recommended since the functional |
| 23 |
interface does not preserve time zone information with the date, and |
| 24 |
may therefore give incorrect results in some cases. However, working |
| 25 |
in the time zone of the system should give correct results. |
| 26 |
|
| 27 |
It should be noted that, in the examples below, it appears that the OO |
| 28 |
method often requires more lines of code than the functional |
| 29 |
interface. There are a number of ways to shorten the OO method, but |
| 30 |
for the examples, I wanted to include all the steps explicitly. |
| 31 |
|
| 32 |
=head1 PARSING A DATE |
| 33 |
|
| 34 |
Dates can be parsed in practically any form in common usage: |
| 35 |
|
| 36 |
=over 4 |
| 37 |
|
| 38 |
=item B<OO method> |
| 39 |
|
| 40 |
$date = new Date::Manip::Date; |
| 41 |
$err = $date->parse("today"); |
| 42 |
$err = $date->parse("1st Thursday in June 1992"); |
| 43 |
$err = $date->parse("05/10/93"); |
| 44 |
$err = $date->parse("12:30 Dec 12th 1880"); |
| 45 |
$err = $date->parse("8:00pm December tenth"); |
| 46 |
|
| 47 |
=item B<Functional> |
| 48 |
|
| 49 |
$date = ParseDate("today"); |
| 50 |
$date = ParseDate("1st Thursday in June 1992"); |
| 51 |
$date = ParseDate("05/10/93"); |
| 52 |
$date = ParseDate("12:30 Dec 12th 1880"); |
| 53 |
$date = ParseDate("8:00pm December tenth"); |
| 54 |
|
| 55 |
=back |
| 56 |
|
| 57 |
The L<Date::Manip::Date> manual has a list of all valid formats. |
| 58 |
|
| 59 |
=head1 PARSING AN AMOUNT OF TIME |
| 60 |
|
| 61 |
Amounts of time (referred to as deltas) can also be parsed: |
| 62 |
|
| 63 |
=over 4 |
| 64 |
|
| 65 |
=item B<OO method> |
| 66 |
|
| 67 |
$delta = new Date::Manip::Delta; |
| 68 |
$err = $delta->parse("in 12 hours"); |
| 69 |
$err = $delta->parse("-1:30:0"); |
| 70 |
$err = $delta->parse("4 business days later"); |
| 71 |
|
| 72 |
=item B<Functional> |
| 73 |
|
| 74 |
$delta = ParseDateDelta("in 12 hours"); |
| 75 |
$delta = ParseDateDelta("-1:30:0"); |
| 76 |
$delta = ParseDateDelta("4 business days later"); |
| 77 |
|
| 78 |
=back |
| 79 |
|
| 80 |
=head1 TO CALCULATE THE AMOUNT OF TIME BETWEEN TWO DATES |
| 81 |
|
| 82 |
$datestr1 = "Jan 30 1999 13:00 EST"; |
| 83 |
$datestr2 = "2/Mar/1999 15:30:00 +0500"; |
| 84 |
|
| 85 |
=over 4 |
| 86 |
|
| 87 |
=item B<OO method> |
| 88 |
|
| 89 |
$date1 = new Date::Manip::Date; |
| 90 |
$date2 = $date1->new_date(); |
| 91 |
$err = $date1->parse($datestr1); |
| 92 |
$err = $date2->parse($datestr2); |
| 93 |
|
| 94 |
To get an exact amount of time between the two dates (expressed only in terms of |
| 95 |
hours, minutes, seconds), use: |
| 96 |
|
| 97 |
$delta = $date1->calc($date2); |
| 98 |
|
| 99 |
To get an approximate amount of time (expressed in terms of years, months, weeks, |
| 100 |
etc. in terms that a human would typically think of), use: |
| 101 |
|
| 102 |
$delta = $date1->calc($date2,"approx"); |
| 103 |
|
| 104 |
=item B<Functional> |
| 105 |
|
| 106 |
$date1 = ParseDate($string1); |
| 107 |
$date2 = ParseDate($string2); |
| 108 |
|
| 109 |
To get an exact amount: |
| 110 |
|
| 111 |
$delta = DateCalc($date1,$date2); |
| 112 |
|
| 113 |
and the approximate amount: |
| 114 |
|
| 115 |
$delta = DateCalc($date1,$date2,1); |
| 116 |
|
| 117 |
=back |
| 118 |
|
| 119 |
The L<Date::Manip::Calc> manual has information about these, and other types of |
| 120 |
calculations. |
| 121 |
|
| 122 |
=head1 TO ADD AN AMOUNT OF TIME TO A DATE |
| 123 |
|
| 124 |
To find a second date a given amount of time before or after a first date, |
| 125 |
use the following: |
| 126 |
|
| 127 |
$datestr = "Jan 30 1999 13:00 EST"; |
| 128 |
$deltastr = "12 hours ago"; |
| 129 |
$deltastr = "in 3 business days"; |
| 130 |
|
| 131 |
=over 4 |
| 132 |
|
| 133 |
=item B<OO method> |
| 134 |
|
| 135 |
$date = new Date::Manip::Date; |
| 136 |
$delta = $date->new_delta(); |
| 137 |
$date->parse($datestr); |
| 138 |
$delta->parse($deltastr); |
| 139 |
|
| 140 |
$d = $date->calc($delta); |
| 141 |
|
| 142 |
=item B<Functional> |
| 143 |
|
| 144 |
$date = DateCalc($datestr,$deltastr); |
| 145 |
|
| 146 |
=back |
| 147 |
|
| 148 |
If the delta is a business delta, it will do a business mode calculation. |
| 149 |
|
| 150 |
The L<Date::Manip::Calc> manual has information about these, and other types of |
| 151 |
calculations. |
| 152 |
|
| 153 |
=head1 COMPARE TWO DATES |
| 154 |
|
| 155 |
To take two different dates and see which is earlier, do the |
| 156 |
following: |
| 157 |
|
| 158 |
$datestr1 = "Jan 30 1999 13:00 EST"; |
| 159 |
$datestr2 = "2/Mar/1999 15:30:00 +0500"; |
| 160 |
|
| 161 |
=over 4 |
| 162 |
|
| 163 |
=item B<OO method> |
| 164 |
|
| 165 |
$date1 = new Date::Manip::Date; |
| 166 |
$date2 = $date1->new_date; |
| 167 |
$date1->parse($datestr1); |
| 168 |
$date2->parse($datestr2); |
| 169 |
|
| 170 |
$date1->cmp($date2); |
| 171 |
=> -1, 0, 1 |
| 172 |
|
| 173 |
=item B<Functional> |
| 174 |
|
| 175 |
$date1 = ParseDate($datestr1); |
| 176 |
$date2 = ParseDate($datestr2); |
| 177 |
|
| 178 |
Date_Cmp($date1,$date2); |
| 179 |
=> -1, 0, 1 |
| 180 |
|
| 181 |
=back |
| 182 |
|
| 183 |
=head1 TO EXTRACT INFORMATION ABOUT A DATE OR DELTA |
| 184 |
|
| 185 |
If you have a date or a delta, you can extract information |
| 186 |
about them as follows: |
| 187 |
|
| 188 |
$datestr = "1:24:08 PM EST Feb 3, 1996"; |
| 189 |
$deltastr = "12 hours ago"; |
| 190 |
|
| 191 |
=over 4 |
| 192 |
|
| 193 |
=item B<OO method> |
| 194 |
|
| 195 |
$date = new Date::Manip::Date; |
| 196 |
$delta = $date->new_delta(); |
| 197 |
$date->parse($datestr); |
| 198 |
$delta->parse($deltastr); |
| 199 |
|
| 200 |
$str = $date->printf("It is now %T on %b %e, %Y."); |
| 201 |
=> "It is now 13:24:08 on Feb 3, 1996." |
| 202 |
|
| 203 |
$str = $delta->printf("In %hv hours, %mv minutes, %sv seconds"); |
| 204 |
=> "In -12 hours, 0 minutes, 0 seconds"; |
| 205 |
|
| 206 |
=item B<Functional> |
| 207 |
|
| 208 |
$str = UnixDate($datestr,"It is now %T on %b %e, %Y."); |
| 209 |
=> "It is now 13:24:08 on Feb 3, 1996." |
| 210 |
|
| 211 |
$str = Delta_Format($deltastr,"In %hv hours, %mv minutes, %sv seconds"); |
| 212 |
=> "In -12 hours, 0 minutes, 0 seconds"; |
| 213 |
|
| 214 |
=back |
| 215 |
|
| 216 |
The L<Date::Manip::Date> manual contains all of the format codes that can |
| 217 |
be used to extract information from a date. The L<Date::Manip::Delta> manual |
| 218 |
contains the codes for a delta. |
| 219 |
|
| 220 |
=head1 WORKING WITH EPOCH |
| 221 |
|
| 222 |
Date::Manip can easily be used to work with the number of seconds |
| 223 |
since the epoch (Jan 1, 1970 00:00:00 UTC). |
| 224 |
|
| 225 |
If you have a date, and you want to find out how many seconds it is |
| 226 |
after the epoch, you can do it in the following ways: |
| 227 |
|
| 228 |
$datestr = "1999-04-30-15:30:00 EDT"; |
| 229 |
$secs = 1234567; |
| 230 |
|
| 231 |
=over 4 |
| 232 |
|
| 233 |
=item B<OO method> |
| 234 |
|
| 235 |
To find out how many seconds have elapsed on a certain date, you |
| 236 |
can do the following: |
| 237 |
|
| 238 |
$date = new Date::Manip::Date; |
| 239 |
$err = $date->parse($datestr); |
| 240 |
|
| 241 |
$str = $date->printf('%s'); |
| 242 |
=> number of seconds |
| 243 |
|
| 244 |
To find out the date that is a certain number of seconds since the |
| 245 |
epoch, you can use the following: |
| 246 |
|
| 247 |
$date = new Date::Manip::Date; |
| 248 |
$err = $date->parse("epoch $secs"); |
| 249 |
|
| 250 |
C<$date> now contains the date wanted (in the local time zone) |
| 251 |
|
| 252 |
=item B<Functional> |
| 253 |
|
| 254 |
To find out how many seconds have elapsed: |
| 255 |
|
| 256 |
$str = UnixDate($datestr,'%s'); |
| 257 |
=> number of seconds |
| 258 |
|
| 259 |
To find the date that is a number of seconds since the epoch: |
| 260 |
|
| 261 |
$date = ParseDateString("epoch $secs"); |
| 262 |
|
| 263 |
=back |
| 264 |
|
| 265 |
Note that Date::Manip will work with both positive seconds (for dates |
| 266 |
that have come since the epoch) and negative seconds (for dates that |
| 267 |
occurred before the epoch). |
| 268 |
|
| 269 |
=head1 RECURRING EVENTS |
| 270 |
|
| 271 |
To find a list of dates where a recurring event happens (even very complex |
| 272 |
recurrences), do the following: |
| 273 |
|
| 274 |
=over 4 |
| 275 |
|
| 276 |
=item B<OO method> |
| 277 |
|
| 278 |
# To find the 2nd Tuesday of every month from Jan 1 1999 to Apr 30 1999 |
| 279 |
|
| 280 |
$recur = new Date::Manip::Recur; |
| 281 |
$start = $recur->new_date(); |
| 282 |
$end = $recur->new_date(); |
| 283 |
$start->parse("Jan 1 1999"); |
| 284 |
$end->parse("Apr 30 1999"); |
| 285 |
|
| 286 |
$recur->parse("0:1*2:2:0:0:0",$start,$end); |
| 287 |
@date = $recur->dates(); |
| 288 |
|
| 289 |
# To find the Monday after Easter in 1997-1999 |
| 290 |
|
| 291 |
$recur = new Date::Manip::Recur; |
| 292 |
$recur->parse("*1997-1999:0:0:0:0:0:0*EASTER,ND1"); |
| 293 |
@date = $recur->dates(); |
| 294 |
|
| 295 |
=item B<Functional> |
| 296 |
|
| 297 |
# To find the 2nd Tuesday of every month from Jan 1 1999 to Apr 30 1999 |
| 298 |
@date = ParseRecur("0:1*2:2:0:0:0","","Jan 1 1999","Apr 30 1999"); |
| 299 |
|
| 300 |
# To find the Monday after Easter in 1997-1999. |
| 301 |
@date = ParseRecur("*1997-1999:0:0:0:0:0:0*EASTER,ND1"); |
| 302 |
|
| 303 |
=back |
| 304 |
|
| 305 |
The L<Date::Manip::Recur> manual contains information about recurring events. |
| 306 |
|
| 307 |
=head1 WORKING WITH DATES IN ANOTHER LANGUAGE |
| 308 |
|
| 309 |
If you want to work with dates in a language other than English (but you |
| 310 |
are only working with a single language), do the following: |
| 311 |
|
| 312 |
=over 4 |
| 313 |
|
| 314 |
=item B<OO method> |
| 315 |
|
| 316 |
$date = new Date::Manip::Date; |
| 317 |
$date->config("Language","French","DateFormat","non-US"); |
| 318 |
$date->parse("1er decembre 1990"); |
| 319 |
|
| 320 |
=item B<Functional> |
| 321 |
|
| 322 |
Date_Init("Language=French","DateFormat=non-US"); |
| 323 |
$date = ParseDate("1er decembre 1990"); |
| 324 |
|
| 325 |
=back |
| 326 |
|
| 327 |
The L<Date::Manip::Config> manual has a list of all supported languages (in the |
| 328 |
section on the Language config variable). The meaning of the DateFormat |
| 329 |
config variable is also included. |
| 330 |
|
| 331 |
=head1 WORKING WITH TWO DIFFERENT LANGUAGES |
| 332 |
|
| 333 |
If you want to work with dates in two (or more) languages, it is STRONGLY |
| 334 |
recommended that you use the OO interface. The functional interface will |
| 335 |
be much slower since it has to re-initialize a lot of language-specific |
| 336 |
stuff every time you switch back and forth between languages. |
| 337 |
|
| 338 |
=over 4 |
| 339 |
|
| 340 |
=item B<OO method> |
| 341 |
|
| 342 |
$date_eng = new Date::Manip::Date; |
| 343 |
$date_eng->config("Language","English","DateFormat","US"); |
| 344 |
|
| 345 |
$date_fre = new Date::Manip::Date; |
| 346 |
$date_fre->config("Language","French","DateFormat","non-US"); |
| 347 |
|
| 348 |
Use the C<$date_eng> object to do English operations, the C<$date_fre> object to |
| 349 |
do French operations. |
| 350 |
|
| 351 |
=item B<Functional> |
| 352 |
|
| 353 |
If you are working with both French and English dates, you can call |
| 354 |
the following to switch between them: |
| 355 |
|
| 356 |
Date_Init("Language=French","DateFormat=non-US"); |
| 357 |
Date_Init("Language=English","DateFormat=US"); |
| 358 |
|
| 359 |
This is NOT recommended. Use the OO method instead. |
| 360 |
|
| 361 |
=back |
| 362 |
|
| 363 |
=head1 BUGS AND QUESTIONS |
| 364 |
|
| 365 |
Please refer to the L<Date::Manip::Problems> documentation for |
| 366 |
information on submitting bug reports or questions to the author. |
| 367 |
|
| 368 |
=head1 SEE ALSO |
| 369 |
|
| 370 |
L<Date::Manip> - main module documentation |
| 371 |
|
| 372 |
=head1 LICENSE |
| 373 |
|
| 374 |
This script is free software; you can redistribute it and/or |
| 375 |
modify it under the same terms as Perl itself. |
| 376 |
|
| 377 |
=head1 AUTHOR |
| 378 |
|
| 379 |
Sullivan Beck ([email protected]) |
| 380 |
|
| 381 |
=cut |