Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

POSIX strftime

by Gwalchmai (Novice)
on Jun 25, 2003 at 12:13 UTC ( #268828=perlquestion: print w/replies, xml ) Need Help??
Gwalchmai has asked for the wisdom of the Perl Monks concerning the following question:

Brothers, I seek knowledge of POSIX. Yeah I know how to return the current date using strftime(%Y%m%d, localtime). But how can you return the date from 14 days ago?

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

    Nothing to do with POSIX. you just need to work out how to get the correct date out of localtime.

    use constant DAY => 60 * 60 * 24; use POSIX 'strftime'; print strftime('%Y%m%d', localtime(time - (14 * DAY)));

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

Re: POSIX strftime
by crouchingpenguin (Priest) on Jun 25, 2003 at 12:30 UTC

    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)


    #!/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% + --

    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."

      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.


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

Re: POSIX strftime
by BrowserUk (Pope) on Jun 25, 2003 at 12:39 UTC

    perl -mPOSIX -eprint+POSIX::strftime('%Y%m%d',localtime(time-(14*60*60*24)))

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: POSIX strftime
by nite_man (Deacon) on Jun 25, 2003 at 12:53 UTC

    If you need to do with dates some operations, try to look at The Perl DateTime Project, which produces a suite of inter-operable modules for dealing with dates and times.

    Also you can use my module Date. Maybe it will be useful for you.

    SV* sv_bless(SV* sv, HV* stash);
Re: POSIX strftime
by xdg (Monsignor) on Jun 25, 2003 at 14:25 UTC

    Again, as an alternative to POSIX, you can get a lot of nicely parsed offset functionality with Date::Manip.

    $ perl -MDate::Manip -e 'print ParseDate("14 days ago")."\n"' 2003061110:22:53 $ perl -MDate::Manip -e 'print UnixDate("14 days ago","%Y%m%d")."\n"' 20030611 $ perl -MDate::Manip -e 'print localtime(UnixDate("14 days ago","%s")) +."\n"' Wed Jun 11 10:23:04 2003


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://268828]
Approved by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2016-12-03 19:46 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (59 votes). Check out past polls.