请问如何将 struct timeval
格式化为人类可读的格式,例如 "2010-01-01 15:35:10.0001"?
由于在strftime
()中处理的struct tm
中不存在微秒部分,因此您需要手动添加。以下是一个片段:
struct timeval tv;
time_t nowtime;
struct tm *nowtm;
char tmbuf[64], buf[64];
gettimeofday(&tv, NULL);
nowtime = tv.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
snprintf(buf, sizeof buf, "%s.%06ld", tmbuf, tv.tv_usec);
注意我们如何使用显式精度06
来获得填充了零的微秒字段。由于微秒从0到999,999,它必须始终填充为6个数字。我们不想将57微秒误传为570000(比较“1.57”和“1.000057”)。
",%s.%06ld"
。祝好! - lrleonsnprintf(buf, sizeof buf, "%s.%06ld", tmbuf, tv.tv_usec)
会产生编译警告/错误,如下所示:'%06ld' 指令输出可能被截断,在大小为0到63之间的区域中写入6到20个字节[-Werror=format-truncation=]- leiyc
使用localtime
和strftime
将tv_sec
转换,然后添加tv_usec
部分。
将之前的回答和评论结合起来,将格式更改为RFC3339兼容,并检查所有错误条件,您会得到以下结果:
#include <stdio.h>
#include <sys/time.h>
ssize_t format_timeval(struct timeval *tv, char *buf, size_t sz)
{
ssize_t written = -1;
struct tm *gm = gmtime(&tv->tv_sec);
if (gm)
{
written = (ssize_t)strftime(buf, sz, "%Y-%m-%dT%H:%M:%S", gm);
if ((written > 0) && ((size_t)written < sz))
{
int w = snprintf(buf+written, sz-(size_t)written, ".%06dZ", tv->tv_usec);
written = (w > 0) ? written + w : -1;
}
}
return written;
}
int main() {
struct timeval tv;
char buf[28];
if (gettimeofday(&tv, NULL) != 0) {
perror("gettimeofday");
return 1;
}
if (format_timeval(&tv, buf, sizeof(buf)) > 0) {
printf("%s\n", buf);
// sample output:
// 2015-05-09T04:18:42.514551Z
}
return 0;
}
ctime((const time_t *) &timeval.ts.tv_sec)
我想您正在寻找此代码,仅供参考。
ctime()
和asctime()
返回的值格式(例如“Wed Jun 30 21:49:08 1993\n”)与OP的要求不匹配,因为它们不包括小数秒,并且甚至没有最后一秒,因此没有简单的方法在之后附加小数秒。 - John Hascall这是我使用的:
#include <time.h>
#include <string.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#define gmtime_r(ptime,ptm) (gmtime_s((ptm),(ptime)), (ptm))
#else
#include <sys/time.h>
#endif
#define ISO8601_LEN (sizeof "1970-01-01T23:59:59.123456Z")
char *timeval_to_str(char iso8601[restrict static ISO8601_LEN], unsigned precision, const struct timeval * restrict tv) {
struct tm tm;
if (!gmtime_r(&tv->tv_sec, &tm))
return memcpy(iso8601, "Error: Year overflow", sizeof "Error: Year overflow");
tm.tm_year %= 10*1000;
char *frac = iso8601 + strftime(iso8601, sizeof "1970-01-01T23:59:59.", "%Y-%m-%dT%H:%M:%SZ", &tm);
if (precision) {
unsigned long usecs = tv->tv_usec;
for (int i = precision; i < 6; i++) usecs /= 10;
char *spaces = frac + sprintf(frac - 1, ".%-*luZ", precision, usecs) - 3;
if (spaces > frac) while (*spaces == ' ') *spaces-- = '0';
}
return iso8601;
}
precision
指定了秒的小数部分的宽度。代码是 y10k- 和 yINT_MAX
- 证明的。
使用localtime_s而不是localtime来转换tv_sec,因为如果您正在编写全局函数,则可能会导致一些问题。如果您的功能可能在多线程解决方案中起作用,请考虑使用localtime_r。
您可以使用strftime函数将日期和时间转换为字符串。