Perl: the Markov chain saw PerlMonks

### Find time after current time

 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.

Comment on Find time after current time
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",
);

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 (Parson) 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
Hello,
Use the Date::calc () and try to do this........
Regards,
qsl.
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.

Cheers,

JohnGG

Create A New User
Node Status?
node history
Node Type: perlquestion [id://558170]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2015-11-26 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?