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