14 # include <sys/select.h>
27 # if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE)
28 # define NVTYPE long double
30 # define NVTYPE double
37 # if IVSIZE == LONGSIZE
41 # if IVSIZE == INTSIZE
52 # if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE) && \
53 defined(PERL_PRIgldbl) /* Not very likely, but let's try anyway. */
54 # define NVgf PERL_PRIgldbl
62 #if (IVSIZE == PTRSIZE) && (UVSIZE == PTRSIZE)
64 # define INT2PTR(any,d) (any)(d)
66 # if PTRSIZE == LONGSIZE
67 # define PTRV unsigned long
69 # define PTRV unsigned
71 # define INT2PTR(any,d) (any)(PTRV)(d)
73 #define PTR2IV(p) INT2PTR(IV,p)
79 sv_2pv_nolen(pTHX_ register SV *sv)
82 return sv_2pv(sv, &n_a);
84 # define SvPV_nolen(sv) \
85 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
86 ? SvPVX(sv) : sv_2pv_nolen(sv))
89 #ifndef PerlProc_pause
90 # define PerlProc_pause() Pause()
93 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
94 * is not supported in Cygwin as of August 2002, ditto for Win32.
95 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
97 #if defined(__CYGWIN__) || defined(WIN32)
98 # undef ITIMER_VIRTUAL
100 # undef ITIMER_REALPROF
103 /* 5.004 doesn't define PL_sv_undef */
104 #ifndef ATLEASTFIVEOHOHFIVE
106 #define PL_sv_undef sv_undef
110 #include "const-c.inc"
112 #if !defined(HAS_GETTIMEOFDAY) && defined(WIN32)
113 #define HAS_GETTIMEOFDAY
115 /* shows up in winsock.h?
123 unsigned __int64 ft_i64;
127 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
129 #define Const64(x) x##LL
131 #define Const64(x) x##i64
133 #define EPOCH_BIAS Const64(116444736000000000)
135 /* NOTE: This does not compute the timezone info (doing so can be expensive,
136 * and appears to be unsupported even by glibc) */
138 gettimeofday (struct timeval *tp, void *not_used)
142 /* this returns time in 100-nanosecond units (i.e. tens of usecs) */
143 GetSystemTimeAsFileTime(&ft.ft_val);
145 /* seconds since epoch */
146 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
148 /* microseconds remaining */
149 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
155 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
156 #define HAS_GETTIMEOFDAY
159 #include <time.h> /* gettimeofday */
160 #include <stdlib.h> /* qdiv */
161 #include <starlet.h> /* sys$gettim */
164 #include <lib$routines.h> /* lib$ediv() */
168 VMS binary time is expressed in 100 nano-seconds since
169 system base time which is 17-NOV-1858 00:00:00.00
172 #define DIV_100NS_TO_SECS 10000000L
173 #define DIV_100NS_TO_USECS 10L
176 gettimeofday is supposed to return times since the epoch
177 so need to determine this in terms of VMS base time
179 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
182 static long base_adjust[2]={0L,0L};
184 static __int64 base_adjust=0;
189 If we don't have gettimeofday, then likely we are on a VMS machine that
190 operates on local time rather than UTC...so we have to zone-adjust.
191 This code gleefully swiped from VMS.C
194 /* method used to handle UTC conversions:
195 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
197 static int gmtime_emulation_type;
198 /* number of secs to add to UTC POSIX-style time to get local time */
199 static long int utc_offset_secs;
200 static struct dsc$descriptor_s fildevdsc =
201 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
202 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
204 static time_t toutc_dst(time_t loc) {
207 if ((rsltmp = localtime(&loc)) == NULL) return -1;
208 loc -= utc_offset_secs;
209 if (rsltmp->tm_isdst) loc -= 3600;
213 static time_t toloc_dst(time_t utc) {
216 utc += utc_offset_secs;
217 if ((rsltmp = localtime(&utc)) == NULL) return -1;
218 if (rsltmp->tm_isdst) utc += 3600;
222 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
223 ((gmtime_emulation_type || timezone_setup()), \
224 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
225 ((secs) - utc_offset_secs))))
227 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
228 ((gmtime_emulation_type || timezone_setup()), \
229 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
230 ((secs) + utc_offset_secs))))
237 if (gmtime_emulation_type == 0) {
239 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
240 /* results of calls to gmtime() and localtime() */
243 gmtime_emulation_type++;
244 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
245 char off[LNM$C_NAMLENGTH+1];;
247 gmtime_emulation_type++;
248 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
249 gmtime_emulation_type++;
251 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
253 else { utc_offset_secs = atol(off); }
255 else { /* We've got a working gmtime() */
256 struct tm gmt, local;
259 tm_p = localtime(&base);
261 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
262 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
263 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
264 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
272 gettimeofday (struct timeval *tp, void *tpz)
278 long div_100ns_to_secs;
279 long div_100ns_to_usecs;
287 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
288 The return from function is also set to -1.
289 This is not exactly as per the manual page.
295 if (base_adjust[0]==0 && base_adjust[1]==0) {
297 if (base_adjust==0) { /* Need to determine epoch adjustment */
299 ret=sys$bintim(&dscepoch,&base_adjust);
300 if (1 != (ret &&1)) {
306 ret=sys$gettim(&quad); /* Get VMS system time */
307 if ((1 && ret) == 1) {
309 quad[0] -= base_adjust[0]; /* convert to epoch offset */
310 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
311 div_100ns_to_secs = DIV_100NS_TO_SECS;
312 div_100ns_to_usecs = DIV_100NS_TO_USECS;
313 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
316 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
317 tp->tv_sec = quo; /* Whole seconds */
318 tp->tv_usec = quo1; /* Micro-seconds */
320 quad -= base_adjust; /* convert to epoch offset */
321 ans1=qdiv(quad,DIV_100NS_TO_SECS);
322 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
323 tp->tv_sec = ans1.quot; /* Whole seconds */
324 tp->tv_usec = ans2.quot; /* Micro-seconds */
332 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
334 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
342 #if !defined(HAS_USLEEP) && defined(HAS_NANOSLEEP)
344 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
347 hrt_nanosleep(unsigned long usec)
350 res.tv_sec = usec/1000/1000;
351 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
352 nanosleep(&res, NULL);
357 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
358 #ifndef SELECT_IS_BROKEN
360 #define usleep hrt_usleep /* could conflict with ncurses for static build */
363 hrt_usleep(unsigned long usec)
368 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
369 (Select_fd_set_t)NULL, &tv);
374 #if !defined(HAS_USLEEP) && defined(WIN32)
376 #define usleep hrt_usleep /* could conflict with ncurses for static build */
379 hrt_usleep(unsigned long usec)
388 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
390 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
393 hrt_ualarm(int usec, int interval)
395 struct itimerval itv;
396 itv.it_value.tv_sec = usec / 1000000;
397 itv.it_value.tv_usec = usec % 1000000;
398 itv.it_interval.tv_sec = interval / 1000000;
399 itv.it_interval.tv_usec = interval % 1000000;
400 return setitimer(ITIMER_REAL, &itv, 0);
404 #if !defined(HAS_UALARM) && defined(VMS)
406 #define ualarm vms_ualarm
408 #include <lib$routines.h>
416 #define VMSERR(s) (!((s)&1))
419 us_to_VMS(useconds_t mseconds, unsigned long v[])
428 iss = lib$addx(qq,qq,qq);
429 if (VMSERR(iss)) lib$signal(iss);
430 iss = lib$subx(v,qq,v);
431 if (VMSERR(iss)) lib$signal(iss);
432 iss = lib$addx(qq,qq,qq);
433 if (VMSERR(iss)) lib$signal(iss);
434 iss = lib$subx(v,qq,v);
435 if (VMSERR(iss)) lib$signal(iss);
436 iss = lib$subx(v,qq,v);
437 if (VMSERR(iss)) lib$signal(iss);
441 VMS_to_us(unsigned long v[])
444 unsigned long div=10,quot, rem;
446 iss = lib$ediv(&div,v,",&rem);
447 if (VMSERR(iss)) lib$signal(iss);
452 typedef unsigned short word;
453 typedef struct _ualarm {
456 unsigned long delay[2];
457 unsigned long interval[2];
458 unsigned long remain[2];
463 static Alarm *a0, alarm_base;
468 static void ualarm_AST(Alarm *a);
471 vms_ualarm(int mseconds, int interval)
480 static struct item_list3 itmlst[2];
481 static int first = 1;
487 itmlst[0].code = JPI$_ASTEN;
488 itmlst[0].length = sizeof(asten);
489 itmlst[0].retlenaddr = NULL;
491 itmlst[1].length = 0;
492 itmlst[1].bufaddr = NULL;
493 itmlst[1].retlenaddr = NULL;
495 iss = lib$get_ef(&alarm_ef);
496 if (VMSERR(iss)) lib$signal(iss);
499 a0->function = UAL_NULL;
501 itmlst[0].bufaddr = &asten;
503 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
504 if (VMSERR(iss)) lib$signal(iss);
505 if (!(asten&0x08)) return -1;
509 a->function = UAL_SET;
511 a->function = UAL_CLEAR;
514 us_to_VMS(mseconds, a->delay);
516 us_to_VMS(interval, a->interval);
521 iss = sys$clref(alarm_ef);
522 if (VMSERR(iss)) lib$signal(iss);
524 iss = sys$dclast(ualarm_AST,a,0);
525 if (VMSERR(iss)) lib$signal(iss);
527 iss = sys$waitfr(alarm_ef);
528 if (VMSERR(iss)) lib$signal(iss);
530 if (a->function == UAL_ACTIVE)
531 return VMS_to_us(a->remain);
542 unsigned long now[2];
544 iss = sys$gettim(now);
545 if (VMSERR(iss)) lib$signal(iss);
547 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
548 if (a0->function == UAL_ACTIVE) {
549 iss = sys$cantim(a0,PSL$C_USER);
550 if (VMSERR(iss)) lib$signal(iss);
552 iss = lib$subx(a0->remain, now, a->remain);
553 if (VMSERR(iss)) lib$signal(iss);
555 if (a->remain[1] & 0x80000000)
556 a->remain[0] = a->remain[1] = 0;
559 if (a->function == UAL_SET) {
560 a->function = a0->function;
561 a0->function = UAL_ACTIVE;
562 a0->repeat = a->repeat;
564 a0->interval[0] = a->interval[0];
565 a0->interval[1] = a->interval[1];
567 a0->delay[0] = a->delay[0];
568 a0->delay[1] = a->delay[1];
570 iss = lib$subx(now, a0->delay, a0->remain);
571 if (VMSERR(iss)) lib$signal(iss);
573 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
574 if (VMSERR(iss)) lib$signal(iss);
576 a->function = a0->function;
577 a0->function = UAL_NULL;
579 iss = sys$setef(alarm_ef);
580 if (VMSERR(iss)) lib$signal(iss);
581 } else if (a->function == UAL_ACTIVE) {
583 iss = lib$subx(now, a->interval, a->remain);
584 if (VMSERR(iss)) lib$signal(iss);
586 iss = sys$setimr(0,a->interval,ualarm_AST,a);
587 if (VMSERR(iss)) lib$signal(iss);
589 a->function = UAL_NULL;
592 if (VMSERR(iss)) lib$signal(iss);
593 lib$signal(SS$_ASTFLT);
595 lib$signal(SS$_BADPARAM);
599 #endif /* !HAS_UALARM && VMS */
601 #ifdef HAS_GETTIMEOFDAY
608 status = gettimeofday (&Tp, NULL);
619 status = gettimeofday (&Tp, NULL);
620 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
625 MODULE = Time::HiRes PACKAGE = Time::HiRes
630 #ifdef ATLEASTFIVEOHOHFIVE
631 #ifdef HAS_GETTIMEOFDAY
634 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
635 if (myU2time(auv) == 0)
636 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
641 INCLUDE: const-xs.inc
643 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
649 struct timeval Ta, Tb;
651 gettimeofday(&Ta, NULL);
653 if (useconds > 1E6) {
654 IV seconds = (IV) (useconds / 1E6);
655 /* If usleep() has been implemented using setitimer()
656 * then this contortion is unnecessary-- but usleep()
657 * may be implemented in some other way, so let's contort. */
660 useconds -= 1E6 * seconds;
662 } else if (useconds < 0.0)
663 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
664 usleep((U32)useconds);
667 gettimeofday(&Tb, NULL);
669 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
671 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
679 struct timeval Ta, Tb;
681 gettimeofday(&Ta, NULL);
683 NV seconds = SvNV(ST(0));
684 if (seconds >= 0.0) {
685 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
688 if ((IV)useconds < 0) {
689 #if defined(__sparc64__) && defined(__GNUC__)
690 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
691 * where (0.5 - (UV)(0.5)) will under certain
692 * circumstances (if the double is cast to UV more
693 * than once?) evaluate to -0.5, instead of 0.5. */
694 useconds = -(IV)useconds;
696 if ((IV)useconds < 0)
697 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
701 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
704 gettimeofday(&Tb, NULL);
706 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
708 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
718 ualarm(useconds,interval=0)
722 if (useconds < 0 || interval < 0)
723 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
724 RETVAL = ualarm(useconds, interval);
730 alarm(seconds,interval=0)
734 if (seconds < 0.0 || interval < 0.0)
735 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
736 RETVAL = (NV)ualarm(seconds * 1000000,
737 interval * 1000000) / 1E6;
744 #ifdef HAS_GETTIMEOFDAY
745 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
753 status = gettimeofday (&Tp, &Tz);
754 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
756 if (GIMME == G_ARRAY) {
758 /* Mac OS (Classic) has unsigned time_t */
759 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
760 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
763 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
773 status = gettimeofday (&Tp, &Tz);
774 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
775 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
779 # else /* MACOS_TRADITIONAL */
786 status = gettimeofday (&Tp, NULL);
787 if (GIMME == G_ARRAY) {
789 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
790 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
793 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
802 status = gettimeofday (&Tp, NULL);
803 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
807 # endif /* MACOS_TRADITIONAL */
810 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
812 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
815 setitimer(which, seconds, interval = 0)
820 struct itimerval newit;
821 struct itimerval oldit;
823 if (seconds < 0.0 || interval < 0.0)
824 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
825 newit.it_value.tv_sec = seconds;
826 newit.it_value.tv_usec =
827 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
828 newit.it_interval.tv_sec = interval;
829 newit.it_interval.tv_usec =
830 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
831 if (setitimer(which, &newit, &oldit) == 0) {
833 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
834 if (GIMME == G_ARRAY) {
836 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
844 struct itimerval nowit;
846 if (getitimer(which, &nowit) == 0) {
848 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
849 if (GIMME == G_ARRAY) {
851 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));