可以继续尝试更大的缓冲区,直到代码成功(或认为这太多了)。下面使用 VLA(不是 C++),偷偷地避免“尝试动态分配额外的内存” - 眨眼眨眼。
简单地分配一个大缓冲区,比如char buf[J_STRFTIME_MAX],对于实际编码应该足够了。 @Michaël Roy 并避免使用迭代方法。
#include <stdio.h>
#include <time.h>
#define J_STRFTIME_MAX 100
size_t j_strftime(char * s, size_t maxsize, const char * fmt, const struct tm * t) {
size_t sz = strftime(s, maxsize, fmt, t);
if (sz) {
return sz;
}
size_t new_size = maxsize ? maxsize : 1;
do {
new_size *= 2;
char new_s[new_size];
sz = strftime(new_s, sizeof new_s, fmt, t);
if (sz) {
s[0] = 0;
// strncat(s, new_s, maxsize);
strncat(s, new_s, maxsize - 1);
return strlen(new_s);
}
} while (sz < J_STRFTIME_MAX/2);
return 0;
}
int main() {
time_t now;
time(&now);
struct tm tm = *gmtime(&now);
for (size_t i = 1; i < 30; i += 3) {
char s[i];
size_t sz = j_strftime(s, sizeof s, "%c", &tm);
printf("%2zu %2zu <%s>\n", i, sz, s);
}
}
输出
1 24 <T>
4 24 <Thu >
7 24 <Thu Jul>
10 24 <Thu Jul 6>
13 24 <Thu Jul 6 14>
16 24 <Thu Jul 6 14:45>
19 24 <Thu Jul 6 14:45:00>
22 24 <Thu Jul 6 14:45:00 20>
25 24 <Thu Jul 6 14:45:00 2017>
28 24 <Thu Jul 6 14:45:00 2017>
非迭代
size_t j_strftime2(char * s, size_t maxsize, const char * fmt, const struct tm * t) {
size_t sz = strftime(s, maxsize, fmt, t);
if (sz == 0) {
char new_s[J_STRFTIME_MAX];
sz = strftime(new_s, sizeof new_s, fmt, t);
if (sz == 0) {
return 0; // Too too big
}
s[0] = 0;
// strncat(s, new_s, maxsize);
strncat(s, new_s, maxsize - 1);
}
return sz;
}
[编辑] 代码已更正。