[Dub-pm] Time::Piece && while loops

Fergal Daly fergal at esatclear.ie
Wed May 5 17:20:49 CDT 2004


If you're worried about wasting cycles calling localtime(time()) and
extracting the date on every trip through the loop then following would
avoid that

while ($keep_logging)
{
	my $tomorrow = tomorrow();
	while (time() < $tomorrow)
	{
		log_something();
	}
	switch_files();
}

sub tomorrow
{
	# figures out when tomorrow starts in terms of seconds from epoch so
	# that it can be compared directly with a value given by time()

	my ($sec, $min, $hour) = localtime(time());
	my $tomorrow = $now - (($hour*60) * 60 + $sec) + 24*60*60;

	return $tomorrow;
}

A more efficient solution (the most efficient?) would be to use eval{} and
alarm() to set a timer to go off when tomorrow arrives but that'd be
overkill and has nasty pitfalls for a very small improvement over the code
above,

F

On Wed, May 05, 2004 at 09:51:31PM +0100, Andrew Barnes wrote:
> Hiya Guys
> 
> OK, as here's the thing I'm trying to do (sorry to repeat on ya DoC ;>)
> 
> I'm working on a script that will write out TCP connection information
> into a log file (let's call it $logfile ;>)  Let's say that
> 
> $date = yymmdd;
> $logfile = <hostname>.yymmdd-hhmmss.txt;
> 
> where the time info is of course substituted with the actual numeric
> values of course ;>
> 
> What I want this script to do is keep writing out the connection info into
> this same file *until* the day changes - ala "automated log rotation" ;>
> 
> SOooo... when talking to Doc on IRC last night we discussed using a while
> loop to do this.  My catch is trying to work out the "best way"
> (programatically, as well as "resource-wise") to do this - ie. check the
> day, and while $date != $date + 1, write into the log file - otherwise,
> update $date, $logfile, and write into the new file
> 
> I hope that this makes sense, and look forward to hearing the various
> answers/suggestions :-)
> 
> Regards,
>   Andrew
> 
> -- 
> Andrew Barnes
> Dublin Perl Mongers
> W: http://dublin.pm.org
> M: andrewbarnes at ramsesit.com
> M: +353873803633
> PGP Fingerprint: 2C8E E948 C62D 8C2C 48E3 0925 6ED3 78AA 5B17 6E26
> _______________________________________________
> Dublin-pm mailing list - Dublin-pm at mail.pm.org
> http://dublin.pm.org/ - IRC irc.linux.ie #dublin-pm
> 
> 



More information about the Dublin-pm mailing list