MCPcopy Create free account
hub / github.com/RomanKubiak/ctrlr / os_date

Function os_date

Source/Misc/lua/src/lua.c:13558–13601  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13556
13557
13558static int os_date (lua_State *L) {
13559const char *s = luaL_optstring(L, 1, "%c");
13560time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
13561struct tm *stm;
13562if (*s == '!') { /* UTC? */
13563stm = gmtime(&t);
13564s++; /* skip `!' */
13565}
13566else
13567stm = localtime(&t);
13568if (stm == NULL) /* invalid date? */
13569lua_pushnil(L);
13570else if (strcmp(s, "*t") == 0) {
13571lua_createtable(L, 0, 9); /* 9 = number of fields */
13572setfield(L, "sec", stm->tm_sec);
13573setfield(L, "min", stm->tm_min);
13574setfield(L, "hour", stm->tm_hour);
13575setfield(L, "day", stm->tm_mday);
13576setfield(L, "month", stm->tm_mon+1);
13577setfield(L, "year", stm->tm_year+1900);
13578setfield(L, "wday", stm->tm_wday+1);
13579setfield(L, "yday", stm->tm_yday+1);
13580setboolfield(L, "isdst", stm->tm_isdst);
13581}
13582else {
13583char cc[3];
13584luaL_Buffer b;
13585cc[0] = '%'; cc[2] = '\0';
13586luaL_buffinit(L, &b);
13587for (; *s; s++) {
13588if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
13589luaL_addchar(&b, *s);
13590else {
13591size_t reslen;
13592char buff[200]; /* should be big enough for any conversion result */
13593cc[1] = *(++s);
13594reslen = strftime(buff, sizeof(buff), cc, stm);
13595luaL_addlstring(&b, buff, reslen);
13596}
13597}
13598luaL_pushresult(&b);
13599}
13600return 1;
13601}
13602
13603
13604static int os_time (lua_State *L) {

Callers

nothing calls this directly

Calls 7

lua_pushnilFunction · 0.85
lua_createtableFunction · 0.85
setfieldFunction · 0.85
setboolfieldFunction · 0.85
luaL_buffinitFunction · 0.85
luaL_addlstringFunction · 0.85
luaL_pushresultFunction · 0.85

Tested by

no test coverage detected