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

clock_realtime_coarse:clock_getres:0 1000000 clock_gettime:1410750427 191422911

clock_monotonic:clock_getres:0 1 clock_gettime:4269 364748226
#include <sys/time.h>
int gettimeofday (struct timeval* restrict TP, void* restrict TZP); Return: Always returns 0
Parameter 2 TZP must be null
struct Timeval {time_t tv_sec; seconds [Long int] suseconds_t tv_usec; microseconds [long int]}
Program Use cases:
#include <stdio.h> #include <sys/time.h>
int main (int argc, char* argv[]) {timeval tp;      int ret = Gettimeofday (&AMP;TP, NULL); printf ("RET:%d\t sec:%d\t usec:%d\n", ret, tp.tv_sec, tp.tv_usec);
return 0; }
Output:
ret:0 sec:1410752524 usec:95274

char* asctime (const struct tm* TM); char* asctime_r (const struct tm* TM, char* BUF); buf:26 bytes at least
char* CTime (const time_t* TIMEP);   char* ctime_r (const time_t* TIMEP, char* buf); buf:26 bytes at least
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);
TM Definition:
struct tm  {     int tm_sec;        /* seconds */     int Tm_mi N        /* minutes */     int Tm_hour;        /* hours */     int tm_mday;        /* Day of the month */     int Tm_mon;        /* Month */     int tm_year;        /* Year */     int tm_wday;        /* Day of the week * *

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.