Cocos2d-x3.3 acquisition time
1. Obtain the time
Struct tm * tm;
Time_t timep;
# If (CC_TARGET_PLATFORM = CC_PLATFORM_WIN32)
Time (& timep );
# Else
Struct timeval TV;
Gettimeofday (& TV, NULL );
Timep = TV. TV _sec;
# Endif
Tm = localtime (& timep );
Int year = tm-> tm_year + 1900; // year
Int month = tm-> tm_mon + 1; // month
Int day = tm-> tm_mday; // day
2. Obtain the number of days between two dates
1. Get the number of days of the current date
GetAbsDays (MyDate x ){
Int I;
Int month_day [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31 };
Int year = x. year-1;
Int days = year * 365 + year/4-year/100 + year/400;
If (x. year % 4 = 0 & x. year % 100! = 0 | x. year % 400 = 0)
Month_day [1] ++;
For (I = 0; I <x. month-1; I ++)
Days + = month_day [I];
Days + = x. day-1;
Return days;
}
2. GetAbsDays (MyDate1)-SignDialog: GetAbsDays (MyDate2 );
3. Calculate the time difference using the timestamp
1. Obtain the current time (in seconds)
Time_t t = time (0 );
2. save time
Data data;
Data. copy (unsigned char *) & t, sizeof (t ));
UserDefault: getInstance ()-> setDataForKey ("time_1", data );
3. Obtain the time
Auto d = UserDefault: getInstance ()-> getDataForKey ("time_1", Data ());
Time_t lastExitTime = * (time_t *) d. getBytes ());
Time_t currentTime = time (0 );
Time_t time = currentTime-lastExitTime;
4. converted to minutes
Int k = time/60;