linux date time

1. 秒级精度的时间

time() .

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

sysinfo结构

struct sysinfo {
               long uptime;             /* Seconds since boot */
               unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
               unsigned long totalram;  /* Total usable main memory size */
               unsigned long freeram;   /* Available memory size */
               unsigned long sharedram; /* Amount of shared memory */
               unsigned long bufferram; /* Memory used by buffers */
               unsigned long totalswap; /* Total swap space size */
               unsigned long freeswap;  /* swap space still available */
               unsigned short procs;    /* Number of current processes */
               char _f[22];             /* Pads structure to 64 bytes */
           };

成员 uptime 表示从计算机上电到当前的运行秒数,比如开机了20分钟,uptime就是 20*60 = 1200;

2. 毫秒、微妙级别精度

此时 time(), sysinfo 无法满足需求,需要

int clock_gettime(clockid_t clk_id, struct timespec *tp)

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

clock_gettime() 参数 clk_id 常用:

CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,如果系统时间被用户改成其他,则对应的时间相应改变
CLOCK_REALTIME_COARSE:和CLOCK_REALTIME类似,但是执行速度快,精度低
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_MONOTONIC_COARSE :和CLOCK_MONOTONIC类似,但是执行速度快,精度低
CLOCK_BOOTTIME:和  CLOCK_MONOTONIC 类似,但是包括了系统休眠的时间。
CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间

猜你喜欢

转载自blog.csdn.net/yudingding6197/article/details/82145915