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
40 # if IVSIZE == INTSIZE
50 # if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE) && \
51 defined(PERL_PRIgldbl) /* Not very likely, but let's try anyway. */
52 # define NVgf PERL_PRIgldbl
60 #if (IVSIZE == PTRSIZE) && (UVSIZE == PTRSIZE)
62 # define INT2PTR(any,d) (any)(d)
64 # if PTRSIZE == LONGSIZE
65 # define PTRV unsigned long
67 # define PTRV unsigned
69 # define INT2PTR(any,d) (any)(PTRV)(d)
71 #define PTR2IV(p) INT2PTR(IV,p)
77 sv_2pv_nolen(pTHX_ register SV *sv)
80 return sv_2pv(sv, &n_a);
82 # define SvPV_nolen(sv) \
83 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
84 ? SvPVX(sv) : sv_2pv_nolen(sv))
87 #ifndef PerlProc_pause
88 # define PerlProc_pause() Pause()
91 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
92 * is not supported in Cygwin as of August 2002, ditto for Win32.
93 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
95 #if defined(__CYGWIN__) || defined(WIN32)
96 # undef ITIMER_VIRTUAL
98 # undef ITIMER_REALPROF
101 /* 5.004 doesn't define PL_sv_undef */
102 #ifndef ATLEASTFIVEOHOHFIVE
104 #define PL_sv_undef sv_undef
108 #include "const-c.inc"
110 #if !defined(HAS_GETTIMEOFDAY) && defined(WIN32)
111 #define HAS_GETTIMEOFDAY
113 /* shows up in winsock.h?
121 unsigned __int64 ft_i64;
125 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
127 #define Const64(x) x##LL
129 #define Const64(x) x##i64
131 #define EPOCH_BIAS Const64(116444736000000000)
133 /* NOTE: This does not compute the timezone info (doing so can be expensive,
134 * and appears to be unsupported even by glibc) */
136 gettimeofday (struct timeval *tp, void *not_used)
140 /* this returns time in 100-nanosecond units (i.e. tens of usecs) */
141 GetSystemTimeAsFileTime(&ft.ft_val);
143 /* seconds since epoch */
144 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
146 /* microseconds remaining */
147 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
153 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
154 #define HAS_GETTIMEOFDAY
157 #include <time.h> /* gettimeofday */
158 #include <stdlib.h> /* qdiv */
159 #include <starlet.h> /* sys$gettim */
162 #include <lib$routines.h> /* lib$ediv() */
166 VMS binary time is expressed in 100 nano-seconds since
167 system base time which is 17-NOV-1858 00:00:00.00
170 #define DIV_100NS_TO_SECS 10000000L
171 #define DIV_100NS_TO_USECS 10L
174 gettimeofday is supposed to return times since the epoch
175 so need to determine this in terms of VMS base time
177 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
180 static long base_adjust[2]={0L,0L};
182 static __int64 base_adjust=0;
187 If we don't have gettimeofday, then likely we are on a VMS machine that
188 operates on local time rather than UTC...so we have to zone-adjust.
189 This code gleefully swiped from VMS.C
192 /* method used to handle UTC conversions:
193 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
195 static int gmtime_emulation_type;
196 /* number of secs to add to UTC POSIX-style time to get local time */
197 static long int utc_offset_secs;
198 static struct dsc$descriptor_s fildevdsc =
199 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
200 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
202 static time_t toutc_dst(time_t loc) {
205 if ((rsltmp = localtime(&loc)) == NULL) return -1;
206 loc -= utc_offset_secs;
207 if (rsltmp->tm_isdst) loc -= 3600;
211 static time_t toloc_dst(time_t utc) {
214 utc += utc_offset_secs;
215 if ((rsltmp = localtime(&utc)) == NULL) return -1;
216 if (rsltmp->tm_isdst) utc += 3600;
220 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
221 ((gmtime_emulation_type || timezone_setup()), \
222 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
223 ((secs) - utc_offset_secs))))
225 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
226 ((gmtime_emulation_type || timezone_setup()), \
227 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
228 ((secs) + utc_offset_secs))))
235 if (gmtime_emulation_type == 0) {
237 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
238 /* results of calls to gmtime() and localtime() */
241 gmtime_emulation_type++;
242 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
243 char off[LNM$C_NAMLENGTH+1];;
245 gmtime_emulation_type++;
246 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
247 gmtime_emulation_type++;
249 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
251 else { utc_offset_secs = atol(off); }
253 else { /* We've got a working gmtime() */
254 struct tm gmt, local;
257 tm_p = localtime(&base);
259 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
260 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
261 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
262 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
270 gettimeofday (struct timeval *tp, void *tpz)
276 long div_100ns_to_secs;
277 long div_100ns_to_usecs;
285 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
286 The return from function is also set to -1.
287 This is not exactly as per the manual page.
293 if (base_adjust[0]==0 && base_adjust[1]==0) {
295 if (base_adjust==0) { /* Need to determine epoch adjustment */
297 ret=sys$bintim(&dscepoch,&base_adjust);
298 if (1 != (ret &&1)) {
304 ret=sys$gettim(&quad); /* Get VMS system time */
305 if ((1 && ret) == 1) {
307 quad[0] -= base_adjust[0]; /* convert to epoch offset */
308 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
309 div_100ns_to_secs = DIV_100NS_TO_SECS;
310 div_100ns_to_usecs = DIV_100NS_TO_USECS;
311 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
314 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
315 tp->tv_sec = quo; /* Whole seconds */
316 tp->tv_usec = quo1; /* Micro-seconds */
318 quad -= base_adjust; /* convert to epoch offset */
319 ans1=qdiv(quad,DIV_100NS_TO_SECS);
320 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
321 tp->tv_sec = ans1.quot; /* Whole seconds */
322 tp->tv_usec = ans2.quot; /* Micro-seconds */
330 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
332 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
340 #if !defined(HAS_USLEEP) && defined(HAS_NANOSLEEP)
342 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
345 hrt_nanosleep(unsigned long usec)
348 res.tv_sec = usec/1000/1000;
349 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
350 nanosleep(&res, NULL);
355 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
356 #ifndef SELECT_IS_BROKEN
358 #define usleep hrt_usleep /* could conflict with ncurses for static build */
361 hrt_usleep(unsigned long usec)
366 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
367 (Select_fd_set_t)NULL, &tv);
372 #if !defined(HAS_USLEEP) && defined(WIN32)
374 #define usleep hrt_usleep /* could conflict with ncurses for static build */
377 hrt_usleep(unsigned long usec)
386 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
388 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
391 hrt_ualarm(int usec, int interval)
393 struct itimerval itv;
394 itv.it_value.tv_sec = usec / 1000000;
395 itv.it_value.tv_usec = usec % 1000000;
396 itv.it_interval.tv_sec = interval / 1000000;
397 itv.it_interval.tv_usec = interval % 1000000;
398 return setitimer(ITIMER_REAL, &itv, 0);
402 #if !defined(HAS_UALARM) && defined(VMS)
404 #define ualarm vms_ualarm
406 #include <lib$routines.h>
414 #define VMSERR(s) (!((s)&1))
417 us_to_VMS(useconds_t mseconds, unsigned long v[])
426 iss = lib$addx(qq,qq,qq);
427 if (VMSERR(iss)) lib$signal(iss);
428 iss = lib$subx(v,qq,v);
429 if (VMSERR(iss)) lib$signal(iss);
430 iss = lib$addx(qq,qq,qq);
431 if (VMSERR(iss)) lib$signal(iss);
432 iss = lib$subx(v,qq,v);
433 if (VMSERR(iss)) lib$signal(iss);
434 iss = lib$subx(v,qq,v);
435 if (VMSERR(iss)) lib$signal(iss);
439 VMS_to_us(unsigned long v[])
442 unsigned long div=10,quot, rem;
444 iss = lib$ediv(&div,v,",&rem);
445 if (VMSERR(iss)) lib$signal(iss);
450 typedef unsigned short word;
451 typedef struct _ualarm {
454 unsigned long delay[2];
455 unsigned long interval[2];
456 unsigned long remain[2];
461 static Alarm *a0, alarm_base;
466 static void ualarm_AST(Alarm *a);
469 vms_ualarm(int mseconds, int interval)
478 static struct item_list3 itmlst[2];
479 static int first = 1;
485 itmlst[0].code = JPI$_ASTEN;
486 itmlst[0].length = sizeof(asten);
487 itmlst[0].retlenaddr = NULL;
489 itmlst[1].length = 0;
490 itmlst[1].bufaddr = NULL;
491 itmlst[1].retlenaddr = NULL;
493 iss = lib$get_ef(&alarm_ef);
494 if (VMSERR(iss)) lib$signal(iss);
497 a0->function = UAL_NULL;
499 itmlst[0].bufaddr = &asten;
501 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
502 if (VMSERR(iss)) lib$signal(iss);
503 if (!(asten&0x08)) return -1;
507 a->function = UAL_SET;
509 a->function = UAL_CLEAR;
512 us_to_VMS(mseconds, a->delay);
514 us_to_VMS(interval, a->interval);
519 iss = sys$clref(alarm_ef);
520 if (VMSERR(iss)) lib$signal(iss);
522 iss = sys$dclast(ualarm_AST,a,0);
523 if (VMSERR(iss)) lib$signal(iss);
525 iss = sys$waitfr(alarm_ef);
526 if (VMSERR(iss)) lib$signal(iss);
528 if (a->function == UAL_ACTIVE)
529 return VMS_to_us(a->remain);
540 unsigned long now[2];
542 iss = sys$gettim(now);
543 if (VMSERR(iss)) lib$signal(iss);
545 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
546 if (a0->function == UAL_ACTIVE) {
547 iss = sys$cantim(a0,PSL$C_USER);
548 if (VMSERR(iss)) lib$signal(iss);
550 iss = lib$subx(a0->remain, now, a->remain);
551 if (VMSERR(iss)) lib$signal(iss);
553 if (a->remain[1] & 0x80000000)
554 a->remain[0] = a->remain[1] = 0;
557 if (a->function == UAL_SET) {
558 a->function = a0->function;
559 a0->function = UAL_ACTIVE;
560 a0->repeat = a->repeat;
562 a0->interval[0] = a->interval[0];
563 a0->interval[1] = a->interval[1];
565 a0->delay[0] = a->delay[0];
566 a0->delay[1] = a->delay[1];
568 iss = lib$subx(now, a0->delay, a0->remain);
569 if (VMSERR(iss)) lib$signal(iss);
571 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
572 if (VMSERR(iss)) lib$signal(iss);
574 a->function = a0->function;
575 a0->function = UAL_NULL;
577 iss = sys$setef(alarm_ef);
578 if (VMSERR(iss)) lib$signal(iss);
579 } else if (a->function == UAL_ACTIVE) {
581 iss = lib$subx(now, a->interval, a->remain);
582 if (VMSERR(iss)) lib$signal(iss);
584 iss = sys$setimr(0,a->interval,ualarm_AST,a);
585 if (VMSERR(iss)) lib$signal(iss);
587 a->function = UAL_NULL;
590 if (VMSERR(iss)) lib$signal(iss);
591 lib$signal(SS$_ASTFLT);
593 lib$signal(SS$_BADPARAM);
597 #endif /* !HAS_UALARM && VMS */
599 #ifdef HAS_GETTIMEOFDAY
606 status = gettimeofday (&Tp, NULL);
617 status = gettimeofday (&Tp, NULL);
618 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
623 MODULE = Time::HiRes PACKAGE = Time::HiRes
628 #ifdef ATLEASTFIVEOHOHFIVE
629 #ifdef HAS_GETTIMEOFDAY
632 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
633 if (myU2time(auv) == 0)
634 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
639 INCLUDE: const-xs.inc
641 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
647 struct timeval Ta, Tb;
649 gettimeofday(&Ta, NULL);
651 if (useconds > 1E6) {
652 IV seconds = (IV) (useconds / 1E6);
653 /* If usleep() has been implemented using setitimer()
654 * then this contortion is unnecessary-- but usleep()
655 * may be implemented in some other way, so let's contort. */
658 useconds -= 1E6 * seconds;
660 } else if (useconds < 0.0)
661 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
662 usleep((U32)useconds);
665 gettimeofday(&Tb, NULL);
667 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
669 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
677 struct timeval Ta, Tb;
679 gettimeofday(&Ta, NULL);
681 NV seconds = SvNV(ST(0));
682 if (seconds >= 0.0) {
683 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
688 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
691 gettimeofday(&Tb, NULL);
693 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
695 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
705 ualarm(useconds,interval=0)
709 if (useconds < 0 || interval < 0)
710 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
711 RETVAL = ualarm(useconds, interval);
717 alarm(seconds,interval=0)
721 if (seconds < 0.0 || interval < 0.0)
722 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
723 RETVAL = (NV)ualarm(seconds * 1000000,
724 interval * 1000000) / 1E6;
731 #ifdef HAS_GETTIMEOFDAY
732 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
740 status = gettimeofday (&Tp, &Tz);
741 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
743 if (GIMME == G_ARRAY) {
745 /* Mac OS (Classic) has unsigned time_t */
746 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
747 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
750 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
760 status = gettimeofday (&Tp, &Tz);
761 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
762 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
766 # else /* MACOS_TRADITIONAL */
773 status = gettimeofday (&Tp, NULL);
774 if (GIMME == G_ARRAY) {
776 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
777 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
780 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
789 status = gettimeofday (&Tp, NULL);
790 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
794 # endif /* MACOS_TRADITIONAL */
797 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
799 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
802 setitimer(which, seconds, interval = 0)
807 struct itimerval newit;
808 struct itimerval oldit;
810 if (seconds < 0.0 || interval < 0.0)
811 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
812 newit.it_value.tv_sec = seconds;
813 newit.it_value.tv_usec =
814 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
815 newit.it_interval.tv_sec = interval;
816 newit.it_interval.tv_usec =
817 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
818 if (setitimer(which, &newit, &oldit) == 0) {
820 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
821 if (GIMME == G_ARRAY) {
823 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
831 struct itimerval nowit;
833 if (getitimer(which, &nowit) == 0) {
835 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
836 if (GIMME == G_ARRAY) {
838 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));