Blame lib/Date/Manip/Base.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::Base - Base methods for date manipulation
Packit 95306a
Packit 95306a
=head1 SYNOPSIS
Packit 95306a
Packit 95306a
   use Date::Manip::Base;
Packit 95306a
   $dmb = new Date::Manip::Base;
Packit 95306a
Packit 95306a
=head1 DESCRIPTION
Packit 95306a
Packit 95306a
The Date::Manip package of modules consists of several modules
Packit 95306a
for doing high level date operations with full error checking
Packit 95306a
and a lot of flexibility.
Packit 95306a
Packit 95306a
The high level operations, though intended to be used in most
Packit 95306a
situations, have a lot of overhead associated with them. As such, a
Packit 95306a
number of the most useful low level routines (which the high level
Packit 95306a
routines use to do much of the real work) are included in this module
Packit 95306a
and are available directly to users.
Packit 95306a
Packit 95306a
These low level routines are powerful enough that they can be used
Packit 95306a
independent of the high level routines and perform useful (though much
Packit 95306a
simpler) operations. They are also significantly faster than the high
Packit 95306a
level routines.
Packit 95306a
Packit 95306a
These routines do NO error checking on input. Invalid data will result
Packit 95306a
in meaningless results.  If you need error checking, you must call the
Packit 95306a
higher level Date::Manip routines instead of these.
Packit 95306a
Packit 95306a
These routines also ignore all effects of time zones and daylight
Packit 95306a
saving time. One way to think of these routines is working with times
Packit 95306a
and dates in the GMT time zone.
Packit 95306a
Packit 95306a
=head1 BASE METHODS
Packit 95306a
Packit 95306a
This class inherits several base methods from the L<Date::Manip::Obj>
Packit 95306a
class. Please refer to the documentation for that class for a
Packit 95306a
description of those methods.
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<err>
Packit 95306a
Packit 95306a
=item B<new>
Packit 95306a
Packit 95306a
=item B<new_config>
Packit 95306a
Packit 95306a
Please refer to the L<Date::Manip::Obj> documentation for these methods.
Packit 95306a
Packit 95306a
=item B<config>
Packit 95306a
Packit 95306a
   $dmb->config($var1,$val1,$var2,$val2,...);
Packit 95306a
Packit 95306a
This will set the value of any configuration variable. Please refer to the
Packit 95306a
L<Date::Manip::Config> manual for a list of all configuration variables and their
Packit 95306a
description.
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
=head1 DATE METHODS
Packit 95306a
Packit 95306a
In all of the following methods, the following variables are used:
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<$date>
Packit 95306a
Packit 95306a
This is a list reference containing a full date and time:
Packit 95306a
Packit 95306a
   [$y, $m, $d, $h, $mn, $s]
Packit 95306a
Packit 95306a
=item B<$ymd>
Packit 95306a
Packit 95306a
A list reference containing only the date portion:
Packit 95306a
Packit 95306a
   [$y, $m, $d]
Packit 95306a
Packit 95306a
=item B<$hms>
Packit 95306a
Packit 95306a
A list reference containing only the time portion:
Packit 95306a
Packit 95306a
   [$h, $mn, $s]
Packit 95306a
Packit 95306a
=item B<$time>
Packit 95306a
Packit 95306a
A list reference containing an amount of time:
Packit 95306a
Packit 95306a
   [$dh, $dmn, $ds]
Packit 95306a
Packit 95306a
=item B<$delta>
Packit 95306a
Packit 95306a
A list containing a full delta:
Packit 95306a
Packit 95306a
   [$dy, $dm, $dw, $dd, $dh, $dmn, $ds]
Packit 95306a
Packit 95306a
=item B<$offset>
Packit 95306a
Packit 95306a
A list containing a time zone expressed as an offset:
Packit 95306a
Packit 95306a
   [ $offh, $offm, $offs ]
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
In all of the above, the elements C<($y, $m, $d, $h, $mn, $s)> are all
Packit 95306a
numeric. In most of the routines described below, no error checking is
Packit 95306a
done on the input.  C<$y> should be between 1 and 9999, C<$m> between 1 and
Packit 95306a
12, C<$d> between 1 and 31, C<$h> should be between 0 and 23, C<$mn> and C<$s>
Packit 95306a
between 0 and 59.
Packit 95306a
Packit 95306a
C<$hms> can be between 00:00:00 and 24:00:00, but an C<$offset> must be
Packit 95306a
between -23:59:59 and +23:59:59.
Packit 95306a
Packit 95306a
Years are not translated to 4 digit years, so passing in a year of
Packit 95306a
"04" will be equivalent to "0004", NOT "2004".
Packit 95306a
Packit 95306a
The elements C<($dy, $dm, $dw, $dd, $dh, $dmn, $ds)> are all numeric,
Packit 95306a
but can be positive or negative. They represent an elapsed amount
Packit 95306a
of time measured in years, months, weeks, etc.
Packit 95306a
Packit 95306a
Since no error checking is done, passing in C<($y,$m,$d) = (2004,2,31)>
Packit 95306a
will NOT trigger an error, even though February does not have 31 days.
Packit 95306a
Instead, some meaningless result will be returned.
Packit 95306a
Packit 95306a
=over 4
Packit 95306a
Packit 95306a
=item B<calc_date_date>
Packit 95306a
Packit 95306a
=item B<calc_date_days>
Packit 95306a
Packit 95306a
=item B<calc_date_delta>
Packit 95306a
Packit 95306a
=item B<calc_date_time>
Packit 95306a
Packit 95306a
=item B<calc_time_time>
Packit 95306a
Packit 95306a
These are all routines for doing simple date and time calculations.
Packit 95306a
As mentioned above, they ignore all affects of time zones and daylight
Packit 95306a
saving time.
Packit 95306a
Packit 95306a
The following methods are available:
Packit 95306a
Packit 95306a
   $time = $dmb->calc_date_date($date1,$date2);
Packit 95306a
Packit 95306a
This take two dates and determine the amount of time between them.
Packit 95306a
Packit 95306a
   $date = $dmb->calc_date_days($date,$n [,$subtract]);
Packit 95306a
   $ymd  = $dmb->calc_date_days($ymd,$n [,$subtract]);
Packit 95306a
Packit 95306a
This returns a date C<$n> days later (if C<$nE<gt>0>) or earlier (if C<$nE<lt>0>)
Packit 95306a
than the date passed in. If C<$subtract> is passed in, the sign of C<$n> is reversed.
Packit 95306a
Packit 95306a
   $date = $dmb->calc_date_delta($date,$delta [,$subtract]);
Packit 95306a
Packit 95306a
This take a date and add the given delta to it (or subtract the delta
Packit 95306a
if C<$subtract> is non-zero).
Packit 95306a
Packit 95306a
   $date = $dmb->calc_date_time($date,$time [,$subtract]);
Packit 95306a
Packit 95306a
This take a date and add the given time to it (or subtract the time
Packit 95306a
if C<$subtract> is non-zero).
Packit 95306a
Packit 95306a
   $time = $dmb->calc_time_time(@time1,@time2 [,$subtract]);
Packit 95306a
Packit 95306a
This take two times and add them together (or subtract the second from
Packit 95306a
the first if C<$subtract> is non-zero).
Packit 95306a
Packit 95306a
=item B<check>
Packit 95306a
Packit 95306a
=item B<check_time>
Packit 95306a
Packit 95306a
   $valid = $dmb->check($date);
Packit 95306a
   $valid = $dmb->check_time($hms);
Packit 95306a
Packit 95306a
This tests a list of values to see if they form a valid date or time
Packit 95306a
ignoring all time zone affects. The date/time would be valid in GMT,
Packit 95306a
but perhaps not in all time zones.
Packit 95306a
Packit 95306a
1 is returned if the the fields are valid, 0 otherwise.
Packit 95306a
Packit 95306a
C<$hms> is in the range 00:00:00 to 24:00:00.
Packit 95306a
Packit 95306a
=item B<cmp>
Packit 95306a
Packit 95306a
   $flag = $dmb->cmp($date1,$date2);
Packit 95306a
Packit 95306a
Returns -1, 0, or 1 if date1 is before, the same as, or after date2.
Packit 95306a
Packit 95306a
=item B<day_of_week>
Packit 95306a
Packit 95306a
   $day = $dmb->day_of_week($date);
Packit 95306a
   $day = $dmb->day_of_week($ymd);
Packit 95306a
Packit 95306a
Returns the day of the week (1 for Monday, 7 for Sunday).
Packit 95306a
Packit 95306a
=item B<day_of_year>
Packit 95306a
Packit 95306a
   $day = $dmb->day_of_year($ymd);
Packit 95306a
   $day = $dmb->day_of_year($date);
Packit 95306a
Packit 95306a
In the first case, returns the day of the year (1 to 366) for C<($y, $m, $d)>.
Packit 95306a
In the second case, it returns a fractional day (1.0 <= C<$day> < 366.0 or
Packit 95306a
1.0 <= C<$day> < 367.0 for a leap-year).  For example, day 1.5
Packit 95306a
falls on Jan 1, at noon.  The somewhat non-intuitive answer (1.5
Packit 95306a
instead of 0.5) is to make the two forms return numerically equivalent
Packit 95306a
answers for times of 00:00:00 . You can look at the integer part of
Packit 95306a
the number as being the day of the year, and the fractional part of
Packit 95306a
the number as the fraction of the day that has passed at the given
Packit 95306a
time.
Packit 95306a
Packit 95306a
The inverse operations can also be done:
Packit 95306a
Packit 95306a
   $ymd   = $dmb->day_of_year($y,$day);
Packit 95306a
   $date  = $dmb->day_of_year($y,$day);
Packit 95306a
Packit 95306a
If C<$day> is an integer, the year, month, and day is returned. If C<$day>
Packit 95306a
is a floating point number, it returns the year, month, day, hour,
Packit 95306a
minutes, and decimal seconds.
Packit 95306a
Packit 95306a
C<$day> must be greater than or equal to 1 and less than 366 on non-leap years
Packit 95306a
or 367 on leap years.
Packit 95306a
Packit 95306a
=item B<days_in_month>
Packit 95306a
Packit 95306a
   $days = $dmb->days_in_month($y,$m);
Packit 95306a
Packit 95306a
Returns the number of days in the month.
Packit 95306a
Packit 95306a
   @days = $dmb->days_in_month($y,0);
Packit 95306a
Packit 95306a
Returns a list of 12 elements with the days in each month of the year.
Packit 95306a
Packit 95306a
=item B<days_in_year>
Packit 95306a
Packit 95306a
   $days = $dmb->days_in_year($y);
Packit 95306a
Packit 95306a
Returns the number of days in the year (365 or 366)
Packit 95306a
Packit 95306a
=item B<days_since_1BC>
Packit 95306a
Packit 95306a
   $days = $dmb->days_since_1BC($date);
Packit 95306a
   $days = $dmb->days_since_1BC($ymd);
Packit 95306a
Packit 95306a
Returns the number of days since Dec 31, 1BC. Since the calendar has
Packit 95306a
changed a number of times, the number returned is based on the current
Packit 95306a
calendar projected backwards in time, and in no way reflects a true
Packit 95306a
number of days since then. As such, the result is largely meaningless,
Packit 95306a
except when called twice as a means of determining the number of days
Packit 95306a
separating two dates.
Packit 95306a
Packit 95306a
The inverse operation is also available:
Packit 95306a
Packit 95306a
   $ymd = $dmb->days_since_1BC($days);
Packit 95306a
Packit 95306a
Returns the date C<$days> since Dec 31, 1BC. So day 1 is Jan 1, 0001.
Packit 95306a
Packit 95306a
=item B<leapyear>
Packit 95306a
Packit 95306a
   $flag = $dmb->leapyear($y);
Packit 95306a
Packit 95306a
Returns 1 if the argument is a leap year.  Originally copied from code
Packit 95306a
written by David Muir Sharnoff.
Packit 95306a
Packit 95306a
=item B<nth_day_of_week>
Packit 95306a
Packit 95306a
   $ymd = $dmb->nth_day_of_week($y,$n,$dow);
Packit 95306a
Packit 95306a
Returns the C<$n>th occurrence of C<$dow> (1 for Monday, 7 for Sunday) in the
Packit 95306a
year.  C<$n> must be between 1 and 53 or -1 through -53.
Packit 95306a
Packit 95306a
   $ymd = $dmb->nth_day_of_week($y,$n,$dow,$m);
Packit 95306a
Packit 95306a
Returns the C<$n>th occurrence of C<$dow> in the given month.  C<$n> must be between
Packit 95306a
1 and 5 or it can be -1 through -5.
Packit 95306a
Packit 95306a
In all cases, nothing is returned if C<$n> is beyond the last actual
Packit 95306a
result (i.e. the 5th Sunday in a month with only four Sundays).
Packit 95306a
Packit 95306a
=item B<secs_since_1970>
Packit 95306a
Packit 95306a
   $secs = $dmb->secs_since_1970($date);
Packit 95306a
Packit 95306a
Returns the number of seconds since Jan 1, 1970 00:00:00 (negative if date is
Packit 95306a
earlier).
Packit 95306a
Packit 95306a
   $date = $dmb->secs_since_1970($secs);
Packit 95306a
Packit 95306a
Translates number of seconds into a date.
Packit 95306a
Packit 95306a
=item B<split>
Packit 95306a
Packit 95306a
=item B<join>
Packit 95306a
Packit 95306a
The split and join functions are used to take a string containing a common
Packit 95306a
type of time data and split it into a list of fields. The join function takes
Packit 95306a
the list and forms it into a string.
Packit 95306a
Packit 95306a
Rudimentary error checking is performed with both of these functions
Packit 95306a
and undef is returned in the case of any error. No error checking is done
Packit 95306a
on the specific values.
Packit 95306a
Packit 95306a
The following are allowed:
Packit 95306a
Packit 95306a
   $date = $dmb->split("date",$string);
Packit 95306a
   $string = $dmb->join("date",$date);
Packit 95306a
Packit 95306a
This splits a string containing a date or creates one from a list reference.
Packit 95306a
The string split must be of one of the forms:
Packit 95306a
Packit 95306a
   YYYYMMDDHH:MN:SS
Packit 95306a
   YYYYMMDDHHMNSS
Packit 95306a
   YYYY-MM-DD-HH:MN:SS
Packit 95306a
Packit 95306a
The string formed by join is one of the above, depending on the value of
Packit 95306a
the Printable config variable. The default format is YYYYMMDDHH:MN:SS,
Packit 95306a
but if Printable is set to 1, YYYYMMDDHHMNSS is produced, and if Printable
Packit 95306a
is set to 2, the YYYY-MM-DD-HH:MN:SS form is produced.
Packit 95306a
Packit 95306a
   $hms = $dmb->split("hms",$string);
Packit 95306a
   $string = $dmb->join("hms",$hms);
Packit 95306a
Packit 95306a
This works with the hours, minutes, and seconds portion of a date.
Packit 95306a
Packit 95306a
When splitting a string, the string can be of any of the forms:
Packit 95306a
Packit 95306a
   H
Packit 95306a
   H:MN
Packit 95306a
   H:MN:SS
Packit 95306a
   HH
Packit 95306a
   HHMN
Packit 95306a
   HHMNSS
Packit 95306a
Packit 95306a
Here, H is a 1 or 2 digit representation of the hours. All other fields are
Packit 95306a
two digit representations.
Packit 95306a
Packit 95306a
The string formed by the join function will always be of the form HH:MN:SS.
Packit 95306a
Packit 95306a
The time must be between 00:00:00 and 24:00:00.
Packit 95306a
Packit 95306a
   $offset = $dmb->split("offset",$string);
Packit 95306a
   $string = $dmb->join("offset",$offset);
Packit 95306a
Packit 95306a
An offset string should have a sign (though it is optional if it is
Packit 95306a
positive) and is any of the forms:
Packit 95306a
Packit 95306a
   +H
Packit 95306a
   +H:MN
Packit 95306a
   +H:MN:SS
Packit 95306a
   +HH
Packit 95306a
   +HHMN
Packit 95306a
   +HHMNSS
Packit 95306a
Packit 95306a
Here, H is a 1 or 2 digit representation of the hours. All other fields are
Packit 95306a
two digit representations.
Packit 95306a
Packit 95306a
The string formed by the join function will always be of the form
Packit 95306a
+HH:MN:SS.
Packit 95306a
Packit 95306a
The offset must be between -23:59:59 and +23:59:59 .
Packit 95306a
Packit 95306a
   $time = $dmb->split("time",$string [,$no_normalize]);
Packit 95306a
   $string = $dmb->join("time",$time [,$no_normalize]);
Packit 95306a
Packit 95306a
This works with an amount of time in hours, minutes, and seconds. The
Packit 95306a
string is of the format:
Packit 95306a
Packit 95306a
   +H:MN:S
Packit 95306a
Packit 95306a
where all signs are optional. The returned value (whether a list reference
Packit 95306a
from the split function, or a string from the join function) will have
Packit 95306a
all fields normalized unless C<$no_normalize> is passed in.
Packit 95306a
Packit 95306a
   $delta = $dmb->split("delta",$string [,$no_normalize]);
Packit 95306a
   $delta = $dmb->split("business",$string [,$no_normalize]);
Packit 95306a
Packit 95306a
   $string = $dmb->join("delta",$delta [,$no_normalize]);
Packit 95306a
   $string = $dmb->join("business",$delta [,$no_normalize]);
Packit 95306a
Packit 95306a
Both of these split a string containing a delta, or create a string
Packit 95306a
containing one. The difference is whether the delta is treated as
Packit 95306a
a business or non-business delta (see L<Date::Manip::Delta> documentation
Packit 95306a
for a detailed description).
Packit 95306a
Packit 95306a
The string that can be split is of the form:
Packit 95306a
Packit 95306a
  +Y:M:+W:+D:H:MN:S
Packit 95306a
Packit 95306a
All signs are optional in the string being split. The string
Packit 95306a
produced is of the form +Y:M:+W:D:H:MN:S (for a non-business
Packit 95306a
delta) or +Y:M:+W:+D:H:MN:S (for a business delta).
Packit 95306a
Packit 95306a
Fields may be omitted entirely. For example:
Packit 95306a
Packit 95306a
  D:H:MN:S
Packit 95306a
  D:::S
Packit 95306a
Packit 95306a
are both valid.
Packit 95306a
Packit 95306a
The string or list output is normalized unless L<$no_normalize> is passed
Packit 95306a
in.
Packit 95306a
Packit 95306a
=item B<week1_day1>
Packit 95306a
Packit 95306a
   $ymd = $dmb->week1_day1($y);
Packit 95306a
Packit 95306a
This returns the date of the 1st day of the 1st week in the given year.
Packit 95306a
Note that this uses the ISO 8601 definition of week, so the year returned
Packit 95306a
may be the year before the one passed in.
Packit 95306a
Packit 95306a
This uses the FirstDay and Jan1Week1 config variables to evaluate the
Packit 95306a
results.
Packit 95306a
Packit 95306a
=item B<weeks_in_year>
Packit 95306a
Packit 95306a
   $w = $dmb->weeks_in_year($y);
Packit 95306a
Packit 95306a
This returns the number of ISO 8601 weeks in the year. It will always be
Packit 95306a
52 or 53.
Packit 95306a
Packit 95306a
=item B<week_of_year>
Packit 95306a
Packit 95306a
   ($y,$w) = $dmb->week_of_year($date);
Packit 95306a
   ($y,$w) = $dmb->week_of_year($ymd);
Packit 95306a
Packit 95306a
This returns the week number (1-53) of the given date and the year
Packit 95306a
that it falls in. Since the ISO 8601 definition of a week is used, the
Packit 95306a
year returned is not necessarily the one passed in (it may differ for
Packit 95306a
the first or last week of the year).
Packit 95306a
Packit 95306a
The inverse operation is also available:
Packit 95306a
Packit 95306a
   $ymd = $dmb->week_of_year($y,$w);
Packit 95306a
Packit 95306a
which returns the first day of the given week.
Packit 95306a
Packit 95306a
This uses the FirstDay and Jan1Week1 config variables to evaluate the
Packit 95306a
results.
Packit 95306a
Packit 95306a
=back
Packit 95306a
Packit 95306a
=head1 KNOWN BUGS
Packit 95306a
Packit 95306a
None known.
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