Linux C Time function Application _c language

Source: Internet
Author: User

About the type of time:

time_t Long representing the number of seconds elapsed from January 1, 1970 to the present.

#include <time.h>
struct TM {
     int tm_sec;    /* seconds – The value range is [0,59]
     /int tm_min;    /* Divide-take the value range is [0,59]/
     int tm_hour;   /* Time-value interval is [0,23]/
     int tm_mday;   * * One months date-value range is [1,31]/
     int tm_mon;    /* Month (starting from January, 0 represents January)-the value range is [0,11]/
     int tm_year;   /* Year with the value equal to the actual year minus 1900
     /int tm_wday;   * Week – The value range is [0,6], where 0 represents Sunday, 1 represents Monday, and so on
     /int tm_yday;   /* The number of days from January 1 of each year – the value interval is [0,365], of which 0 represents January 1 and 1 is January 2

 };

General use time_t: Define time_t now to return the current time (in seconds) through the present =time (NULL), in the form of "time_t Time" (time_t*), or to get "time (&now)" like this. Users look at this large long data is meaningless, generally through this function to get time to calculate the difference is more convenient. Srand (Time (NULL)) Gets the use of the seed in this number of seconds. unsigned

To get a clear picture of the current year and year information, you can not pass this time_t, need to use struct TM to display, and the time_t and struct TM conversion function is localtime (time_t*), return a struct tm* type, Note is the pointer type. You can then get the members of the TM, as above, for shaping. But note that to get the current date, you need to do some tm_year+1900 is the current year, Tm_mon+1 is the current month

If you want to get a string of time directly.

With char * asctime (const struct TM * timeptr); According to struct TM structure

or use char* CTime (time_t* t); According to time_t

The form of output is like this "Tue 6 13:53:16 2015"

Think about why we just need to define a struct tm* pointer, and no application space, but can get its members? Because the Locatime return pointer is the address of a static variable. Likewise asctime is the same principle.

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.