Fortunately, the latest 2019 [airship] formula racing rule 567 yards formula plan skills practical skills Wynn does not lose time acquisition function

Time acquisition function

 

The basic time provided by the Linux kernel since this particular time 1970-01-01 00:00:00 +0000 (UTC) after a number of seconds, this description is based on data type time_t representation, which we call the calendar time.
Function to get the calendar time there are three: time, clock_gettime and gettimeofday.

time function

#include <time.h>

//成功返回日历时间,出错返回-1;若time非NULL,则也通过其返回时间值
time_t time(time_t *time);
#include <stdio.h>
#include <string.h> #include <time.h> void print_time() { time_t seconds = time(NULL); printf("seconds = %ld\n", seconds); } int main() { print_time(); return 0; }

 

clock_gettime function

clock_gettime function obtains a specified clock time, return time saved by struct timespec structure, the structure and the time is expressed in seconds nanoseconds.

#include <time.h>

struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; //Link with -lrt. int clock_gettime(clockid_t clock_id, struct timespec *tsp);

clock_id generally set CLOCK_REALTIMEto obtain a high precision calendar time.

#include <stdio.h>
#include <string.h> #include <time.h> void print_time() { time_t seconds; struct timespec tsp; seconds = time(NULL); printf("seconds = %ld\n", seconds); clock_gettime(CLOCK_REALTIME, &tsp); printf("tsp.tv_sec = %ld, tsp.tv_nsec = %ld\n", tsp.tv_sec, tsp.tv_nsec); } int main() { print_time(); return 0; }

 

gettimeofday function

Clock_gettime functions and the like, by the gettimeofday returns calendar time struct timeval structure that the time is expressed in seconds and subtle.

#include <sys/time.h>

struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ }; int gettimeofday(struct timeval *tv, struct timezone *tz);

It should be noted that the only legitimate tz parameter value is NULL.

#include <stdio.h>
#include <string.h> #include <time.h> #include <sys/time.h> void print_time() { time_t seconds; struct timespec tsp; struct timeval tv; seconds = time(NULL); printf("seconds = %ld\n", seconds); clock_gettime(CLOCK_REALTIME, &tsp); printf("tsp.tv_sec = %ld, tsp.tv_nsec = %ld\n", tsp.tv_sec, tsp.tv_nsec); gettimeofday(&tv, NULL); printf("tv.tv_sec = %ld, tv.tv_usec = %ld\n", tv.tv_sec, tv.tv_usec); } int main() { print_time(); return 0; }

The basic time provided by the Linux kernel since this particular time 1970-01-01 00:00:00 +0000 (UTC) after a number of seconds, this description is based on data type time_t representation, which we call the calendar time.
Function to get the calendar time there are three: time, clock_gettime and gettimeofday.

time function

#include <time.h>

//成功返回日历时间,出错返回-1;若time非NULL,则也通过其返回时间值
time_t time(time_t *time);
#include <stdio.h>
#include <string.h> #include <time.h> void print_time() { time_t seconds = time(NULL); printf("seconds = %ld\n", seconds); } int main() { print_time(); return 0; }

 

clock_gettime function

clock_gettime function obtains a specified clock time, return time saved by struct timespec structure, the structure and the time is expressed in seconds nanoseconds.

#include <time.h>

struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; //Link with -lrt. int clock_gettime(clockid_t clock_id, struct timespec *tsp);

clock_id generally set CLOCK_REALTIMEto obtain a high precision calendar time.

#include <stdio.h>
#include <string.h> #include <time.h> void print_time() { time_t seconds; struct timespec tsp; seconds = time(NULL); printf("seconds = %ld\n", seconds); clock_gettime(CLOCK_REALTIME, &tsp); printf("tsp.tv_sec = %ld, tsp.tv_nsec = %ld\n", tsp.tv_sec, tsp.tv_nsec); } int main() { print_time(); return 0; }

 

gettimeofday function

Clock_gettime functions and the like, by the gettimeofday returns calendar time struct timeval structure that the time is expressed in seconds and subtle.

#include <sys/time.h>

struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ }; int gettimeofday(struct timeval *tv, struct timezone *tz);

It should be noted that the only legitimate tz parameter value is NULL.

#include <stdio.h>
#include <string.h> #include <time.h> #include <sys/time.h> void print_time() { time_t seconds; struct timespec tsp; struct timeval tv; seconds = time(NULL); printf("seconds = %ld\n", seconds); clock_gettime(CLOCK_REALTIME, &tsp); printf("tsp.tv_sec = %ld, tsp.tv_nsec = %ld\n", tsp.tv_sec, tsp.tv_nsec); gettimeofday(&tv, NULL); printf("tv.tv_sec = %ld, tv.tv_usec = %ld\n", tv.tv_sec, tv.tv_usec); } int main() { print_time(); return 0; }

Guess you like

Origin www.cnblogs.com/dtydd377455/p/11701300.html