Kernel_mktime () function analysis

Source: Internet
Author: User

************************* *******/

Struct TM {
Int tm_sec;/* seconds, 0-59 */
Int tm_min;/*, 0-59 */
Int tm_hour;/*, 0-23 */
Int tm_mday;/* Days, 1-31 */
Int tm_mon;/* number of months, 0-11 */
Int tm_year;/* years from 1900 */
Int tm_wday;/****** Number of days from Sunday 0-6 ****/
Int tm_yday;/***** Number of days since January 1, January 1, 0-365 */
Int tm_isdst;/* Indicates whether to use the daylight saving time, which is a positive number */
};

/** Kernel_mktime () function, used to convert the current time to the benchmark time on January 1, 1970, in seconds

That is, to unify a time standard for the system, that is, the current Boot time of the system

/*************************************** ************************************/

# Define minute 60/* number of seconds in one minute */
# Define hour (60 * minute)/* The number of seconds in an hour */
# Define Day (24 * hour)/* number of seconds per day */
# Define year (365 * day)/* number of seconds in a year */
Static int month [12] = {/*******************************
0,
Day * (31),/******** this array stores ********/
Day * (31 + 29),/******** number of days in a leap year **********/
Day * (31 + 29 + 31),/******* because on March 29, February, the leap year is 29 days ******/
Day * (31 + 29 + 31 + 30),/********, and the year February is 28 days ********/
Day * (31 + 29 + 31 + 30 + 31 ),
Day * (31 + 29 + 31 + 30 + 31 + 30 ),
Day * (31 + 29 + 31 + 30 + 31 + 30 + 31 ),
Day * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 ),
Day * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 ),
Day * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 31 + 31 ),
Day * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 31 + 31 + 30 + 30)
};

/******************* The calculation is based on 00:00:00, January 1 ***/

/******************* Here, you must note that the processing of year and month ********** *******/
Long kernel_mktime (struct TM * TM)
{
Long res;
Int year;

Year = TM-> tm_year-70;

/*********** The current computing time is several years from January 1, 1970 ***********/
Res = year * year + day * (Year + 1)/4 );

/*************** Because year defines the number of seconds in the first day

We also need to add up to 1970 for several leap years, and add one more second for each leap year.

Number, for example, Today is July 15, 1973, day * (Year + 1)/4) = 1, that is

The number of leap years passed in this period is one (actually 1972)

, So you need to add one more day */

/*************************************** *********/
Res + = month [Tm-> tm_mon];

/*************************************** *********/

Now let's assume it's a leap year first, and then calculate 0 month 1 day 00:00:00 to the current

The number of seconds at 00:00:00. For example, if it is 0 months, it is 0 seconds.

It is July 22, (in fact, July 15), which is month [7] seconds.

**************************************** *********/
If (Tm-> tm_mon> 1 & (Year + 2) % 4) RES-= Day;

/***** It is assumed that the current year is a leap year.

It takes several seconds until 00:00:00 of the current month.

The first day of the month. For example, it is March 1973, not a leap year,

The above is based on day * (31 + 29), so it must be deducted

One day, and one day will not be deducted in March 1972.

**************************************** *********/
Res + = day * (Tm-> tm_mday-1 );
Res + = hour * TM-> tm_hour;
Res + = minute * TM-> tm_min;
Res + = TM-> tm_sec;
Return res;
}

 

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.