Time Processing functions in C Language

Source: Internet
Author: User
Source: http://cbqcgq.blog.hexun.com/8892167_d.html

C language time functions [original 23:30:50] C language standard library functions include a series of date and time processing functions, which are described in the header file. These functions are listed below. Three types are defined in the header file: time_t, struct TM, and clock_t.

C language time functions described in the header file

Time_t time (time_t * timer );

Double difftime (time_t time1, time_t time2 );

Struct TM * gmtime (const time_t * timer );

Struct TM * localtime (const time_t * timer );

Char * asctime (const struct TM * timeptr );

Char * ctime (const time_t * timer );

Size_t strftime (char * s, size_t maxsize, const char * format, const struct TM * timeptr );

Time_t mktime (struct TM * timeptr );

Clock_t clock (void );

The following is the time function set I collected from the Internet.

Asctime (represents the time and date in string format)
Related functions
Time, ctime, gmtime, localtime
Header file
# I nclude <time. h>
Define functions
Char * asctime (const struct TM * timeptr );
Function Description
Asctime () converts the information in the TM structure referred to by the timeptr parameter to the time and date representation method used in the real world, and then returns the result in string form. This function has been converted from the time zone to the local time. The string format is: "wed Jun 30 21:49:08 1993/N"
Return Value
If you call the related time and date functions again, this string may be damaged. The difference between this function and ctime is that the input parameters are different structures.
Additional instructions
Returns a string representing the current local time and date.
Example
# Include <time. h>
Main ()
{
Time_t timep;
Time (& timep );
Printf ("% s", asctime (gmtime (& timep )));
}
Run
Sat Oct 28 02:10:06 2000
 

Ctime (Representation of time and date in string format)
Related functions
Time, asctime, gmtime, localtime
Header file
# Include <time. h>
Define functions
Char * ctime (const time_t * timep );
Function Description
Ctime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result in string form. This function has been converted from the time zone to the local time. The string format is "wed Jun 30 21: 49: 08 1993/N ". If you call the related time and date functions again, this string may be damaged.
Return Value
Returns a string representing the current local time and date.
Example
# Include <time. h>
Main ()
{
Time_t timep;
Time (& timep );
Printf ("% s", ctime (& timep ));
}
Run
Sat Oct 28 10: 12: 05 2000
 

Gettimeofday (get current time)
Related functions
Time, ctime, ftime, settimeofday
Header file
# Include <time. h>
Define functions
Int gettimeofday (struct timeval * TV, struct timezone * tz)
Function Description
Gettimeofday () will return the current time with the structure indicated by TV, and put the information of the local time zone in the structure indicated by tz.
The timeval structure is defined:
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
The timezone structure is defined:
Struct timezone {
Int tz_minuteswest;/* the time difference between Greenwich and */
Int tz_dsttime;/* state of daylight saving time */
};
The above two structures are defined in/usr/include/sys/time. h. The status of tz_dsttime is as follows:
Dst_none/* not used */
Dst_usa/* United States */
Dst_aust/* Australia */
Dst_wet/* Western Europe */
Dst_met/* Central Europe */
Dst_eet/* Eastern Europe */
Dst_can/* Canada */
Dst_gb/* Great Britain */
Dst_rum/* Romania */
Dst_tur/* Turkey */
Dst_austalt/* Australia (after January 1, 1986 )*/
Return Value
If the call succeeds, 0 is returned. If the call fails,-1 is returned. The error code is stored in errno. Note that the memory space specified by the efault pointer TV and TZ exceeds the access permission.
Example
# I nclude <time. h>

Main (){
Struct timeval TV;
Struct timezone TZ;
Gettimeofday (& TV, & Tz );
Printf ("TV _sec; % d/N", TV,. TV _sec );
Printf ("TV _usec; % d/N", TV. TV _usec );
Printf ("tz_minuteswest; % d/N", tz. tz_minuteswest );
Printf ("tz_dsttime, % d/N", tz. tz_dsttime );
}
Run
TV _sec: 974857339
TV _usec: 136996
Tz_minuteswest:-540
Tz_dsttime: 0
 

Gmtime (get current time and date)
Related functions
Time, asctime, ctime, localtime
Header file
# Include <time. h>
Define functions
Struct TM * gmtime (const time_t * timep );
Function Description
Gmtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the structure TM.
The structure TM is defined
Struct TM
{
Int tm_sec;
Int tm_min;
Int tm_hour;
Int tm_mday;
Int tm_mon;
Int tm_year;
Int tm_wday;
Int tm_yday;
Int tm_isdst;
};
Int tm_sec indicates the current number of seconds. The normal range is 0-59, but the value can be 61 seconds.
Int tm_min indicates the current score, range: 0-59
Int tm_hour, which is counted from midnight, ranges from 0 to 23.
The number of days in the current month of int tm_mday, ranging from 01 to 31.
Int tm_mon indicates the current month. The value ranges from 0 to 11 from January 1, January.
Int tm_year: The number of years since January 1, 1900
The number of days in a week for int tm_wday. The value ranges from 0 to 6 from Monday.
Int tm_yday indicates the number of days since January 1, January 1 this year. The value range is 0-365.
Int tm_isdst time saving flag
The time and date returned by this function are not converted by the time zone, but UTC time.
Return Value
The returned structure TM indicates the current UTC time.
Example
# Include <time. h>
Main (){
Char * wday [] = {"sun", "mon", "Tue", "wed", "Thu", "fri", "sat "};
Time_t timep;
Struct TM * P;
Time (& timep );
P = gmtime (& timep );
Printf ("% d", (1900 + P-> tm_year), (1 + P-> tm_mon), p-> tm_mday );
Printf ("% S % d; % d/N", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec );
}
Run
2000/10/28 sat 8:15:38
 

Localtime (obtain the current local time and date)
Related functions
Time, asctime, ctime, gmtime
Header file
# Include <time. h>
Define functions
Struct TM * localtime (const time_t * timep );
Function Description
Localtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the TM structure. For the definition of the structure TM, see gmtime (). The time date returned by this function has been converted to the local time zone.
Return Value
The returned structure TM indicates the current local time.
Example
# Include <time. h>
Main (){
Char * wday [] = {"sun", "mon", "Tue", "wed", "Thu", "fri", "sat "};
Time_t timep;
Struct TM * P;
Time (& timep );
P = localtime (& timep);/* obtain the local time */
Printf ("% d", (1900 + P-> tm_year), (L + P-> tm_mon), p-> tm_mday );
Printf ("% S % d: % d/N", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec );
}
Run
2000/10/28 sat 11:12:22
 

Mktime (the number of seconds after the time structure data is converted)
Related functions
Time, asctime, gmtime, localtime
Header file
# Include <time. h>
Define functions
Time_t mktime (strcut TM * timeptr );
Function Description
Mktime () is used to convert the TM structure data referred to by the timeptr parameter to the number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970 AD.
Return Value
Returns the number of seconds that have elapsed.
Example
/* Obtain the time (in seconds) with time (), and use localtime ()
Convert to struct TM and then use mktine () to convert struct TM to the original number of seconds */
# 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 );
}
Run
Time (): 974943297
Time ()-> localtime ()-> mktime (): 974943297
 

Settimeofday (set the current time)
Related functions
Time, ctime, ftime, gettimeofday
Header file
# Include <time. h>

Define functions
Int settimeofday (const struct timeval * TV, const struct timezone * tz );
Function Description
Settimeofday () sets the current time to the structure information indicated by TV, and the local time zone information to the structure indicated by tz. For more information, see gettimeofday (). Note: Only the root permission can use this function to modify the time.
Return Value
If the call succeeds, 0 is returned. If the call fails,-1 is returned. The error code is stored in errno.
Error Code
The root permission of eperm is not enough to call settimeofday.
The einval time zone or data is incorrect and the time cannot be set correctly.
 

Time (obtain the current time)
Related functions
Ctime, ftime, gettimeofday
Header file
# Include <time. h>
Define functions
Time_t time (time_t * t );
Function Description
This function returns the number of seconds that have elapsed since January 1, 1970 ad utc time since 00:00:00. If t is not a null pointer, this function also saves the returned value to the memory indicated by T pointer.
Return Value
The number of seconds is returned for success, and (time_t)-1) is returned for failure. The cause of the error is stored in errno.
Example
# Include <time. h>
Mian ()
{
Int seconds = Time (time_t *) null );
Printf ("% d/N", seconds );
}
Run
9.73e + 08
Bytes ------------------------------------------------------------------------------------------------

Keywords: C language time function time. h c language time function, time header function all code compilation environment: msvc6.0 1 , Time Acquisition:Use the time () function to obtain the calendar time. Its prototype is time_t time (time_t * timer); # include "stdafx. H "# include" time. H "# include" stdio. H "# include" stdlib. H "int main (void) {struct TM t; // defines the TM time structure, which is used to store the time_t t_of_day data information in the time format; // defines the time_t time structure T. tm_year = 2006-1900; // calculate the time t based on January 1, 1900. tm_mon = 6; // assign a value to the struct T. tm_mday = 1; T. tm_hour = 0; T. tm_min = 0; T. tm_sec = 1; T. tm_isdst = 0; t_of_day = mktime (& T); // use the mktime () function to convert the time represented in the TM structure to the calendar time: time_t Type Change Quantity. The function prototype is as follows: time_t mktime (struct TM * timeptr); The ctime () function (the parameter is in the time_t structure) displays the time in a fixed format, the returned value is a char * string. Return 0 ;} 2 The storage of time is stored through the predefined two structures:1. The calendar time is represented by the time_t data type. The time expressed by time_t (calendar time) is from a time point (for example, January 1, 1970 00:00:00) the number of seconds. In time. h, we can also see that time_t is a long integer: # ifndef _ time_t_definedtypedef long time_t; /* Time Value */# DEFINE _ time_t_defined/* avoid repeatedly defining time_t */# endif2. In Standard C/C ++, we can use the TM structure to obtain the date and time. The TM structure is in time. the definition in H is as follows: struct TM {int tm_sec;/* Second-value range: [0, 59] */INT tm_min; /* minute-value range: [] */INT tm_hour;/* Time-value range: [] */INT tm_mday; /* date in a month-value range: [1, 31] */INT tm_mon;/* month (starting from January 1, January, 0 indicates January 1, January) -The value range is [0, 11] */INT tm_year ;/* Year. The value is equal to the actual year minus 1900 */INT tm_wday;/* the week-value range is [], where 0 indicates Sunday, 1 indicates Monday, and so on */INT tm_yday; /* The number of days starting from January 1, 0,365-the value range is []. 0 indicates January 1, and so on. */INT tm_isdst;/* indicates the identifier of the hour, when the sequence is executed, tm_isdst is positive. When the sequence is not implemented, the tm_isdst value is 0. If the sequence is unknown, the tm_isdst () value is negative. */}; 3 , Time display:Time. the H header file provides the asctime () function (the parameter is a TM structure pointer) and ctime () function (the parameter is a time_t structure) to display the time in a fixed format, both return values are char * strings. The returned time format is: day of the week, month, hour, minute, second, year, N, 0, time. H also provides two different functions to convert the calendar time (an integer expressed in time_t) to the time format that we usually see separately displaying year, month, day, hour, minute, and second. TM: struct TM * gmtime (const time_t * timer); The gmtime () function converts the calendar time to the world's Standard Time (GMT ), returns a TM struct to save the time struct TM * localtime (const time_t * timer). The localtime () function converts the calendar time to the local time # include <stdafx. h> # include <time. h> # include <stdio. h> # include <stdlib. h> int main (void) {struct TM * local, * PTR; // defines the TM structure pointer to store the time information time_t; // Time structure or object t = time (null); // obtain the current system calendar time // use the time () function to obtain the calendar time ), // Its prototype is time_t time (time_t * timer); Local = localtime (& T); // localtime () the function is to convert the calendar time to the local time printf ("Local hour is: % d/N", local-> tm_hour ); // printf ("UTC hour is: % d/N", local-> tm_hour); // local = gmtime (& T ); // The gmtime () function converts the calendar time to the world standard time (Greenwich Mean Time). // it returns a TM struct to save the time PTR = gmtime (& T ); // convert the calendar time to the world standard time printf ("the UTC time is % S/N ", asctime (PTR); // format the output World Standard Time printf (" The local time is % s/n ", ctime (& T )); // output the local time/* asctime () function (the parameter is a TM structure pointer) and ctime () function (the parameter is a time_t structure) to display the time in a fixed format, both return values are char * strings. The returned time format is: day of the week, month, hour, minute, second, year, N/0 */return 0 ;} 4 , Time difference calculation:Function used: the timing function in C/C ++ is clock (), and the related data type is clock_t. The clock function is defined as follows in msdn: clock_t clock (void); the function returns from "enable this program process" to "program call clock () the number of clock units (clock tick) between functions. clock_t is a long integer and the Data Type of the storage time. In time. in the H file, a constant clocks_per_sec is also defined to indicate the number of clock units in one second. Its definition is as follows: # define clocks_per_sec (clock_t) 1000) every 1‰ seconds (1 millisecond), the value returned by calling the clock () function is increased by 1, and the length of the clock timing unit is 1 millisecond, so the timing accuracy is also 1 millisecond, can we change the definition of clocks_per_sec to a greater value, so that the timing accuracy is higher? This is not acceptable. In standard C/C ++, the minimum unit of time is one millisecond. Double difftime (time_t time1, time_t time0); this function is used to calculate the time difference. # Include "stdafx. H "# include" time. H "# include" stdio. H "# include" stdlib. H "int main (void) {time_t c_start, t_start, c_end, t_end; c_start = clock (); t_start = Time (null); System (" pause "); c_end = clock (); t_end = Time (null); printf ("the pause used % F The MS by time (). /n ", difftime (c_end, c_start); printf (" the pause used % f s by clock (). /n ", difftime (t_end, t_start); System (" pause "); Return 0 ;}5, hour Other purposes are used as the seed of random numbers. Since time is actually a long integer of the double type, it is obtained through the time (null) function, as srand (Time (null )) is better. # Include "stdafx. H "# include" time. H "# include" stdio. H "# include" stdlib. H "int main (void) {srand (Time (null); // sets the seed. If you comment out this function, for (INT I = 0; I <100; I ++) {printf ("% d/T", Rand ());} system ("pause"); Return 0 ;}

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.