Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended way of doing date arithmetics in Perl?

What is the recommended way of doing date arithmetics in Perl?

Say for example that I want to know the date three days ago from today (where today = 2010-10-17 and today - 3 days = 2010-10-13). How would you do that in Perl?

like image 240
knorv Avatar asked Oct 17 '10 18:10

knorv


3 Answers

You can use DateTime and DateTime::Duration

http://search.cpan.org/dist/DateTime/lib/DateTime/Duration.pm

Or work with unix timestamps:

my $now = time();
my $threeDaysAgo = $now - 3 * 86400;
my ($day, $mon, $year) = (localtime($threeDaysAgo))[3, 4, 5];
printf("Three days ago was %04d-%02d-%02d", $year+1900, $mon+1, $day);
like image 104
eumiro Avatar answered Nov 08 '22 09:11

eumiro


See DateTime on CPAN (or here).

like image 44
zoul Avatar answered Nov 08 '22 11:11

zoul


There are many, many different date and time manipulation modules.

These include:

  • Date::Calc - and the Add_Delta_Days function
  • Date::Manip - and the DateCalc function
  • DateTime

All of these are well thought of. There are many others in addition. A lot depends on the sort of arithmetic you want to do. DateTime is perhaps the most rigorous, but Date::Calc and Date::Manip may be easier to handle for the work you need.

like image 32
Jonathan Leffler Avatar answered Nov 08 '22 09:11

Jonathan Leffler