Operations on time functions in C Language

Source: Internet
Author: User
Tags month name ranges

Guidance:
Calendar time: indicates the calendar time, which is expressed by the number of seconds elapsed from a standard time point to the current time. This standard time point varies with compilers, but for a compilation system, this standard time point remains unchanged, the calendar time corresponding to the time in the compilation system is measured by the standard time point. Therefore, it can be said that the calendar time is relative time, But no matter which time zone you are in, the calendar time is the same for the same standard time point at the same time point.
In the C language, we can understand the time in this way. First, we get the calendar time and store the calendar time as a long data. That is, the return value of time (null) is the long data.
There is a struct in C language, struct TM.
# Ifndef _ tm_defined/* If NO _ tm_defined */
Struct TM {
Int tm_sec;/* Second-value range: [0, 59] */
Int tm_min;/* minute-value range: [0, 59] */
Int tm_hour;/* hour-value range: [0, 23] */
Int tm_mday;/* date in a month-value range: [] */
Int tm_mon;/* month (from January 1, 0 represents January 1,)-value range: [] */
Int tm_year;/* year, whose value is equal to the actual year minus 1900 */
Int tm_wday;/* day-value range: [0, 6], where 0 indicates Sunday, 1 indicates Monday, and so on */
Int tm_yday;/* Number of days from January 1, 0,365 each year-value range: [1,]. 0 indicates January 1, and so on */
Int tm_isdst;/* identifier of the sequence. When the sequence is executed, the sequence 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. */
};
# DEFINE _ tm_defined/* prevent repeated definition of struct TM */
# Endif
After reading the structure above, do you feel that the time operation is much simpler? You don't need to worry about complicated calendar time. You just need to use the function to fill the calendar time into struct TM.
Which functions can complete the above functions?
Struct TM * gmtime (const time_t * timer);/* enter the calendar time and convert it to Greenwich mean time */
Struct TM * localtime (const time_t * timer);/* fill in the calendar time in the struct, which is the local time */
For example, we only want to make some simple printing, rather than completely customized, that is, their return values are strings.
Char * asctime (const struct TM * timeptr);/* pointer to the form parameter of the function as struct TM
Char * ctime (const time_t * timer);/* The form parameter of the function is the calendar time */
The time format is completely customized.
The strftime () function that implements this function. Its prototype is
Size_t strftime (
Char * strdest,
Size_t maxsize,
Const char * format,
Const struct TM * timeptr
);
The following are some basic operations of the function:
The strftime () function is similar to sprintf (): recognizes the command set starting with the percent sign (%), and formats the output result in a string. The formatting command specifies the exact representation of various date and time information in strdest. Other characters in the format string are put into the string as they are. Format Commands are listed below, which are case sensitive.
% A abbreviation of the day of the week
% A full name of the day of the week
Abbreviation of % B month
% B full name
% C standard date time string
The last two digits of the Year % C
% D indicates the day of the month in decimal format.
% D month/day/year
% E indicates the day of the month in decimal format in the two-character field
% F-month-day
The last two digits of the Year % G. The year is based on the week.
% G year, based on the week Year
% H abbreviated month name
% H in 24-hour format
% I 12-hour
% J indicates the day of the year in decimal format.
Month in % m decimal format
% M decimal number of minutes
% N new line operator
% P equivalent display of local am or PM
% R 12 hours
% R display hour and minute: hh: mm
% S decimal seconds
% T horizontal Tab
% T display time in seconds: hh: mm: SS
% U the day of the week, Monday is the first day (value ranges from 0 to 6, Monday is 0)
% U indicates the week of the year. Sunday is regarded as the first day (the value ranges from 0 to 53)
% V indicates the week of the year, which is based on the year of the week.
% W decimal indicates the day of the week (the value ranges from 0 to 6, and Sunday is 0)
% W indicates the week of the year. Monday is the first day (from 0 to 53)
% X standard date string
% X standard time string
% Y does not contain the century's decimal year (the value ranges from 0 to 99)
% Y indicates the decimal Year of the century.
% Z, % Z Time Zone name. If the time zone name cannot be obtained, an empty character is returned.
% Percent sign
However, it is verified that not every option can be used.
Example
The following program displays the current complete date:
# Include
# Include
Void main (void)
{
Struct TM * newtime;
Char tmpbuf [128];
Time_t lt1;
Time (newtime = localtime (strftime (tmpbuf, 128, "Today is % A, day % d of % B in the year % Y./N", newtime );
Printf (tmpbuf );
}
Running result:
Today is Saturday, day 30 of July in the year 2005.
Length of computing duration
1)
Sometimes, in practical applications, we need to calculate the duration of an event, for example, calculating the typing speed. In the timing section of section 1st, I have used the clock function as an example. The clock () function can be precise to milliseconds. At the same time, we can also use the difftime () function, but it can only be accurate to seconds. The function is defined as follows:
Double difftime (time_t time1, time_t time0 );
Although the time interval calculated in seconds returned by the function is of the double type, this does not indicate that the time has the same precision as the double type, this is determined by its parameters (time_t is calculated in seconds ). For example, the following program:
# Include "time. H"
# Include "stdio. H"
# Include "stdlib. H"
Int main (void)
{
Time_t start, end;
Start = Time (null );
System ("pause ");
End = Time (null );
Printf ("the pause used % F seconds./N", difftime (end, start); // system ("pause ");
Return 0;
}
The running result is:
Press any key to continue...
The pause used 2.000000 seconds.
Press any key to continue...
As you can imagine, the pause time is not so good as 2 seconds. In fact, you include "// printf (" the pause used % F seconds./N ", end-Start );
2)
# Include "stdio. h"
# Include "stdlib. H"
# Include "time. h"
Int main (void)
{
Long I = 0000000l;
Clock_t start, finish;
Double duration;
/* Measure the duration of an event */
Printf ("time to do % LD empty loops is", I );
Start = clock ();
While (I --);
Finish = clock ();
Duration = (double) (finish-Start)/clocks_per_sec;
Printf ("% F seconds/N", duration );
System ("pause ");
}
On the author's machine, the running result is as follows:
Time to do 10000000 empty loops is 0.03000 seconds
We can see that the length of the clock timing unit is 1 millisecond, and the timing accuracy is also 1 millisecond. Can we change the definition of clocks_per_sec and define it larger, so that the timing accuracy is higher? By trying, you will find that this is not the case. In standard C/C ++, the minimum unit of time is one millisecond.
The decomposition time is converted to the calendar time.
The decomposition time here is the time structure stored by the year, month, day, hour, minute, second, and other components. In C/C ++, It is the TM structure. We can use the mktime () function to convert the time expressed in the TM structure to the calendar time. The function prototype is as follows:
Time_t mktime (struct TM * timeptr );
The returned value is the converted calendar time. In this way, we can first set a decomposition time and then perform operations on this time. The following example shows the day of the week on January 1, July 1, 1997:
# Include "time. H"
# Include "stdio. H"
# Include "stdlib. H"
Int main (void)
{
Struct tm t;
Time_t t_of_day;
T. tm_year = 1997-1900;
T. tm_mon = 6;
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 );
Printf (ctime (& t_of_day ));
Return 0;
}
Running result:
Tue Jul 01 00:00:01 1997
Now, with the mktime () function, can we operate any time before now? Can you use this method to calculate the day of the week on January 1, August 15, 1945? The answer is no. Because this time was before January 1, January 1, 1970, in most compilers, such programs can be compiled and passed, but will end abnormally during runtime.

This article is transferred from
Http://blog.cfan.com.cn/html/88/10088_itemid_57440.html

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.