writeout: add %time{}

Output the current UTC time using strftime format. %f is an extra curl
specific flag to output the microsecond fraction of the current second.

Verified by test 1981

Closes #18119
This commit is contained in:
Daniel Stenberg 2025-07-31 16:41:36 +02:00
parent 5b80b4c012
commit fadc487567
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
5 changed files with 340 additions and 3 deletions

View file

@ -539,6 +539,81 @@ matchvar(const void *m1, const void *m2)
#define MAX_WRITEOUT_NAME_LENGTH 24
/* return the position after %time{} */
static const char *outtime(const char *ptr, /* %time{ ... */
FILE *stream)
{
const char *end;
ptr += 6;
end = strchr(ptr, '}');
if(end) {
struct tm *utc;
struct dynbuf format;
char output[256]; /* max output time length */
#ifdef HAVE_GETTIMEOFDAY
struct timeval cnow;
#else
struct curltime cnow;
#endif
time_t secs;
unsigned int usecs;
size_t i;
size_t vlen;
CURLcode result = CURLE_OK;
#ifdef HAVE_GETTIMEOFDAY
gettimeofday(&cnow, NULL);
#else
cnow.tv_sec = time(NULL);
cnow.tv_usec = 0;
#endif
secs = cnow.tv_sec;
usecs = (unsigned int)cnow.tv_usec;
#ifdef DEBUGBUILD
{
const char *timestr = getenv("CURL_TIME");
if(timestr) {
curl_off_t val;
curlx_str_number(&timestr, &val, TIME_T_MAX);
secs = (time_t)val;
usecs = (unsigned int)(val % 1000000);
}
}
#endif
vlen = end - ptr;
curlx_dyn_init(&format, 1024);
/* insert sub-seconds for %f */
/* insert +0000 for %z because it is otherwise not portable */
/* insert UTC for %Z because it is otherwise not portable */
for(i = 0; !result && i < vlen; i++) {
if((i < vlen - 1) && ptr[i] == '%' &&
((ptr[i + 1] == 'f') || ((ptr[i + 1] | 0x20) == 'z'))) {
if(ptr[i + 1] == 'f')
result = curlx_dyn_addf(&format, "%06u", usecs);
else if(ptr[i + 1] == 'Z')
result = curlx_dyn_addn(&format, "UTC", 3);
else
result = curlx_dyn_addn(&format, "+0000", 5);
i++;
}
else
result = curlx_dyn_addn(&format, &ptr[i], 1);
}
if(!result) {
/* !checksrc! disable BANNEDFUNC 1 */
utc = gmtime(&secs);
strftime(output, sizeof(output), curlx_dyn_ptr(&format), utc);
fputs(output, stream);
curlx_dyn_free(&format);
}
ptr = end + 1;
}
else
fputs("%time{", stream);
return ptr;
}
void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
CURLcode per_result)
{
@ -640,6 +715,9 @@ void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
else
fputs("%header{", stream);
}
else if(!strncmp("time{", &ptr[1], 5)) {
ptr = outtime(ptr, stream);
}
else if(!strncmp("output{", &ptr[1], 7)) {
bool append = FALSE;
ptr += 8;