| 138 | |
| 139 | |
| 140 | static int os_date (lua_State *L) { |
| 141 | const char *s = luaL_optstring(L, 1, "%c"); |
| 142 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); |
| 143 | struct tm *stm; |
| 144 | struct tm mytime; |
| 145 | if (*s == '!') { /* UTC? */ |
| 146 | stm = gmtime_r(&t, &mytime); |
| 147 | s++; /* skip `!' */ |
| 148 | } |
| 149 | else |
| 150 | stm = localtime_r(&t, &mytime); |
| 151 | if (stm == NULL) /* invalid date? */ |
| 152 | lua_pushnil(L); |
| 153 | else if (strcmp(s, "*t") == 0) { |
| 154 | lua_createtable(L, 0, 9); /* 9 = number of fields */ |
| 155 | setfield(L, "sec", stm->tm_sec); |
| 156 | setfield(L, "min", stm->tm_min); |
| 157 | setfield(L, "hour", stm->tm_hour); |
| 158 | setfield(L, "day", stm->tm_mday); |
| 159 | setfield(L, "month", stm->tm_mon+1); |
| 160 | setfield(L, "year", stm->tm_year+1900); |
| 161 | setfield(L, "wday", stm->tm_wday+1); |
| 162 | setfield(L, "yday", stm->tm_yday+1); |
| 163 | setboolfield(L, "isdst", stm->tm_isdst); |
| 164 | } |
| 165 | else { |
| 166 | char cc[3]; |
| 167 | luaL_Buffer b; |
| 168 | cc[0] = '%'; cc[2] = '\0'; |
| 169 | luaL_buffinit(L, &b); |
| 170 | for (; *s; s++) { |
| 171 | if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ |
| 172 | luaL_addchar(&b, *s); |
| 173 | else { |
| 174 | size_t reslen; |
| 175 | char buff[200]; /* should be big enough for any conversion result */ |
| 176 | cc[1] = *(++s); |
| 177 | reslen = strftime(buff, sizeof(buff), cc, stm); |
| 178 | luaL_addlstring(&b, buff, reslen); |
| 179 | } |
| 180 | } |
| 181 | luaL_pushresult(&b); |
| 182 | } |
| 183 | return 1; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static int os_time (lua_State *L) { |
nothing calls this directly
no test coverage detected