« REO | Main | Debugging CPAN Build Problems »

strftime++

The strftime(3) call should replace convoluted code required after gmtime(3) or localtime(3) calls. For comparison, using Perl, two subroutines that both produce a ISO 8601 calendar date such as 2007-04-14, based on the current time:

sub date_from_gmtime { my ( $mday, $mon, $year ) = (gmtime)[ 3 .. 5 ]; $year += 1900; $mon += 1; return sprintf( '%4d-%02d-%02d', $year, $mon, $mday ); } sub date_from_strftime { use POSIX qw(strftime); return strftime( '%Y-%m-%d', gmtime ); }

strftime requires fewer lines of code, requires no local array slicing and number fiddling, and easily supports different date templates.

Technorati Tags: ,