From ca412214cbbeb8f40e9abea534e6171044cc0a57 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy <roberto@inf.puc-rio.br> Date: Fri, 9 Feb 1996 17:02:30 -0200 Subject: new function "date", replaces old "date" and "time". --- iolib.c | 58 ++++++++++++++++++++++------------------------------------ manual.tex | 20 ++++++++++---------- 2 files changed, 32 insertions(+), 46 deletions(-) diff --git a/iolib.c b/iolib.c index d014ec4f..a454084e 100644 --- a/iolib.c +++ b/iolib.c @@ -3,7 +3,7 @@ ** Input/output library to LUA */ -char *rcs_iolib="$Id: iolib.c,v 1.32 1996/01/29 16:40:09 roberto Exp roberto $"; +char *rcs_iolib="$Id: iolib.c,v 1.33 1996/02/05 21:32:19 roberto Exp roberto $"; #include <stdio.h> #include <ctype.h> @@ -12,6 +12,7 @@ char *rcs_iolib="$Id: iolib.c,v 1.32 1996/01/29 16:40:09 roberto Exp roberto $"; #include <string.h> #include <time.h> #include <stdlib.h> +#include <errno.h> #include "lua.h" #include "luadebug.h" @@ -490,9 +491,14 @@ static void io_remove (void) lua_pushnil(); } +static void io_errorno (void) +{ +/* lua_pushstring(strerror(errno));*/ +} + /* -** To get a environment variables +** To get a environment variable */ static void io_getenv (void) { @@ -502,42 +508,23 @@ static void io_getenv (void) } /* -** Return time: hour, min, sec -*/ -static void io_time (void) -{ - time_t t; - struct tm *s; - - time(&t); - s = localtime(&t); - lua_pushnumber(s->tm_hour); - lua_pushnumber(s->tm_min); - lua_pushnumber(s->tm_sec); -} - -/* -** Return date: dd, mm, yyyy, weekday +** Return user formatted time stamp */ static void io_date (void) { time_t t; - struct tm *s; - - time(&t); - s = localtime(&t); - lua_pushnumber(s->tm_mday); - lua_pushnumber(s->tm_mon+1); - lua_pushnumber(s->tm_year+1900); - lua_pushnumber(s->tm_wday+1); -} - -/* -** Beep -*/ -static void io_beep (void) -{ - printf("\a"); + struct tm *tm; + char *s; + char b[BUFSIZ]; + if (lua_getparam(1) == LUA_NOOBJECT) + s = "%c"; + else + s = lua_check_string(1, "date"); + time(&t); tm = localtime(&t); + if (strftime(b,sizeof(b),s,tm)) + lua_pushstring(b); + else + lua_error("`date' format too long"); } /* @@ -628,10 +615,9 @@ void iolib_open (void) lua_register ("write", io_write); lua_register ("execute", io_execute); lua_register ("remove", io_remove); + lua_register ("ioerror", io_errorno); lua_register ("getenv", io_getenv); - lua_register ("time", io_time); lua_register ("date", io_date); - lua_register ("beep", io_beep); lua_register ("exit", io_exit); lua_register ("debug", io_debug); lua_register ("print_stack", errorfb); diff --git a/manual.tex b/manual.tex index b3e37013..c83104f0 100644 --- a/manual.tex +++ b/manual.tex @@ -1,4 +1,4 @@ -% $Id: manual.tex,v 1.7 1996/02/09 16:37:58 roberto Exp roberto $ +% $Id: manual.tex,v 1.8 1996/02/09 17:21:27 roberto Exp roberto $ \documentstyle[A4,11pt,bnf]{article} @@ -32,7 +32,7 @@ Waldemar Celes Filho Departamento de Inform\'atica --- PUC-Rio } -\date{\small \verb$Date: 1996/02/09 16:37:58 $} +\date{\small \verb$Date: 1996/02/09 17:21:27 $} \maketitle @@ -1440,17 +1440,17 @@ and strings with \verb'%s'. For better format facilities, the function \verb'format' should be used (\see{format}). -\subsubsection*{{\tt date ()}}\Deffunc{date} +\subsubsection*{{\tt date ([format])}}\Deffunc{date} -This function returns 4 values: -the current day of the month, -the month ([1-12]), the current year, -and the day of the week (1 = Sunday, 7 = Saturday). +This function returns a string containing date and time +formatted according to the given string \verb'format', +following the same rules of the ANSI C function \verb'strftime'. +When called without arguments, +it returns a reasonable date and time representation. -\subsubsection*{{\tt time ()}}\Deffunc{time} +This function replaces functions \verb'date' and \verb'time' from +previous Lua versions. -This function returns the current time through 3 values: -hours ([0-23]), minutes, and seconds. % \subsubsection*{{\tt debug ()}} % This function, when called, repeatedly presents a prompt \verb'lua_debug> ' -- cgit v1.2.3-55-g6feb