1 /**********
2 Copyright 1990 Regents of the University of California.  All rights reserved.
3 **********/
4 
5 /*
6  * Date and time utility functions
7  */
8 
9 #include "ngspice/ngspice.h"
10 #include <string.h>
11 #include "misc_time.h"
12 
13 #ifdef HAVE_LOCALTIME
14 #include <time.h>
15 #endif
16 
17 #ifdef HAVE_GETRUSAGE
18 #  include <sys/types.h>
19 #  include <sys/time.h>
20 #  include <sys/resource.h>
21 #else
22 #  ifdef HAVE_TIMES
23 #    include <sys/types.h>
24 #    include <sys/times.h>
25 #    include <sys/param.h>
26 #  else
27 #    ifdef HAVE_FTIME
28 /* default to ftime if we can't get real CPU times */
29 #      include <sys/types.h>
30 #      include <sys/timeb.h>
31 #    endif
32 #  endif
33 #endif
34 
35 #ifdef HAVE_FTIME
36 #  include <sys/timeb.h>
37 #endif
38 
39 
40 /* Return the date. Return value is static data. */
41 
42 char *
datestring(void)43 datestring(void)
44 {
45 
46 #ifdef HAVE_LOCALTIME
47     static char tbuf[45];
48     struct tm *tp;
49     char *ap;
50     size_t i;
51 
52     time_t tloc;
53     time(&tloc);
54     tp = localtime(&tloc);
55     ap = asctime(tp);
56     (void) sprintf(tbuf, "%.20s", ap);
57     (void) strcat(tbuf, ap + 19);
58     i = strlen(tbuf);
59     tbuf[i - 1] = '\0';
60     return (tbuf);
61 
62 #else
63 
64     return ("today");
65 
66 #endif
67 }
68 
69 /* return time interval in seconds and milliseconds */
70 
71 #ifdef HAVE_FTIME
72 
73 struct timeb timebegin;
74 
timediff(struct timeb * now,struct timeb * begin,int * sec,int * msec)75 void timediff(struct timeb *now, struct timeb *begin, int *sec, int *msec)
76 {
77 
78     *msec = (int) now->millitm - (int) begin->millitm;
79     *sec = (int) now->time - (int) begin->time;
80     if (*msec < 0) {
81       *msec += 1000;
82       (*sec)--;
83     }
84     return;
85 
86 }
87 
88 #endif
89 
90 /*
91  * How many seconds have elapsed in running time.
92  * This is the routine called in IFseconds
93  */
94 
95 double
seconds(void)96 seconds(void)
97 {
98 #ifdef HAVE_GETRUSAGE
99     int ret;
100     struct rusage ruse;
101 
102     memset(&ruse, 0, sizeof(ruse));
103     ret = getrusage(RUSAGE_SELF, &ruse);
104     if(ret == -1) {
105       perror("getrusage(): ");
106       return 1;
107     }
108     return ((double)ruse.ru_utime.tv_sec + (double) ruse.ru_utime.tv_usec / 1000000.0);
109 #else
110 #ifdef HAVE_TIMES
111 
112     struct tms tmsbuf;
113 
114     times(&tmsbuf);
115     return((double) tmsbuf.tms_utime / HZ);
116 
117 #else
118 #ifdef HAVE_FTIME
119     struct timeb timenow;
120     int sec, msec;
121 
122     ftime(&timenow);
123     timediff(&timenow, &timebegin, &sec, &msec);
124     return(sec + (double) msec / 1000.0);
125 
126 #else /* unknown */
127     /* don't know how to do this in general. */
128     return(-1.0);	/* Obvious error condition */
129 
130 #endif /* !FTIME */
131 #endif /* !SYSV */
132 #endif /* !BSD */
133 }
134