Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: POSIX strftime

by crouchingpenguin (Priest)
on Jun 25, 2003 at 12:30 UTC ( [id://268833]=note: print w/replies, xml ) Need Help??


in reply to POSIX strftime

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."

Replies are listed 'Best First'.
Re: Re: POSIX strftime
by davorg (Chancellor) on Jun 25, 2003 at 14:04 UTC

    I have to say that I'm surprised (and more than a little appalled) to see that this works :)

    Update: To clarify. The bit that surprises me is that $parts[3] -= 14; works even when the calculation makes $parts[3] negative.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://268833]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-03-28 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found