Get current time in seconds in kernel module

后端 未结 1 1866
独厮守ぢ
独厮守ぢ 2020-12-09 18:47

What is the standard way to get the current time in seconds (since the epoch) in a kernel module?

I have seen techniques involving getting xtime which are very long-

相关标签:
1条回答
  • 2020-12-09 19:23

    You can use getnstimeofday for that.

    /* getnstimeofday - Returns the time of day in a timespec */
    void getnstimeofday(struct timespec *ts)
    

    where struct timespec is:

    struct timespec {
        time_t  tv_sec;     /* seconds */
        long    tv_nsec;    /* nanoseconds */
    };
    

    And yes, you'll need #include <linux/time.h>.

    0 讨论(0)
提交回复
热议问题