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(WIN32)
62 #define HAS_GETTIMEOFDAY
64 /* shows up in winsock.h?
72 unsigned __int64 ft_i64;
76 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
78 #define Const64(x) x##LL
80 #define Const64(x) x##i64
82 #define EPOCH_BIAS Const64(116444736000000000)
84 /* NOTE: This does not compute the timezone info (doing so can be expensive,
85 * and appears to be unsupported even by glibc) */
87 gettimeofday (struct timeval *tp, void *not_used)
91 /* this returns time in 100-nanosecond units (i.e. tens of usecs) */
92 GetSystemTimeAsFileTime(&ft.ft_val);
94 /* seconds since epoch */
95 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
97 /* microseconds remaining */
98 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
104 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
105 #define HAS_GETTIMEOFDAY
108 #include <time.h> /* gettimeofday */
109 #include <stdlib.h> /* qdiv */
110 #include <starlet.h> /* sys$gettim */
113 #include <lib$routines.h> /* lib$ediv() */
117 VMS binary time is expressed in 100 nano-seconds since
118 system base time which is 17-NOV-1858 00:00:00.00
121 #define DIV_100NS_TO_SECS 10000000L
122 #define DIV_100NS_TO_USECS 10L
125 gettimeofday is supposed to return times since the epoch
126 so need to determine this in terms of VMS base time
128 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
131 static long base_adjust[2]={0L,0L};
133 static __int64 base_adjust=0;
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
143 /* method used to handle UTC conversions:
144 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
146 static int gmtime_emulation_type;
147 /* number of secs to add to UTC POSIX-style time to get local time */
148 static long int utc_offset_secs;
149 static struct dsc$descriptor_s fildevdsc =
150 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
151 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
153 static time_t toutc_dst(time_t loc) {
156 if ((rsltmp = localtime(&loc)) == NULL) return -1;
157 loc -= utc_offset_secs;
158 if (rsltmp->tm_isdst) loc -= 3600;
162 static time_t toloc_dst(time_t utc) {
165 utc += utc_offset_secs;
166 if ((rsltmp = localtime(&utc)) == NULL) return -1;
167 if (rsltmp->tm_isdst) utc += 3600;
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))))
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))))
186 if (gmtime_emulation_type == 0) {
188 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
189 /* results of calls to gmtime() and localtime() */
192 gmtime_emulation_type++;
193 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
194 char off[LNM$C_NAMLENGTH+1];;
196 gmtime_emulation_type++;
197 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
198 gmtime_emulation_type++;
200 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
202 else { utc_offset_secs = atol(off); }
204 else { /* We've got a working gmtime() */
205 struct tm gmt, local;
208 tm_p = localtime(&base);
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);
221 gettimeofday (struct timeval *tp, void *tpz)
227 long div_100ns_to_secs;
228 long div_100ns_to_usecs;
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.
244 if (base_adjust[0]==0 && base_adjust[1]==0) {
246 if (base_adjust==0) { /* Need to determine epoch adjustment */
248 ret=sys$bintim(&dscepoch,&base_adjust);
249 if (1 != (ret &&1)) {
255 ret=sys$gettim(&quad); /* Get VMS system time */
256 if ((1 && ret) == 1) {
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);
265 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
266 tp->tv_sec = quo; /* Whole seconds */
267 tp->tv_usec = quo1; /* Micro-seconds */
269 quad -= base_adjust; /* convert to epoch offset */
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 */
281 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
283 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
290 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
291 #ifndef SELECT_IS_BROKEN
293 #define usleep hrt_usleep /* could conflict with ncurses for static build */
296 hrt_usleep(unsigned long usec)
301 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
302 (Select_fd_set_t)NULL, &tv);
307 #if !defined(HAS_USLEEP) && defined(WIN32)
309 #define usleep hrt_usleep /* could conflict with ncurses for static build */
312 hrt_usleep(unsigned long usec)
321 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
323 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
326 hrt_ualarm(int usec, int interval)
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);
337 #if !defined(HAS_UALARM) && defined(VMS)
339 #define ualarm vms_ualarm
341 #include <lib$routines.h>
349 #define VMSERR(s) (!((s)&1))
352 us_to_VMS(useconds_t mseconds, unsigned long v[])
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);
374 VMS_to_us(unsigned long v[])
377 unsigned long div=10,quot, rem;
379 iss = lib$ediv(&div,v,",&rem);
380 if (VMSERR(iss)) lib$signal(iss);
385 typedef unsigned short word;
386 typedef struct _ualarm {
389 unsigned long delay[2];
390 unsigned long interval[2];
391 unsigned long remain[2];
396 static Alarm *a0, alarm_base;
401 static void ualarm_AST(Alarm *a);
404 vms_ualarm(int mseconds, int interval)
413 static struct item_list3 itmlst[2];
414 static int first = 1;
420 itmlst[0].code = JPI$_ASTEN;
421 itmlst[0].length = sizeof(asten);
422 itmlst[0].retlenaddr = NULL;
424 itmlst[1].length = 0;
425 itmlst[1].bufaddr = NULL;
426 itmlst[1].retlenaddr = NULL;
428 iss = lib$get_ef(&alarm_ef);
429 if (VMSERR(iss)) lib$signal(iss);
432 a0->function = UAL_NULL;
434 itmlst[0].bufaddr = &asten;
436 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
437 if (VMSERR(iss)) lib$signal(iss);
438 if (!(asten&0x08)) return -1;
442 a->function = UAL_SET;
444 a->function = UAL_CLEAR;
447 us_to_VMS(mseconds, a->delay);
449 us_to_VMS(interval, a->interval);
454 iss = sys$clref(alarm_ef);
455 if (VMSERR(iss)) lib$signal(iss);
457 iss = sys$dclast(ualarm_AST,a,0);
458 if (VMSERR(iss)) lib$signal(iss);
460 iss = sys$waitfr(alarm_ef);
461 if (VMSERR(iss)) lib$signal(iss);
463 if (a->function == UAL_ACTIVE)
464 return VMS_to_us(a->remain);
475 unsigned long now[2];
477 iss = sys$gettim(now);
478 if (VMSERR(iss)) lib$signal(iss);
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);
485 iss = lib$subx(a0->remain, now, a->remain);
486 if (VMSERR(iss)) lib$signal(iss);
488 if (a->remain[1] & 0x80000000)
489 a->remain[0] = a->remain[1] = 0;
492 if (a->function == UAL_SET) {
493 a->function = a0->function;
494 a0->function = UAL_ACTIVE;
495 a0->repeat = a->repeat;
497 a0->interval[0] = a->interval[0];
498 a0->interval[1] = a->interval[1];
500 a0->delay[0] = a->delay[0];
501 a0->delay[1] = a->delay[1];
503 iss = lib$subx(now, a0->delay, a0->remain);
504 if (VMSERR(iss)) lib$signal(iss);
506 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
507 if (VMSERR(iss)) lib$signal(iss);
509 a->function = a0->function;
510 a0->function = UAL_NULL;
512 iss = sys$setef(alarm_ef);
513 if (VMSERR(iss)) lib$signal(iss);
514 } else if (a->function == UAL_ACTIVE) {
516 iss = lib$subx(now, a->interval, a->remain);
517 if (VMSERR(iss)) lib$signal(iss);
519 iss = sys$setimr(0,a->interval,ualarm_AST,a);
520 if (VMSERR(iss)) lib$signal(iss);
522 a->function = UAL_NULL;
525 if (VMSERR(iss)) lib$signal(iss);
526 lib$signal(SS$_ASTFLT);
528 lib$signal(SS$_BADPARAM);
532 #endif /* !HAS_UALARM && VMS */
536 #ifdef HAS_GETTIMEOFDAY
543 status = gettimeofday (&Tp, NULL);
554 status = gettimeofday (&Tp, NULL);
555 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
560 MODULE = Time::HiRes PACKAGE = Time::HiRes
565 #ifdef HAS_GETTIMEOFDAY
568 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
569 if (myU2time(auv) == 0)
570 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
579 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
585 struct timeval Ta, Tb;
587 gettimeofday(&Ta, NULL);
589 if (useconds > 1E6) {
590 IV seconds = (IV) (useconds / 1E6);
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. */
596 useconds -= 1E6 * seconds;
598 } else if (useconds < 0.0)
599 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
600 usleep((UV)useconds);
603 gettimeofday(&Tb, NULL);
605 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
607 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
615 struct timeval Ta, Tb;
617 gettimeofday(&Ta, NULL);
619 NV seconds = SvNV(ST(0));
620 if (seconds >= 0.0) {
621 UV useconds = 1E6 * (seconds - (UV)seconds);
626 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
629 gettimeofday(&Tb, NULL);
631 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
633 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
643 ualarm(useconds,interval=0)
647 if (useconds < 0 || interval < 0)
648 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
649 RETVAL = ualarm(useconds, interval);
655 alarm(seconds,interval=0)
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;
669 #ifdef HAS_GETTIMEOFDAY
670 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
678 status = gettimeofday (&Tp, &Tz);
679 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
681 if (GIMME == G_ARRAY) {
683 /* Mac OS (Classic) has unsigned time_t */
684 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
685 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
688 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
698 status = gettimeofday (&Tp, &Tz);
699 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
700 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
704 # else /* MACOS_TRADITIONAL */
711 status = gettimeofday (&Tp, NULL);
712 if (GIMME == G_ARRAY) {
714 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
715 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
718 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
727 status = gettimeofday (&Tp, NULL);
728 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
732 # endif /* MACOS_TRADITIONAL */
735 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
737 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
740 setitimer(which, seconds, interval = 0)
745 struct itimerval newit;
746 struct itimerval oldit;
748 if (seconds < 0.0 || interval < 0.0)
749 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", which, seconds, interval);
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) {
758 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
759 if (GIMME == G_ARRAY) {
761 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
769 struct itimerval nowit;
771 if (getitimer(which, &nowit) == 0) {
773 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
774 if (GIMME == G_ARRAY) {
776 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
782 # $Id: HiRes.xs,v 1.11 1999/03/16 02:27:38 wegscd Exp wegscd $
785 # Revision 1.11 1999/03/16 02:27:38 wegscd
786 # Add U2time, NVtime. Fix symbols for static link.
788 # Revision 1.10 1998/09/30 02:36:25 wegscd
791 # Revision 1.9 1998/07/07 02:42:06 wegscd
794 # Revision 1.8 1998/07/02 01:47:26 wegscd
795 # Add Win32 code for gettimeofday.
797 # Revision 1.7 1997/11/13 02:08:12 wegscd
798 # Add missing EXTEND in gettimeofday() scalar code.
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.
804 # Revision 1.5 1997/11/06 03:10:47 wegscd
805 # Fake ualarm() if we have setitimer.
807 # Revision 1.4 1997/11/05 05:41:23 wegscd
808 # Turn prototypes ON (suggested by Gisle Aas)
810 # Revision 1.3 1997/10/13 20:56:15 wegscd
811 # Add PROTOTYPES: DISABLE
813 # Revision 1.2 1997/05/23 01:01:38 wegscd
814 # Conditional compilation, depending on what the OS gives us.
816 # Revision 1.1 1996/09/03 18:26:35 wegscd