aboutsummaryrefslogtreecommitdiff
path: root/src/lib_os.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib_os.c')
-rw-r--r--src/lib_os.c37
1 files changed, 21 insertions, 16 deletions
diff --git a/src/lib_os.c b/src/lib_os.c
index 0a784129..de2bc623 100644
--- a/src/lib_os.c
+++ b/src/lib_os.c
@@ -18,7 +18,10 @@
18#include "lualib.h" 18#include "lualib.h"
19 19
20#include "lj_obj.h" 20#include "lj_obj.h"
21#include "lj_gc.h"
21#include "lj_err.h" 22#include "lj_err.h"
23#include "lj_buf.h"
24#include "lj_str.h"
22#include "lj_lib.h" 25#include "lj_lib.h"
23 26
24#if LJ_TARGET_POSIX 27#if LJ_TARGET_POSIX
@@ -185,7 +188,7 @@ LJLIB_CF(os_date)
185#endif 188#endif
186 } 189 }
187 if (stm == NULL) { /* Invalid date? */ 190 if (stm == NULL) { /* Invalid date? */
188 setnilV(L->top-1); 191 setnilV(L->top++);
189 } else if (strcmp(s, "*t") == 0) { 192 } else if (strcmp(s, "*t") == 0) {
190 lua_createtable(L, 0, 9); /* 9 = number of fields */ 193 lua_createtable(L, 0, 9); /* 9 = number of fields */
191 setfield(L, "sec", stm->tm_sec); 194 setfield(L, "sec", stm->tm_sec);
@@ -197,23 +200,25 @@ LJLIB_CF(os_date)
197 setfield(L, "wday", stm->tm_wday+1); 200 setfield(L, "wday", stm->tm_wday+1);
198 setfield(L, "yday", stm->tm_yday+1); 201 setfield(L, "yday", stm->tm_yday+1);
199 setboolfield(L, "isdst", stm->tm_isdst); 202 setboolfield(L, "isdst", stm->tm_isdst);
200 } else { 203 } else if (*s) {
201 char cc[3]; 204 SBuf *sb = &G(L)->tmpbuf;
202 luaL_Buffer b; 205 MSize sz = 0;
203 cc[0] = '%'; cc[2] = '\0'; 206 const char *q;
204 luaL_buffinit(L, &b); 207 for (q = s; *q; q++)
205 for (; *s; s++) { 208 sz += (*q == '%') ? 30 : 1; /* Overflow doesn't matter. */
206 if (*s != '%' || *(s + 1) == '\0') { /* No conversion specifier? */ 209 setsbufL(sb, L);
207 luaL_addchar(&b, *s); 210 for (;;) {
208 } else { 211 char *buf = lj_buf_need(sb, sz);
209 size_t reslen; 212 size_t len = strftime(buf, sbufsz(sb), s, stm);
210 char buff[200]; /* Should be big enough for any conversion result. */ 213 if (len) {
211 cc[1] = *(++s); 214 setstrV(L, L->top++, lj_str_new(L, buf, len));
212 reslen = strftime(buff, sizeof(buff), cc, stm); 215 lj_gc_check(L);
213 luaL_addlstring(&b, buff, reslen); 216 break;
214 } 217 }
218 sz += (sz|1);
215 } 219 }
216 luaL_pushresult(&b); 220 } else {
221 setstrV(L, L->top++, &G(L)->strempty);
217 } 222 }
218 return 1; 223 return 1;
219} 224}