What is the optimal/clearest way to loop between two dates in Perl? There are plenty of modules on CPAN that deal with such matter, but is there any rule of thumb for iterating between two dates?
相关问题
- $ENV{$variable} in perl
- R: eval(parse()) error message: cannot ope
- Date with SimpleDateFormat in Java
- Is it possible to pass command-line arguments to @
- Redirecting STDOUT and STDERR to a file, except fo
相关文章
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- MYSQL: How can I find 'last monday's date&
- Creating a list of functions using a loop in R
- AttributeError: 'Series' object has no att
- Calculate number of working days in a month [dupli
- Can NOT List directory including space using Perl
- Get file created date in node
These days, most people would recommend using
DateTime
:I think the "best" way to do that depends a lot on what you're doing between these two days.
In many cases, a simple
for (0..31)
loop will suffice.In other cases, you may wish to use an epoch value, and add/subtract 86400 seconds on each itteration.
In one application I've written, I do exactly this, using a DateTime object that I add one day to for each iteration. This is likely overkill for many applications, though.
For everything that uses Date manipulation
DateTime
is probably the best module out there. To get all dates between two dates with your own increment use something like this:This will output:
I'm offering up a
Time::Piece
solution, because - unlikeDateTime
it's a core module (as of perl 5.9.5):Both
Time::Piece
andTime::Seconds
are core as of perl 5.9.5. The latter is only needed forONE_DAY
- otherwise you can just add60 * 60 * 24
instead.This has the advantage of being able to parse fairly arbitrary date formats.