struct timespec 和 struct timeval

struct timespec 和 struct timeval
time()提供了秒级的精确度 .

1、头文件 <time.h>
2、函数原型
time_t time(time_t * timer)
函数返回从TC1970-1-1 0:0:0开始到现在的秒数

用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。

如果需要更高的时间精确度,就需要struct timespec 和 struct timeval来处理:

struct timespec 定义:

typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // and nanoseconds
};
#endif

struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒。
一般由函数int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间,常用如下4种时钟:

CLOCK_REALTIME 统当前时间,从19701.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间
struct tm *localtime(const time_t *clock);  //线程不安全
struct tm* localtime_r( const time_t* timer, struct tm* result );//线程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,
													const struct tm* timeptr );

struct timeval 定义:

struct timeval {
time_t tv_sec; // seconds
long tv_usec; // microseconds
};
struct timezone{
int tz_minuteswest; //miniutes west of Greenwich
int tz_dsttime; //type of DST correction
};

struct timeval有两个成员,一个是秒,一个是微秒, 所以最高精确度是微秒。
一般由函数int gettimeofday(struct timeval *tv, struct timezone *tz)获取系统的时间

#include<stdio.h>
#include<time.h>
#include<sys/time.h>

void nowtime_ns()
{
    printf("----------------struct timespec-------------------\n"); 
    printf("[time(NULL)]     :     %ld\n", time(NULL)); 
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n",ts.tv_sec, ts.tv_nsec);
    
    struct tm t;
    char date_time[64];
    strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", 
    										localtime_r(&ts.tv_sec, &t));
    printf("clock_gettime :date_time=%s, tv_nsec=%ld\n", date_time, ts.tv_nsec);
}
void nowtime_us()
{
    printf("------------struct timeval------------------\n"); 
    printf("[time(NULL)]    :    %ld\n", time(NULL)); 
    struct timeval us;
    gettimeofday(&us,NULL);
    printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec);
    
    struct tm t;
    char date_time[64];
    strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", 		
    											localtime_r(&us.tv_sec, &t));
    printf("gettimeofday: date_time=%s, tv_usec=%ld\n", date_time, us.tv_usec);
}

int main(int argc, char* argv[])
{
    nowtime_ns();
    printf("\n");
    nowtime_us();
    printf("\n");
    return 0;
}

nowtime.cpp

执行结果:

$tt
---------------------------struct timespec---------------------------------------
[time(NULL)] : 1400233995
clock_gettime : tv_sec=1400233995, tv_nsec=828222000
clock_gettime : date_time=2014-05-16 17:53:15, tv_nsec=828222000

---------------------------struct timeval----------------------------------------
[time(NULL)] : 1400233995
gettimeofday: tv_sec=1400233995, tv_usec=828342
gettimeofday: date_time=2014-05-16 17:53:15, tv_usec=828342

Linux内核驱动中,通常不建议直接使用`struct tm`来处理时间相关的逻辑。`struct tm`是C标准库`<time.h>`中定义的用于表示日期和时间的结构体,它主要用于用户空间程序。在内核驱动中,更常用的是使用内核提供的时间相关的数据类型和函数。 以下是一些可以在Linux内核驱动中使用的时间相关结构体和函数: 1. `struct timespec`:精确到纳秒级的时间结构体,定义在头文件`<linux/time.h>`中。可以使用`struct timespec`来表示时间戳或者计算时间间隔。 2. `struct timeval`:精确到微秒级的时间结构体,定义在头文件`<linux/time.h>`中。在一些旧的内核驱动中可能会使用这个结构体。 3. `ktime_get()`:获取当前时间的内核函数,返回`ktime_t`类型的时间值,定义在头文件`<linux/ktime.h>`中。 4. `ktime_t`:内核中表示时间的数据类型,定义在头文件`<linux/ktime.h>`中。可以进行时间计算和比较操作。 5. `getnstimeofday()`:获取当前时间的内核函数,将结果存储在`struct timespec`类型的变量中,定义在头文件`<linux/timekeeping.h>`中。 6. `timespec_add()`、`timespec_sub()`等函数:用于进行`struct timespec`类型的时间计算,定义在头文件`<linux/time.h>`中。 通过使用这些时间相关的数据类型和函数,可以在内核驱动中进行时间的获取、计算和处理操作,而不需要依赖于用户空间的`struct tm`和相关函数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值