diff options
author | Haru <haru@dotalux.com> | 2019-04-07 20:27:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-07 20:27:22 +0200 |
commit | 73dacb74a8d2ed52590a6d6b8b937d9e14f77d8e (patch) | |
tree | 9aee364596b3d53f88dda7d465ec1fc7f84aeb9f | |
parent | ed5a86ed44b19d1e4c203f78c7bf2551442d2319 (diff) | |
parent | b478fb0a7373af0d80912641fbe08e9cbb49464e (diff) | |
download | hercules-73dacb74a8d2ed52590a6d6b8b937d9e14f77d8e.tar.gz hercules-73dacb74a8d2ed52590a6d6b8b937d9e14f77d8e.tar.bz2 hercules-73dacb74a8d2ed52590a6d6b8b937d9e14f77d8e.tar.xz hercules-73dacb74a8d2ed52590a6d6b8b937d9e14f77d8e.zip |
Merge pull request #2388 from AnnieRuru/61-gettimestr
Add optional parameter for *gettimestr
-rw-r--r-- | doc/script_commands.txt | 7 | ||||
-rw-r--r-- | src/map/script.c | 32 |
2 files changed, 26 insertions, 13 deletions
diff --git a/doc/script_commands.txt b/doc/script_commands.txt index 237b1b3d7..ba3627d6a 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -3532,20 +3532,21 @@ Examples : --------------------------------------- -*gettimestr(<format string>, <max length>) +*gettimestr(<format string>, <max length>{, <timestamp>}) This function will return a string containing time data as specified by the format string. -This uses the C function 'strfmtime', which obeys special format +This uses the C function 'strftime', which obeys special format characters. For a full description see, for example, the description of -'strfmtime' at http://www.delorie.com/gnu/docs/glibc/libc_437.html +'strftime' at http://www.delorie.com/gnu/docs/glibc/libc_437.html All the format characters given in there should properly work. Max length is the maximum length of a time string to generate. The example given in Hercules sample scripts works like this: mes(gettimestr("%Y-%m/%d %H:%M:%S", 21)); + mes(gettimestr("%Y-%m/%d %H:%M:%S", 21, getcalendartime(0, 0))); This will print a full date and time like 'YYYY-MM/DD HH:MM:SS'. diff --git a/src/map/script.c b/src/map/script.c index 348efae6f..3b99c16f0 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -10604,24 +10604,36 @@ static BUILDIN(gettime) return true; } -/*========================================== +/* * GetTimeStr("TimeFMT", Length); - *------------------------------------------*/ + */ static BUILDIN(gettimestr) { char *tmpstr; const char *fmtstr; int maxlen; - time_t now = time(NULL); + time_t now; + + fmtstr = script_getstr(st, 2); + maxlen = script_getnum(st, 3); - fmtstr=script_getstr(st,2); - maxlen=script_getnum(st,3); + if (script_hasdata(st, 4)) { + int timestamp = script_getnum(st, 4); + if (timestamp < 0) { + ShowWarning("buildin_gettimestr: UNIX timestamp must be in positive value.\n"); + return false; + } + + now = (time_t)timestamp; + } else { + now = time(NULL); + } - tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char)); - strftime(tmpstr,maxlen,fmtstr,localtime(&now)); - tmpstr[maxlen]='\0'; + tmpstr = (char *)aMalloc((maxlen +1)*sizeof(char)); + strftime(tmpstr, maxlen, fmtstr, localtime(&now)); + tmpstr[maxlen] = '\0'; - script_pushstr(st,tmpstr); + script_pushstr(st, tmpstr); return true; } @@ -25379,7 +25391,7 @@ static void script_parse_builtin(void) BUILDIN_DEF(savepoint,"sii"), BUILDIN_DEF(gettimetick,"i"), BUILDIN_DEF(gettime,"i"), - BUILDIN_DEF(gettimestr,"si"), + BUILDIN_DEF(gettimestr, "si?"), BUILDIN_DEF(openstorage,""), BUILDIN_DEF(guildopenstorage,""), BUILDIN_DEF(itemskill,"vi?"), |