linux c 时间函数

1、 time() 函数提供了 秒 级的精确度

time_t time(time_t * timer)
函数返回从UTC1970-1-1 0:0:0开始到现在的秒数

2、 struct timespec 提供了 ns  级的精确度

定义如下:

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

对应的操作函数为

int clock_gettime(clockid_t, struct timespec *)

其中 clockid_t 为使用的时钟,主要有以下四种:

CLOCK_REALTIME    统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC  系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间

3、struct timeval 提供了 us 级 的精确度

struct timeval {
  time_t tv_sec; // seconds
  long tv_usec; // microseconds
};

对应的操作函数为

int gettimeofday(struct timeval *tv, struct timezone *tz)

其中 timezone 为时区,定义如下:

struct timezone{
  int tz_minuteswest; //miniutes west of Greenwich
  int tz_dsttime; //type of DST correction
};

一般时区填写为 NULL即可。

猜你喜欢

转载自www.cnblogs.com/rohens-hbg/p/9765544.html
今日推荐