Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: system-clock-safe timers without time()?

by tachyon (Chancellor)
on Apr 13, 2003 at 13:12 UTC ( [id://250133]=note: print w/replies, xml ) Need Help??


in reply to system-clock-safe timers without time()?

Why not just teach your server admin(s) to sync their clocks using NTP? Then you don't have a problem.....

As to the approach I would probably run a demon that just did:

#!/usr/bin/perl my $start = time(); my $sleep = 600; my $how_much = 5; my $offset_file = '/tmp/offset.txt'; my $offset = 0; print_to_file( $offset ); while ( 1 ) { sleep($sleep); my $now = time(); if ( abs ( $start + $sleep - $now ) > $how_much ) { $offset = $start + $sleep - $now; print_to_file( $offset ); } $start = time(); } sub print_to_file { my $offset = shift; `echo $offset > $offset_file`; }

In this approach we allow use 5 seconds of change in 10 minutes as our significance level. If the time has changed by more than this then someone has probably changed the clock so we write the offset to the offset file.... This is the general approach used by time limited software for instance that gets upset if you change the clock.

Just check the offset file as needed. This should be less load and is not dependent on sleep(1) being exactly 1,000 msec. If it is not your clock will drift considerably over time.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: system-clock-safe timers without time()?
by hatter (Pilgrim) on Apr 13, 2003 at 17:21 UTC
    You should be very wary of the first second of any sleep() call - as per the POSIX spec, this is allowed, and frequently is, somewhere between 0 and 1 seconds (being based on when the computers clock counds the end of a second. So whether your clock currently says 12:00:00.01 or 12:00:00.99, if you ask it to sleep 5, it will finish at 12:00:05.00 This is obviously less of a problem if you're asking it to sleep to just delay between two actions, but if you're using it to issue metronome clicks, then you get strange issues depending on how long it takes to do the processing between the sections.

    Getting back to the main question, I read the issue as being more about changing to summertime, rather than clock corrections. (probably because the clocks only changed recently) But either way it sounds like your sysadmin needs educating - for time correcting, use NTP as suggested. For summertime, the system clock itself shouldn't be changed, but users can have their timezones changed to match their requirements. Meanwhile, your script can set its timezone to something fixed ($ENV{'TZ'} = 'UTC' is my preference) and then it won't be bothered by daylight saving.

    the hatter

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://250133]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found