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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5077192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:58:30  来源:igfitidea点击:

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

clinuxlinux-kernelkernel-module

提问by Tom Macdonald

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.

谢谢。

采纳答案by Zimbabao

time_to_tmfunction 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 中可用的时区,它可以帮助您正确设置偏移量以获得本地时间。

回答by Erik

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

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

回答by sunmoon

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 就可以让我们了解在内核模式下获取当前日期。

回答by Roman

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

回答by Bharath

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