MCPcopy Create free account
hub / github.com/DFHack/dfhack / os_date

Function os_date

depends/lua/src/loslib.c:283–322  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

281
282
283static int os_date (lua_State *L) {
284 size_t slen;
285 const char *s = luaL_optlstring(L, 1, "%c", &slen);
286 time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
287 const char *se = s + slen; /* 's' end */
288 struct tm tmr, *stm;
289 if (*s == '!') { /* UTC? */
290 stm = l_gmtime(&t, &tmr);
291 s++; /* skip '!' */
292 }
293 else
294 stm = l_localtime(&t, &tmr);
295 if (stm == NULL) /* invalid date? */
296 return luaL_error(L,
297 "time result cannot be represented in this installation");
298 if (strcmp(s, "*t") == 0) {
299 lua_createtable(L, 0, 9); /* 9 = number of fields */
300 setallfields(L, stm);
301 }
302 else {
303 char cc[4]; /* buffer for individual conversion specifiers */
304 luaL_Buffer b;
305 cc[0] = '%';
306 luaL_buffinit(L, &b);
307 while (s < se) {
308 if (*s != '%') /* not a conversion specifier? */
309 luaL_addchar(&b, *s++);
310 else {
311 size_t reslen;
312 char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
313 s++; /* skip '%' */
314 s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
315 reslen = strftime(buff, SIZETIMEFMT, cc, stm);
316 luaL_addsize(&b, reslen);
317 }
318 }
319 luaL_pushresult(&b);
320 }
321 return 1;
322}
323
324
325static int os_time (lua_State *L) {

Callers

nothing calls this directly

Calls 8

luaL_optlstringFunction · 0.85
luaL_errorFunction · 0.85
lua_createtableFunction · 0.85
setallfieldsFunction · 0.85
luaL_buffinitFunction · 0.85
luaL_prepbuffsizeFunction · 0.85
checkoptionFunction · 0.85
luaL_pushresultFunction · 0.85

Tested by

no test coverage detected