How to Print Current Time in Kernel

How can i print current time in kernel?

Yes, do_gettimeofday has been removed because of y2038 problem. Instead the kernel provides time interfaces which you can use as per your need. Check the documentation https://www.kernel.org/doc/html/latest/core-api/timekeeping.html.

For example, you have ktime_get_ts64(struct timespec64 *ts) which will provide you time in seconds and nanoseconds.

struct timespec64 {
time64_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};

If you only want in nanoseconds, you can use u64 ktime_get_ns(void). Please check the documentation above for what suits your purpose.
Also you can check timekeeping.h and ktime.h for further information.

If you want to find an example just search the function name in the kernel source either using grep -rni <func name> or use cscope. You can also search it online here

Get current time in seconds in kernel module

You can use getnstimeofday for that.

/* getnstimeofday - Returns the time of day in a timespec */
void getnstimeofday(struct timespec *ts)

where struct timespec is:

struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};

And yes, you'll need #include <linux/time.h>.

How to get current hour (time of day) in linux kernel space

time_to_tm function can be of your help, which returns the structure tm. Timezone available in variable sys_tz, it can help you to set your offset properly to get local time.

Custom Linux kernel module to display year, date and time

This works. I just tried it. You can use time_to_tm() to convert between values. Note that if you want to be very precise, use the system timezone to know hours, minutes, and seconds locally.

Also, I printed to the system log instead of a proc entry.

    unsigned long get_time;
int sec, hr, min, tmp1,tmp2, tmp3;
struct timeval tv;
struct tm tv2;

do_gettimeofday(&tv);
get_time = tv.tv_sec;
sec = get_time % 60;
tmp1 = get_time / 60;
min = tmp1 % 60;
tmp2 = tmp1 / 60;
hr = (tmp2 % 24) - 4;
time_to_tm(get_time, 0, &tv2);
tmp3 = tv2.tm_year;

printk(KERN_INFO "time :: %d:%d:%d\n",hr,min,sec);
/* Add years since 1900. */
printk(KERN_INFO "Year: %d\n", tmp3 + 1900);

How to print time in kernel source?

One way to accomplish this is to use printk (see http://www.makelinux.net/books/lkd2/ch18lev1sec3 for example) and to enable PRINTK_TIMES feature, so you'll get the output in form of

[8804849.737776] Kernel BUG at fs/nfs/file.c:321

Where these two numbers is the number of seconds and microseconds since system booted.

You can enable this in several ways:

  1. You can define CONFIG_PRINTK_TIME kernel config option
  2. At boot time, passing printk.time=1
  3. At runtime, issuing # echo 1 > /sys/module/printk/parameters/time

Some resources:

  • https://utcc.utoronto.ca/~cks/space/blog/linux/PrintkTimestampMeaning
  • http://elinux.org/Printk_Times

Print time stamp in kernel

You are correct in that TZ and DST live only in userspace. The kernel never knows what time zone it is. If you really need to do this, you will need to have a userspace helper upload timezone offsets to kernelspace - remember to deal with daylight savings time properly here! Alternately have a userspace tool postprocess kernel messages (which would be in UTC) and convert them to local time.



Related Topics



Leave a reply



Submit