linux time相关总结

1.精确级别,纳秒级别,函数clock_gettime(clockid_t which_clock,struct timespec *tp)

linux clock_gettime函数详解
原型 long sys_clock_gettime (clockid_t which_clock, struct timespec *tp);
which_clock参数解释
CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间
struct timespec *tp
struct timespec
{
  
  __time_t tv_sec;  /*seconds 秒*/
  long int tv_nsec; /*nanoseconds 纳秒*/
  
  
}
//test9.c
#include <stdio.h> #include <time.h>
int main()
{
  struct timespec time1 = {0, 0};
  clock_gettime(CLOCK_REALTIME, &time1);
  printf("CLOCK_REALTIME: %d, %d", time1.tv_sec, time1.tv_nsec);
  clock_gettime(CLOCK_MONOTONIC, &time1);
  printf("CLOCK_MONOTONIC: %d, %d", time1.tv_sec, time1.tv_nsec);
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d", time1.tv_sec, time1.tv_nsec);
  clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time1);
  printf("CLOCK_THREAD_CPUTIME_ID: %d, %d", time1.tv_sec, time1.tv_nsec);
  printf("\n%d\n", time(NULL));
  sleep(1);
}

2、精确级别,毫秒级别,函数gettimeofday(struct  timeval*tv,struct  timezone *tz)

struct timeval
{
  long  tv_sec;/*秒*/
  long  tv_usec;/*微妙*/
}
struct  timezone
{
  int tz_minuteswest;/*和greenwich 时间差了多少分钟*/
  int tz_dsttime;/*type of DST correction*/
}
提供了诸多的函数,诸如:time(),gettimeofday(),localtime(),gmtime(),mktime(),asctime(),ctime()和strftime()的时间函数,同时又大量的格式转换,满足我们所需要的格式。
posted @ 2014-03-07 10:45  twin  阅读(245)  评论(0)    收藏  举报