Adjusted script command gettimestr (#2329)

* Fixes #2328.
* Added an optional parameter to pass in a Unix tick value to convert to a readable format.
Thanks to @hendra814 and @Lemongrass3110!
This commit is contained in:
Aleos 2017-08-14 13:51:18 -04:00 committed by GitHub
parent 74d809d139
commit 7554b53d68
2 changed files with 26 additions and 12 deletions

View File

@ -3019,10 +3019,10 @@ It will only return numbers. If another type is supplied -1 will be returned.
--------------------------------------- ---------------------------------------
*gettimestr(<format string>,<max length>) *gettimestr(<"time format">,<max length>{,<time_tick>})
This function will return a string containing time data as specified by the This function will return a string containing time data as specified by the
format string. time format.
This uses the C function 'strfmtime', which obeys special format characters. For This uses the C function 'strfmtime', which obeys special format characters. For
a full description see, for example, the description of 'strfmtime' at a full description see, for example, the description of 'strfmtime' at
@ -3034,7 +3034,11 @@ The example given in rAthena sample scripts works like this:
mes gettimestr("%Y-%m/%d %H:%M:%S",21); mes gettimestr("%Y-%m/%d %H:%M:%S",21);
This will print a full date and time like 'YYYY-MM/DD HH:MM:SS'. The example above will print the current date and time like 'YYYY-MM/DD HH:MM:SS'.
The following example will print the date and time when the player's VIP status
expires by the given <time_tick>:
mes gettimestr("%Y-%m/%d %H:%M:%S",21,vip_status(VIP_STATUS_EXPIRE));
--------------------------------------- ---------------------------------------

View File

@ -9945,19 +9945,29 @@ BUILDIN_FUNC(gettime)
return SCRIPT_CMD_SUCCESS; return SCRIPT_CMD_SUCCESS;
} }
/*========================================== /**
* GetTimeStr("TimeFMT", Length); * Returns the current server time or the provided time in a readable format.
*------------------------------------------*/ * gettimestr(<"time_format">,<max_length>{,<time_tick>});
*/
BUILDIN_FUNC(gettimestr) BUILDIN_FUNC(gettimestr)
{ {
char *tmpstr; char *tmpstr;
const char *fmtstr; const char *fmtstr;
int maxlen; int maxlen;
time_t now = time(NULL); time_t now;
fmtstr = script_getstr(st,2); fmtstr = script_getstr(st,2);
maxlen = script_getnum(st,3); maxlen = script_getnum(st,3);
if (script_hasdata(st, 4)) {
if (script_getnum(st, 4) < 0) {
ShowWarning("buildin_gettimestr: a positive value must be supplied to be valid.\n");
return SCRIPT_CMD_FAILURE;
} else
now = (time_t)script_getnum(st, 4);
} else
now = time(NULL);
tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char)); tmpstr = (char *)aMalloc((maxlen+1)*sizeof(char));
strftime(tmpstr,maxlen,fmtstr,localtime(&now)); strftime(tmpstr,maxlen,fmtstr,localtime(&now));
tmpstr[maxlen] = '\0'; tmpstr[maxlen] = '\0';
@ -23692,7 +23702,7 @@ struct script_function buildin_func[] = {
BUILDIN_DEF(savepoint,"sii???"), BUILDIN_DEF(savepoint,"sii???"),
BUILDIN_DEF(gettimetick,"i"), BUILDIN_DEF(gettimetick,"i"),
BUILDIN_DEF(gettime,"i"), BUILDIN_DEF(gettime,"i"),
BUILDIN_DEF(gettimestr,"si"), BUILDIN_DEF(gettimestr,"si?"),
BUILDIN_DEF(openstorage,""), BUILDIN_DEF(openstorage,""),
BUILDIN_DEF(guildopenstorage,""), BUILDIN_DEF(guildopenstorage,""),
BUILDIN_DEF(itemskill,"vi?"), BUILDIN_DEF(itemskill,"vi?"),