I want to get the current time in a human reading format, I choose strftime
to do this.
The function looks like:
std::string crypto::tools::utc_time_str() {
time_t nowtime = time(0);
struct tm mytime;
gmtime_r(&nowtime, &mytime);
char buf[32];
strftime(buf, 32, "%Y-%m-%dT%H:%M:%S", &mytime);
return buf;
}
It's good, i can get string like this:
2024-08-29T07:25:44
Another requirements is to get it url-encoded. In this case, replace ":
" to "%3A
" is all I need.
I assume it will be easy.
but seems it can't be done in strftime
.
I try the following, all failed:
strftime(buf, 32, "%Y-%m-%dT%H%3A%M%3A%S", &mytime);
2024-08-29T07Thursday28Thursday
strftime(buf, 32, "%Y-%m-%dT%H%\3A%M%\3A%S", &mytime);
2024-08-29T07%A29%A39
I want this function as fast as possible, so, allocate a new string is not a good choice for me.
Can strftime
do this?
%
is a reserved char and %3A
is taken as an invalid sequence. You wanted %%3A
.
%%
Replaced by%
.