This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Increase $English::VERSION to 1.06
[perl5.git] / cpan / Time-HiRes / HiRes.pm
CommitLineData
dcf686c9
JH
1package Time::HiRes;
2
90e44bf6 3{ use 5.006; }
dcf686c9 4use strict;
dcf686c9
JH
5
6require Exporter;
3f2ee006 7require DynaLoader;
dcf686c9 8
90e44bf6 9our @ISA = qw(Exporter DynaLoader);
dcf686c9 10
90e44bf6
Z
11our @EXPORT = qw( );
12our @EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval
ced84e60 13 getitimer setitimer nanosleep clock_gettime clock_getres
170c5524 14 clock clock_nanosleep
ced84e60 15 CLOCK_HIGHRES CLOCK_MONOTONIC CLOCK_PROCESS_CPUTIME_ID
170c5524
SP
16 CLOCK_REALTIME CLOCK_SOFTTIME CLOCK_THREAD_CPUTIME_ID
17 CLOCK_TIMEOFDAY CLOCKS_PER_SEC
3f2ee006 18 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF
170c5524 19 TIMER_ABSTIME
3f2ee006 20 d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
170c5524 21 d_nanosleep d_clock_gettime d_clock_getres
75d5269b
SP
22 d_clock d_clock_nanosleep
23 stat
24 );
bf8300de 25
858dcda5 26our $VERSION = '1.9725';
90e44bf6 27our $XS_VERSION = $VERSION;
105cd853 28$VERSION = eval $VERSION;
3c72ec00 29
90e44bf6 30our $AUTOLOAD;
3c72ec00
JH
31sub AUTOLOAD {
32 my $constname;
98b50af3 33 ($constname = $AUTOLOAD) =~ s/.*:://;
ced84e60 34 # print "AUTOLOAD: constname = $constname ($AUTOLOAD)\n";
98b50af3
JH
35 die "&Time::HiRes::constant not defined" if $constname eq 'constant';
36 my ($error, $val) = constant($constname);
ced84e60 37 # print "AUTOLOAD: error = $error, val = $val\n";
0cf8ddea
RGS
38 if ($error) {
39 my (undef,$file,$line) = caller;
40 die "$error at $file line $line.\n";
41 }
3c72ec00
JH
42 {
43 no strict 'refs';
44 *$AUTOLOAD = sub { $val };
45 }
46 goto &$AUTOLOAD;
47}
dcf686c9 48
ced84e60
SP
49sub import {
50 my $this = shift;
51 for my $i (@_) {
170c5524
SP
52 if (($i eq 'clock_getres' && !&d_clock_getres) ||
53 ($i eq 'clock_gettime' && !&d_clock_gettime) ||
54 ($i eq 'clock_nanosleep' && !&d_clock_nanosleep) ||
55 ($i eq 'clock' && !&d_clock) ||
56 ($i eq 'nanosleep' && !&d_nanosleep) ||
57 ($i eq 'usleep' && !&d_usleep) ||
58 ($i eq 'ualarm' && !&d_ualarm)) {
ced84e60
SP
59 require Carp;
60 Carp::croak("Time::HiRes::$i(): unimplemented in this platform");
61 }
62 }
63 Time::HiRes->export_to_level(1, $this, @_);
64}
65
0cf8ddea 66bootstrap Time::HiRes;
dcf686c9
JH
67
68# Preloaded methods go here.
69
70sub tv_interval {
71 # probably could have been done in C
72 my ($a, $b) = @_;
73 $b = [gettimeofday()] unless defined($b);
74 (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000);
75}
76
dcf686c9
JH
77# Autoload methods go after =cut, and are processed by the autosplit program.
78
791;
80__END__
81
82=head1 NAME
83
f7916ddb 84Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers
dcf686c9
JH
85
86=head1 SYNOPSIS
87
ced84e60 88 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep
75d5269b
SP
89 clock_gettime clock_getres clock_nanosleep clock
90 stat );
dcf686c9
JH
91
92 usleep ($microseconds);
44d3ce20 93 nanosleep ($nanoseconds);
dcf686c9
JH
94
95 ualarm ($microseconds);
96 ualarm ($microseconds, $interval_microseconds);
97
98 $t0 = [gettimeofday];
99 ($seconds, $microseconds) = gettimeofday;
100
101 $elapsed = tv_interval ( $t0, [$seconds, $microseconds]);
102 $elapsed = tv_interval ( $t0, [gettimeofday]);
103 $elapsed = tv_interval ( $t0 );
104
105 use Time::HiRes qw ( time alarm sleep );
3c72ec00 106
dcf686c9
JH
107 $now_fractions = time;
108 sleep ($floating_seconds);
109 alarm ($floating_seconds);
110 alarm ($floating_seconds, $floating_interval);
111
bfe77af1 112 use Time::HiRes qw( setitimer getitimer );
3c72ec00
JH
113
114 setitimer ($which, $floating_seconds, $floating_interval );
115 getitimer ($which);
116
bfe77af1
SP
117 use Time::HiRes qw( clock_gettime clock_getres clock_nanosleep
118 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF );
119
82cbdcc3
SP
120 $realtime = clock_gettime(CLOCK_REALTIME);
121 $resolution = clock_getres(CLOCK_REALTIME);
ced84e60 122
a8fb48f7
SP
123 clock_nanosleep(CLOCK_REALTIME, 1.5e9);
124 clock_nanosleep(CLOCK_REALTIME, time()*1e9 + 10e9, TIMER_ABSTIME);
170c5524
SP
125
126 my $ticktock = clock();
127
bfe77af1
SP
128 use Time::HiRes qw( stat );
129
c09e847b 130 my @stat = stat("file");
75d5269b
SP
131 my @stat = stat(FH);
132
dcf686c9
JH
133=head1 DESCRIPTION
134
4ed0e2d4 135The C<Time::HiRes> module implements a Perl interface to the
44d3ce20
RGS
136C<usleep>, C<nanosleep>, C<ualarm>, C<gettimeofday>, and
137C<setitimer>/C<getitimer> system calls, in other words, high
138resolution time and timers. See the L</EXAMPLES> section below and the
139test scripts for usage; see your system documentation for the
140description of the underlying C<nanosleep> or C<usleep>, C<ualarm>,
141C<gettimeofday>, and C<setitimer>/C<getitimer> calls.
dcf686c9 142
6937b144 143If your system lacks C<gettimeofday()> or an emulation of it you don't
4ed0e2d4 144get C<gettimeofday()> or the one-argument form of C<tv_interval()>.
82cbdcc3
SP
145If your system lacks all of C<nanosleep()>, C<usleep()>,
146C<select()>, and C<poll>, you don't get C<Time::HiRes::usleep()>,
147C<Time::HiRes::nanosleep()>, or C<Time::HiRes::sleep()>.
148If your system lacks both C<ualarm()> and C<setitimer()> you don't get
44d3ce20 149C<Time::HiRes::ualarm()> or C<Time::HiRes::alarm()>.
3f2ee006
HS
150
151If you try to import an unimplemented function in the C<use> statement
152it will fail at compile time.
153
4ed0e2d4
RGS
154If your subsecond sleeping is implemented with C<nanosleep()> instead
155of C<usleep()>, you can mix subsecond sleeping with signals since
64a7a97c
RGS
156C<nanosleep()> does not use signals. This, however, is not portable,
157and you should first check for the truth value of
4ed0e2d4
RGS
158C<&Time::HiRes::d_nanosleep> to see whether you have nanosleep, and
159then carefully read your C<nanosleep()> C API documentation for any
44d3ce20 160peculiarities.
0be47ac6 161
0cf8ddea
RGS
162If you are using C<nanosleep> for something else than mixing sleeping
163with signals, give some thought to whether Perl is the tool you should
164be using for work requiring nanosecond accuracies.
dcf686c9 165
bfe77af1
SP
166Remember that unless you are working on a I<hard realtime> system,
167any clocks and timers will be imprecise, especially so if you are working
168in a pre-emptive multiuser system. Understand the difference between
169I<wallclock time> and process time (in UNIX-like systems the sum of
170I<user> and I<system> times). Any attempt to sleep for X seconds will
171most probably end up sleeping B<more> than that, but don't be surpised
172if you end up sleeping slightly B<less>.
173
3c72ec00
JH
174The following functions can be imported from this module.
175No functions are exported by default.
dcf686c9
JH
176
177=over 4
178
179=item gettimeofday ()
180
0be47ac6 181In array context returns a two-element array with the seconds and
f7916ddb 182microseconds since the epoch. In scalar context returns floating
6937b144 183seconds like C<Time::HiRes::time()> (see below).
dcf686c9
JH
184
185=item usleep ( $useconds )
186
44d3ce20 187Sleeps for the number of microseconds (millionths of a second)
bfe77af1
SP
188specified. Returns the number of microseconds actually slept.
189Can sleep for more than one second, unlike the C<usleep> system call.
190Can also sleep for zero seconds, which often works like a I<thread yield>.
170c5524
SP
191See also C<Time::HiRes::usleep()>, C<Time::HiRes::sleep()>, and
192C<Time::HiRes::clock_nanosleep()>.
44d3ce20
RGS
193
194Do not expect usleep() to be exact down to one microsecond.
195
196=item nanosleep ( $nanoseconds )
197
198Sleeps for the number of nanoseconds (1e9ths of a second) specified.
199Returns the number of nanoseconds actually slept (accurate only to
200microseconds, the nearest thousand of them). Can sleep for more than
bfe77af1
SP
201one second. Can also sleep for zero seconds, which often works like
202a I<thread yield>. See also C<Time::HiRes::sleep()>,
170c5524 203C<Time::HiRes::usleep()>, and C<Time::HiRes::clock_nanosleep()>.
44d3ce20
RGS
204
205Do not expect nanosleep() to be exact down to one nanosecond.
206Getting even accuracy of one thousand nanoseconds is good.
dcf686c9
JH
207
208=item ualarm ( $useconds [, $interval_useconds ] )
209
6937b144
MJD
210Issues a C<ualarm> call; the C<$interval_useconds> is optional and
211will be zero if unspecified, resulting in C<alarm>-like behaviour.
dcf686c9 212
bf8300de
RGS
213Returns the remaining time in the alarm in microseconds, or C<undef>
214if an error occurred.
215
bfe77af1
SP
216ualarm(0) will cancel an outstanding ualarm().
217
993164ab 218Note that the interaction between alarms and sleeps is unspecified.
64a7a97c 219
443572f5
RB
220=item tv_interval
221
0be47ac6 222tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )
dcf686c9 223
f7916ddb 224Returns the floating seconds between the two times, which should have
6937b144 225been returned by C<gettimeofday()>. If the second argument is omitted,
f7916ddb 226then the current time is used.
dcf686c9
JH
227
228=item time ()
229
f7916ddb 230Returns a floating seconds since the epoch. This function can be
6937b144
MJD
231imported, resulting in a nice drop-in replacement for the C<time>
232provided with core Perl; see the L</EXAMPLES> below.
dcf686c9 233
6937b144
MJD
234B<NOTE 1>: This higher resolution timer can return values either less
235or more than the core C<time()>, depending on whether your platform
236rounds the higher resolution timer values up, down, or to the nearest second
237to get the core C<time()>, but naturally the difference should be never
ced84e60
SP
238more than half a second. See also L</clock_getres>, if available
239in your system.
f7916ddb 240
6937b144
MJD
241B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT, when
242the C<time()> seconds since epoch rolled over to 1_000_000_000, the
0be47ac6
JH
243default floating point format of Perl and the seconds since epoch have
244conspired to produce an apparent bug: if you print the value of
4ed0e2d4
RGS
245C<Time::HiRes::time()> you seem to be getting only five decimals, not
246six as promised (microseconds). Not to worry, the microseconds are
64a7a97c 247there (assuming your platform supports such granularity in the first
4ed0e2d4
RGS
248place). What is going on is that the default floating point format of
249Perl only outputs 15 digits. In this case that means ten digits
250before the decimal separator and five after. To see the microseconds
251you can use either C<printf>/C<sprintf> with C<"%.6f">, or the
252C<gettimeofday()> function in list context, which will give you the
253seconds and microseconds as two separate values.
389199d8 254
dcf686c9
JH
255=item sleep ( $floating_seconds )
256
f7916ddb 257Sleeps for the specified amount of seconds. Returns the number of
64a7a97c
RGS
258seconds actually slept (a floating point value). This function can
259be imported, resulting in a nice drop-in replacement for the C<sleep>
6937b144 260provided with perl, see the L</EXAMPLES> below.
dcf686c9 261
993164ab 262Note that the interaction between alarms and sleeps is unspecified.
64a7a97c 263
dcf686c9
JH
264=item alarm ( $floating_seconds [, $interval_floating_seconds ] )
265
6937b144 266The C<SIGALRM> signal is sent after the specified number of seconds.
bf8300de
RGS
267Implemented using C<setitimer()> if available, C<ualarm()> if not.
268The C<$interval_floating_seconds> argument is optional and will be
269zero if unspecified, resulting in C<alarm()>-like behaviour. This
270function can be imported, resulting in a nice drop-in replacement for
271the C<alarm> provided with perl, see the L</EXAMPLES> below.
272
273Returns the remaining time in the alarm in seconds, or C<undef>
274if an error occurred.
dcf686c9 275
64a7a97c
RGS
276B<NOTE 1>: With some combinations of operating systems and Perl
277releases C<SIGALRM> restarts C<select()>, instead of interrupting it.
278This means that an C<alarm()> followed by a C<select()> may together
858dcda5 279take the sum of the times specified for the C<alarm()> and the
64a7a97c
RGS
280C<select()>, not just the time of the C<alarm()>.
281
993164ab 282Note that the interaction between alarms and sleeps is unspecified.
3f2ee006 283
6937b144 284=item setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )
3c72ec00 285
bfe77af1 286Start up an interval timer: after a certain time, a signal ($which) arrives,
64a7a97c
RGS
287and more signals may keep arriving at certain intervals. To disable
288an "itimer", use C<$floating_seconds> of zero. If the
289C<$interval_floating_seconds> is set to zero (or unspecified), the
290timer is disabled B<after> the next delivered signal.
3c72ec00 291
6937b144
MJD
292Use of interval timers may interfere with C<alarm()>, C<sleep()>,
293and C<usleep()>. In standard-speak the "interaction is unspecified",
0be47ac6 294which means that I<anything> may happen: it may work, it may not.
3c72ec00
JH
295
296In scalar context, the remaining time in the timer is returned.
297
298In list context, both the remaining time and the interval are returned.
299
bfe77af1 300There are usually three or four interval timers (signals) available: the
4ed0e2d4
RGS
301C<$which> can be C<ITIMER_REAL>, C<ITIMER_VIRTUAL>, C<ITIMER_PROF>, or
302C<ITIMER_REALPROF>. Note that which ones are available depends: true
e5620114
RGS
303UNIX platforms usually have the first three, but only Solaris seems to
304have C<ITIMER_REALPROF> (which is used to profile multithreaded programs).
305Win32 unfortunately does not haveinterval timers.
3c72ec00 306
993164ab 307C<ITIMER_REAL> results in C<alarm()>-like behaviour. Time is counted in
6937b144 308I<real time>; that is, wallclock time. C<SIGALRM> is delivered when
3c72ec00
JH
309the timer expires.
310
4ed0e2d4
RGS
311C<ITIMER_VIRTUAL> counts time in (process) I<virtual time>; that is,
312only when the process is running. In multiprocessor/user/CPU systems
313this may be more or less than real or wallclock time. (This time is
314also known as the I<user time>.) C<SIGVTALRM> is delivered when the
315timer expires.
3c72ec00 316
6937b144 317C<ITIMER_PROF> counts time when either the process virtual time or when
0be47ac6
JH
318the operating system is running on behalf of the process (such as I/O).
319(This time is also known as the I<system time>.) (The sum of user
6937b144
MJD
320time and system time is known as the I<CPU time>.) C<SIGPROF> is
321delivered when the timer expires. C<SIGPROF> can interrupt system calls.
3c72ec00
JH
322
323The semantics of interval timers for multithreaded programs are
324system-specific, and some systems may support additional interval
bfe77af1
SP
325timers. For example, it is unspecified which thread gets the signals.
326See your C<setitimer()> documentation.
3c72ec00
JH
327
328=item getitimer ( $which )
329
6937b144 330Return the remaining time in the interval timer specified by C<$which>.
3c72ec00
JH
331
332In scalar context, the remaining time is returned.
333
334In list context, both the remaining time and the interval are returned.
6937b144 335The interval is always what you put in using C<setitimer()>.
3c72ec00 336
ced84e60
SP
337=item clock_gettime ( $which )
338
339Return as seconds the current value of the POSIX high resolution timer
340specified by C<$which>. All implementations that support POSIX high
341resolution timers are supposed to support at least the C<$which> value
342of C<CLOCK_REALTIME>, which is supposed to return results close to the
343results of C<gettimeofday>, or the number of seconds since 00:00:00:00
344January 1, 1970 Greenwich Mean Time (GMT). Do not assume that
345CLOCK_REALTIME is zero, it might be one, or something else.
346Another potentially useful (but not available everywhere) value is
347C<CLOCK_MONOTONIC>, which guarantees a monotonically increasing time
e5620114
RGS
348value (unlike time() or gettimeofday(), which can be adjusted).
349See your system documentation for other possibly supported values.
ced84e60
SP
350
351=item clock_getres ( $which )
352
353Return as seconds the resolution of the POSIX high resolution timer
354specified by C<$which>. All implementations that support POSIX high
355resolution timers are supposed to support at least the C<$which> value
170c5524
SP
356of C<CLOCK_REALTIME>, see L</clock_gettime>.
357
a8fb48f7 358=item clock_nanosleep ( $which, $nanoseconds, $flags = 0)
170c5524 359
a8fb48f7
SP
360Sleeps for the number of nanoseconds (1e9ths of a second) specified.
361Returns the number of nanoseconds actually slept. The $which is the
170c5524
SP
362"clock id", as with clock_gettime() and clock_getres(). The flags
363default to zero but C<TIMER_ABSTIME> can specified (must be exported
364explicitly) which means that C<$nanoseconds> is not a time interval
365(as is the default) but instead an absolute time. Can sleep for more
366than one second. Can also sleep for zero seconds, which often works
367like a I<thread yield>. See also C<Time::HiRes::sleep()>,
368C<Time::HiRes::usleep()>, and C<Time::HiRes::nanosleep()>.
369
370Do not expect clock_nanosleep() to be exact down to one nanosecond.
371Getting even accuracy of one thousand nanoseconds is good.
372
373=item clock()
374
375Return as seconds the I<process time> (user + system time) spent by
376the process since the first call to clock() (the definition is B<not>
377"since the start of the process", though if you are lucky these times
378may be quite close to each other, depending on the system). What this
379means is that you probably need to store the result of your first call
380to clock(), and subtract that value from the following results of clock().
381
382The time returned also includes the process times of the terminated
383child processes for which wait() has been executed. This value is
384somewhat like the second value returned by the times() of core Perl,
385but not necessarily identical. Note that due to backward
ff7df920
SP
386compatibility limitations the returned value may wrap around at about
3872147 seconds or at about 36 minutes.
ced84e60 388
75d5269b
SP
389=item stat
390
391=item stat FH
392
393=item stat EXPR
394
395As L<perlfunc/stat> but with the access/modify/change file timestamps
396in subsecond resolution, if the operating system and the filesystem
397both support such timestamps. To override the standard stat():
398
399 use Time::HiRes qw(stat);
400
401Test for the value of &Time::HiRes::d_hires_stat to find out whether
402the operating system supports subsecond file timestamps: a value
403larger than zero means yes. There are unfortunately no easy
404ways to find out whether the filesystem supports such timestamps.
c09e847b
SP
405UNIX filesystems often do; NTFS does; FAT doesn't (FAT timestamp
406granularity is B<two> seconds).
75d5269b
SP
407
408A zero return value of &Time::HiRes::d_hires_stat means that
409Time::HiRes::stat is a no-op passthrough for CORE::stat(),
410and therefore the timestamps will stay integers. The same
bfe77af1 411thing will happen if the filesystem does not do subsecond timestamps,
c09e847b 412even if the &Time::HiRes::d_hires_stat is non-zero.
75d5269b
SP
413
414In any case do not expect nanosecond resolution, or even a microsecond
bfe77af1
SP
415resolution. Also note that the modify/access timestamps might have
416different resolutions, and that they need not be synchronized, e.g.
417if the operations are
418
419 write
420 stat # t1
421 read
422 stat # t2
423
424the access time stamp from t2 need not be greater-than the modify
425time stamp from t1: it may be equal or I<less>.
75d5269b 426
dcf686c9
JH
427=back
428
429=head1 EXAMPLES
430
431 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
432
433 $microseconds = 750_000;
70cf0185 434 usleep($microseconds);
dcf686c9
JH
435
436 # signal alarm in 2.5s & every .1s thereafter
70cf0185 437 ualarm(2_500_000, 100_000);
bfe77af1
SP
438 # cancel that ualarm
439 ualarm(0);
dcf686c9
JH
440
441 # get seconds and microseconds since the epoch
70cf0185 442 ($s, $usec) = gettimeofday();
dcf686c9
JH
443
444 # measure elapsed time
445 # (could also do by subtracting 2 gettimeofday return values)
446 $t0 = [gettimeofday];
447 # do bunch of stuff here
448 $t1 = [gettimeofday];
449 # do more stuff here
450 $t0_t1 = tv_interval $t0, $t1;
0be47ac6 451
dcf686c9
JH
452 $elapsed = tv_interval ($t0, [gettimeofday]);
453 $elapsed = tv_interval ($t0); # equivalent code
454
455 #
456 # replacements for time, alarm and sleep that know about
457 # floating seconds
458 #
459 use Time::HiRes;
460 $now_fractions = Time::HiRes::time;
461 Time::HiRes::sleep (2.5);
462 Time::HiRes::alarm (10.6666666);
0be47ac6 463
dcf686c9
JH
464 use Time::HiRes qw ( time alarm sleep );
465 $now_fractions = time;
466 sleep (2.5);
467 alarm (10.6666666);
468
3c72ec00
JH
469 # Arm an interval timer to go off first at 10 seconds and
470 # after that every 2.5 seconds, in process virtual time
471
472 use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
473
36d6c396 474 $SIG{VTALRM} = sub { print time, "\n" };
3c72ec00
JH
475 setitimer(ITIMER_VIRTUAL, 10, 2.5);
476
1a7d3a53
SP
477 use Time::HiRes qw( clock_gettime clock_getres CLOCK_REALTIME );
478 # Read the POSIX high resolution timer.
479 my $high = clock_getres(CLOCK_REALTIME);
480 # But how accurate we can be, really?
481 my $reso = clock_getres(CLOCK_REALTIME);
ced84e60 482
170c5524
SP
483 use Time::HiRes qw( clock_nanosleep TIMER_ABSTIME );
484 clock_nanosleep(CLOCK_REALTIME, 1e6);
485 clock_nanosleep(CLOCK_REALTIME, 2e9, TIMER_ABSTIME);
486
487 use Time::HiRes qw( clock );
488 my $clock0 = clock();
489 ... # Do something.
490 my $clock1 = clock();
491 my $clockd = $clock1 - $clock0;
492
c09e847b
SP
493 use Time::HiRes qw( stat );
494 my ($atime, $mtime, $ctime) = (stat("istics"))[8, 9, 10];
495
dcf686c9
JH
496=head1 C API
497
498In addition to the perl API described above, a C API is available for
499extension writers. The following C functions are available in the
500modglobal hash:
501
502 name C prototype
503 --------------- ----------------------
504 Time::NVtime double (*)()
06252d99 505 Time::U2time void (*)(pTHX_ UV ret[2])
dcf686c9 506
6937b144
MJD
507Both functions return equivalent information (like C<gettimeofday>)
508but with different representations. The names C<NVtime> and C<U2time>
dcf686c9 509were selected mainly because they are operating system independent.
56c1b3bd
RGS
510(C<gettimeofday> is Unix-centric, though some platforms like Win32 and
511VMS have emulations for it.)
dcf686c9 512
6937b144 513Here is an example of using C<NVtime> from C:
dcf686c9 514
993164ab 515 double (*myNVtime)(); /* Returns -1 on failure. */
dcf686c9
JH
516 SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
517 if (!svp) croak("Time::HiRes is required");
518 if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
356234a5 519 myNVtime = INT2PTR(double(*)(), SvIV(*svp));
dcf686c9
JH
520 printf("The current time is: %f\n", (*myNVtime)());
521
db0b859f
JH
522=head1 DIAGNOSTICS
523
34f69483
SP
524=head2 useconds or interval more than ...
525
526In ualarm() you tried to use number of microseconds or interval (also
527in microseconds) more than 1_000_000 and setitimer() is not available
528in your system to emulate that case.
529
db0b859f
JH
530=head2 negative time not invented yet
531
532You tried to use a negative time argument.
533
534=head2 internal error: useconds < 0 (unsigned ... signed ...)
535
536Something went horribly wrong-- the number of microseconds that cannot
537become negative just became negative. Maybe your compiler is broken?
538
bf8300de
RGS
539=head2 useconds or uinterval equal to or more than 1000000
540
541In some platforms it is not possible to get an alarm with subsecond
542resolution and later than one second.
543
544=head2 unimplemented in this platform
545
546Some calls simply aren't available, real or emulated, on every platform.
547
f03b998d
JH
548=head1 CAVEATS
549
6937b144 550Notice that the core C<time()> maybe rounding rather than truncating.
d8cb5b61
RGS
551What this means is that the core C<time()> may be reporting the time
552as one second later than C<gettimeofday()> and C<Time::HiRes::time()>.
553
554Adjusting the system clock (either manually or by services like ntp)
555may cause problems, especially for long running programs that assume
556a monotonously increasing time (note that all platforms do not adjust
557time as gracefully as UNIX ntp does). For example in Win32 (and derived
558platforms like Cygwin and MinGW) the Time::HiRes::time() may temporarily
559drift off from the system clock (and the original time()) by up to 0.5
560seconds. Time::HiRes will notice this eventually and recalibrate.
ced84e60 561Note that since Time::HiRes 1.77 the clock_gettime(CLOCK_MONOTONIC)
1a7d3a53 562might help in this (in case your system supports CLOCK_MONOTONIC).
f03b998d 563
e5620114
RGS
564Some systems have APIs but not implementations: for example QNX and Haiku
565have the interval timer APIs but not the functionality.
566
26e22fd9
RGS
567=head1 SEE ALSO
568
ced84e60
SP
569Perl modules L<BSD::Resource>, L<Time::TAI64>.
570
bfe77af1
SP
571Your system documentation for C<clock>, C<clock_gettime>,
572C<clock_getres>, C<clock_nanosleep>, C<clock_settime>, C<getitimer>,
573C<gettimeofday>, C<setitimer>, C<sleep>, C<stat>, C<ualarm>.
26e22fd9 574
dcf686c9
JH
575=head1 AUTHORS
576
577D. Wegscheid <wegscd@whirlpool.com>
578R. Schertler <roderick@argon.org>
579J. Hietaniemi <jhi@iki.fi>
580G. Aas <gisle@aas.no>
581
3f2ee006 582=head1 COPYRIGHT AND LICENSE
dcf686c9 583
3f2ee006 584Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
dcf686c9 585
bf8300de
RGS
586Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Jarkko Hietaniemi.
587All rights reserved.
dcf686c9 588
858dcda5 589Copyright (C) 2011, 2012 Andrew Main (Zefram) <zefram@fysh.org>
91a2e9f6 590
3f2ee006
HS
591This program is free software; you can redistribute it and/or modify
592it under the same terms as Perl itself.
dcf686c9
JH
593
594=cut