human readable timestamp in linux kernel

时间秒杀一切 提交于 2019-12-01 03:30:44
Fred

Later kernels have a function time_to_tm to break epoch time into human readable format.

Here's an example:

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);

Again, this is only available in later kernels. The second parameter time_to_tm is an offset to the epoch time. In my local time is 0, I don't know which one you should use.

Though I don't think there's a function for this, you can do it quite easily if you don't need the day.

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);

Note that you get GMT time, not local time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!