Knowing the Current Time

Kernel code can always retrieve the current time by looking at the value of jiffies. Usually the fact that the value represents only the time since the last boot is not relevant to the driver, because its life is limited to the system uptime. Drivers can use the current value of jiffies to calculate time intervals across events (I used it to tell double clicks from single clicks in the kmouse module). In short, looking at jiffies is almost always sufficient when you need to measure time intervals.

It’s quite unlikely that a driver will ever need to know the wall-clock time, as this knowledge is usually needed only by user programs like cron and at. If such a capability is needed, it will be a particular case of device usage, and the driver can be correctly instructed by a user program, which can easily do the conversion from wall-clock time to the system clock.

If your driver really needs the current time, the do_gettimeofday function comes to the rescue. The function doesn’t tell the current day of the week or anything like that; rather, it fills a struct timeval pointer with the usual seconds-microseconds values. It responds to the following prototype:

#include <linux/time.h>
void do_gettimeofday(struct timeval *tv);

The source states that do_gettimeofday has ``near microsecond resolution'' for all architectures except the Alpha and the Sparc, where it has the same resolution as jiffies. The Sparc port has been upgraded in 2.1.34 to support fine-grained ...

Get Linux Device Drivers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.