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.
To get the local time in kernel, add the below code snippet your kernel driver:
struct timeval time;unsigned long local_time;do_gettimeofday(&time);local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));rtc_time_to_tm(local_time, &tm);printk(" @ (%04d-%02d-%02d %02d:%02d:%02d)\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
This works well for me:
#include <linux/time.h>.../* getnstimeofday - Returns the time of day in a timespec */void getnstimeofday(struct timespec *ts)
For getting usual time format you can use:
printk("TIME: %.2lu:%.2lu:%.2lu:%.6lu \r\n", (curr_tm.tv_sec / 3600) % (24), (curr_tm.tv_sec / 60) % (60), curr_tm.tv_sec % 60, curr_tm.tv_nsec / 1000);