Panda::Date::Rel - Relative date object.
Relative date is a period of time not bound to any particular point in time and is used for date calculations. Reldate consists of 6 units - seconds, minutes, hours, days, months and years. Some units convert to another inaccurate.
Creates a date object using one of these source data types:
Treated as a number of seconds
Missing elements are considered = 0.
Any part can be absent or negative.
Clones another object
Creates relative date $rel so that $from + $rel == $till.
$from and $till can be Panda::Date objects or any data supported by Panda::Date constructor.
Set relative date from data (data can be anything that constructor supports). This is much faster than creating new object.
Number of years in relative date
Number of month in relative date
Number of days in relative date
Number of hours in relative date
Number of minutes in relative date
Number of seconds in relative date
Converts relative date to number of seconds. If any of day/month/year are non-zero then this value can be inaccurate because one need to know exact dates to calculate exact number of seconds.
For such calculations the following assumptions are made:
Converts relative date to number of minutes. If any of day/month/year are non-zero then this value can be inaccurate, see to_sec().
Converts relative date to number of hours. If any of day/month/year are non-zero then this value can be inaccurate, see to_sec().
Converts relative date to number of days. If any of sec/min/hour/month/year are non-zero then this value can be inaccurate, see to_sec().
Converts relative date to number of months. If any of sec/min/hour/day are non-zero then this value can be inaccurate, see to_sec().
Converts relative date to number of years. If any of sec/min/hour/day are non-zero then this value can be inaccurate, see to_sec().
Returns string in "4M 15D 123s" format, any of YMDhms can be absent or negative. If all the parts YMDhms are 0, then "" is returned.
Called implicitly in boolean context. Returns FALSE, if sec = 0 and min = 0 and .... year = 0, i.e. duration = 0. Otherwise TRUE.
Returns to_sec().
Multiplies relative date by $num. $num can be fractional but the result is always integer.
Examples
$rel = "1M 1D"; print $rel * 2; # 2M 2D print rdate("10h")->multiply(10); # 100h
Relative date can only be multiplied by number (scalar).
No normalization are made, i.e. 12h*2 = 24h is not normalized to 1D because that would be inaccurate (on DST border day for example, 1D is 25 or 23h)
Same as multiply_new(), but changes current object instead of creating new one.
multiply_new()
Divides relative date by $num. $num can be fractional but the result is always integer.
System will denormalize values if in another way (rounding) precision loses are bigger, for example "1Y" / 2 = "6M" (without denormalization it would be 0).
This applies even if units are not converted accurate. In this case assumptions mentioned in to_sec() are made.
$rel = "2Y"; print $rel/2; # 1Y print $rel/4; # 6M print rdate("1D")/3; # 8h print (rdate("1D")/3)*3; # 24h print MONTH/2; # "15D 5h 14m 32s"
P.S. Keep in mind that ($rel / N) * N is not always equals $rel, as well as ($rel * N) / N
Same as divide_new(), but changes current object instead of creating new one.
divide_new()
Adds another rel date to current. Another reldate can be Panda::Date::Rel object or any data valid for its constructor.
my $rel = 2*MONTH; print $rel+MONTH; # 3M print $rel+'30D'; # 2M 30D print $rel+[1,2,3]; # 1Y 4M 3D
Same as add_new(), but changes current object instead of creating new one.
add_new()
my $rel = 2*MONTH; $rel += '16h'; # 2M 16h $rel += {sec => 10, min => 20}; # 2M 16h 20m 10s $rel += $rel; # 4M 32h 40m 20s
Subtracts another reldate from current. Another reldate can be Panda::Date::Rel object or any data valid for its constructor.
my $rel = 2*MONTH; print $rel-MONTH; # 1M print $rel-'30D'; # 2M -30D print $rel-[1,2,3]; # -1Y -3D
Same as subtract_new(), but changes current object instead of creating new one.
subtract_new()
Changes sign of YMDhms
Same as negative_new(), but changes current object instead of creating new one.
negative_new()
Compares 2 relative dates and returns -1, 0 or 1. Another reldate can be Panda::Date::Rel object or any data valid for its constructor.
Dates are compared using to_sec(), therefore 2 dates can be equal even if they consist of different components. If you want full equality test, use 'eq'.
to_sec()
MONTH > YEAR; # false rdate("1Y 1M") > YEAR; #true 12*MONTH == YEAR; #true 12*MONTH eq YEAR; #false
Same as '==' but returns TRUE only if 2 reldates are fully identical.
$reldate = rdate("1Y 2M"); $reldate == "1Y 2M"; # true $reldate == "14M"; # true $reldate eq "1Y 2M"; # true $reldate eq "14M"; # false
Clones object.
Hook for Panda::Lib::clone().
See screenshot http://crazypanda.ru/v/clip2net/g/0/KfYbuNhu0b.png
Storable serialization is fully supported. That means you're able to freeze Panda::Date::Rel objects and thaw serialized data back without losing any information.
Pronin Oleg <syber@cpan.org>, Crazy Panda, CP Decision LTD
You may distribute this code under the same terms as Perl itself.
To install Panda::Date, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Panda::Date
CPAN shell
perl -MCPAN -e shell install Panda::Date
For more information on module installation, please visit the detailed CPAN module installation guide.