Linux设备驱动程序 之 获取当前时间

墙上时间

内核通常经过jiffies来获取当前时间,该数值表示的是最近一次系统启动到当前的时间间隔,它和设备驱动程序无关,由于它的声明期只限于系统的运行期;可是驱动程序能够用jiffies来计算不一样事件之间的间隔;一般,利用jiffies值来测量时间间隔已经足够了,若是要测量更短的时间差,只能使用处理器特定的寄存器了,但这会带来严重的兼容性问题linux

驱动程序通常不须要知道墙钟时间,一般只有cron和syslogd这样的用户程序才使用,对真实世界时间的处理最好留给用户空间,C函数库提供了更好的支持;这些代码一般有更高的策略相关性,不该该属于内核;可是内核也提供了将墙钟时间转换为jiffies值的函数,包含在<linux/time.h>:函数

1 time64_t mktime64(const unsigned int year, const unsigned int mon, 2             const unsigned int day, const unsigned int hour, 3             const unsigned int min, const unsigned int sec);
绝对时间戳

当内核须要处理绝对时间戳是,须要使用do_gettimeofday函数,该函数用秒或者微秒来填充一个指向struct timeval的指针变量–gettimeofday系统调用中用的也是同一种变量,do_gettimeofday的原型以下:spa

1 void do_gettimeofday(struct timeval *tv)

当前时间也能够经过timespec值来得到,函数以下:指针

1 struct timespec64 current_kernel_time64(void)

 

 1 #if __BITS_PER_LONG == 64
 2 # define timespec64 timespec  3 #define itimerspec64 itimerspec
 4 #else
 5 struct timespec64 {  6     time64_t    tv_sec;            /* seconds */
 7     long        tv_nsec;        /* nanoseconds */
 8 };  9 
10 struct itimerspec64 { 11     struct timespec64 it_interval; 12     struct timespec64 it_value; 13 }; 14 
15 #endif
相关文章
相关标签/搜索