14 # include <sys/select.h>
22 constant(char *name, int arg)
27 if (strEQ(name, "ITIMER_REAL"))
33 if (strEQ(name, "ITIMER_REALPROF"))
34 #ifdef ITIMER_REALPROF
35 return ITIMER_REALPROF;
39 if (strEQ(name, "ITIMER_VIRTUAL"))
41 return ITIMER_VIRTUAL;
45 if (strEQ(name, "ITIMER_PROF"))
61 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
62 #define HAS_GETTIMEOFDAY
65 #include <time.h> /* gettimeofday */
66 #include <stdlib.h> /* qdiv */
67 #include <starlet.h> /* sys$gettim */
70 #include <lib$routines.h> /* lib$ediv() */
74 VMS binary time is expressed in 100 nano-seconds since
75 system base time which is 17-NOV-1858 00:00:00.00
78 #define DIV_100NS_TO_SECS 10000000L
79 #define DIV_100NS_TO_USECS 10L
82 gettimeofday is supposed to return times since the epoch
83 so need to determine this in terms of VMS base time
85 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
88 static long base_adjust[2]={0L,0L};
90 static __int64 base_adjust=0;
95 If we don't have gettimeofday, then likely we are on a VMS machine that
96 operates on local time rather than UTC...so we have to zone-adjust.
97 This code gleefully swiped from VMS.C
100 /* method used to handle UTC conversions:
101 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
103 static int gmtime_emulation_type;
104 /* number of secs to add to UTC POSIX-style time to get local time */
105 static long int utc_offset_secs;
106 static struct dsc$descriptor_s fildevdsc =
107 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
108 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
110 static time_t toutc_dst(time_t loc) {
113 if ((rsltmp = localtime(&loc)) == NULL) return -1;
114 loc -= utc_offset_secs;
115 if (rsltmp->tm_isdst) loc -= 3600;
119 static time_t toloc_dst(time_t utc) {
122 utc += utc_offset_secs;
123 if ((rsltmp = localtime(&utc)) == NULL) return -1;
124 if (rsltmp->tm_isdst) utc += 3600;
128 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
129 ((gmtime_emulation_type || timezone_setup()), \
130 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
131 ((secs) - utc_offset_secs))))
133 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
134 ((gmtime_emulation_type || timezone_setup()), \
135 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
136 ((secs) + utc_offset_secs))))
143 if (gmtime_emulation_type == 0) {
145 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
146 /* results of calls to gmtime() and localtime() */
149 gmtime_emulation_type++;
150 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
151 char off[LNM$C_NAMLENGTH+1];;
153 gmtime_emulation_type++;
154 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
155 gmtime_emulation_type++;
157 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
159 else { utc_offset_secs = atol(off); }
161 else { /* We've got a working gmtime() */
162 struct tm gmt, local;
165 tm_p = localtime(&base);
167 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
168 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
169 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
170 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
178 gettimeofday (struct timeval *tp, void *tpz)
184 long div_100ns_to_secs;
185 long div_100ns_to_usecs;
193 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
194 The return from function is also set to -1.
195 This is not exactly as per the manual page.
201 if (base_adjust[0]==0 && base_adjust[1]==0) {
203 if (base_adjust==0) { /* Need to determine epoch adjustment */
205 ret=sys$bintim(&dscepoch,&base_adjust);
206 if (1 != (ret &&1)) {
212 ret=sys$gettim(&quad); /* Get VMS system time */
213 if ((1 && ret) == 1) {
215 quad[0] -= base_adjust[0]; /* convert to epoch offset */
216 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
217 div_100ns_to_secs = DIV_100NS_TO_SECS;
218 div_100ns_to_usecs = DIV_100NS_TO_USECS;
219 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
222 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
223 tp->tv_sec = quo; /* Whole seconds */
224 tp->tv_usec = quo1; /* Micro-seconds */
226 quad -= base_adjust; /* convert to epoch offset */
227 ans1=qdiv(quad,DIV_100NS_TO_SECS);
228 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
229 tp->tv_sec = ans1.quot; /* Whole seconds */
230 tp->tv_usec = ans2.quot; /* Micro-seconds */
238 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
240 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
247 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
248 #ifndef SELECT_IS_BROKEN
250 #define usleep hrt_usleep /* could conflict with ncurses for static build */
253 hrt_usleep(unsigned long usec)
258 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
259 (Select_fd_set_t)NULL, &tv);
264 #if !defined(HAS_USLEEP) && defined(WIN32)
266 #define usleep hrt_usleep /* could conflict with ncurses for static build */
269 hrt_usleep(unsigned long usec)
278 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
280 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
283 hrt_ualarm(int usec, int interval)
285 struct itimerval itv;
286 itv.it_value.tv_sec = usec / 1000000;
287 itv.it_value.tv_usec = usec % 1000000;
288 itv.it_interval.tv_sec = interval / 1000000;
289 itv.it_interval.tv_usec = interval % 1000000;
290 return setitimer(ITIMER_REAL, &itv, 0);
294 #if !defined(HAS_UALARM) && defined(VMS)
296 #define ualarm vms_ualarm
298 #include <lib$routines.h>
306 #define VMSERR(s) (!((s)&1))
309 us_to_VMS(useconds_t mseconds, unsigned long v[])
318 iss = lib$addx(qq,qq,qq);
319 if (VMSERR(iss)) lib$signal(iss);
320 iss = lib$subx(v,qq,v);
321 if (VMSERR(iss)) lib$signal(iss);
322 iss = lib$addx(qq,qq,qq);
323 if (VMSERR(iss)) lib$signal(iss);
324 iss = lib$subx(v,qq,v);
325 if (VMSERR(iss)) lib$signal(iss);
326 iss = lib$subx(v,qq,v);
327 if (VMSERR(iss)) lib$signal(iss);
331 VMS_to_us(unsigned long v[])
334 unsigned long div=10,quot, rem;
336 iss = lib$ediv(&div,v,",&rem);
337 if (VMSERR(iss)) lib$signal(iss);
342 typedef unsigned short word;
343 typedef struct _ualarm {
346 unsigned long delay[2];
347 unsigned long interval[2];
348 unsigned long remain[2];
353 static Alarm *a0, alarm_base;
358 static void ualarm_AST(Alarm *a);
361 vms_ualarm(int mseconds, int interval)
370 static struct item_list3 itmlst[2];
371 static int first = 1;
377 itmlst[0].code = JPI$_ASTEN;
378 itmlst[0].length = sizeof(asten);
379 itmlst[0].retlenaddr = NULL;
381 itmlst[1].length = 0;
382 itmlst[1].bufaddr = NULL;
383 itmlst[1].retlenaddr = NULL;
385 iss = lib$get_ef(&alarm_ef);
386 if (VMSERR(iss)) lib$signal(iss);
389 a0->function = UAL_NULL;
391 itmlst[0].bufaddr = &asten;
393 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
394 if (VMSERR(iss)) lib$signal(iss);
395 if (!(asten&0x08)) return -1;
399 a->function = UAL_SET;
401 a->function = UAL_CLEAR;
404 us_to_VMS(mseconds, a->delay);
406 us_to_VMS(interval, a->interval);
411 iss = sys$clref(alarm_ef);
412 if (VMSERR(iss)) lib$signal(iss);
414 iss = sys$dclast(ualarm_AST,a,0);
415 if (VMSERR(iss)) lib$signal(iss);
417 iss = sys$waitfr(alarm_ef);
418 if (VMSERR(iss)) lib$signal(iss);
420 if (a->function == UAL_ACTIVE)
421 return VMS_to_us(a->remain);
432 unsigned long now[2];
434 iss = sys$gettim(now);
435 if (VMSERR(iss)) lib$signal(iss);
437 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
438 if (a0->function == UAL_ACTIVE) {
439 iss = sys$cantim(a0,PSL$C_USER);
440 if (VMSERR(iss)) lib$signal(iss);
442 iss = lib$subx(a0->remain, now, a->remain);
443 if (VMSERR(iss)) lib$signal(iss);
445 if (a->remain[1] & 0x80000000)
446 a->remain[0] = a->remain[1] = 0;
449 if (a->function == UAL_SET) {
450 a->function = a0->function;
451 a0->function = UAL_ACTIVE;
452 a0->repeat = a->repeat;
454 a0->interval[0] = a->interval[0];
455 a0->interval[1] = a->interval[1];
457 a0->delay[0] = a->delay[0];
458 a0->delay[1] = a->delay[1];
460 iss = lib$subx(now, a0->delay, a0->remain);
461 if (VMSERR(iss)) lib$signal(iss);
463 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
464 if (VMSERR(iss)) lib$signal(iss);
466 a->function = a0->function;
467 a0->function = UAL_NULL;
469 iss = sys$setef(alarm_ef);
470 if (VMSERR(iss)) lib$signal(iss);
471 } else if (a->function == UAL_ACTIVE) {
473 iss = lib$subx(now, a->interval, a->remain);
474 if (VMSERR(iss)) lib$signal(iss);
476 iss = sys$setimr(0,a->interval,ualarm_AST,a);
477 if (VMSERR(iss)) lib$signal(iss);
479 a->function = UAL_NULL;
482 if (VMSERR(iss)) lib$signal(iss);
483 lib$signal(SS$_ASTFLT);
485 lib$signal(SS$_BADPARAM);
489 #endif /* !HAS_UALARM && VMS */
493 #ifdef HAS_GETTIMEOFDAY
500 status = PerlProc_gettimeofday(&Tp,NULL);
511 status = PerlProc_gettimeofday(&Tp,NULL);
512 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
517 MODULE = Time::HiRes PACKAGE = Time::HiRes
522 #ifdef HAS_GETTIMEOFDAY
525 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
526 if (myU2time(auv) == 0)
527 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
536 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
542 struct timeval Ta, Tb;
544 PerlProc_gettimeofday(&Ta,NULL);
546 if (useconds > 1E6) {
547 IV seconds = (IV) (useconds / 1E6);
548 /* If usleep() has been implemented using setitimer()
549 * then this contortion is unnecessary-- but usleep()
550 * may be implemented in some other way, so let's contort. */
553 useconds -= 1E6 * seconds;
555 } else if (useconds < 0.0)
556 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
557 usleep((UV)useconds);
560 PerlProc_gettimeofday(&Tb,NULL);
562 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
564 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
572 struct timeval Ta, Tb;
574 PerlProc_gettimeofday(&Ta,NULL);
576 NV seconds = SvNV(ST(0));
577 if (seconds >= 0.0) {
578 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
583 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
586 PerlProc_gettimeofday(&Tb,NULL);
588 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
590 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
600 ualarm(useconds,interval=0)
604 if (useconds < 0 || interval < 0)
605 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
606 RETVAL = ualarm(useconds, interval);
612 alarm(seconds,interval=0)
616 if (seconds < 0.0 || interval < 0.0)
617 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
618 RETVAL = (NV)ualarm(seconds * 1000000,
619 interval * 1000000) / 1E6;
626 #ifdef HAS_GETTIMEOFDAY
627 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
635 status = PerlProc_gettimeofday(&Tp,&Tz);
636 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
638 if (GIMME == G_ARRAY) {
640 /* Mac OS (Classic) has unsigned time_t */
641 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
642 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
645 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
655 status = PerlProc_gettimeofday(&Tp,&Tz);
656 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
657 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
661 # else /* MACOS_TRADITIONAL */
668 status = PerlProc_gettimeofday(&Tp,NULL);
669 if (GIMME == G_ARRAY) {
671 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
672 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
675 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
684 status = PerlProc_gettimeofday(&Tp,NULL);
685 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
689 # endif /* MACOS_TRADITIONAL */
692 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
694 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
697 setitimer(which, seconds, interval = 0)
702 struct itimerval newit;
703 struct itimerval oldit;
705 if (seconds < 0.0 || interval < 0.0)
706 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", which, seconds, interval);
707 newit.it_value.tv_sec = seconds;
708 newit.it_value.tv_usec =
709 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
710 newit.it_interval.tv_sec = interval;
711 newit.it_interval.tv_usec =
712 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
713 if (setitimer(which, &newit, &oldit) == 0) {
715 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
716 if (GIMME == G_ARRAY) {
718 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
726 struct itimerval nowit;
728 if (getitimer(which, &nowit) == 0) {
730 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
731 if (GIMME == G_ARRAY) {
733 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
739 # $Id: HiRes.xs,v 1.11 1999/03/16 02:27:38 wegscd Exp wegscd $
742 # Revision 1.11 1999/03/16 02:27:38 wegscd
743 # Add U2time, NVtime. Fix symbols for static link.
745 # Revision 1.10 1998/09/30 02:36:25 wegscd
748 # Revision 1.9 1998/07/07 02:42:06 wegscd
751 # Revision 1.8 1998/07/02 01:47:26 wegscd
752 # Add Win32 code for gettimeofday.
754 # Revision 1.7 1997/11/13 02:08:12 wegscd
755 # Add missing EXTEND in gettimeofday() scalar code.
757 # Revision 1.6 1997/11/11 02:32:35 wegscd
758 # Do something useful when calling gettimeofday() in a scalar context.
759 # The patch is courtesy of Gisle Aas.
761 # Revision 1.5 1997/11/06 03:10:47 wegscd
762 # Fake ualarm() if we have setitimer.
764 # Revision 1.4 1997/11/05 05:41:23 wegscd
765 # Turn prototypes ON (suggested by Gisle Aas)
767 # Revision 1.3 1997/10/13 20:56:15 wegscd
768 # Add PROTOTYPES: DISABLE
770 # Revision 1.2 1997/05/23 01:01:38 wegscd
771 # Conditional compilation, depending on what the OS gives us.
773 # Revision 1.1 1996/09/03 18:26:35 wegscd