Time#1

Time's Data Structures

The original Representation

typedef long time_t;

And Now. Microsecond Precision

#include 
struct timeval{
    time_t               tv_sec; /* seconds */
    suseconds_t  tv_usec; /* microseconds */
};

Even Better: Nanosecond Precision

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

Breaking Down Time

#include 
struct tm {
    int tm_sec; /* seconds */
    int tm_min; /* minutes */
    int tm_hour; /* hours */
    int tm_mday; /* the day of the month */
    int tm_mon; /* the month */
    int tm_year; /* the year */
    int tm_wday; /* the day of the week */
    int tm_yday; /* the day in the year */
    int tm_isdst; /* daylight savings time? */
#ifdef _BSD_SOURCE
    long tm_gmtoff; /* time zone's offset from GMT */
      const char *tm_zone; /* time zone abbreviation */
#endif /* _BSD_SOURCE */
};
#include 
#include 
 
#define BST (+1)
#define CCT (+8)
 
int main ()
{
 
   time_t rawtime;
   struct tm *info;
 
   time(&rawtime);
   /* 获取 GMT 时间 */
   info = gmtime(&rawtime );
   
   printf("当前的世界时钟:\n");
   printf("伦敦:%2d:%02d\n", (info->tm_hour+BST)%24, info->tm_min);
   printf("中国:%2d:%02d\n", (info->tm_hour+CCT)%24, info->tm_min);
 
   return(0);
}

A Type for Process Time

类型clock_t表示时钟滴答。它是一个整数类型,通常是long。根据接口的不同,clock_t表示的滴答是系统的实际计时器频率(HZ)或Clock_PER_SEC

POSIX Clocks

clockid_t 有如下的参数选择:

  • CLOCK_REALTIME
    全系统实时(wall rime)时钟.设置这个时钟需要特殊的特权。
  • CLOCK_MONOTONIC
    一种单调增加的时钟,任何进程都不能设置。它表示自某些未指定的起始点(如系统引导)以来所花费的时间。
  • CLOCK_MONOTONIC_RAW
    与CLOCK_MONOTONIC相似,但时钟不适合旋转(时钟偏斜的校正)。也就是说,如果硬件时钟比墙壁时间运行得更快或更慢,当读这个时钟时不会对其进行调整。这个时钟是linux专用的。
  • CLOCK_PROCESS_CPUTIME_ID
    处理器提供的一种高分辨率的进程时钟.例如,在x86架构上,这个时钟使用时间戳计数器(TSC)寄存器。
  • CLOCK_THREAD_CPUTIME_ID
    类似于每个进程的时钟,但对进程中的每个线程都是唯一的.

Time Source Resolution

POSIX定义了用于获取给定时间源的分辨率的函数clock_getres():

#include 
int clock_getres(clockid_t clock_id, struct timespec *res);

成功返回0, 失败返回-1并设置errno。

int main()
{
    clockid_t clocks[] = {
            CLOCK_REALTIME,
            CLOCK_MONOTONIC,
            CLOCK_PROCESS_CPUTIME_ID,
            CLOCK_THREAD_CPUTIME_ID,
            CLOCK_MONOTONIC_RAW,
            (clockid_t)-1
    };   
    
    int i;
    for(i = 0; clocks[i]!=(clockid_t)-1; ++i){
        struct timespec res;
        int ret;
        
        ret = clock_getres(clocks[i], &res);
        if(ret)
            perror("clock_getres");
        else
            printf("clock=%d sec=%ld nsec=%ld\n",
                    clocks[i], res.tv_sec, res.tv_nsec);
    }
    
    return 0;
}
Time#1_第1张图片
测试结果of树莓派

Getting the Current Time of Day

#include 
time_t time(time_t *t);
//example
    time_t t;
    printf("current time: %ld\n", (long)time(&t));
    printf("the same vale: %ld\n", (long)t);
result

A Better Interface

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

timezone已经过时了,不要用 传NULL给tz

    struct timeval tv;
    int ret;
    ret = gettimeofday(&tv, NULL);
    if(ret)
        perror("gettimeofday");
    else
        printf("second=%ld useconds=%ld\n",
               (long)tv.tv_sec, (long)tv.tv_usec);
result

An Advanced Interface

#include 
int clock_gettime (clockid_t clock_id, struct timespec *ts);

成功调用返回0, 失败返回-1并设置errno

    clockid_t clocks[] = {
            CLOCK_REALTIME,
            CLOCK_MONOTONIC,
            CLOCK_PROCESS_CPUTIME_ID,
            CLOCK_THREAD_CPUTIME_ID,
            CLOCK_MONOTONIC_RAW,
            (clockid_t)-1
    };

    int i;
    for(i = 0; clocks[i]!=(clockid_t)-1; ++i){
        struct timespec ts;
        int ret;

        ret = clock_gettime(clocks[i], &ts);
        if(ret)
            perror("clock_getres");
        else
            printf("clock=%d sec=%ld nsec=%ld\n",
                   clocks[i], ts.tv_sec, ts.tv_nsec);
    }
Time#1_第2张图片
测试结果

Getting the Process Time

#include 
struct tms {
    clock_t tms_utime; /* user time consumed */
    clock_t tms_stime; /* system time consumed */
    clock_t tms_cutime; /* user time consumed by children */
    clock_t tms_cstime; /* system time consumed by children */
};
clock_t times (struct tms *buf);

失败返回-1,并设置errno。

你可能感兴趣的:(Time#1)