Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Find time after current time

by Anonymous Monk
on Jun 28, 2006 at 22:06 UTC ( #558170=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I have to find the time 2 minutes after the current time.Eg.Let us assume the current timestamp is,20060629 06:58:00.I have to increement 2 minutes and i want to get 20060629 07:00:00.
How we do this.Any modules availabe for this. Expecting your reply.

Replies are listed 'Best First'.
Re: Find time after current time
by saintmike (Vicar) on Jun 28, 2006 at 22:56 UTC
    Date calculations can be surprisingly tricky.

    Pop quiz: What time do you get when you add 2 seconds to 03:59:59 pm on 31/12/2005 in the PST timezone? Let's find out:

    use DateTime; my $dt = DateTime->new( year => 2005, month => 12, day => 31, hour => 15, minute => 59, second => 59, time_zone => "America/Los_Angeles", ); $dt->add(seconds => 2); print "$dt\n";
    Result is 2005-12-31T16:00:00 since there was a leap second in between.
Re: Find time after current time
by ioannis (Prior) on Jun 28, 2006 at 22:35 UTC
    It can be as simple as this; or, format it accordingly:

    print scalar localtime(time+(60*2));

Re: Find time after current time
by qsl (Scribe) on Jun 28, 2006 at 22:08 UTC
    Use the Date::calc () and try to do this........
Re: Find time after current time
by johngg (Abbot) on Jun 28, 2006 at 22:37 UTC
    You can split the timestamp up into year, month etc. and use the timelocal() subroutine of the Time::Local module (comes with Perl) to convert the timestamp to seconds since the epoch. Then add 120 to go forward two minutes and use localtime() to convert back to seconds, minutes etc.

    Be sure to watch out for years counting from 1900, e.g. 2006 should be supplied to timelocal() as 106 and you'll get the same back from localtime(). Also January is 0, December is 11.

    I hope this is of use.



Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2017-05-29 04:48 GMT
Find Nodes?
    Voting Booth?