| 122 | |
| 123 | |
| 124 | static int os_date (lua_State *L) { |
| 125 | const char *s = luaL_optstring(L, 1, "%c"); |
| 126 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); |
| 127 | struct tm *stm; |
| 128 | if (*s == '!') { /* UTC? */ |
| 129 | stm = gmtime(&t); |
| 130 | s++; /* skip `!' */ |
| 131 | } |
| 132 | else |
| 133 | stm = localtime(&t); |
| 134 | if (stm == NULL) /* invalid date? */ |
| 135 | lua_pushnil(L); |
| 136 | else if (strcmp(s, "*t") == 0) { |
| 137 | lua_createtable(L, 0, 9); /* 9 = number of fields */ |
| 138 | setfield(L, "sec", stm->tm_sec); |
| 139 | setfield(L, "min", stm->tm_min); |
| 140 | setfield(L, "hour", stm->tm_hour); |
| 141 | setfield(L, "day", stm->tm_mday); |
| 142 | setfield(L, "month", stm->tm_mon+1); |
| 143 | setfield(L, "year", stm->tm_year+1900); |
| 144 | setfield(L, "wday", stm->tm_wday+1); |
| 145 | setfield(L, "yday", stm->tm_yday+1); |
| 146 | setboolfield(L, "isdst", stm->tm_isdst); |
| 147 | } |
| 148 | else { |
| 149 | char cc[3]; |
| 150 | luaL_Buffer b; |
| 151 | cc[0] = '%'; cc[2] = '\0'; |
| 152 | luaL_buffinit(L, &b); |
| 153 | for (; *s; s++) { |
| 154 | if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ |
| 155 | luaL_addchar(&b, *s); |
| 156 | else { |
| 157 | size_t reslen; |
| 158 | char buff[200]; /* should be big enough for any conversion result */ |
| 159 | cc[1] = *(++s); |
| 160 | reslen = strftime(buff, sizeof(buff), cc, stm); |
| 161 | luaL_addlstring(&b, buff, reslen); |
| 162 | } |
| 163 | } |
| 164 | luaL_pushresult(&b); |
| 165 | } |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | static int os_time (lua_State *L) { |
nothing calls this directly
no test coverage detected