Rework #16506 some more.
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / HiRes.xs
CommitLineData
dcf686c9 1#ifdef __cplusplus
2extern "C" {
3#endif
4#include "EXTERN.h"
5#include "perl.h"
6#include "XSUB.h"
7#ifdef WIN32
8#include <time.h>
9#else
10#include <sys/time.h>
11#endif
36df99d6 12#ifdef HAS_SELECT
13# ifdef I_SYS_SELECT
14# include <sys/select.h>
15# endif
16#endif
dcf686c9 17#ifdef __cplusplus
18}
19#endif
20
3c72ec00 21static IV
22constant(char *name, int arg)
23{
24 errno = 0;
25 switch (*name) {
26 case 'I':
27 if (strEQ(name, "ITIMER_REAL"))
28#ifdef ITIMER_REAL
29 return ITIMER_REAL;
30#else
31 goto not_there;
32#endif
33 if (strEQ(name, "ITIMER_REALPROF"))
34#ifdef ITIMER_REALPROF
35 return ITIMER_REALPROF;
36#else
37 goto not_there;
38#endif
39 if (strEQ(name, "ITIMER_VIRTUAL"))
40#ifdef ITIMER_VIRTUAL
41 return ITIMER_VIRTUAL;
42#else
43 goto not_there;
44#endif
45 if (strEQ(name, "ITIMER_PROF"))
46#ifdef ITIMER_PROF
47 return ITIMER_PROF;
48#else
49 goto not_there;
50#endif
51 break;
52 }
53 errno = EINVAL;
54 return 0;
55
56not_there:
57 errno = ENOENT;
58 return 0;
59}
60
fd44fdfd 61#if !defined(HAS_GETTIMEOFDAY) && defined(WIN32)
62#define HAS_GETTIMEOFDAY
63
6e3b076d 64/* shows up in winsock.h?
65struct timeval {
66 long tv_sec;
67 long tv_usec;
68}
69*/
70
fd44fdfd 71typedef union {
72 unsigned __int64 ft_i64;
73 FILETIME ft_val;
74} FT_t;
75
6e3b076d 76/* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
fd44fdfd 77#ifdef __GNUC__
78#define Const64(x) x##LL
79#else
80#define Const64(x) x##i64
81#endif
fd44fdfd 82#define EPOCH_BIAS Const64(116444736000000000)
83
84/* NOTE: This does not compute the timezone info (doing so can be expensive,
85 * and appears to be unsupported even by glibc) */
6e3b076d 86int
87gettimeofday (struct timeval *tp, void *not_used)
fd44fdfd 88{
89 FT_t ft;
90
91 /* this returns time in 100-nanosecond units (i.e. tens of usecs) */
92 GetSystemTimeAsFileTime(&ft.ft_val);
93
94 /* seconds since epoch */
95 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
96
97 /* microseconds remaining */
98 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
99
100 return 0;
101}
6e3b076d 102#endif
fd44fdfd 103
dcf686c9 104#if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
105#define HAS_GETTIMEOFDAY
106
9b6f56ad 107#include <lnmdef.h>
dcf686c9 108#include <time.h> /* gettimeofday */
109#include <stdlib.h> /* qdiv */
110#include <starlet.h> /* sys$gettim */
111#include <descrip.h>
3785778e 112#ifdef __VAX
113#include <lib$routines.h> /* lib$ediv() */
114#endif
dcf686c9 115
116/*
117 VMS binary time is expressed in 100 nano-seconds since
118 system base time which is 17-NOV-1858 00:00:00.00
119*/
120
121#define DIV_100NS_TO_SECS 10000000L
122#define DIV_100NS_TO_USECS 10L
123
124/*
125 gettimeofday is supposed to return times since the epoch
126 so need to determine this in terms of VMS base time
127*/
128static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
129
5cdb7193 130#ifdef __VAX
3785778e 131static long base_adjust[2]={0L,0L};
5cdb7193 132#else
dcf686c9 133static __int64 base_adjust=0;
5cdb7193 134#endif
dcf686c9 135
9b6f56ad 136/*
137
138 If we don't have gettimeofday, then likely we are on a VMS machine that
139 operates on local time rather than UTC...so we have to zone-adjust.
140 This code gleefully swiped from VMS.C
141
142*/
143/* method used to handle UTC conversions:
144 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
145 */
146static int gmtime_emulation_type;
147/* number of secs to add to UTC POSIX-style time to get local time */
148static long int utc_offset_secs;
149static struct dsc$descriptor_s fildevdsc =
150 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
151static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
152
153static time_t toutc_dst(time_t loc) {
154 struct tm *rsltmp;
155
156 if ((rsltmp = localtime(&loc)) == NULL) return -1;
157 loc -= utc_offset_secs;
158 if (rsltmp->tm_isdst) loc -= 3600;
159 return loc;
160}
161
162static time_t toloc_dst(time_t utc) {
163 struct tm *rsltmp;
164
165 utc += utc_offset_secs;
166 if ((rsltmp = localtime(&utc)) == NULL) return -1;
167 if (rsltmp->tm_isdst) utc += 3600;
168 return utc;
169}
170
171#define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
172 ((gmtime_emulation_type || timezone_setup()), \
173 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
174 ((secs) - utc_offset_secs))))
175
176#define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
177 ((gmtime_emulation_type || timezone_setup()), \
178 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
179 ((secs) + utc_offset_secs))))
180
181static int
182timezone_setup(void)
183{
184 struct tm *tm_p;
185
186 if (gmtime_emulation_type == 0) {
187 int dstnow;
188 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
189 /* results of calls to gmtime() and localtime() */
190 /* for same &base */
191
192 gmtime_emulation_type++;
193 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
194 char off[LNM$C_NAMLENGTH+1];;
195
196 gmtime_emulation_type++;
197 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
198 gmtime_emulation_type++;
199 utc_offset_secs = 0;
200 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
201 }
202 else { utc_offset_secs = atol(off); }
203 }
204 else { /* We've got a working gmtime() */
205 struct tm gmt, local;
206
207 gmt = *tm_p;
208 tm_p = localtime(&base);
209 local = *tm_p;
210 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
211 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
212 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
213 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
214 }
215 }
216 return 1;
217}
218
219
dcf686c9 220int
221gettimeofday (struct timeval *tp, void *tpz)
222{
223 long ret;
5cdb7193 224#ifdef __VAX
3785778e 225 long quad[2];
226 long quad1[2];
227 long div_100ns_to_secs;
228 long div_100ns_to_usecs;
229 long quo,rem;
230 long quo1,rem1;
5cdb7193 231#else
dcf686c9 232 __int64 quad;
233 __qdiv_t ans1,ans2;
5cdb7193 234#endif
dcf686c9 235/*
236 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
237 The return from function is also set to -1.
238 This is not exactly as per the manual page.
239*/
240
241 tp->tv_usec = 0;
242
3785778e 243#ifdef __VAX
244 if (base_adjust[0]==0 && base_adjust[1]==0) {
245#else
dcf686c9 246 if (base_adjust==0) { /* Need to determine epoch adjustment */
3785778e 247#endif
dcf686c9 248 ret=sys$bintim(&dscepoch,&base_adjust);
249 if (1 != (ret &&1)) {
250 tp->tv_sec = ret;
251 return -1;
252 }
253 }
254
255 ret=sys$gettim(&quad); /* Get VMS system time */
256 if ((1 && ret) == 1) {
5cdb7193 257#ifdef __VAX
3785778e 258 quad[0] -= base_adjust[0]; /* convert to epoch offset */
259 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
260 div_100ns_to_secs = DIV_100NS_TO_SECS;
261 div_100ns_to_usecs = DIV_100NS_TO_USECS;
262 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
263 quad1[0] = rem;
264 quad1[1] = 0L;
265 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
266 tp->tv_sec = quo; /* Whole seconds */
267 tp->tv_usec = quo1; /* Micro-seconds */
5cdb7193 268#else
3785778e 269 quad -= base_adjust; /* convert to epoch offset */
dcf686c9 270 ans1=qdiv(quad,DIV_100NS_TO_SECS);
271 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
272 tp->tv_sec = ans1.quot; /* Whole seconds */
273 tp->tv_usec = ans2.quot; /* Micro-seconds */
3785778e 274#endif
dcf686c9 275 } else {
276 tp->tv_sec = ret;
277 return -1;
278 }
9b6f56ad 279# ifdef VMSISH_TIME
280# ifdef RTL_USES_UTC
281 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
282# else
283 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
284# endif
285# endif
dcf686c9 286 return 0;
287}
288#endif
289
290#if !defined(HAS_USLEEP) && defined(HAS_SELECT)
291#ifndef SELECT_IS_BROKEN
292#define HAS_USLEEP
293#define usleep hrt_usleep /* could conflict with ncurses for static build */
294
295void
296hrt_usleep(unsigned long usec)
297{
298 struct timeval tv;
299 tv.tv_sec = 0;
300 tv.tv_usec = usec;
301 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
302 (Select_fd_set_t)NULL, &tv);
303}
304#endif
305#endif
306
307#if !defined(HAS_USLEEP) && defined(WIN32)
308#define HAS_USLEEP
309#define usleep hrt_usleep /* could conflict with ncurses for static build */
310
311void
312hrt_usleep(unsigned long usec)
313{
314 long msec;
315 msec = usec / 1000;
316 Sleep (msec);
317}
318#endif
319
320
321#if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
322#define HAS_UALARM
323#define ualarm hrt_ualarm /* could conflict with ncurses for static build */
324
325int
326hrt_ualarm(int usec, int interval)
327{
328 struct itimerval itv;
329 itv.it_value.tv_sec = usec / 1000000;
330 itv.it_value.tv_usec = usec % 1000000;
331 itv.it_interval.tv_sec = interval / 1000000;
332 itv.it_interval.tv_usec = interval % 1000000;
333 return setitimer(ITIMER_REAL, &itv, 0);
334}
335#endif
336
ca40fe49 337#if !defined(HAS_UALARM) && defined(VMS)
338#define HAS_UALARM
339#define ualarm vms_ualarm
340
341#include <lib$routines.h>
342#include <ssdef.h>
343#include <starlet.h>
344#include <descrip.h>
345#include <signal.h>
346#include <jpidef.h>
347#include <psldef.h>
348
349#define VMSERR(s) (!((s)&1))
350
351static void
352us_to_VMS(useconds_t mseconds, unsigned long v[])
353{
354 int iss;
355 unsigned long qq[2];
356
357 qq[0] = mseconds;
358 qq[1] = 0;
359 v[0] = v[1] = 0;
360
361 iss = lib$addx(qq,qq,qq);
362 if (VMSERR(iss)) lib$signal(iss);
363 iss = lib$subx(v,qq,v);
364 if (VMSERR(iss)) lib$signal(iss);
365 iss = lib$addx(qq,qq,qq);
366 if (VMSERR(iss)) lib$signal(iss);
367 iss = lib$subx(v,qq,v);
368 if (VMSERR(iss)) lib$signal(iss);
369 iss = lib$subx(v,qq,v);
370 if (VMSERR(iss)) lib$signal(iss);
371}
372
373static int
374VMS_to_us(unsigned long v[])
375{
376 int iss;
377 unsigned long div=10,quot, rem;
378
379 iss = lib$ediv(&div,v,&quot,&rem);
380 if (VMSERR(iss)) lib$signal(iss);
381
382 return quot;
383}
384
385typedef unsigned short word;
386typedef struct _ualarm {
387 int function;
388 int repeat;
389 unsigned long delay[2];
390 unsigned long interval[2];
391 unsigned long remain[2];
392} Alarm;
393
394
395static int alarm_ef;
396static Alarm *a0, alarm_base;
397#define UAL_NULL 0
398#define UAL_SET 1
399#define UAL_CLEAR 2
400#define UAL_ACTIVE 4
401static void ualarm_AST(Alarm *a);
402
403static int
404vms_ualarm(int mseconds, int interval)
405{
406 Alarm *a, abase;
407 struct item_list3 {
408 word length;
409 word code;
410 void *bufaddr;
411 void *retlenaddr;
412 } ;
413 static struct item_list3 itmlst[2];
414 static int first = 1;
415 unsigned long asten;
416 int iss, enabled;
417
418 if (first) {
419 first = 0;
420 itmlst[0].code = JPI$_ASTEN;
421 itmlst[0].length = sizeof(asten);
422 itmlst[0].retlenaddr = NULL;
423 itmlst[1].code = 0;
424 itmlst[1].length = 0;
425 itmlst[1].bufaddr = NULL;
426 itmlst[1].retlenaddr = NULL;
427
428 iss = lib$get_ef(&alarm_ef);
429 if (VMSERR(iss)) lib$signal(iss);
430
431 a0 = &alarm_base;
432 a0->function = UAL_NULL;
433 }
434 itmlst[0].bufaddr = &asten;
435
436 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
437 if (VMSERR(iss)) lib$signal(iss);
438 if (!(asten&0x08)) return -1;
439
440 a = &abase;
441 if (mseconds) {
442 a->function = UAL_SET;
443 } else {
444 a->function = UAL_CLEAR;
445 }
446
447 us_to_VMS(mseconds, a->delay);
448 if (interval) {
449 us_to_VMS(interval, a->interval);
450 a->repeat = 1;
451 } else
452 a->repeat = 0;
453
454 iss = sys$clref(alarm_ef);
455 if (VMSERR(iss)) lib$signal(iss);
456
457 iss = sys$dclast(ualarm_AST,a,0);
458 if (VMSERR(iss)) lib$signal(iss);
459
460 iss = sys$waitfr(alarm_ef);
461 if (VMSERR(iss)) lib$signal(iss);
462
463 if (a->function == UAL_ACTIVE)
464 return VMS_to_us(a->remain);
465 else
466 return 0;
467}
468
469
470
471static void
472ualarm_AST(Alarm *a)
473{
474 int iss;
475 unsigned long now[2];
476
477 iss = sys$gettim(now);
478 if (VMSERR(iss)) lib$signal(iss);
479
480 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
481 if (a0->function == UAL_ACTIVE) {
482 iss = sys$cantim(a0,PSL$C_USER);
483 if (VMSERR(iss)) lib$signal(iss);
484
485 iss = lib$subx(a0->remain, now, a->remain);
486 if (VMSERR(iss)) lib$signal(iss);
487
488 if (a->remain[1] & 0x80000000)
489 a->remain[0] = a->remain[1] = 0;
490 }
491
492 if (a->function == UAL_SET) {
493 a->function = a0->function;
494 a0->function = UAL_ACTIVE;
495 a0->repeat = a->repeat;
496 if (a0->repeat) {
497 a0->interval[0] = a->interval[0];
498 a0->interval[1] = a->interval[1];
499 }
500 a0->delay[0] = a->delay[0];
501 a0->delay[1] = a->delay[1];
502
503 iss = lib$subx(now, a0->delay, a0->remain);
504 if (VMSERR(iss)) lib$signal(iss);
505
506 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
507 if (VMSERR(iss)) lib$signal(iss);
508 } else {
509 a->function = a0->function;
510 a0->function = UAL_NULL;
511 }
512 iss = sys$setef(alarm_ef);
513 if (VMSERR(iss)) lib$signal(iss);
514 } else if (a->function == UAL_ACTIVE) {
515 if (a->repeat) {
516 iss = lib$subx(now, a->interval, a->remain);
517 if (VMSERR(iss)) lib$signal(iss);
518
519 iss = sys$setimr(0,a->interval,ualarm_AST,a);
520 if (VMSERR(iss)) lib$signal(iss);
521 } else {
522 a->function = UAL_NULL;
523 }
524 iss = sys$wake(0,0);
525 if (VMSERR(iss)) lib$signal(iss);
526 lib$signal(SS$_ASTFLT);
527 } else {
528 lib$signal(SS$_BADPARAM);
529 }
530}
531
532#endif /* !HAS_UALARM && VMS */
533
534
535
dcf686c9 536#ifdef HAS_GETTIMEOFDAY
537
a2e20b18 538static int
dcf686c9 539myU2time(UV *ret)
540{
541 struct timeval Tp;
542 int status;
6e3b076d 543 status = gettimeofday (&Tp, NULL);
dcf686c9 544 ret[0] = Tp.tv_sec;
545 ret[1] = Tp.tv_usec;
a2e20b18 546 return status;
dcf686c9 547}
548
3c72ec00 549static NV
dcf686c9 550myNVtime()
551{
552 struct timeval Tp;
553 int status;
6e3b076d 554 status = gettimeofday (&Tp, NULL);
a2e20b18 555 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
dcf686c9 556}
557
558#endif
559
560MODULE = Time::HiRes PACKAGE = Time::HiRes
561
562PROTOTYPES: ENABLE
563
564BOOT:
dcf686c9 565#ifdef HAS_GETTIMEOFDAY
a2e20b18 566{
567 UV auv[2];
cfb1f18c 568 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
a2e20b18 569 if (myU2time(auv) == 0)
570 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
571}
dcf686c9 572#endif
dcf686c9 573
3c72ec00 574IV
575constant(name, arg)
576 char * name
577 int arg
578
52d72fba 579#if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
dcf686c9 580
92bc48ca 581NV
dcf686c9 582usleep(useconds)
92bc48ca 583 NV useconds
52d72fba 584 PREINIT:
585 struct timeval Ta, Tb;
586 CODE:
6e3b076d 587 gettimeofday(&Ta, NULL);
52d72fba 588 if (items > 0) {
92bc48ca 589 if (useconds > 1E6) {
590 IV seconds = (IV) (useconds / 1E6);
f7916ddb 591 /* If usleep() has been implemented using setitimer()
592 * then this contortion is unnecessary-- but usleep()
593 * may be implemented in some other way, so let's contort. */
594 if (seconds) {
595 sleep(seconds);
596 useconds -= 1E6 * seconds;
597 }
598 } else if (useconds < 0.0)
599 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
92bc48ca 600 usleep((UV)useconds);
52d72fba 601 } else
602 PerlProc_pause();
6e3b076d 603 gettimeofday(&Tb, NULL);
92bc48ca 604#if 0
605 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
606#endif
607 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
dcf686c9 608
52d72fba 609 OUTPUT:
610 RETVAL
611
612NV
f9d00e57 613sleep(...)
52d72fba 614 PREINIT:
615 struct timeval Ta, Tb;
dcf686c9 616 CODE:
6e3b076d 617 gettimeofday(&Ta, NULL);
92bc48ca 618 if (items > 0) {
619 NV seconds = SvNV(ST(0));
f7916ddb 620 if (seconds >= 0.0) {
7c436af3 621 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
4880edd6 622 if (seconds >= 1.0)
623 sleep((UV)seconds);
f7916ddb 624 usleep(useconds);
625 } else
626 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
92bc48ca 627 } else
f9d00e57 628 PerlProc_pause();
6e3b076d 629 gettimeofday(&Tb, NULL);
92bc48ca 630#if 0
631 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
632#endif
52d72fba 633 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
634
635 OUTPUT:
636 RETVAL
dcf686c9 637
638#endif
639
640#ifdef HAS_UALARM
641
3de7a4ec 642int
dcf686c9 643ualarm(useconds,interval=0)
644 int useconds
645 int interval
f7916ddb 646 CODE:
647 if (useconds < 0 || interval < 0)
3de7a4ec 648 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
f7916ddb 649 RETVAL = ualarm(useconds, interval);
dcf686c9 650
f7916ddb 651 OUTPUT:
652 RETVAL
653
654NV
655alarm(seconds,interval=0)
656 NV seconds
657 NV interval
dcf686c9 658 CODE:
f7916ddb 659 if (seconds < 0.0 || interval < 0.0)
660 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
661 RETVAL = (NV)ualarm(seconds * 1000000,
662 interval * 1000000) / 1E6;
dcf686c9 663
c6c619a9 664 OUTPUT:
665 RETVAL
666
dcf686c9 667#endif
668
669#ifdef HAS_GETTIMEOFDAY
db835671 670# ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
671void
672gettimeofday()
673 PREINIT:
674 struct timeval Tp;
675 struct timezone Tz;
676 PPCODE:
677 int status;
6e3b076d 678 status = gettimeofday (&Tp, &Tz);
db835671 679 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
680
681 if (GIMME == G_ARRAY) {
682 EXTEND(sp, 2);
683 /* Mac OS (Classic) has unsigned time_t */
684 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
685 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
686 } else {
687 EXTEND(sp, 1);
688 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
689 }
690
691NV
692time()
693 PREINIT:
694 struct timeval Tp;
695 struct timezone Tz;
696 CODE:
697 int status;
6e3b076d 698 status = gettimeofday (&Tp, &Tz);
db835671 699 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
700 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
701 OUTPUT:
702 RETVAL
dcf686c9 703
db835671 704# else /* MACOS_TRADITIONAL */
dcf686c9 705void
706gettimeofday()
707 PREINIT:
708 struct timeval Tp;
709 PPCODE:
710 int status;
6e3b076d 711 status = gettimeofday (&Tp, NULL);
dcf686c9 712 if (GIMME == G_ARRAY) {
713 EXTEND(sp, 2);
714 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
715 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
716 } else {
717 EXTEND(sp, 1);
718 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
719 }
720
3c72ec00 721NV
dcf686c9 722time()
723 PREINIT:
724 struct timeval Tp;
725 CODE:
726 int status;
6e3b076d 727 status = gettimeofday (&Tp, NULL);
dcf686c9 728 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
729 OUTPUT:
730 RETVAL
731
db835671 732# endif /* MACOS_TRADITIONAL */
dcf686c9 733#endif
734
3c72ec00 735#if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
736
737#define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
738
739void
740setitimer(which, seconds, interval = 0)
741 int which
742 NV seconds
743 NV interval
744 PREINIT:
745 struct itimerval newit;
746 struct itimerval oldit;
747 PPCODE:
f7916ddb 748 if (seconds < 0.0 || interval < 0.0)
749 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", which, seconds, interval);
3c72ec00 750 newit.it_value.tv_sec = seconds;
751 newit.it_value.tv_usec =
752 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
753 newit.it_interval.tv_sec = interval;
754 newit.it_interval.tv_usec =
755 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
756 if (setitimer(which, &newit, &oldit) == 0) {
757 EXTEND(sp, 1);
758 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
759 if (GIMME == G_ARRAY) {
760 EXTEND(sp, 1);
761 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
762 }
763 }
764
765void
766getitimer(which)
767 int which
768 PREINIT:
769 struct itimerval nowit;
770 PPCODE:
771 if (getitimer(which, &nowit) == 0) {
772 EXTEND(sp, 1);
773 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
774 if (GIMME == G_ARRAY) {
775 EXTEND(sp, 1);
776 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
777 }
778 }
779
780#endif
781
dcf686c9 782# $Id: HiRes.xs,v 1.11 1999/03/16 02:27:38 wegscd Exp wegscd $
783
784# $Log: HiRes.xs,v $
785# Revision 1.11 1999/03/16 02:27:38 wegscd
786# Add U2time, NVtime. Fix symbols for static link.
787#
788# Revision 1.10 1998/09/30 02:36:25 wegscd
789# Add VMS changes.
790#
791# Revision 1.9 1998/07/07 02:42:06 wegscd
792# Win32 usleep()
793#
794# Revision 1.8 1998/07/02 01:47:26 wegscd
795# Add Win32 code for gettimeofday.
796#
797# Revision 1.7 1997/11/13 02:08:12 wegscd
798# Add missing EXTEND in gettimeofday() scalar code.
799#
800# Revision 1.6 1997/11/11 02:32:35 wegscd
801# Do something useful when calling gettimeofday() in a scalar context.
802# The patch is courtesy of Gisle Aas.
803#
804# Revision 1.5 1997/11/06 03:10:47 wegscd
805# Fake ualarm() if we have setitimer.
806#
807# Revision 1.4 1997/11/05 05:41:23 wegscd
808# Turn prototypes ON (suggested by Gisle Aas)
809#
810# Revision 1.3 1997/10/13 20:56:15 wegscd
811# Add PROTOTYPES: DISABLE
812#
813# Revision 1.2 1997/05/23 01:01:38 wegscd
814# Conditional compilation, depending on what the OS gives us.
815#
816# Revision 1.1 1996/09/03 18:26:35 wegscd
817# Initial revision
818#
819#