如何获取linux内核空间中的当前小时(时间)

时间:2023-02-09 20:24:15

I'm writing a kernel module that checks to see if the time is between two specified hours, and disables input if it is. This has to do with me wanting to make sure I go to bed early. (I know I could also use any number of different techniques including cron etc, but I wanted to learn kernel programming...)

我正在编写一个内核模块,用于检查时间是否在两个指定的小时之间,如果是,则禁用输入。这与我想要确保我早点睡觉有关。 (我知道我也可以使用任何数量的不同技术,包括cron等,但我想学习内核编程......)

As a first version, I therefore check if the current hour is between start and end, which are set via parameters to the module.

因此,作为第一个版本,我检查当前小时是否在开始和结束之间,这是通过模块的参数设置的。

My question is therefore : How do I get the current hour? I have no access to the usual time functions in the standard library because I am in kernel space. I'm guessing that I should be using do_gettimeofday() for this, but that only gives me seconds and nanoseconds, and I need hours in the current day.

因此,我的问题是:如何获得当前时间?我无法访问标准库中的常用时间函数,因为我在内核空间中。我猜我应该使用do_gettimeofday()来做这件事,但这只能给我几秒和几秒,而且我需要几小时才能完成。

Thanks.

谢谢。

5 个解决方案

#1


14  

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.

time_to_tm函数可以是你的帮助,它返回结构tm。变量sys_tz中可用的时区,它可以帮助您正确设置偏移量以获得当地时间。

#2


6  

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

#3


1  

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

#4


0  

We can use clock_gettime function with CLOCK_REALTIME as the type of clock.

我们可以使用clock_gettime函数和CLOCK_REALTIME作为时钟类型。

Reference http://linux.die.net/man/3/clock_gettime

参考http://linux.die.net/man/3/clock_gettime

Just doing a strace on date executable gives us an idea to get the current date in the kernel mode.

只是在日期可执行文件上做一个strace让我们想到在内核模式下获取当前日期。

#5


-1  

Converting the do_gettimeofday result to an hour is pretty simple, since it starts at midnight GMT.

将do_gettimeofday结果转换为一小时非常简单,因为它从格林威治标准时间午夜开始。

time_t t = time(0);
time_t SecondsOfDay = t % (24*60*60);
time_t HourGMT = SecondsOfDay / (60*60);

Then adjust for your local timezone

然后根据您当地的时区进行调整

#1


14  

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.

time_to_tm函数可以是你的帮助,它返回结构tm。变量sys_tz中可用的时区,它可以帮助您正确设置偏移量以获得当地时间。

#2


6  

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

#3


1  

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

#4


0  

We can use clock_gettime function with CLOCK_REALTIME as the type of clock.

我们可以使用clock_gettime函数和CLOCK_REALTIME作为时钟类型。

Reference http://linux.die.net/man/3/clock_gettime

参考http://linux.die.net/man/3/clock_gettime

Just doing a strace on date executable gives us an idea to get the current date in the kernel mode.

只是在日期可执行文件上做一个strace让我们想到在内核模式下获取当前日期。

#5


-1  

Converting the do_gettimeofday result to an hour is pretty simple, since it starts at midnight GMT.

将do_gettimeofday结果转换为一小时非常简单,因为它从格林威治标准时间午夜开始。

time_t t = time(0);
time_t SecondsOfDay = t % (24*60*60);
time_t HourGMT = SecondsOfDay / (60*60);

Then adjust for your local timezone

然后根据您当地的时区进行调整