Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding Time in perl 1

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hi there!!!

I'm wondering is there any easy way to add or manipulate time in the built-in time function.

In the code below if i add $sec=$sec + 10; then if its greater than 60 seconds increase the minutes by one and if the minutes is greater than 60 then increase the hour by one. on and on ...so i'm wondering is there any easy way to add minutes and hours in the time function.

Thanks.

$time=time();
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($time);

$year += 1900;
$month=$mon + 1;



 
See Date::Calc -
It'll handle that kinda stuff for you, taking into account more complex things such as change of year, leapyears, leap seconds, and lots of other things that are really difficult to account for when reinventing this wheel.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Code:
$time=time();
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($time+1);

$year += 1900;
$month=$mon + 1;

See where its +1? That adds one second to the current time on the computer the program is running on. Multiply by 60 for one minute, 60*60 for 1 hour, 60*60*24 for one day, etc.

Or use a module like Date::Calc which is a handy interface into many date and time requirements.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
In the above I should have said:

See where its $time+1?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks KevinADC. That will work.
 
Thanks Kirsle for the module recommendation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top