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 */
72 #define EPOCH_BIAS 116444736000000000i64
74 /* NOTE: This does not compute the timezone info (doing so can be expensive,
75 * and appears to be unsupported even by glibc) */
77 gettimeofday (struct timeval *tp, void *not_used)
81 /* this returns time in 100-nanosecond units (i.e. tens of usecs) */
82 GetSystemTimeAsFileTime(&ft.ft_val);
84 /* seconds since epoch */
85 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / 10000000i64);
87 /* microseconds remaining */
88 tp->tv_usec = (long)((ft.ft_i64 / 10i64) % 1000000i64);
94 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
95 #define HAS_GETTIMEOFDAY
98 #include <time.h> /* gettimeofday */
99 #include <stdlib.h> /* qdiv */
100 #include <starlet.h> /* sys$gettim */
103 #include <lib$routines.h> /* lib$ediv() */
107 VMS binary time is expressed in 100 nano-seconds since
108 system base time which is 17-NOV-1858 00:00:00.00
111 #define DIV_100NS_TO_SECS 10000000L
112 #define DIV_100NS_TO_USECS 10L
115 gettimeofday is supposed to return times since the epoch
116 so need to determine this in terms of VMS base time
118 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
121 static long base_adjust[2]={0L,0L};
123 static __int64 base_adjust=0;
128 If we don't have gettimeofday, then likely we are on a VMS machine that
129 operates on local time rather than UTC...so we have to zone-adjust.
130 This code gleefully swiped from VMS.C
133 /* method used to handle UTC conversions:
134 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
136 static int gmtime_emulation_type;
137 /* number of secs to add to UTC POSIX-style time to get local time */
138 static long int utc_offset_secs;
139 static struct dsc$descriptor_s fildevdsc =
140 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
141 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
143 static time_t toutc_dst(time_t loc) {
146 if ((rsltmp = localtime(&loc)) == NULL) return -1;
147 loc -= utc_offset_secs;
148 if (rsltmp->tm_isdst) loc -= 3600;
152 static time_t toloc_dst(time_t utc) {
155 utc += utc_offset_secs;
156 if ((rsltmp = localtime(&utc)) == NULL) return -1;
157 if (rsltmp->tm_isdst) utc += 3600;
161 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
162 ((gmtime_emulation_type || timezone_setup()), \
163 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
164 ((secs) - utc_offset_secs))))
166 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
167 ((gmtime_emulation_type || timezone_setup()), \
168 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
169 ((secs) + utc_offset_secs))))
176 if (gmtime_emulation_type == 0) {
178 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
179 /* results of calls to gmtime() and localtime() */
182 gmtime_emulation_type++;
183 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
184 char off[LNM$C_NAMLENGTH+1];;
186 gmtime_emulation_type++;
187 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
188 gmtime_emulation_type++;
190 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
192 else { utc_offset_secs = atol(off); }
194 else { /* We've got a working gmtime() */
195 struct tm gmt, local;
198 tm_p = localtime(&base);
200 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
201 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
202 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
203 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
211 gettimeofday (struct timeval *tp, void *tpz)
217 long div_100ns_to_secs;
218 long div_100ns_to_usecs;
226 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
227 The return from function is also set to -1.
228 This is not exactly as per the manual page.
234 if (base_adjust[0]==0 && base_adjust[1]==0) {
236 if (base_adjust==0) { /* Need to determine epoch adjustment */
238 ret=sys$bintim(&dscepoch,&base_adjust);
239 if (1 != (ret &&1)) {
245 ret=sys$gettim(&quad); /* Get VMS system time */
246 if ((1 && ret) == 1) {
248 quad[0] -= base_adjust[0]; /* convert to epoch offset */
249 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
250 div_100ns_to_secs = DIV_100NS_TO_SECS;
251 div_100ns_to_usecs = DIV_100NS_TO_USECS;
252 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
255 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
256 tp->tv_sec = quo; /* Whole seconds */
257 tp->tv_usec = quo1; /* Micro-seconds */
259 quad -= base_adjust; /* convert to epoch offset */
260 ans1=qdiv(quad,DIV_100NS_TO_SECS);
261 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
262 tp->tv_sec = ans1.quot; /* Whole seconds */
263 tp->tv_usec = ans2.quot; /* Micro-seconds */
271 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
273 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
280 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
281 #ifndef SELECT_IS_BROKEN
283 #define usleep hrt_usleep /* could conflict with ncurses for static build */
286 hrt_usleep(unsigned long usec)
291 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
292 (Select_fd_set_t)NULL, &tv);
297 #if !defined(HAS_USLEEP) && defined(WIN32)
299 #define usleep hrt_usleep /* could conflict with ncurses for static build */
302 hrt_usleep(unsigned long usec)
311 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
313 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
316 hrt_ualarm(int usec, int interval)
318 struct itimerval itv;
319 itv.it_value.tv_sec = usec / 1000000;
320 itv.it_value.tv_usec = usec % 1000000;
321 itv.it_interval.tv_sec = interval / 1000000;
322 itv.it_interval.tv_usec = interval % 1000000;
323 return setitimer(ITIMER_REAL, &itv, 0);
327 #ifdef HAS_GETTIMEOFDAY
334 status = gettimeofday (&Tp, NULL);
345 status = gettimeofday (&Tp, NULL);
346 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
351 MODULE = Time::HiRes PACKAGE = Time::HiRes
356 #ifdef HAS_GETTIMEOFDAY
359 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv((IV) myNVtime()), 0);
360 if (myU2time(auv) == 0)
361 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
370 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
376 struct timeval Ta, Tb;
378 gettimeofday(&Ta, NULL);
380 if (useconds > 1E6) {
381 IV seconds = (IV) (useconds / 1E6);
382 /* If usleep() has been implemented using setitimer()
383 * then this contortion is unnecessary-- but usleep()
384 * may be implemented in some other way, so let's contort. */
387 useconds -= 1E6 * seconds;
389 } else if (useconds < 0.0)
390 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
391 usleep((UV)useconds);
394 gettimeofday(&Tb, NULL);
396 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
398 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
406 struct timeval Ta, Tb;
408 gettimeofday(&Ta, NULL);
410 NV seconds = SvNV(ST(0));
411 if (seconds >= 0.0) {
412 UV useconds = 1E6 * (seconds - (UV)seconds);
416 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
419 gettimeofday(&Tb, NULL);
421 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
423 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
433 ualarm(useconds,interval=0)
437 if (useconds < 0 || interval < 0)
438 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
439 RETVAL = ualarm(useconds, interval);
445 alarm(seconds,interval=0)
449 if (seconds < 0.0 || interval < 0.0)
450 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
451 RETVAL = (NV)ualarm(seconds * 1000000,
452 interval * 1000000) / 1E6;
459 #ifdef HAS_GETTIMEOFDAY
460 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
468 status = gettimeofday (&Tp, &Tz);
469 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
471 if (GIMME == G_ARRAY) {
473 /* Mac OS (Classic) has unsigned time_t */
474 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
475 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
478 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
488 status = gettimeofday (&Tp, &Tz);
489 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
490 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
494 # else /* MACOS_TRADITIONAL */
501 status = gettimeofday (&Tp, NULL);
502 if (GIMME == G_ARRAY) {
504 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
505 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
508 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
517 status = gettimeofday (&Tp, NULL);
518 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
522 # endif /* MACOS_TRADITIONAL */
525 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
527 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
530 setitimer(which, seconds, interval = 0)
535 struct itimerval newit;
536 struct itimerval oldit;
538 if (seconds < 0.0 || interval < 0.0)
539 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", which, seconds, interval);
540 newit.it_value.tv_sec = seconds;
541 newit.it_value.tv_usec =
542 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
543 newit.it_interval.tv_sec = interval;
544 newit.it_interval.tv_usec =
545 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
546 if (setitimer(which, &newit, &oldit) == 0) {
548 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
549 if (GIMME == G_ARRAY) {
551 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
559 struct itimerval nowit;
561 if (getitimer(which, &nowit) == 0) {
563 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
564 if (GIMME == G_ARRAY) {
566 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
572 # $Id: HiRes.xs,v 1.11 1999/03/16 02:27:38 wegscd Exp wegscd $
575 # Revision 1.11 1999/03/16 02:27:38 wegscd
576 # Add U2time, NVtime. Fix symbols for static link.
578 # Revision 1.10 1998/09/30 02:36:25 wegscd
581 # Revision 1.9 1998/07/07 02:42:06 wegscd
584 # Revision 1.8 1998/07/02 01:47:26 wegscd
585 # Add Win32 code for gettimeofday.
587 # Revision 1.7 1997/11/13 02:08:12 wegscd
588 # Add missing EXTEND in gettimeofday() scalar code.
590 # Revision 1.6 1997/11/11 02:32:35 wegscd
591 # Do something useful when calling gettimeofday() in a scalar context.
592 # The patch is courtesy of Gisle Aas.
594 # Revision 1.5 1997/11/06 03:10:47 wegscd
595 # Fake ualarm() if we have setitimer.
597 # Revision 1.4 1997/11/05 05:41:23 wegscd
598 # Turn prototypes ON (suggested by Gisle Aas)
600 # Revision 1.3 1997/10/13 20:56:15 wegscd
601 # Add PROTOTYPES: DISABLE
603 # Revision 1.2 1997/05/23 01:01:38 wegscd
604 # Conditional compilation, depending on what the OS gives us.
606 # Revision 1.1 1996/09/03 18:26:35 wegscd