A summary of time-related functions under Linux

Source: Internet
Author: User
Tags function prototype local time

1. The time function    returns the number of seconds since 1970-1-1, after 00:00:00    prototype: time_t time (time_t *calptr)          result can be obtained by return value or by parameter, see instance    header file <time.h>    return Value:         success: Number of seconds, from 1970-1-1,00:00:00 can be used as an integer output or for other functions        failed: -1    cases:      time_t now;      Time (&now);//equivalent to now = Time (NULL)      printf ("Now Time was%d\n", now);
2. The LocalTime function    transforms the time value to the cost of time, taking into account the local time zone and daylight saving times flag; Prototype: struct TM *localtime (const time_t * calptr);  Header file <time.h> return value:        success: struct TM * struct, prototype as follows:            struct TM {                   int tm_sec;       /* seconds – The value interval is [0,59]/                    int tm_min;       /* min-value interval is [0,59]/                    int tm_hour;      /* When-the value interval is [0,23]/                    int tm_mday;     /* Date in one months-the value interval is [1,31]/                    int tm_mon;     /* Month (starting from January, 0 for January)-the value interval is [0,11]/                    int tm_year;     /* year, whose value is equal to the actual year minus 1900 */                    int tm_wday;    /* Week – The value interval is [0,6], where 0 represents Sunday, 1 represents Monday */                    int tm_yday;    /* Number of days starting January 1 per year – value interval [0,365], where 0 represents January 1 */                    int tm_isdst;    /* Daylight saving time identifier, daylight saving time tm_isdst is positive; no daylight saving time TM_ISDST is 0 */                };                This struct space is automatically assigned by the kernel, and does not release it.        Failure: NULL
Cases:
time_t now; struct TM *Tm_now; time (&= localtime (& now);p rintf ("now DateTime:%d-%d-%d%d:%d:%d\n", tm_now->tm_year+1900, tm_now->tm_mon+1, tm_ Now->tm_mday, Tm_now->tm_hour, Tm_now->tm_min, tm_now->tm_sec);

3. Gmtime ()
Header file: Time.h prototype: struct TM *gmtime (long *clock); Function: A function that converts a date and time to Greenwich (GMT) time.     Converts the information in the time_t structure referred to by the parameter TIMEP to the time date representation used by the real world, and then returns the result from the Fabric TM.     The time date returned by this function is not converted by the time zone, but UTC time. Return value returns the Structure TM represents the current UTC time
#include"stdio.h"#include"time.h"#include"stdlib.h"intMainvoid) {time_t T;structTM *gmt, *Area;tzset ();/*Tzset ()*/T=Time (NULL);= LocalTime (&t);p rintf ("Local time is:%s", Asctime (area)); GMT= Gmtime (&t);p rintf ("GMT is:%s", Asctime (GMT));return 0;}

4.mktime ()

Header file: time.h

Prototype: time_t mktime (Strcut TM * timeptr);
Function Description: Mktime () is used to convert the TM structure data referred to by the parameter timeptr to the number of seconds elapsed from the UTC time of January 1, 1970 0:0 0 seconds.
Return value: Returns the number of seconds elapsed.
Example:

  

 /*   takes time (in seconds) to convert from localtime () to struct TM re-uses Mktine () to convert the struct TM to the original number of seconds  */ # Include  <stdio.h> #include  <time.h>    main () {time_t timep;    Strcut TM  *P;    Time ( &TIMEP); printf (  " time ():%d/n  "  ,TIMEP);    P  =localtime (&TIMEP);    TIMEP  = Mktime (p); printf (  " time ()->localtime () Mktime ():%d/n   ,TIMEP);}  

5.asctime ()

Header file:

Function prototype: char *asctime (const struct TM *tblock);

Function Description: Converts the TM structure type time date to an ASCII code.

Example:

int Main () {    time_t t;     struct tm *p;    T= time(NULL);    P=localtime (&t);    printf ("%s\n", Asctime (p));     return 0 ;}

  

A summary of time-related functions under Linux

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.