Advanced Programming for the UNIX Environment reading notes (5)
Source: Internet
Author: User
Catalog: http://blog.csdn.net/alex_my/article/details/39346381
Date and time
The functions involved are listed below, followed by examples to run, output results, more intuitive.
Time this piece of information is limited, if wrong, still look correct.
#include <time.h>
time_t time (Time_t*tloc);
int Clock_getres (clockid_t clk_id, struct timespec* res); int Clock_gettime (clockid_t clk_id, struct timespec* tp); int Clock_settime (clockid_t clk_id, const struct timespec* tp);
#include <sys/time.h>
int gettimeofday (struct timeval* restrict TP, void* restrict TZP);
#include <time.h>
char* asctime (const struct tm* TM); char* asctime_r (const struct tm* TM, char* BUF);
char* CTime (const time_t* TIMEP); char* ctime_r (const time_t* TIMEP, char* buf);
struct tm* gmtime (const time_t* TIMEP); struct tm* gmtime_r (const time_t* TIMEP, struct tm* result);
struct tm* localtime (const time_t* TIMEP); struct tm* localtime_r (const time_t* TIMEP, struct tm* result);
time_t mktime (struct tm* tm);
#include <time.h>
size_t strftime (char* s, size_t max, const char* format, const struct tm* TM);
#define _XOPEN_SOURCE/* See Feature_test_macros (7) */#include <time.h>
char* strptime (const char* S, const char* format, struct tm* TM);
#include <time.h>
time_t time (time_t* tloc);
Epoch: Refers to a specific time, January 1, 1970 00:00:00 UTC
The Return:time function returns the number of seconds from the epoch to the current, and if the parameter tloc is not NULL, it is also assigned to Tloc. If there is an error, it returns-1.
Program Use cases:
#include <stdio.h> #include <time.h>
int main (int argc, char* argv[]) {time_t t = time (NULL); printf ("Time:%d\n", static_cast<int> (t));
return 0; }
Output:
time:1410606639
#include <time.h>
int Clock_getres (clockid_t clk_id, struct timespec* res); int Clock_gettime (clockid_t clk_id, struct timespec* tp); int Clock_settime (clockid_t clk_id, const struct timespec* tp);
Return: Returns 0 if normal, error returns-1. The results are populated in RES (TP).
Definition of Timespec:
struct Timespec {time_t tv_sec; /* seconds */long tv_nsec; /* nanoseconds */};
The clk_id is used to specify the type of timing clock, with the following options:
Clock_realtime: System real time, from epoch, can be changed by user as well as adjtime and NTP influence.
Clock_realtime_coarse: System real time, faster acquisition speed than clock_realtime, lower accuracy.
Clock_monotonic: Starting from the moment the system starts, it is not affected even if the system time is changed by the user. The system does not clock when it sleeps. Affected by Adjtime and NTP.
Clock_monotonic_coarse: Like Clock_monotonic, but with faster acquisition speed and lower accuracy. Affected by NTP.
Clock_monotonic_raw: As with Clock_monotonic, the system is timed when it is turned on, but is unaffected by NTP and is affected by adjtime.
Clock_boottime: Starting from the moment the system starts, including sleep time, is affected by settimeofday.
CLOCK_PROCESS_CPUTIME_ID: The time at which this process began to invoke.
CLOCK_THREAD_CPUTIME_ID: The time that this thread begins to call the assassin.
Clock_getres gets the precision of the specified type of clock.
Clock_gettime and Clock_settime get and set the clk_id specified time.
Read the use case to understand.
Program Use cases: Clock_getres and Clock_gettime
#include <stdio.h> #include <time.h>
static void ShowTime (const char* DESC, const timespec* res) {printf ("%s: \ t%d\t%d\n", Desc, Res->tv_sec, RES-&G T;TV_NSEC); }
static void Testtime (clockid_t clk_id) {struct TIMESPEC res;
Clock_getres (clk_id, &res); ShowTime ("Clock_getres", &res);
Clock_gettime (clk_id, &res); ShowTime ("Clock_gettime", &res); }
int main (int argc, char* argv[]) {//For readability, suppose the function call succeeds printf ("Clock_realtime: \ n"); Testtime (Clock_realtime);
printf ("\n\nclock_realtime_coarse:\n"); Testtime (Clock_realtime_coarse);
printf ("\n\nclock_monotonic:\n"); Testtime (Clock_monotonic);
return 0; }
Output:
clock_realtime:clock_getres:0 1 clock_gettime:1410750427 192193358
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.