Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional parameter for *gettimestr #2388

Merged
merged 3 commits into from Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions doc/script_commands.txt
Expand Up @@ -3531,20 +3531,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'.

Expand Down
32 changes: 22 additions & 10 deletions src/map/script.c
Expand Up @@ -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;
}

Expand Down Expand Up @@ -25378,7 +25390,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?"),
Expand Down