Blame lib/Date/Manip/Examples.pod

Packit 95306a
# Copyright (c) 1995-2017 Sullivan Beck. All rights reserved.
Packit 95306a
# This program is free software; you can redistribute it and/or modify it
Packit 95306a
# under the same terms as Perl itself.
Packit 95306a
Packit 95306a
=pod
Packit 95306a
Packit 95306a
=head1 NAME
Packit 95306a
Packit 95306a
Date::Manip::Examples - examples of how to use Date::Manip
Packit 95306a
Packit 95306a
=head1 DESCRIPTION
Packit 95306a
Packit 95306a
This document includes a number of examples on how to do common
Packit 95306a
Date::Manip operations.  I will be happy to add new examples over
Packit 95306a
time, and welcome suggestions and examples to include.
Packit 95306a
Packit 95306a
In most cases, an example will include two different ways of getting
Packit 95306a
the answer.  The first way will be using the new (as of 6.00) OO
Packit 95306a
modules. The second will be using the old-style functional interface.
Packit 95306a
Packit 95306a
It should be noted that any time you want to work with alternate
Packit 95306a
time zones, the OO interface is STRONGLY recommended since the functional
Packit 95306a
interface does not preserve time zone information with the date, and
Packit 95306a
may therefore give incorrect results in some cases. However, working
Packit 95306a
in the time zone of the system should give correct results.
Packit 95306a
Packit 95306a
It should be noted that, in the examples below, it appears that the OO
Packit 95306a
method often requires more lines of code than the functional
Packit 95306a
interface.  There are a number of ways to shorten the OO method, but
Packit 95306a
for the examples, I wanted to include all the steps explicitly.
Packit 95306a
Packit 95306a
=head1 PARSING A DATE
Packit 95306a
Packit 95306a
Dates can be parsed in practically any form in common usage:
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
   $date = new Date::Manip::Date;
Packit 95306a
   $err = $date->parse("today");
Packit 95306a
   $err = $date->parse("1st Thursday in June 1992");
Packit 95306a
   $err = $date->parse("05/10/93");
Packit 95306a
   $err = $date->parse("12:30 Dec 12th 1880");
Packit 95306a
   $err = $date->parse("8:00pm December tenth");
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
   $date = ParseDate("today");
Packit 95306a
   $date = ParseDate("1st Thursday in June 1992");
Packit 95306a
   $date = ParseDate("05/10/93");
Packit 95306a
   $date = ParseDate("12:30 Dec 12th 1880");
Packit 95306a
   $date = ParseDate("8:00pm December tenth");
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
The L<Date::Manip::Date> manual has a list of all valid formats.
Packit 95306a
Packit 95306a
=head1 PARSING AN AMOUNT OF TIME
Packit 95306a
Packit 95306a
Amounts of time (referred to as deltas) can also be parsed:
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
   $delta = new Date::Manip::Delta;
Packit 95306a
   $err = $delta->parse("in 12 hours");
Packit 95306a
   $err = $delta->parse("-1:30:0");
Packit 95306a
   $err = $delta->parse("4 business days later");
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
   $delta = ParseDateDelta("in 12 hours");
Packit 95306a
   $delta = ParseDateDelta("-1:30:0");
Packit 95306a
   $delta = ParseDateDelta("4 business days later");
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
=head1 TO CALCULATE THE AMOUNT OF TIME BETWEEN TWO DATES
Packit 95306a
Packit 95306a
   $datestr1 = "Jan 30 1999 13:00 EST";
Packit 95306a
   $datestr2 = "2/Mar/1999 15:30:00 +0500";
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
   $date1 = new Date::Manip::Date;
Packit 95306a
   $date2 = $date1->new_date();
Packit 95306a
   $err = $date1->parse($datestr1);
Packit 95306a
   $err = $date2->parse($datestr2);
Packit 95306a
Packit 95306a
To get an exact amount of time between the two dates (expressed only in terms of
Packit 95306a
weeks, days, hours, minutes, seconds), use:
Packit 95306a
Packit 95306a
   $delta = $date1->calc($date2);
Packit 95306a
Packit 95306a
To get an approximate amount of time (expressed in terms of years, months, weeks,
Packit 95306a
etc. in terms that a human would typically think of), use:
Packit 95306a
Packit 95306a
   $delta = $date1->calc($date2,"approx");
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
   $date1 = ParseDate($string1);
Packit 95306a
   $date2 = ParseDate($string2);
Packit 95306a
Packit 95306a
To get an exact amount:
Packit 95306a
Packit 95306a
   $delta = DateCalc($date1,$date2);
Packit 95306a
Packit 95306a
and the approximate amount:
Packit 95306a
Packit 95306a
   $delta = DateCalc($date1,$date2,1);
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
The L<Date::Manip::Calc> manual has information about these, and other types of
Packit 95306a
calculations.
Packit 95306a
Packit 95306a
=head1 TO ADD AN AMOUNT OF TIME TO A DATE
Packit 95306a
Packit 95306a
To find a second date a given amount of time before or after a first date,
Packit 95306a
use the following:
Packit 95306a
Packit 95306a
   $datestr  = "Jan 30 1999 13:00 EST";
Packit 95306a
   $deltastr = "12 hours ago";
Packit 95306a
   $deltastr = "in 3 business days";
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
   $date = new Date::Manip::Date;
Packit 95306a
   $delta = $date->new_delta();
Packit 95306a
   $date->parse($datestr);
Packit 95306a
   $delta->parse($deltastr);
Packit 95306a
Packit 95306a
   $d = $date->calc($delta);
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
   $date = DateCalc($datestr,$deltastr);
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
If the delta is a business delta, it will do a business mode calculation.
Packit 95306a
Packit 95306a
The L<Date::Manip::Calc> manual has information about these, and other types of
Packit 95306a
calculations.
Packit 95306a
Packit 95306a
=head1 COMPARE TWO DATES
Packit 95306a
Packit 95306a
To take two different dates and see which is earlier, do the
Packit 95306a
following:
Packit 95306a
Packit 95306a
   $datestr1 = "Jan 30 1999 13:00 EST";
Packit 95306a
   $datestr2 = "2/Mar/1999 15:30:00 +0500";
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
   $date1 = new Date::Manip::Date;
Packit 95306a
   $date2 = $date1->new_date;
Packit 95306a
   $date1->parse($datestr1);
Packit 95306a
   $date2->parse($datestr2);
Packit 95306a
Packit 95306a
   $date1->cmp($date2);
Packit 95306a
      => -1, 0, 1
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
   $date1 = ParseDate($datestr1);
Packit 95306a
   $date2 = ParseDate($datestr2);
Packit 95306a
Packit 95306a
   Date_Cmp($date1,$date2);
Packit 95306a
      => -1, 0, 1
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
=head1 TO EXTRACT INFORMATION ABOUT A DATE OR DELTA
Packit 95306a
Packit 95306a
If you have a date or a delta, you can extract information
Packit 95306a
about them as follows:
Packit 95306a
Packit 95306a
   $datestr  = "1:24:08 PM EST Feb 3, 1996";
Packit 95306a
   $deltastr = "12 hours ago";
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
   $date = new Date::Manip::Date;
Packit 95306a
   $delta = $date->new_delta();
Packit 95306a
   $date->parse($datestr);
Packit 95306a
   $delta->parse($deltastr);
Packit 95306a
Packit 95306a
   $str = $date->printf("It is now %T on %b %e, %Y.");
Packit 95306a
     =>  "It is now 13:24:08 on Feb  3, 1996."
Packit 95306a
Packit 95306a
   $str = $delta->printf("In %hv hours, %mv minutes, %sv seconds");
Packit 95306a
     => "In -12 hours, 0 minutes, 0 seconds";
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
   $str = UnixDate($datestr,"It is now %T on %b %e, %Y.");
Packit 95306a
     =>  "It is now 13:24:08 on Feb  3, 1996."
Packit 95306a
Packit 95306a
   $str = Delta_Format($deltastr,"In %hv hours, %mv minutes, %sv seconds");
Packit 95306a
     => "In -12 hours, 0 minutes, 0 seconds";
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
The L<Date::Manip::Date> manual contains all of the format codes that can
Packit 95306a
be used to extract information from a date. The L<Date::Manip::Delta> manual
Packit 95306a
contains the codes for a delta.
Packit 95306a
Packit 95306a
=head1 WORKING WITH EPOCH
Packit 95306a
Packit 95306a
Date::Manip can easily be used to work with the number of seconds
Packit 95306a
since the epoch (Jan 1, 1970 00:00:00 UTC).
Packit 95306a
Packit 95306a
If you have a date, and you want to find out how many seconds it is
Packit 95306a
after the epoch, you can do it in the following ways:
Packit 95306a
Packit 95306a
   $datestr  = "1999-04-30-15:30:00 EDT";
Packit 95306a
   $secs  = 1234567;
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
To find out how many seconds have elapsed on a certain date, you
Packit 95306a
can do the following:
Packit 95306a
Packit 95306a
   $date     = new Date::Manip::Date;
Packit 95306a
   $err      = $date->parse($datestr);
Packit 95306a
Packit 95306a
   $str      = $date->printf('%s');
Packit 95306a
      => number of seconds
Packit 95306a
Packit 95306a
To find out the date that is a certain number of seconds since the
Packit 95306a
epoch, you can use the following:
Packit 95306a
Packit 95306a
   $date  = new Date::Manip::Date;
Packit 95306a
   $err   = $date->parse("epoch $secs");
Packit 95306a
Packit 95306a
C<$date> now contains the date wanted (in the local time zone)
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
To find out how many seconds have elapsed:
Packit 95306a
Packit 95306a
   $str = UnixDate($datestr,'%s');
Packit 95306a
     => number of seconds
Packit 95306a
Packit 95306a
To find the date that is a number of seconds since the epoch:
Packit 95306a
Packit 95306a
   $date = ParseDateString("epoch $secs");
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
Note that Date::Manip will work with both positive seconds (for dates
Packit 95306a
that have come since the epoch) and negative seconds (for dates that
Packit 95306a
occurred before the epoch).
Packit 95306a
Packit 95306a
=head1 RECURRING EVENTS
Packit 95306a
Packit 95306a
To find a list of dates where a recurring event happens (even very complex
Packit 95306a
recurrences), do the following:
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
  # To find the 2nd Tuesday of every month from Jan 1 1999 to Apr 30 1999
Packit 95306a
Packit 95306a
  $recur = new Date::Manip::Recur;
Packit 95306a
  $start = $recur->new_date();
Packit 95306a
  $end   = $recur->new_date();
Packit 95306a
  $start->parse("Jan 1 1999");
Packit 95306a
  $end->parse("Apr 30 1999");
Packit 95306a
Packit 95306a
  $recur->parse("0:1*2:2:0:0:0",$start,$end);
Packit 95306a
  @date = $recur->dates();
Packit 95306a
Packit 95306a
  # To find the Monday after Easter in 1997-1999
Packit 95306a
Packit 95306a
  $recur = new Date::Manip::Recur;
Packit 95306a
  $recur->parse("*1997-1999:0:0:0:0:0:0*EASTER,ND1");
Packit 95306a
  @date = $recur->dates();
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
  # To find the 2nd Tuesday of every month from Jan 1 1999 to Apr 30 1999
Packit 95306a
  @date = ParseRecur("0:1*2:2:0:0:0","","Jan 1 1999","Apr 30 1999");
Packit 95306a
Packit 95306a
  # To find the Monday after Easter in 1997-1999.
Packit 95306a
  @date = ParseRecur("*1997-1999:0:0:0:0:0:0*EASTER,ND1");
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
The L<Date::Manip::Recur> manual contains information about recurring events.
Packit 95306a
Packit 95306a
=head1 WORKING WITH DATES IN ANOTHER LANGUAGE
Packit 95306a
Packit 95306a
If you want to work with dates in a language other than English (but you
Packit 95306a
are only working with a single language), do the following:
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
   $date = new Date::Manip::Date;
Packit 95306a
   $date->config("Language","French","DateFormat","non-US");
Packit 95306a
   $date->parse("1er decembre 1990");
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
   Date_Init("Language=French","DateFormat=non-US");
Packit 95306a
   $date = ParseDate("1er decembre 1990");
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
The L<Date::Manip::Config> manual has a list of all supported languages (in the
Packit 95306a
section on the Language config variable). The meaning of the DateFormat
Packit 95306a
config variable is also included.
Packit 95306a
Packit 95306a
=head1 WORKING WITH TWO DIFFERENT LANGUAGES
Packit 95306a
Packit 95306a
If you want to work with dates in two (or more) languages, it is STRONGLY
Packit 95306a
recommended that you use the OO interface. The functional interface will
Packit 95306a
be much slower since it has to re-initialize a lot of language-specific
Packit 95306a
stuff every time you switch back and forth between languages.
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<OO method>
Packit 95306a
Packit 95306a
   $date_eng = new Date::Manip::Date;
Packit 95306a
   $date_eng->config("Language","English","DateFormat","US");
Packit 95306a
Packit 95306a
   $date_fre = new Date::Manip::Date;
Packit 95306a
   $date_fre->config("Language","French","DateFormat","non-US");
Packit 95306a
Packit 95306a
Use the C<$date_eng> object to do English operations, the C<$date_fre> object to
Packit 95306a
do French operations.
Packit 95306a
Packit 95306a
=item B<Functional>
Packit 95306a
Packit 95306a
If you are working with both French and English dates, you can call
Packit 95306a
the following to switch between them:
Packit 95306a
Packit 95306a
   Date_Init("Language=French","DateFormat=non-US");
Packit 95306a
   Date_Init("Language=English","DateFormat=US");
Packit 95306a
Packit 95306a
This is NOT recommended. Use the OO method instead.
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
=head1 BUGS AND QUESTIONS
Packit 95306a
Packit 95306a
Please refer to the L<Date::Manip::Problems> documentation for
Packit 95306a
information on submitting bug reports or questions to the author.
Packit 95306a
Packit 95306a
=head1 SEE ALSO
Packit 95306a
Packit 95306a
L<Date::Manip>        - main module documentation
Packit 95306a
Packit 95306a
=head1 LICENSE
Packit 95306a
Packit 95306a
This script is free software; you can redistribute it and/or
Packit 95306a
modify it under the same terms as Perl itself.
Packit 95306a
Packit 95306a
=head1 AUTHOR
Packit 95306a
Packit 95306a
Sullivan Beck (sbeck@cpan.org)
Packit 95306a
Packit 95306a
=cut