In the third quarter - Lesson 10 - Time Programming

Lesson 10 - Time Programming

 

10.1 Time Type

Codinated Universal Time (UTC): Coordinated Universal Time, which is known to everyone GMT (Greenwich Mean Time, GMT).

Calendar Time: calendar time, is "from a standard point in time (eg: at 0:00 on January 1, 1970) this number of seconds elapsed" to indicate the time.

10.2 Function Learning

There is a function of learning and learning methods before the same, that three-step learning method: function name; function prototypes; function uses.

1. Get a calendar time

(1) function name

time

(2) function prototype

time_t time(time_t *t);

(3) function-

Back to calendar time

(4) belongs to the header file

#include<time.h>

(5) Return value

Success: Time Calendar

Failure: -1

(6) Parameter Description

t: return value is not the case empty.

(7) sample program

#include<time.h>

void main()

{                

         time_t ctime;

         ctime = time(NULL);

         printf("ctime is %d\n",ctime);

}

2. Get GMT

(1) function name

gmtime

(2) function prototype

struct tm *gmtime(const time_t *timep);

(3) function-

Timep parameters specified calendar time grasping Huawei Standard Time.

(4) belongs to the header file

#include<time.h>

(5) Return value

strict tm

{

int tm_sec;             //seconds

int tm_min;            //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

int tm_yday;           //day in the year

int tm_isdst;           //daylight saving time

};

Success: Coordinated Universal Time to struct tm stored in the form of

Failed: NULL

(6) Parameter Description

timep: with time into the world of

(7) sample program

#include<time.h>

void main()

{                

         time_t ctime;

         struct tm *tm;

         ctime = time(NULL);

         // will be converted to standard time calendar time

         tm = gmtime(&ctime);

         printf("now is hour %d,min is %d\n",tm->tm_hour,tm->tm_min);

        // Note that the method for the identification of structures in the body member.

}

3. Get the local time

(1) function name

localtime()

(2) function prototype

struct tm *localtime(const time_t *timep);

(3) function-

The parameters timep pointed calendar time into local time.

(4) belongs to the header file

#include<time.h>

(5) Return value

strict tm

{

int tm_sec;             //seconds

int tm_min;            //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

int tm_yday;           //day in the year

int tm_isdst;           //daylight saving time

};

Success: returns the local time stored in the structure tm

Failed: NULL

(6) Parameter Description

timep: calendar time to be transformed

(7) sample program

#include<time.h>

void main()

{                

         time_t ctime;

         struct tm *tm;

         ctime = time(NULL);

         // calendar time into local time

         tm = localtime(&ctime);

         printf("now is hour %d,min is %d\n",tm->tm_hour,tm->tm_min);

}

Note: To validate our program is correct, we can see the time by date command, and we will find the time to print out the above procedure is the same.

4. The string (format) displayed time

(1) function name

asctime

(2) function prototype

char *asctime(const struct tm *tm);

(3) function-

Struct tm is the time format into a string

(4) belongs to the header file

#include<time.h>

(5) Return value

Character string display time

(6) Parameter Description

tm: time format to be converted tm

(7) sample program

#include<time.h>

void main()

{                

         time_t ctime;

         struct tm *tm;

         char *stime;

         ctime = time(NULL);

         // calendar time into local time

         tm = gmtime(&ctime);

         printf("now is hour %d,min is %d\n",tm->tm_hour,tm->tm_min);

         stime = asctime(tm);

         printf("time is %s\n",stime);

}

The acquisition time precision

(1) function name

gettimeofday();

(2) function prototype

int gettimeofday(struct timeval *tv, strct timezone *tz);

(3) function-

Acquiring high precision time.

(4) belongs to the header file

#include<sys/time.h>

(4) Return value

Success: 0;

Failure: -1

(6) Parameter Description

struct timeval

{

time_t       tv_sec;     //seconds

Suseconds_t  tv_usec;    //microseconds

}

strutct timezone

{

int  tz_minuteswest;      //minutes west of Greewich

int  tz_dsttime;          //type of DST correction

}

tv: Save 0 from 1 January 1970: 0: 0 Number of seconds to go through here and microseconds;

tzone: usually NULL

(7) sample program

The following procedure is used to detect FUNC () function of time to run, here we use the header #include <stdio.h>, because this requires a NULL symbol header.

#include<sys/time.h>

#include<stdio.h>

void func()

{

         int i,j;

         int ret;

         for(i=0;i<1000;i++)

                  for(j=0;j<1000;j++)

                           K = j;

}

void main()

{

         int i,j;

         struct timeval tv1;

         struct timeval tv2;

         gettimeofday(&tv1,NULL);

         func();

         gettimeofday(&tv1,NULL);

    i = tv2.tv_sec-tv1.tv_sec;

         j = tv2.tv_usec-tv1.tv_usec;

         printf("sec is %d,usec is%d\n",i,j);

}

10.3 summary

 

 

Guess you like

Origin www.cnblogs.com/free-1122/p/11345373.html