in reply to date span in YYYYMMDD
This makes heavy use of the conditional operator (?:) to back the date up to the beginning of the previous half-month, meanwhile putting the year in the proper century. Then map is used to generate the similarly-formatted $start_day and $end_day, while also correcting the month value. Note that to subtract one from the month, we are adding 11, and that the month correction takes this value modulo 12 before doing the final increment. Also, no account is made of months shorter than 31 days. Since this is used only in an expression for comparing real dates from a file, it shouldn't matter.use strict; my ($day, $month, $year) = (localtime) [3, 4, 5]; $year += ($month += ($day = $day > 15 ? 0 : 15) ? 11 : 0) == 11 ? 1899 + : 1900; my ($start_day, $end_day) = map {sprintf("%4.4d%2.2d%2.2d", $year, $mo +nth % 12 + 1, $_)} ($day, $day ? 31 : 15); print "New Releases from $start_day to $end_day \n"
Of course, Perl's shortcuts sometimes do make code hard to read, and I may have crossed the line somewhat into obfuscation territory to make my point. But used carefully, they support a brevity that can actually foster comprehension by stripping a script of redundancy.
As with any code you get from the Monks, "try it before you buy it". I may have overlooked something. :-)
|
|---|