Perl: the Markov chain saw PerlMonks

### Re: How long 'tween now and then?

by reptile (Monk)
 on Sep 13, 2000 at 03:49 UTC ( #32213=note: print w/replies, xml ) Need Help??

in reply to How long 'tween now and then?

Time::Local has a function called timelocal (surprise) that converts information like that returned from localtime back into epoch seconds (like that returned from time). Consider this:

```# get the epoch seconds for 8:00am the following day
# NOTE if it's after midnight this doesn't work but
# should be trivial to fix
my \$now = time();
my (\$mday, \$mon, \$year) = (localtime(\$now))[3,4,5];
my \$then = timelocal(0, 0, 8, \$mday + 1, \$mon, \$year);
my \$difference = \$then - \$now;
sleep(\$difference);

That should sleep until 8am the following day, but if you start that after midnight and before 8am, it won't work quite right. A little logic can fix that (add 1 to \$mday unless the current hour is less than 8). Also it gets even more complicated if it's the last day of the month. That will break this completely and requires a lot more work to check for. And what if it's the last day of the year too? You'll need to check for that. Ok, so you set all that up too, but it's Februrary 28th on a leap year. Oops broke again.

In other words, it ain't easy. Maybe you should use Date::Calc.

Update I thought of something that might work also that doesn't involve these kinds of calculations. Sleep for 1 second until it's 8am, perhaps like so:

```sleep(1) until ((localtime)[2] == 8);
# it's now 8:00am.. do your thing.

Just an idea.

```local \$_ = "0A72656B636148206C72655020726568746F6E41207473754A";
while(s/..\$//) { print chr(hex(\$&)) }

Replies are listed 'Best First'.
RE: Re: How long 'tween now and then?
by myocom (Deacon) on Sep 13, 2000 at 23:02 UTC

A most interesting update, though it'd chew too much CPU time. Just as I was falling asleep last night, it occured to me to loop through sleeping for an hour or so until the day changes, then figure the seconds after that (which would get rid of the 'exceptions' like end of month/year).

Since this will all be compiled into an EXE (for the sake of ease), the fewer big ol' modules like Date::Calc I have to include, the better. Something just bugs me about adding several hundred K to an EXE just to figure out what time it is...

Create A New User
Node Status?
node history
Node Type: note [id://32213]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2018-03-23 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (295 votes). Check out past polls.

Notices?