I'm well aware of its content.
I'd still use Date::Manip if I were to calculate it. If it's going to be run as a time critical program, the program would either be written in C, or a lookup instead of a calculation would be used.
| [reply] |
I want to see you get the lookup right for all edge cases.
Date::Manip is a fine choice if, as the author himself says, you want to allow users to specify dates in fairly freeform, descriptive fashion.
But other than that, I'd avoid it. It doesn't even make for particularly readable code. The only thing that's intuitive about your snippet is DateCalc()'s offset format. The output format string isn't self-documenting and the implicit truncation is not obvious. I had to read your explanation to know what it does.
Try this on for size:
use DateTime;
my $date = DateTime
->now
->truncate( to => 'month' )
->add( months => 1, days => -1 );
print $date->day;
DateTime rocks.
Makeshifts last the longest. | [reply] [d/l] |
Trying it for size? Your fragment, without the use statement, uses 87 non-whitespace characters characters. Mine can be reduced to:
print UnixDate DateCalc("@ARGV", "+1m -1d"), "%e";
for 46 non-whitespace characters, while it even takes an argument (yours only does the current month). So. size-wise, you lose (unless you want to the longest solution....).
Whether something is intuitive or not is a subjective thing. The output format may not be very intuitive, it's far more flexible (it could easily be changed to, say "The answer to the question is %e". The "%e" directive of UnixDate is exactly the same as the one of strftime.
As for the implicite truncation not being obvious, I fully agree with that as well. It isn't obvious to me either. In fact, I've no idea what truncation you are referring to.
BTW, I'm pretty convinced I'd get all the edge cases correctly when doing a look-up table. How the lookup-table would look like strongly depends on the edges. (Which years, which calendar, which country, etc). Or did you think the well known formula that checks the remainers of division of the year by 4, 100 and 400 got the edge cases right? ;-)
| [reply] [d/l] [select] |