How to handle time and date in C language

Source: Internet
Author: User
Tags range time and date

Chuck Allison is a software architect at the Family History Research division of the Church of Christ, Salt Lake City St. Latter Day Church headquarters. He has a Bachelor of Mathematics and a master's degree in mathematics. He has been programming since 1975, and since 1984 he has been engaged in the teaching and development of C language. His current interest is in object-oriented technology and its education. He is a member of the X3j16,ansi C + + standardization committee.

Most operating systems have the means to get the current date and time. By defining the library functions in Time.h, ANSI C can get this information in many different forms. function time returns a value of type time_t (usually long) that encodes the current date and time during the run. You can then pass this return value to other functions that can decode and format the value.

The program in Listing 1 uses functions time,localtime and strftime to output the current date and time in different forms. The function localtime decodes the encoded time into the following struct:

struct tm
{
    int tm_sec; /* (0 - 61) */
    int tm_min; /* (0 - 59) */
    int tm_hour; /* (0 - 23) */
    int tm_mday; /* (1 - 31) */
    int tm_mon; /* (0 - 11) */
    int tm_year; /* past 1900 */
    int tm_wday; /* (0 - 6) */
    int tm_yday; /* (0 - 365) */
    int tm_isdst; /* daylight savings flag */
};

Every time you call localtime, it rewrites a static structure and returns the address of the struct (so that you can only get one of these structures in a program at a time and not make a significant copy). function CTime Returns a pointer to a static string that contains the full time and date in a standard format. Strftime formats a string based on the user's specified format (for example,%a represents the name of each day of the week). Table 1 lists the full list of format descriptors.

Time/Date Operations

The time/date can be calculated by changing the value in the TM structure. The program in Listing 2 shows how to calculate the date of the Future Day and the program execution time calculated in seconds. Note the syntax of the function time (the parameter time_t is passed in by the address, not as the return value of the function). The function mktime changes the value of the TM structure so that the date and time are within a suitable range, and then the Day-of-week (Tm_wday) and Day-of-year (tm_yday) fields are updated accordingly. Mktime the value of the date and time in the TM structure within the appropriate range, and updates the value of day of Week (Tm-wday) and Days of year (Tm-yday) accordingly. This happens when a date is beyond the scope that your implementation can support. For example, my MS-dos compiler cannot encode a date before January 1970. The function Asctime returns the standard string for the time described by the TM parameter (so CTime (&tval) is equal to Asctime (localtime). function Difftime Returns the difference of two time_t in seconds.

If you need to deal with a date that is out of system range, or if you need to calculate an interval of two dates instead of seconds, you need to design your own date code. Applications in Listing 3 through Listing 5 demonstrate techniques for determining the number of years, months, and days of two date intervals by using a simple month-day-year structure. Subtract dates like you did in primary school (for example, subtract the number of days first, borrow the number of months if necessary, and so on). Note that the skipped years are counted. For brevity, the Date_interval function assumes that the date is valid and that the first date is before the second date. The function returns a pointer to the static date structure that contains the answers we want.

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.