1753730 Members
4791 Online
108799 Solutions
New Discussion юеВ

Date and Time Computing

 
Leslie Chaim
Regular Advisor

Date and Time Computing

In my script I need to sleep (delay the next step) until a specified time. In my case it is 4:00 PM.

I came up with this:

perl -e '
($h, $m, $s) = (localtime)[2, 1, 0];
$diff = (16 * 60 * 60) - (($h*60*60) + ($m*60) + ($s));
sleep $diff > 1 ? $diff : 0;
'

Is there a more elegant way to accomplish this.

Thanks,
Leslie
If life serves you lemons, make lemonade
3 REPLIES 3
H.Merijn Brand (procura
Honored Contributor

Re: Date and Time Computing

you can have a look at the time modules.

Time::Local and Time::localtime and look at mktime ()

perl -e '
($h, $m, $s) = (localtime)[2, 1, 0];
$diff = (16 * 60 * 60) - (($h*60*60) + ($m*60) + ($s));
sleep $diff > 1 ? $diff : 0;
'

can be written

perl -e '
($s, $m, $h) = (localtime)[0..2];
$diff = (16 * 60 * 60) - (($h * 60 + $m) * 60 + $s);
sleep $diff > 1 ? $diff : 0;'

making it just a /bit/ more comprehensable
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: Date and Time Computing

Plan A:

Use the Time::Local timelocal function to compute the epoch seconds of a given date/time. That will also make it easier to cross day boundaries with your calculations.

Plan B.

Set up a signal handler and use the system() function to issue an at command to send your process a signal at a specified time.
If it ain't broke, I can fix that.
Leslie Chaim
Regular Advisor

Re: Date and Time Computing

Can't use at..

Its possible that cmd 1 in my script would be done by 4:00 before cmd 2, in this case I want no delay.

at would run it the next day:)
If life serves you lemons, make lemonade