17 constant(char *name, int arg)
22 if (strEQ(name, "ITIMER_REAL"))
28 if (strEQ(name, "ITIMER_REALPROF"))
29 #ifdef ITIMER_REALPROF
30 return ITIMER_REALPROF;
34 if (strEQ(name, "ITIMER_VIRTUAL"))
36 return ITIMER_VIRTUAL;
40 if (strEQ(name, "ITIMER_PROF"))
56 #if !defined(HAS_GETTIMEOFDAY) && defined(WIN32)
57 #define HAS_GETTIMEOFDAY
59 /* shows up in winsock.h?
67 unsigned __int64 ft_i64;
71 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
73 #define Const64(x) x##LL
75 #define Const64(x) x##i64
77 #define EPOCH_BIAS Const64(116444736000000000)
79 /* NOTE: This does not compute the timezone info (doing so can be expensive,
80 * and appears to be unsupported even by glibc) */
82 gettimeofday (struct timeval *tp, void *not_used)
86 /* this returns time in 100-nanosecond units (i.e. tens of usecs) */
87 GetSystemTimeAsFileTime(&ft.ft_val);
89 /* seconds since epoch */
90 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
92 /* microseconds remaining */
93 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
99 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
100 #define HAS_GETTIMEOFDAY
103 #include <time.h> /* gettimeofday */
104 #include <stdlib.h> /* qdiv */
105 #include <starlet.h> /* sys$gettim */
108 #include <lib$routines.h> /* lib$ediv() */
112 VMS binary time is expressed in 100 nano-seconds since
113 system base time which is 17-NOV-1858 00:00:00.00
116 #define DIV_100NS_TO_SECS 10000000L
117 #define DIV_100NS_TO_USECS 10L
120 gettimeofday is supposed to return times since the epoch
121 so need to determine this in terms of VMS base time
123 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
126 static long base_adjust[2]={0L,0L};
128 static __int64 base_adjust=0;
133 If we don't have gettimeofday, then likely we are on a VMS machine that
134 operates on local time rather than UTC...so we have to zone-adjust.
135 This code gleefully swiped from VMS.C
138 /* method used to handle UTC conversions:
139 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
141 static int gmtime_emulation_type;
142 /* number of secs to add to UTC POSIX-style time to get local time */
143 static long int utc_offset_secs;
144 static struct dsc$descriptor_s fildevdsc =
145 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
146 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
148 static time_t toutc_dst(time_t loc) {
151 if ((rsltmp = localtime(&loc)) == NULL) return -1;
152 loc -= utc_offset_secs;
153 if (rsltmp->tm_isdst) loc -= 3600;
157 static time_t toloc_dst(time_t utc) {
160 utc += utc_offset_secs;
161 if ((rsltmp = localtime(&utc)) == NULL) return -1;
162 if (rsltmp->tm_isdst) utc += 3600;
166 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
167 ((gmtime_emulation_type || timezone_setup()), \
168 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
169 ((secs) - utc_offset_secs))))
171 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
172 ((gmtime_emulation_type || timezone_setup()), \
173 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
174 ((secs) + utc_offset_secs))))
181 if (gmtime_emulation_type == 0) {
183 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
184 /* results of calls to gmtime() and localtime() */
187 gmtime_emulation_type++;
188 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
189 char off[LNM$C_NAMLENGTH+1];;
191 gmtime_emulation_type++;
192 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
193 gmtime_emulation_type++;
195 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
197 else { utc_offset_secs = atol(off); }
199 else { /* We've got a working gmtime() */
200 struct tm gmt, local;
203 tm_p = localtime(&base);
205 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
206 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
207 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
208 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
216 gettimeofday (struct timeval *tp, void *tpz)
222 long div_100ns_to_secs;
223 long div_100ns_to_usecs;
231 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
232 The return from function is also set to -1.
233 This is not exactly as per the manual page.
239 if (base_adjust[0]==0 && base_adjust[1]==0) {
241 if (base_adjust==0) { /* Need to determine epoch adjustment */
243 ret=sys$bintim(&dscepoch,&base_adjust);
244 if (1 != (ret &&1)) {
250 ret=sys$gettim(&quad); /* Get VMS system time */
251 if ((1 && ret) == 1) {
253 quad[0] -= base_adjust[0]; /* convert to epoch offset */
254 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
255 div_100ns_to_secs = DIV_100NS_TO_SECS;
256 div_100ns_to_usecs = DIV_100NS_TO_USECS;
257 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
260 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
261 tp->tv_sec = quo; /* Whole seconds */
262 tp->tv_usec = quo1; /* Micro-seconds */
264 quad -= base_adjust; /* convert to epoch offset */
265 ans1=qdiv(quad,DIV_100NS_TO_SECS);
266 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
267 tp->tv_sec = ans1.quot; /* Whole seconds */
268 tp->tv_usec = ans2.quot; /* Micro-seconds */
276 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
278 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
285 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
286 #ifndef SELECT_IS_BROKEN
288 #define usleep hrt_usleep /* could conflict with ncurses for static build */
291 hrt_usleep(unsigned long usec)
296 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
297 (Select_fd_set_t)NULL, &tv);
302 #if !defined(HAS_USLEEP) && defined(WIN32)
304 #define usleep hrt_usleep /* could conflict with ncurses for static build */
307 hrt_usleep(unsigned long usec)
316 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
318 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
321 hrt_ualarm(int usec, int interval)
323 struct itimerval itv;
324 itv.it_value.tv_sec = usec / 1000000;
325 itv.it_value.tv_usec = usec % 1000000;
326 itv.it_interval.tv_sec = interval / 1000000;
327 itv.it_interval.tv_usec = interval % 1000000;
328 return setitimer(ITIMER_REAL, &itv, 0);
332 #if !defined(HAS_UALARM) && defined(VMS)
334 #define ualarm vms_ualarm
336 #include <lib$routines.h>
344 #define VMSERR(s) (!((s)&1))
347 us_to_VMS(useconds_t mseconds, unsigned long v[])
356 iss = lib$addx(qq,qq,qq);
357 if (VMSERR(iss)) lib$signal(iss);
358 iss = lib$subx(v,qq,v);
359 if (VMSERR(iss)) lib$signal(iss);
360 iss = lib$addx(qq,qq,qq);
361 if (VMSERR(iss)) lib$signal(iss);
362 iss = lib$subx(v,qq,v);
363 if (VMSERR(iss)) lib$signal(iss);
364 iss = lib$subx(v,qq,v);
365 if (VMSERR(iss)) lib$signal(iss);
369 VMS_to_us(unsigned long v[])
372 unsigned long div=10,quot, rem;
374 iss = lib$ediv(&div,v,",&rem);
375 if (VMSERR(iss)) lib$signal(iss);
380 typedef unsigned short word;
381 typedef struct _ualarm {
384 unsigned long delay[2];
385 unsigned long interval[2];
386 unsigned long remain[2];
391 static Alarm *a0, alarm_base;
396 static void ualarm_AST(Alarm *a);
399 vms_ualarm(int mseconds, int interval)
408 static struct item_list3 itmlst[2];
409 static int first = 1;
415 itmlst[0].code = JPI$_ASTEN;
416 itmlst[0].length = sizeof(asten);
417 itmlst[0].retlenaddr = NULL;
419 itmlst[1].length = 0;
420 itmlst[1].bufaddr = NULL;
421 itmlst[1].retlenaddr = NULL;
423 iss = lib$get_ef(&alarm_ef);
424 if (VMSERR(iss)) lib$signal(iss);
427 a0->function = UAL_NULL;
429 itmlst[0].bufaddr = &asten;
431 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
432 if (VMSERR(iss)) lib$signal(iss);
433 if (!(asten&0x08)) return -1;
437 a->function = UAL_SET;
439 a->function = UAL_CLEAR;
442 us_to_VMS(mseconds, a->delay);
444 us_to_VMS(interval, a->interval);
449 iss = sys$clref(alarm_ef);
450 if (VMSERR(iss)) lib$signal(iss);
452 iss = sys$dclast(ualarm_AST,a,0);
453 if (VMSERR(iss)) lib$signal(iss);
455 iss = sys$waitfr(alarm_ef);
456 if (VMSERR(iss)) lib$signal(iss);
458 if (a->function == UAL_ACTIVE)
459 return VMS_to_us(a->remain);
470 unsigned long now[2];
472 iss = sys$gettim(now);
473 if (VMSERR(iss)) lib$signal(iss);
475 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
476 if (a0->function == UAL_ACTIVE) {
477 iss = sys$cantim(a0,PSL$C_USER);
478 if (VMSERR(iss)) lib$signal(iss);
480 iss = lib$subx(a0->remain, now, a->remain);
481 if (VMSERR(iss)) lib$signal(iss);
483 if (a->remain[1] & 0x80000000)
484 a->remain[0] = a->remain[1] = 0;
487 if (a->function == UAL_SET) {
488 a->function = a0->function;
489 a0->function = UAL_ACTIVE;
490 a0->repeat = a->repeat;
492 a0->interval[0] = a->interval[0];
493 a0->interval[1] = a->interval[1];
495 a0->delay[0] = a->delay[0];
496 a0->delay[1] = a->delay[1];
498 iss = lib$subx(now, a0->delay, a0->remain);
499 if (VMSERR(iss)) lib$signal(iss);
501 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
502 if (VMSERR(iss)) lib$signal(iss);
504 a->function = a0->function;
505 a0->function = UAL_NULL;
507 iss = sys$setef(alarm_ef);
508 if (VMSERR(iss)) lib$signal(iss);
509 } else if (a->function == UAL_ACTIVE) {
511 iss = lib$subx(now, a->interval, a->remain);
512 if (VMSERR(iss)) lib$signal(iss);
514 iss = sys$setimr(0,a->interval,ualarm_AST,a);
515 if (VMSERR(iss)) lib$signal(iss);
517 a->function = UAL_NULL;
520 if (VMSERR(iss)) lib$signal(iss);
521 lib$signal(SS$_ASTFLT);
523 lib$signal(SS$_BADPARAM);
527 #endif /* !HAS_UALARM && VMS */
531 #ifdef HAS_GETTIMEOFDAY
538 status = gettimeofday (&Tp, NULL);
549 status = gettimeofday (&Tp, NULL);
550 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
555 MODULE = Time::HiRes PACKAGE = Time::HiRes
560 #ifdef HAS_GETTIMEOFDAY
563 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv((IV) myNVtime()), 0);
564 if (myU2time(auv) == 0)
565 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
574 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
580 struct timeval Ta, Tb;
582 gettimeofday(&Ta, NULL);
584 if (useconds > 1E6) {
585 IV seconds = (IV) (useconds / 1E6);
586 /* If usleep() has been implemented using setitimer()
587 * then this contortion is unnecessary-- but usleep()
588 * may be implemented in some other way, so let's contort. */
591 useconds -= 1E6 * seconds;
593 } else if (useconds < 0.0)
594 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
595 usleep((UV)useconds);
598 gettimeofday(&Tb, NULL);
600 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
602 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
610 struct timeval Ta, Tb;
612 gettimeofday(&Ta, NULL);
614 NV seconds = SvNV(ST(0));
615 if (seconds >= 0.0) {
616 UV useconds = 1E6 * (seconds - (UV)seconds);
620 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
623 gettimeofday(&Tb, NULL);
625 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
627 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
637 ualarm(useconds,interval=0)
641 if (useconds < 0 || interval < 0)
642 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
643 RETVAL = ualarm(useconds, interval);
649 alarm(seconds,interval=0)
653 if (seconds < 0.0 || interval < 0.0)
654 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
655 RETVAL = (NV)ualarm(seconds * 1000000,
656 interval * 1000000) / 1E6;
663 #ifdef HAS_GETTIMEOFDAY
664 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
672 status = gettimeofday (&Tp, &Tz);
673 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
675 if (GIMME == G_ARRAY) {
677 /* Mac OS (Classic) has unsigned time_t */
678 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
679 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
682 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
692 status = gettimeofday (&Tp, &Tz);
693 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
694 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
698 # else /* MACOS_TRADITIONAL */
705 status = gettimeofday (&Tp, NULL);
706 if (GIMME == G_ARRAY) {
708 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
709 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
712 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
721 status = gettimeofday (&Tp, NULL);
722 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
726 # endif /* MACOS_TRADITIONAL */
729 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
731 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
734 setitimer(which, seconds, interval = 0)
739 struct itimerval newit;
740 struct itimerval oldit;
742 if (seconds < 0.0 || interval < 0.0)
743 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", which, seconds, interval);
744 newit.it_value.tv_sec = seconds;
745 newit.it_value.tv_usec =
746 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
747 newit.it_interval.tv_sec = interval;
748 newit.it_interval.tv_usec =
749 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
750 if (setitimer(which, &newit, &oldit) == 0) {
752 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
753 if (GIMME == G_ARRAY) {
755 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
763 struct itimerval nowit;
765 if (getitimer(which, &nowit) == 0) {
767 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
768 if (GIMME == G_ARRAY) {
770 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
776 # $Id: HiRes.xs,v 1.11 1999/03/16 02:27:38 wegscd Exp wegscd $
779 # Revision 1.11 1999/03/16 02:27:38 wegscd
780 # Add U2time, NVtime. Fix symbols for static link.
782 # Revision 1.10 1998/09/30 02:36:25 wegscd
785 # Revision 1.9 1998/07/07 02:42:06 wegscd
788 # Revision 1.8 1998/07/02 01:47:26 wegscd
789 # Add Win32 code for gettimeofday.
791 # Revision 1.7 1997/11/13 02:08:12 wegscd
792 # Add missing EXTEND in gettimeofday() scalar code.
794 # Revision 1.6 1997/11/11 02:32:35 wegscd
795 # Do something useful when calling gettimeofday() in a scalar context.
796 # The patch is courtesy of Gisle Aas.
798 # Revision 1.5 1997/11/06 03:10:47 wegscd
799 # Fake ualarm() if we have setitimer.
801 # Revision 1.4 1997/11/05 05:41:23 wegscd
802 # Turn prototypes ON (suggested by Gisle Aas)
804 # Revision 1.3 1997/10/13 20:56:15 wegscd
805 # Add PROTOTYPES: DISABLE
807 # Revision 1.2 1997/05/23 01:01:38 wegscd
808 # Conditional compilation, depending on what the OS gives us.
810 # Revision 1.1 1996/09/03 18:26:35 wegscd