The Linux Programming Interface
Time
(01) 实际时间和进程时间概念
Real time: This is the time as measured either from some standard point (calendar time) or from some fixed point (typically the start) in the life of a process (elapsed or wall clock time).
Process time: This is the amount of CPU time used by a process. Measuring process time is useful for checking or optimizing the performance of a program or algorithm.
(02)gettimeofday()函数
The gettimeofday() system call returns the calendar time in the buffer pointed to by tv.
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
struct timeval {
time_t tv_sec; /* Seconds since 00:00:00, 1 Jan 1970 UTC */
suseconds tv_usec; /* Additional microseconds (long int) */
}
tz argument is now obsolete and should always be specified as NULL.
(03) time函数
The time() system call returns the number of seconds since the Epoch
#include <time.h>
time_t time(time_t *timep);
(04)时间转换函数
(05) ctime() 函数
The ctime() function provides a simple method of converting a time_t value into printable form.
char *ctime(const time_t *timep);
(06)写一个简单的程序打印当前的时间
1 #include <stdio.h>
2 #include <sys/time.h>
3 #include <time.h>
4
5 int main() {
6 time_t timep;
7 struct tm *p;
8 time(&timep);
9 p = localtime(&timep);
10 printf("%d-%d-%d %d:%d:%d\n",
11 (1900 + p->tm_year), (1 + p->tm_mon),
12 p->tm_mday, (p->tm_hour + 12), p->tm_min, p->tm_sec);
13 return 0;
14 }
输出:
wang@wang:~/srccode/yunos$ ./timeprint
2017-3-9 25:48:22
(07) gmtime() 和localtime()函数
The gmtime() and localtime() functions co