Detailed explanation of C function Time Conversion

Source: Internet
Author: User

We often encounter time processing problems, such as developing a schedule function or filtering files based on the modification time. Windows API provides Get * Time () functions to obtain the current Time, but does not provide Time conversion, for example, if we want to get a time of 2 years, 4 months, and 5 days from the current time, we have to calculate it by ourselves. However, there is a problem here. If the number of days to be subtracted is greater than the number of days in the current month, the number of days will become a negative value. To solve this problem, we calculate the offset based on the number of days in different months, and change the month and year at the same time. However, this method is very troublesome, because the number of days each month is different and you still need to consider the leap year and year issues. In fact, C's Time series functions can solve this problem well,

1. First, use the TM structure for the required time offset.

2. Then, use the mktime function to convert the TM structure to the second value starting from 1900.1.1.

3. Use localtime to convert the number of seconds to the TM structure.

The sample code is as follows:
# Include "stdafx. h"
# Include <Windows. h>
# Include <time. h>
# Include <iostream>
Using namespace std;
Void OffsetDateTime (const struct tm * inST, struct tm * outST,
Int dYears, int dMonths, int dDays,
Int dHours, int dMinutes, int dSeconds)

{
If (inST! = NULL & outST! = NULL)
{
// Offset the current time
OutST-> tm_year = inST-> tm_year-dYears;
OutST-> tm_mon = inST-> tm_mon-dMonths;
OutST-> tm_mday = inST-> tm_mday-dDays;
OutST-> tm_hour = inST-> tm_hour-dHours;
OutST-> tm_min = inST-> tm_min-dMinutes;
OutST-> tm_sec = inST-> tm_sec-dSeconds;
// The total number of seconds from the conversion to 1900.1.1
Time_t newRawTime = mktime (outST );
// Converts the number of seconds to the time struct.
OutST = localtime (& newRawTime );
}
}

Int _ tmain (int argc, _ TCHAR * argv [])
{
Time_t rawtime;
Struct tm * st;
// Obtain the current local time
Time (& rawtime );
St = localtime (& rawtime );
Cout <st-> tm_year <"-" <st-> tm_mon <"-" <st-> tm_mday <endl;
// Calculate the time offset
Struct tm outst;
OffsetDateTime (st, & outst, 2, 3, 20, 0, 0, 0 );
Time_t newTime = mktime (& outst );
Cout <outst. tm_year <"-" <outst. tm_mon <"-" <outst. tm_mday <endl;
Cout <"rawTime:" <rawtime <endl <"newTime:" <newTime <endl;
Return 0;
}

Related Article

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.