File: unixtime.c

package info (click to toggle)
gcl 2.6.7%2Bdfsga-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 84,796 kB
  • sloc: ansic: 452,686; lisp: 156,133; asm: 111,405; sh: 29,299; cpp: 18,599; perl: 5,602; makefile: 5,201; tcl: 3,181; sed: 469; yacc: 378; lex: 174; fortran: 48; awk: 30; csh: 23
file content (314 lines) | stat: -rwxr-xr-x 7,277 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa

This file is part of GNU Common Lisp, herein referred to as GCL

GCL is free software; you can redistribute it and/or modify it under
the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GCL is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public 
License for more details.

You should have received a copy of the GNU Library General Public License 
along with GCL; see the file COPYING.  If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/*
	unixtime.c
*/

#define IN_UNIXTIME

#include <unistd.h>

#include "include.h"
#include <sys/types.h>
#ifdef UNIX
/* all we want from this is HZ the number of clock ticks per second
which is usually 60 maybe 100 or something else. */
#undef PAGESIZE
#ifndef NO_SYS_PARAM_H
#include <sys/param.h>
#endif
#endif

#ifndef HZ
/* #define HZ 60 */
#define HZ 100
#endif

/* #define HZ1 (HZ > 100 ? 100 : HZ) */
#define HZ1 HZ

#ifdef USE_ATT_TIME
#  undef BSD
#  define ATT
#endif

#if defined __MINGW32__ || !defined NO_SYSTEM_TIME_ZONE

#  ifdef __MINGW32__
#    include <windows.h>
#    include <time.h>
#    include <sys/timeb.h>

static struct timeb t0;
int usleep ( unsigned int microseconds );

#  endif

static void FFN(siLget_system_time_zone)(void);

#endif /* __MINGW32__ or  !defined NO_SYSTEM_TIME_ZONE */

#ifdef BSD
#include <time.h>
#include <sys/timeb.h>
#ifndef NO_SYS_TIMES_H
#include <sys/times.h>
#endif
#include <sys/time.h>
/* static struct timeb beginning; */
#endif

#ifdef ATT
#include <sys/times.h>
static long beginning;
#endif

int
runtime(void)
{

#ifdef USE_INTERNAL_REAL_TIME_FOR_RUNTIME

#  ifdef __MINGW32__    
    struct timeb t;
    if ( t0.time == 0 ) {
        ftime(&t0);
    }
    ftime ( &t );
    return ( ( t.time - t0.time ) * HZ1 + ( (t.millitm) * HZ1 ) / 1000 );
#  else
#  error Need to return runtime without generating a fixnum (else GBC(t_fixnum) will loop)
#  endif
    
#else	
	{
	  struct tms buf;
	  times(&buf);
	  return(buf.tms_utime);
	}
#endif
}

object
unix_time_to_universal_time(int i)
{
	object x;
	vs_mark;

	vs_push(make_fixnum(24*60*60));
	vs_push(make_fixnum(70*365+17));
	x = number_times(vs_top[-1], vs_top[-2]);
	vs_push(x);
	vs_push(make_fixnum(i));
	x = number_plus(vs_top[-1], vs_top[-2]);
	vs_reset;
	return(x);
}

DEFUN_NEW("GET-UNIVERSAL-TIME",object,fLget_universal_time,LISP
   ,0,0,NONE,OO,OO,OO,OO,(void),"")
{
	/* 0 args */
	RETURN1(unix_time_to_universal_time(time(0)));
}

LFD(Lsleep)(void)
{
	object z;
	
	check_arg(1);
	check_type_or_rational_float(&vs_base[0]);
	if (number_minusp(vs_base[0]) == TRUE)
		FEerror("~S is not a non-negative number.", 1, vs_base[0]);
	vs_base[0]=number_times(vs_base[0],make_fixnum(1000000));
	Lround();
	z = vs_base[0];
	if (type_of(z) == t_fixnum)
		usleep(fix(z));
	else
            /* What is this for? -- MJT */
		for(;;)
#ifdef __MINGW32__
			Sleep ( 10000 );
#else                    
			sleep(1000);
#endif        
	vs_top = vs_base;
	vs_push(Cnil);
}

LFD(Lget_internal_run_time)(void)
{

#ifdef USE_INTERNAL_REAL_TIME_FOR_RUNTIME
        vs_push(fLget_internal_real_time());
	vs_push(small_fixnum(0));
	return;
#else
	struct tms buf;

	check_arg(0);
	times(&buf);
	vs_push(make_fixnum(buf.tms_utime));
	vs_push(make_fixnum(buf.tms_cutime));

#endif	
	
}


DEFUN_NEW("GETTIMEOFDAY",object,fSgettimeofday,SI,0,0,NONE,OO,OO,OO,OO,(void),"Return time with maximum resolution") {
#ifdef __MINGW32__
  LARGE_INTEGER uu,ticks;
  if (QueryPerformanceFrequency(&ticks)) {
    QueryPerformanceCounter(&uu);
    return make_longfloat((longfloat)uu.QuadPart/ticks.QuadPart);
  } else {
    FEerror("microsecond timing not available",0);
    return Cnil;
   /* static struct timeb t0;  */
   /* static unsigned u;  */
   /* struct timeb t;   */
   /* ftime(&t);  */
   /* if (t.time!=t0.time || t.millitm!=t0.millitm) {t0=t;u=0;}  */
   /* u++;  */
   /* return make_longfloat(((longfloat)t.time+1.0e-3*t.millitm+1.0e-6*(u%1000))); */
  }
#endif  
#ifdef BSD
  struct timeval tzp;
  gettimeofday(&tzp,0);
  return make_longfloat((longfloat)tzp.tv_sec+1.0e-6*tzp.tv_usec);
#endif
#ifdef ATT
  return make_longfloat((longfloat)time(0));
#endif
}


DEFUN_NEW("GET-INTERNAL-REAL-TIME",object,fLget_internal_real_time,LISP,0,0,NONE,OO,OO,OO,OO,(void),"Run time relative to beginning")
     
{
#ifdef __MINGW32__
    struct timeb t;
    if ( t0.time == 0 ) {
        ftime ( &t0 );
    }
    ftime(&t);
    return ( make_fixnum ( ( t.time - t0.time ) * HZ1 + ( (t.millitm) * HZ1 ) / 1000 ) );
#endif  
#ifdef BSD
	static struct timeval begin_tzp;
	struct timeval tzp;
	if (begin_tzp.tv_sec==0)
	  gettimeofday(&begin_tzp,0);
	gettimeofday(&tzp,0);
/* the value returned will be relative to the first time this is called,
   plus the fraction of a second.  We must make it relative, so this
   will only wrap if the process lasts longer than 818 days
   */
	return make_fixnum(((tzp.tv_sec-begin_tzp.tv_sec)*HZ1
			    + ((tzp.tv_usec)*HZ1)/1000000));
#endif
#ifdef ATT
	return make_fixnum((time(0) - beginning)*HZ1);
#endif
}

#if defined __MINGW32__ || !defined NO_SYSTEM_TIME_ZONE
DEFVAR("*DEFAULT-TIME-ZONE*",sSAdefault_time_zoneA,SI,make_fixnum ( system_time_zone_helper() ),"");
#else
DEFVAR("*DEFAULT-TIME-ZONE*",sSAdefault_time_zoneA,SI,make_fixnum(TIME_ZONE),"");
#endif

void
gcl_init_unixtime(void)
{
/* #ifdef BSD */
/* 	ftime(&beginning); */
/* #endif */
#ifdef ATT
	beginning = time(0);
#endif
#  if defined __MINGW32__
        ftime(&t0);
#  endif        

	make_constant("INTERNAL-TIME-UNITS-PER-SECOND", make_fixnum(HZ1));

	make_function("SLEEP", Lsleep);
	make_function("GET-INTERNAL-RUN-TIME", Lget_internal_run_time);
#if defined __MINGW32__   || !defined NO_SYSTEM_TIME_ZONE
	make_si_function("GET-SYSTEM-TIME-ZONE", siLget_system_time_zone);
#endif        
}

#ifdef __MINGW32__
int usleep ( unsigned int microseconds )
{
    unsigned int milliseconds = microseconds / 1000;
    return ( SleepEx ( milliseconds, TRUE ) );
}

int system_time_zone_helper(void)
{
    TIME_ZONE_INFORMATION tzi;
    DWORD TZResult;
    int tz=0;

    TZResult = GetTimeZoneInformation ( &tzi );

    /* Now UTC = (local time + bias), in units of minutes, so */
    /*fprintf ( stderr, "Bias = %ld\n", tzi.Bias );*/
    tz = (int) (tzi.Bias / 60);
    return ( tz );                                    
}
#endif

/* At GCC 3.2, Mingw struct tm does not include tm_gmtoff so avoid this version */
#if !defined ( NO_SYSTEM_TIME_ZONE ) && !defined ( __MINGW32__ )
int system_time_zone_helper(void){
  
  struct tm *local;
  time_t TIME;
  int nsecs;

  TIME = time(0);
  local = localtime(&TIME);
  nsecs = local->tm_gmtoff;
  if (nsecs == 0)
    return (nsecs);
  else
    return(- (nsecs / 60 / 60));
}
#endif /* !defined NO_SYSTEM_TIME_ZONE */

#if defined __MINGW32__ || !defined NO_SYSTEM_TIME_ZONE

static void
FFN(siLget_system_time_zone)(void)
{
  check_arg(0);
  vs_push ( make_fixnum ( system_time_zone_helper() ) );
}

#endif /*__MINGW32__ or !defined NO_SYSTEM_TIME_ZONE */