Linux time-related API summary

Source: Internet
Author: User



One: Correlation function prototype

        #include  <time.h>        time_t time (time_t *t);        char *asctime (const &NBSP;STRUCT&NBSP;TM&NBSP;*TM);        char *ctime (const time_t &NBSP;*TIMEP);        struct tm *gmtime (const time_t * TIMEP);        struct tm *localtime (CONST&NBSP;TIME_T&NBSP;*TIMEP) ;        time_t mktime (STRUCT&NBSP;TM&NBSP;*TM);       char *strptime (const char *s, const char *format, struct &NBSP;TM&NBSP;*TM);       size_t strftime (Char *s, size_t max , const char *format,                                                   const struct tm &NBSP;*TM);          struct 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 */           }; 

II: API Collation


Time () is used for the current timestamp, and if T is not NULL then the timestamp returned is also stored in T.

Asctime () passes in the time structure of a TM, returning this readable time string

The CTime () function is the same as the asctime, but the CTime is more powerful and can convert the timestamp of the time_t type

into a good-readable time string.

Gmtime () passes in a time_t type of time value, converts it to a time TM struct, but Gmtime has a drawback when the format is UTC time, not the time of the current host time zone.

LocalTime () and gmtime function the same, except that LocalTime returns the time of the current host time zone.

Mktime () passes in a TM struct to convert it into a timestamp of type time_t.

Strftime () passes in a TM struct and a formatted string, depending on the time format in which the formatted string will be formatted.

Strptime () passes in a time string, specifying its format, converting it to a TM time structure body


Asctime () CTime () can be divided into a class that uses different functions for different parameters.

gmtime () localtime ()is divided into one category, depending on whether the time zone is required to use.

Strftime () Strptime () is divided into a class that processes a formatted time string.

Time () to get the current times


Three: API function related use

#include <stdio.h> #include <stdlib.h> #define  _XOPEN_SOURCE  #这句没有的话, the compilation will have a warning message, There is no strptime declaration in the Gun Library #include<time.h>time_t return_current_time (); Void print_time (time_t  Tdata);int main  ( int argc, char *argv[] ) {         print_time (Return_current_time ());         return  exit_success;} Void print_time (time_t tdata) {        char *wday[]={" SUn "," Mon "," Tue "," Wed "," Thu "," Fri "," Sat "};        struct tm  *pt;        time_t tmp;         char buf[256];        //specifying the format of the output string          char *format =  "%a %d %b, %i:%s %p";//       pt = gmtime (&tdata);        pt =  LocalTime (&tdata);         printf ("%ld:convert to time:\n ", Tdata);         printf (" isdst:%d\n ", PT-&GT;TM_ISDST);         printf ("year:%d year \ \", pt->tm_year+1900);         printf ("mon:%d month \ n", pt->tm_mon+1);         printf (" mday:%d (Pt->tm_mday),         printf ("week:%s\n", wday[pt->tm_ Wday]);         printf ("hour:%d hours \ n", Pt->tm_hour);         printf ("min:%d min \ n", Pt->tm_min);         printf ("sec:%d sec \ n", pt->tm_sec);         printf ("Output of asctime \ n") ;         printf ("%s\n", Asctime (PT));         printf ("Ctime   Output: \ n ");         printf ("%s\n ", CTime (&tdata));         printf ("Mktime converts the TM structure to a value of time_t type \ n");         if ((Tmp = mktime (PT))  == -1) {                 perror ("Mktime error:");                 exit (1);         }        printf ("mktime convert to time_t:% ld\n ", TMP);         printf (" strftime use: \ n ");         if (Strftime (buf,256,format,pt)  == -1) {          &nbsP;      perror ("Strftime error:");                 exit (1);         }        printf ("%s\n", buf);         printf ("Strptime use: \ n");         #buf中存放的是指定format格式的时间字符串 , and then converted to a TM structure         strptime (BUF,FORMAT,PT) according to its Fotamt;} #获取当前的时间time_t  return_current_time  () {        return  Time ((time_t *) 0);}

This article is from the "Focus on Linux" blog, so be sure to keep this source http://forlinux.blog.51cto.com/8001278/1530550

Linux time-related API summary

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.