Linux kernel linux内核中的人类可读时间戳

Linux kernel linux内核中的人类可读时间戳,linux-kernel,kernel,kernel-module,Linux Kernel,Kernel,Kernel Module,如何在linux内核中编写人类可读的时间戳?我认为,do_gettimeofday返回epoch,但我不想尝试将其转换为可读时间。我只需要一个类似于Hour:Min:Sec:Msec的格式。 谢谢虽然我不认为这有什么功能,但如果你不需要这一天,你可以很容易地完成。 struct timeval now; unsinged int temp, second, minute, hour; do_gettimeofday(&now); temp = now.tv_sec; second = t

如何在linux内核中编写人类可读的时间戳?我认为,
do_gettimeofday
返回epoch,但我不想尝试将其转换为可读时间。我只需要一个类似于
Hour:Min:Sec:Msec
的格式。
谢谢

虽然我不认为这有什么功能,但如果你不需要这一天,你可以很容易地完成。

struct timeval now;
unsinged int temp, second, minute, hour;
do_gettimeofday(&now);
temp = now.tv_sec;
second = temp%60;
temp /= 60;
minute = temp%60;
temp /= 60;
hour = temp%24;
printf("%02d:%02d:%02d:%06d\n", hour, minute, second, now.tv_usec);

请注意,您得到的是GMT时间,而不是本地时间。

以后的内核有一个函数
time\u to_tm
将历元时间转换为人类可读的格式

下面是一个例子:

struct timeval t;
struct tm broken;
do_gettimeofday(&t);
time_to_tm(t.tv_sec, 0, &broken);
printk("%d:%d:%d:%ld\n", broken.tm_hour, broken.tm_min, 
                         broken.tm_sec, t.tv_usec);

同样,这只在以后的内核中可用。第二个参数
time_to_tm
是历元时间的偏移量。在我的当地时间是0,我不知道你应该用哪一个。

do_gettimeofday是你最好的选择。您将自己格式化它。确切的用例是什么?