Preliminary understanding and use of C-language time-date functions

Source: Internet
Author: User
Tags local time time and date

In the header file time.h of the C language, data types and operations for date and time operations are defined.

Here we refer to the time.h in MinGW (minimalist GNU for Windows).

Let's start by looking at the data types declared by Time.h:

1typedefLongclock_t;2 3 typedef __int32 __time32_t;4 typedef __time32_t time_t;5 6 /*7 * A structure for storing all kinds of useful information about the8 * Current (or another) time.9  */Ten structTM One { A     intTm_sec;/*seconds:0-59 (K&r says 0-61?)*/ -     intTm_min;/*minutes:0-59*/ -     intTm_hour;/*Hours since midnight:0-23*/ the     intTm_mday;/*Day of the month:1-31*/ -     intTm_mon;/*Months *since* january:0-11*/ -     intTm_year;/*years since 1900*/ -     intTm_wday;/*Days since Sunday (0-6)*/ +     intTm_yday;/*Days since Jan 1:0-365*/ -     intTM_ISDST;/*+1 Daylight Savings time, 0 No DST, + *-1 don ' t know*/ A};

Clock_t is the number of milliseconds.

time_t is the number of seconds.

A struct TM is a structure that holds the current time and date information.

Then let's look at the declaration and function of each function. (Refer to "C Programming Language" (second edition))

  1. clock_t clock (void): Returns the time (in milliseconds) that the corresponding process consumes the processor after the program starts executing, and we can use the clock ()/clocks_per_sec to get the number of seconds. where macro clocks_per_sec is 1000.
  2. time_t time (time_t *TP): Returns the number of seconds in the current distance from January 1, 1970 0 o'clock, and assigns the number of seconds to the address to which TP points. Returns 1 if the time is not available.
  3. Double Difftime (time_t time2, time_t time1): Returns the number of seconds between Time2 and time1.
  4. time_t mktime (struct TM *TP): reads the struct TM type that the TP points to, returning the current time in seconds from 0 o'clock January 1, 1970. If it cannot be represented, returns-1.
  5. Char *asctime (const struct TM *TP): Converts the structure of the TP point to a string form, such as Fri 07 11:41:56 2014. This function returns a pointer to a static cache that may be overwritten by other calls. The return value of the function shown below 6,7,8 also has the same problem.
  6. Char *ctime (const time_t *TP): Converts the time that TP points to a string form. It is equivalent to Asctime (localtime (TP)).
  7. struct TM *localtime (const time_t *TP): Converts the time at which TP points to the local time, the data is a struct TM structure, and returns a pointer to a static cache (which may be overwritten by other calls, especially gmtime (), please note! )
  8. struct TM *gmtime (const time_t *TP): Converts the time TP points to local time, the data is a struct TM structure, returns a pointer to a static cache (which may be overwritten by other calls, especially localtime (), please note! )
  9. size_t strftime (char *s, size_t smax, const char *FMT, const struct TM *TP): The function converts the time structure pointed to by the TP into a string according to the format in the FMT, copying the memory in S point, up to the SMA x characters are written to s, including ' + '. The function returns 0 if the content of the FMT format is more than Smax. Otherwise, the function returns the number of characters copied, not including '/'. If the FMT is "%a", you should write "Fri" to S, and if Smax is greater than or equal to 4, the function returns 3, otherwise the copy fails and the function returns 0. The format of the FMT is shown on page No. 235 of the C programming language (second edition).

Simple code to invoke the above function:

1#include <time.h>2#include <stdio.h>3#include <stdlib.h>4 5 #defineRound_to_int (x) ((INT) ((x) + (((x) >0) 0.5:-0.5))6 7 int8MainintargcChar**argv)9 {Ten     //Time () One time_t T1; Atime_t*TP; -TP = (time_t*) malloc (sizeof(time_t)); -T1 =Time (TP); theprintf"Current Calendar time (number of seconds from 0 o'clock January 1, 1970):%u\n", T1);  -     //Time (), Difftime () - time_t T2; -t2 =Time (TP); +     DoubleDifft =difftime (T2, T1); -printf"Current Calendar time (number of seconds from 0 o'clock January 1, 1970):%u, two calendar time difference:%f (sec) \ n", T2, DIFFT);  +     //localtime (), struct TM, Asctime (), CTime () A     //mktime (), converting a struct TM to a time_t type at     structTM *LOCALTP; -LOCALTP =localtime (TP); -printf"Daylight Saving Time:%d, distance from January 1:%d (days), distance Sunday:%d (days), distance from 1900:%d years, \ n" -            "distance January:%d (month), today:%d%d seconds \ %d", LOCALTP-&GT;TM_ISDST, localtp->Tm_yday, -Localtp->tm_wday, Localtp->tm_year, Localtp->tm_mon, Localtp->tm_mday, localtp->Tm_hour, -Localtp->tm_min, localtp->tm_sec);  inprintf"render local time in Asctime:%s", Asctime (LOCALTP)); -printf"render local time in CTime:%s", CTime (TP));//CTime (TP) = = Asctime (localtime (TP)) totime_t t3 =mktime (LOCALTP); +printf"Current Calendar time (number of seconds from 0 o'clock January 1, 1970):%u\n", T3); -     //gmtime (), when we use Gmtime (), we actually change the address that LOCALTP points to.  the     //because Asctime (), CTime (), Gmtime (), localtime () returns a pointer to a static object that can be overridden by another call *     structTM *gmtp; $GMTP =gmtime (TP);Panax Notoginsengprintf"UTC time:%s", Asctime (GMTP));  -time_t T4 =mktime (GMTP); theprintf"Current Calendar time (number of seconds from 0 o'clock January 1, 1970):%u\n", T4); +printf"local time and UTC time difference:%d (hours) \ n", (int) (Difftime (T3, T4)/3600));  A     //strftime () the     Char* BUF = (Char*) malloc (1024x768); +     if(BUF = =NULL) { -printf"Out of space!\n"); $         return-1; $     } -     intcount; -     if(Count = strftime (buf,1024x768,"%Z%y Year%m month%d day%h%m minutes%s seconds", localtime (TP)))! =0) { theprintf"formatted Word count (2 in Chinese):%d\n formatted time:%s\n", Count, buf);  -     }Wuyi     //clock () the     //sleep (+); -     intNclock =clock (); Wuprintf"Processor Time elapsed after program execution started:%d (milliseconds), approx.%d (sec) \ n",  -Nclock, Round_to_int ((Double) nclock/clocks_per_sec)); AboutSystem"Pause"); $     return 0; -}

Preliminary understanding and use of C-language time-date functions

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.