User readable timestamp in linux kernel

How can I write a human timestamp in the Linux kernel? I think do_gettimeofday brings back an era, but I don't want to try to convert it to a readable time. I just want a format like Hour:Min:Sec:Msec . Thanks

+8
linux-kernel kernel kernel-module
source share
2 answers

In later kernels, there is a time_to_tm function to split the time of an era into a human-readable format.

Here is 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 subsequent kernels. The second parameter time_to_tm is the offset to the time of the era. In my local time 0, I do not know which one you should use.

+7
source share

Although I do not think there is a function for this, you can do it quite easily if you do not need this 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); 

Please note that you are getting GMT, not local time.

+1
source share

All Articles