File: time.c

package info (click to toggle)
lua-posix 36.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,720 kB
  • sloc: ansic: 5,462; makefile: 21; sh: 6
file content (414 lines) | stat: -rw-r--r-- 9,556 bytes parent folder | download | duplicates (2)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * POSIX library for Lua 5.1, 5.2, 5.3 & 5.4.
 * Copyright (C) 2013-2025 Gary V. Vaughan
 * Copyright (C) 2010-2013 Reuben Thomas <rrt@sc3d.org>
 * Copyright (C) 2008-2010 Natanael Copa <natanael.copa@gmail.com>
 * Clean up and bug fixes by Leo Razoumov <slonik.az@gmail.com> 2006-10-11
 * Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> 07 Apr 2006 23:17:49
 * Based on original by Claudio Terra for Lua 3.x.
 * With contributions by Roberto Ierusalimschy.
 * With documentation from Steve Donovan 2012
 */
/***
 Time and Clock Functions.

@module posix.time
*/

#include <sys/time.h>
#include <time.h>

#include "_helpers.c"


static const char *Stimespec_fields[] = { "tv_sec", "tv_nsec" };

static void
totimespec(lua_State *L, int index, struct timespec *ts)
{
	luaL_checktype(L, index, LUA_TTABLE);
	ts->tv_sec  = (time_t)optintegerfield(L, index, "tv_sec", 0);
	ts->tv_nsec = optlongfield(L, index, "tv_nsec", 0);

	checkfieldnames(L, index, Stimespec_fields);
}


/***
Timespec record.
@table PosixTimespec
@int tv_sec seconds
@int tv_nsec nanoseconds
@see posix.sys.time.PosixTimeval
*/
static int
pushtimespec(lua_State *L, struct timespec *ts)
{
	if (!ts)
		return lua_pushnil(L), 1;

	lua_createtable(L, 0, 2);
	setintegerfield(ts, tv_sec);
	setintegerfield(ts, tv_nsec);

	settypemetatable("PosixTimespec");
	return 1;
}

/* for compatibility, we accept tm_gmtoff and tm_zone as fields even
 * if the underlying OS doesn't. */
static const char *Stm_fields[] = {
  "tm_sec", "tm_min", "tm_hour", "tm_mday", "tm_mon", "tm_year", "tm_wday",
  "tm_yday", "tm_isdst", "tm_gmtoff", "tm_zone"
};

static void
totm(lua_State *L, int index, struct tm *t)
{
	static const struct tm nulltm;
	*t = nulltm;

	luaL_checktype(L, index, LUA_TTABLE);
	t->tm_sec   = optintfield(L, index, "tm_sec", 0);
	t->tm_min   = optintfield(L, index, "tm_min", 0);
	t->tm_hour  = optintfield(L, index, "tm_hour", 0);
	t->tm_mday  = optintfield(L, index, "tm_mday", 0);
	t->tm_mon   = optintfield(L, index, "tm_mon", 0);
	t->tm_year  = optintfield(L, index, "tm_year", 0);
	t->tm_wday  = optintfield(L, index, "tm_wday", 0);
	t->tm_yday  = optintfield(L, index, "tm_yday", 0);
	t->tm_isdst = optintfield(L, index, "tm_isdst", 0);
#if HAVE_TM_GMTOFF
	t->tm_gmtoff = optintfield(L, index, "tm_gmtoff", 0);
#endif
#if HAVE_TM_ZONE
	t->tm_zone = optstringfield(L, index, "tm_zone", NULL);
#endif

	checkfieldnames(L, index, Stm_fields);
}


/***
Datetime record.
@table PosixTm
@int tm_sec second [0,60]
@int tm_min minute [0,59]
@int tm_hour hour [0,23]
@int tm_mday day of month [1, 31]
@int tm_mon month of year [0,11]
@int tm_year years since 1900
@int tm_wday day of week [0=Sunday,6]
@int tm_yday day of year [0,365[
@int tm_isdst 0 if daylight savings time is not in effect
*/
static int
pushtm(lua_State *L, struct tm *t)
{
	if (!t)
		return lua_pushnil(L), 1;

	lua_createtable(L, 0, 9);
	setintegerfield(t, tm_sec);
	setintegerfield(t, tm_min);
	setintegerfield(t, tm_hour);
	setintegerfield(t, tm_mday);
	setintegerfield(t, tm_mon);
	setintegerfield(t, tm_year);
	setintegerfield(t, tm_wday);
	setintegerfield(t, tm_yday);
	setintegerfield(t, tm_isdst);
#if HAVE_TM_GMTOFF
	setintegerfield(t, tm_gmtoff);
#endif
#if HAVE_TM_ZONE
	setstringfield(t, tm_zone);
#endif

	settypemetatable("PosixTm");
	return 1;
}


#if defined _POSIX_TIMERS && _POSIX_TIMERS != -1
/***
Find the precision of a clock.
@function clock_getres
@int clk name of clock, one of `CLOCK_REALTIME`, `CLOCK_PROCESS_CPUTIME_ID`,
  `CLOCK_MONOTONIC` or `CLOCK_THREAD_CPUTIME_ID`
@treturn[1] PosixTimespec resolution of *clk*, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see clock_getres(3)
*/
static int
Pclock_getres(lua_State *L)
{
	struct timespec resolution;
	int clk = checkint(L, 1);
	checknargs(L, 1);
	if (clock_getres(clk, &resolution) == -1)
		return pusherror(L, "clock_getres");
	return pushtimespec(L, &resolution);
}


/***
Read a clock.
@function clock_gettime
@int clk name of clock, one of `CLOCK_REALTIME`, `CLOCK_PROCESS_CPUTIME_ID`,
  `CLOCK_MONOTONIC` or `CLOCK_THREAD_CPUTIME_ID`
@treturn[1] PosixTimespec current value of *clk*, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see clock_gettime(3)
*/
static int
Pclock_gettime(lua_State *L)
{
	struct timespec ts;
	int clk = checkint(L, 1);
	checknargs(L, 1);
	if (clock_gettime(clk, &ts) == -1)
		return pusherror(L, "clock_gettime");
	return pushtimespec(L, &ts);
}
#endif


/***
Convert epoch time value to a broken-down UTC time.
@function gmtime
@int t seconds since epoch
@treturn[1] PosixTm broken-down time, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see gmtime(3)
*/
static int
Pgmtime(lua_State *L)
{
	struct tm t;
	time_t epoch = checklong(L, 1);
	checknargs(L, 1);
	if (gmtime_r(&epoch, &t) == NULL)
		return pusherror(L, "gmtime");
	return pushtm(L, &t);
}


/***
Convert epoch time value to a broken-down local time.
@function localtime
@int t seconds since epoch
@treturn[1] PosixTm broken-down time, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see localtime(3)
@see mktime
*/
static int
Plocaltime(lua_State *L)
{
	struct tm t;
	time_t epoch = checklong(L, 1);
	checknargs(L, 1);
	if (localtime_r(&epoch, &t) == NULL)
		return pusherror(L, "localtime");
	return pushtm(L, &t);
}


/***
Convert a broken-down localtime table into an epoch time.
@function mktime
@tparam PosixTm broken-down localtime
@treturn int seconds since epoch
@see mktime(3)
@see localtime
*/
static int
Pmktime(lua_State *L)
{
	struct tm t;
	time_t epoch;
	checknargs(L, 1);
	totm(L, 1, &t);
	if ((epoch = mktime(&t)) < 0)
		return 0;
	return pushintegerresult(epoch);
}


/***
Sleep with nanosecond precision.
@function nanosleep
@tparam PosixTimespec requested sleep time
@treturn[1] int `0` if requested time has elapsed, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@treturn[2] PosixTimespec unslept time remaining, if interrupted
@see nanosleep(2)
@see posix.unistd.sleep
*/
static int
Pnanosleep(lua_State *L)
{
	struct timespec req;
	struct timespec rem;
	int r;

	totimespec(L, 1, &req);
	checknargs(L, 1);
	r = pushresult (L, nanosleep(&req, &rem), "nanosleep");
	if (r == 3 && errno == EINTR)
		r = r + pushtimespec (L, &rem);
	return r;
}


/***
Return a time string according to *format*.
Note that if your host POSIX library provides a `strftime` that
assumes the local timezone, %z will always print the local UTC
offset, regardless of the `tm_gmoffset` field value passed in.
@function strftime
@string format specifier with `%` place-holders
@tparam PosixTm tm broken-down local time
@treturn string *format* with place-holders plugged with *tm* values
@see strftime(3)
*/
static int
Pstrftime(lua_State *L)
{
	char tmp[256];
	const char *fmt = luaL_checkstring(L, 1);
	struct tm t;

	totm(L, 2, &t);
	checknargs(L, 2);

	strftime(tmp, sizeof(tmp), fmt, &t);
	return pushstringresult(tmp);
}


/***
Parse a date string.
@function strptime
@string s
@string format same as for `strftime`
@usage posix.strptime('20','%d').monthday == 20
@treturn[1] PosixTm broken-down local time
@treturn[1] int next index of first character not parsed as part of the date, if successful
@return[2] nil
@see strptime(3)
*/
static int
Pstrptime(lua_State *L)
{
	struct tm t;
	const char *s = luaL_checkstring(L, 1);
	const char *fmt = luaL_checkstring(L, 2);
	char *r;
	checknargs(L, 2);

	memset(&t, 0, sizeof(struct tm));
	r = strptime(s, fmt, &t);
	if (r)
	{
		pushtm(L, &t);
		lua_pushinteger(L, r - s + 1);
		return 2;
	} else
		return 0;
}


/***
Get current time.
@function time
@treturn[1] int time in seconds since epoch, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see time(2)
*/
static int
Ptime(lua_State *L)
{
	time_t t = time(NULL);
	checknargs(L, 0);
	if ((time_t) -1 == t)
		return pusherror(L, "time");
	lua_pushinteger(L, t);
	return 1;
}


static const luaL_Reg posix_time_fns[] =
{
#if defined _POSIX_TIMERS && _POSIX_TIMERS != -1
	LPOSIX_FUNC( Pclock_getres	),
	LPOSIX_FUNC( Pclock_gettime	),
#endif
	LPOSIX_FUNC( Pgmtime		),
	LPOSIX_FUNC( Plocaltime		),
	LPOSIX_FUNC( Pmktime		),
	LPOSIX_FUNC( Pnanosleep		),
	LPOSIX_FUNC( Pstrftime		),
	LPOSIX_FUNC( Pstrptime		),
	LPOSIX_FUNC( Ptime		),
	{NULL, NULL}
};


/***
Constants.
@section constants
*/

/***
Standard constants.
Any constants not available in the underlying system will be `nil` valued.
@table posix.time
@int CLOCK_MONOTONIC the identifier for the system-wide monotonic clock
@int CLOCK_PROCESS_CPUTIME_ID the identifier for the current process CPU-time clock
@int CLOCK_REALTIME the identifier for the system-wide realtime clock
@int CLOCK_THREAD_CPUTIME_ID the identifier for the current thread CPU-time clock
@usage
  -- Print posix.time constants supported on this host.
  for name, value in pairs (require "posix.time") do
    if type (value) == "number" then
      print (name, value)
     end
  end
*/

LUALIB_API int
luaopen_posix_time(lua_State *L)
{
	luaL_newlib(L, posix_time_fns);
	lua_pushstring(L, LPOSIX_VERSION_STRING("time"));
	lua_setfield(L, -2, "version");

#if defined CLOCK_MONOTONIC
	LPOSIX_CONST( CLOCK_MONOTONIC		);
#endif
#if defined CLOCK_PROCESS_CPUTIME_ID
	LPOSIX_CONST( CLOCK_PROCESS_CPUTIME_ID	);
#endif
#if defined CLOCK_REALTIME
	LPOSIX_CONST( CLOCK_REALTIME		);
#endif
#if defined CLOCK_THREAD_CPUTIME_ID
	LPOSIX_CONST( CLOCK_THREAD_CPUTIME_ID	);
#endif

	return 1;
}