Time#1

2019-09-26  本文已影响0人  无无吴

Time's Data Structures

The original Representation

typedef long time_t;

And Now. Microsecond Precision

#include <sys/time.h>
struct timeval{
    time_t               tv_sec; /* seconds */
    suseconds_t  tv_usec; /* microseconds */
};

Even Better: Nanosecond Precision

#include <time.h>
struct timespec{
    time_t tv_sec;   /* seconds */
    long    tv_nsec;  /* nanoseconds*/
};

Breaking Down Time

#include <time.h>
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 <stdio.h>
#include <time.h>
 
#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 有如下的参数选择:

Time Source Resolution

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

#include <time.h>
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;
}
测试结果of树莓派

Getting the Current Time of Day

#include <time.h>
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 <sys/time.h>
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 <time.h>
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);
    }
测试结果

Getting the Process Time

#include <sys/times.h>
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。

上一篇 下一篇

猜你喜欢

热点阅读