diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-12-18 11:42:19 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-12-18 11:42:19 -0200 |
commit | af97be026b792308f972870019dcaaf4c945e071 (patch) | |
tree | 1f64bed9481fe91ce46e701cf589b72b820f7b18 /liolib.c | |
parent | cce8ebd30680947dbf52051752c03d0e0d7cdb44 (diff) | |
download | lua-af97be026b792308f972870019dcaaf4c945e071.tar.gz lua-af97be026b792308f972870019dcaaf4c945e071.tar.bz2 lua-af97be026b792308f972870019dcaaf4c945e071.zip |
new function for time and date
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 117 |
1 files changed, 104 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.92 2000/11/23 13:49:35 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.93 2000/12/04 18:33:40 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -230,12 +230,6 @@ static int io_appendto (lua_State *L) { | |||
230 | */ | 230 | */ |
231 | 231 | ||
232 | 232 | ||
233 | |||
234 | |||
235 | #define read_pattern(L, f, p) (lua_error(L, "read patterns are deprecated"), 0) | ||
236 | |||
237 | |||
238 | |||
239 | static int read_number (lua_State *L, FILE *f) { | 233 | static int read_number (lua_State *L, FILE *f) { |
240 | double d; | 234 | double d; |
241 | if (fscanf(f, "%lf", &d) == 1) { | 235 | if (fscanf(f, "%lf", &d) == 1) { |
@@ -481,20 +475,115 @@ static int io_clock (lua_State *L) { | |||
481 | } | 475 | } |
482 | 476 | ||
483 | 477 | ||
478 | /* | ||
479 | ** {====================================================== | ||
480 | ** Time/Date operations | ||
481 | ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, | ||
482 | ** wday=%w+1, yday=%j, isdst=? } | ||
483 | ** ======================================================= | ||
484 | */ | ||
485 | |||
486 | static void setfield (lua_State *L, const char *key, int value) { | ||
487 | lua_pushstring(L, key); | ||
488 | lua_pushnumber(L, value); | ||
489 | lua_rawset(L, -3); | ||
490 | } | ||
491 | |||
492 | |||
493 | static int getfield (lua_State *L, const char *key, int d) { | ||
494 | int res; | ||
495 | lua_pushstring(L, key); | ||
496 | lua_rawget(L, -2); | ||
497 | if (lua_isnumber(L, -1)) | ||
498 | res = lua_tonumber(L, -1); | ||
499 | else { | ||
500 | if (d == -2) | ||
501 | luaL_verror(L, "field `%.20s' missing in date table", key); | ||
502 | res = d; | ||
503 | } | ||
504 | lua_pop(L, 1); | ||
505 | return res; | ||
506 | } | ||
507 | |||
508 | |||
509 | static void tm2table (lua_State *L, struct tm *stm) { | ||
510 | setfield(L, "sec", stm->tm_sec); | ||
511 | setfield(L, "min", stm->tm_min); | ||
512 | setfield(L, "hour", stm->tm_hour); | ||
513 | setfield(L, "day", stm->tm_mday); | ||
514 | setfield(L, "month", stm->tm_mon+1); | ||
515 | setfield(L, "year", stm->tm_year+1900); | ||
516 | setfield(L, "wday", stm->tm_wday+1); | ||
517 | setfield(L, "yday", stm->tm_yday+1); | ||
518 | setfield(L, "isdst", stm->tm_isdst); | ||
519 | } | ||
520 | |||
521 | |||
484 | static int io_date (lua_State *L) { | 522 | static int io_date (lua_State *L) { |
485 | char b[256]; | ||
486 | const char *s = luaL_opt_string(L, 1, "%c"); | 523 | const char *s = luaL_opt_string(L, 1, "%c"); |
524 | time_t t = (time_t)luaL_opt_number(L, 2, -1); | ||
487 | struct tm *stm; | 525 | struct tm *stm; |
488 | time_t t; | 526 | if (t == (time_t)-1) /* no time given? */ |
489 | time(&t); stm = localtime(&t); | 527 | t = time(NULL); /* use current time */ |
490 | if (strftime(b, sizeof(b), s, stm)) | 528 | if (*s == '!') { /* UTC? */ |
491 | lua_pushstring(L, b); | 529 | stm = gmtime(&t); |
530 | s++; /* skip `!' */ | ||
531 | } | ||
492 | else | 532 | else |
493 | lua_error(L, "invalid `date' format"); | 533 | stm = localtime(&t); |
534 | if (stm == NULL) /* invalid date? */ | ||
535 | lua_pushnil(L); | ||
536 | else if (strcmp(s, "*t") == 0) { | ||
537 | lua_newtable(L); | ||
538 | tm2table(L, stm); | ||
539 | } | ||
540 | else { | ||
541 | char b[256]; | ||
542 | if (strftime(b, sizeof(b), s, stm)) | ||
543 | lua_pushstring(L, b); | ||
544 | else | ||
545 | lua_error(L, "invalid `date' format"); | ||
546 | } | ||
494 | return 1; | 547 | return 1; |
495 | } | 548 | } |
496 | 549 | ||
497 | 550 | ||
551 | static int io_time (lua_State *L) { | ||
552 | if (lua_isnull(L, 1)) /* called without args? */ | ||
553 | lua_pushnumber(L, time(NULL)); /* return current time */ | ||
554 | else { | ||
555 | time_t t; | ||
556 | struct tm ts; | ||
557 | luaL_checktype(L, 1, LUA_TTABLE); | ||
558 | lua_settop(L, 1); /* make sure table is at the top */ | ||
559 | ts.tm_sec = getfield(L, "sec", 0); | ||
560 | ts.tm_min = getfield(L, "min", 0); | ||
561 | ts.tm_hour = getfield(L, "hour", 12); | ||
562 | ts.tm_mday = getfield(L, "day", -2); | ||
563 | ts.tm_mon = getfield(L, "month", -2)-1; | ||
564 | ts.tm_year = getfield(L, "year", -2)-1900; | ||
565 | ts.tm_isdst = getfield(L, "isdst", -1); | ||
566 | t = mktime(&ts); | ||
567 | if (t == (time_t)-1) | ||
568 | lua_pushnil(L); | ||
569 | else { | ||
570 | tm2table(L, &ts); /* copy back updated values */ | ||
571 | lua_pushnumber(L, t); | ||
572 | } | ||
573 | } | ||
574 | return 1; | ||
575 | } | ||
576 | |||
577 | |||
578 | static int io_difftime (lua_State *L) { | ||
579 | lua_pushnumber(L, difftime((time_t)luaL_check_number(L, 1), | ||
580 | (time_t)luaL_opt_number(L, 2, 0))); | ||
581 | return 1; | ||
582 | } | ||
583 | |||
584 | /* }====================================================== */ | ||
585 | |||
586 | |||
498 | static int io_setloc (lua_State *L) { | 587 | static int io_setloc (lua_State *L) { |
499 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, | 588 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, |
500 | LC_NUMERIC, LC_TIME}; | 589 | LC_NUMERIC, LC_TIME}; |
@@ -607,12 +696,14 @@ static const struct luaL_reg iolib[] = { | |||
607 | {"clock", io_clock}, | 696 | {"clock", io_clock}, |
608 | {"date", io_date}, | 697 | {"date", io_date}, |
609 | {"debug", io_debug}, | 698 | {"debug", io_debug}, |
699 | {"difftime", io_difftime}, | ||
610 | {"execute", io_execute}, | 700 | {"execute", io_execute}, |
611 | {"exit", io_exit}, | 701 | {"exit", io_exit}, |
612 | {"getenv", io_getenv}, | 702 | {"getenv", io_getenv}, |
613 | {"remove", io_remove}, | 703 | {"remove", io_remove}, |
614 | {"rename", io_rename}, | 704 | {"rename", io_rename}, |
615 | {"setlocale", io_setloc}, | 705 | {"setlocale", io_setloc}, |
706 | {"time", io_time}, | ||
616 | {"tmpname", io_tmpname} | 707 | {"tmpname", io_tmpname} |
617 | }; | 708 | }; |
618 | 709 | ||