How about:
print POSIX::strftime('%Y%m%d', localtime),"\n";
my @parts = localtime();
$parts[3] -= 14;
print POSIX::strftime('%Y%m%d', @parts),"\n";
Update: Sure, why not davorg? :) After all, strftime expects a list after the format:
Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1
+, yday = -1, isdst = -1)
YA-Update:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
use Benchmark qw( cmpthese );
use constant DAY => 60 * 60 * 24;
sub use_array {
my @parts = localtime();
$parts[3] -= 14;
return POSIX::strftime('%Y%m%d', @parts);
}
sub use_math {
return POSIX::strftime(
'%Y%m%d',
localtime(time - (14 * (60 * 60 * 24)))
);
}
sub use_math_with_const {
return POSIX::strftime('%Y%m%d', localtime(time - (14 * DAY )));
}
cmpthese(
90000,
{
'use_array' => sub { use_array(); },
'use_math' => sub { use_math(); },
'use_math_with_const' => sub { use_math_with_const(); },
},
);
__DATA__
Benchmark: timing 90000 iterations of use_array, use_math, use_math_wi
+th_const...
use_array: 6 wallclock secs ( 5.67 usr + 0.10 sys = 5.77 CPU) @ 15
+597.92/s (n=90000)
use_math: 4 wallclock secs ( 3.63 usr + 0.11 sys = 3.74 CPU) @ 24
+064.17/s (n=90000)
use_math_with_const: 3 wallclock secs ( 3.70 usr + 0.05 sys = 3.75
+CPU) @ 24000.00/s (n=90000)
Rate use_array use_math_with_const
+ use_math
use_array 15598/s -- -35%
+ -35%
use_math_with_const 24000/s 54% --
+ -0%
use_math 24064/s 54% 0%
+ --
cp
----
"Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.