Chapter10 time
1: Real time: The starting point for measuring this time is two: (1) a certain standard point; (2) a fixed spot in the process life cycle (usually initiated by the program)
2: Process time: The total amount of CPU time used by a process, which is suitable for checking or optimizing the program, algorithm performance.
10.1 Calendar Time
The calendar time is stored in a variable of type time_t.
System call Gettimeofday () to return the calendar time in a TV-directed buffer
#include <sys/time.h>
int gettimeofday (struct timeval *tv, struct timezone *tz);
The structure that the parameter TV points to:
struct Timeval
{
time_t tv_sec;//from 00:00:00 1 Jan 1970 UTC
suseconds_t tv_usec; Subtle
}
The time () system call returns the number of seconds since the epoch (and the value of the Tv_sec field in the TV parameter returned by the function Gettimeofday ()).
#include <time.h>
time_t time (time_t *TIMEP);
10.2 Time conversion function
10.2.1 converting time_t to printable format
#include <time.h>
Char *ctime (const time_t *TIMEP);
Returns a 26-byte string in format: Wed June 8 14:22:34 2011
10.2.2 conversion between time_t and decomposition time
The functions Gmtime () and localtime () convert a time_t value to a so-called decomposition time. The decomposition time is placed in a statically allocated structure, and its address is returned as a function result.
#include <time.h>
struct TM *gmtime (const time_t *TIMEP);
struct TM *localtime (const time_t *TIMEP);
The function mktime () translates the decomposition time of a local time zone to a time_t value and returns it as a function result.
#include <time.h>
time_t mktime (struct TM *timeptr);
10.2.3 conversion between decomposition time and print format
Convert from decomposition time to print format
#include <time.h>
Char *asctime (const struct TM *timeptr);
When converting a decomposition time into a print format, the function strftime () can provide more precise control. Causes Timeptr to point to the decomposition time, strftime () places a null-terminated string of the date and time into the buffer that outstr points to.
#include <time.h>
size_t strftime (char *outstr, size_t maxsize, const char *format, const struct TM *timeptr);
Convert print format time to decomposition time
function Strptime () is the inverse function of strftime
10.3 Time Zone
Time zone definition
/usr/share/zoncinfo
The local time of the system is defined by the time zone file/etc/localtime, which is usually linked to a file under/usr/share/zoneinfo.
Specify the time zone for the program:
10.4 Regions (locale)
Setting a region for a program
#include <locale.h>
char *setlocale (int category, const char *locale);
10.5 Updating the system clock
Settimeofday () and Adjtime ().
#define _bsd_source
#include <sys/time.h>
int settimeofday (const struct Timeval *tv, const struct Timezon *tz);
10.6 Software Clock
10.7 Process Time
Process time is the amount of CPU time that is used after the process is created.
The system calls times (), retrieves the process time information, and returns the result through the struct that the buf points to.
#include <sys/times.h>
clock_t times (struct TMS *buf);
The function clock () provides a simple interface for getting process time. It returns a value that describes the total CPU time used by the calling process (both users and systems)
#include <time.h>
clock_t clock (void);
"Linux_unix system Programming" CHAPTER10 time