如何在linux内核空间中获取当前小时(一天中的时间) [英] How to get current hour (time of day) in linux kernel space

查看:48
本文介绍了如何在linux内核空间中获取当前小时(一天中的时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

因此,我的问题是:如何获取当前小时数?我无法访问标准库中的常用时间函数,因为我在内核空间中.我猜我应该为此使用 do_gettimeofday() ,但这只会给我几秒和几纳秒,而我在当天需要几个小时.

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.

谢谢.

推荐答案

time_to_tm 函数可以为您提供帮助,它返回结构 tm.时区在变量 sys_tz 中可用,它可以帮助您设置正确偏移以获得当地时间.

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.

这篇关于如何在linux内核空间中获取当前小时(一天中的时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆