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