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 (for example, to tell double clicks from
single clicks in input device drivers). In short, looking at
jiffies is almost always sufficient when you need
to measure time intervals, and if you need very sharp measures for
short time lapses, processor-specific registers come to the rescue.
It’s quite unlikely that a driver will ever need to know the wall-clock time, since this knowledge is usually needed only by user programs such as 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. Dealing directly with wall-clock time in a driver is often a sign that policy is being implemented, and should thus be looked at closely.
If your driver really needs the current time, the
do_gettimeofday function comes to the
rescue. This function doesn’t tell the current day of the week or
anything like that; rather, it fills a struct timeval pointer—the same as used in the
gettimeofday system call—with the usual
seconds and microseconds values. The prototype for
do_gettimeofday ...