/* * Split given time into your years,month,day,hours,minutes,second */ void split_time(int timein, int* year, int* month, int* day, int* hour, int* minute, int *second){ struct tm now_tm; struct tm then_tm; time_t now = time( NULL); time_t then = now + timein; // add timein seconds to the current time now_tm = *localtime( &now); then_tm = *localtime( &then); mktime(&now_tm); mktime(&then_tm); *year = max(then_tm.tm_year - now_tm.tm_year,0); *month = max(then_tm.tm_mon - now_tm.tm_mon,0); *day = max(then_tm.tm_mday - now_tm.tm_mday,0); *hour = max(then_tm.tm_hour - now_tm.tm_hour,0); *minute = max(then_tm.tm_min - now_tm.tm_min,0); *second = max(then_tm.tm_sec - now_tm.tm_sec,0); } /* * Create a "timestamp" with the given argument */ int solve_time(char * modif_p){ int totaltime=0, value=0; struct tm then_tm; time_t now = time( NULL); time_t then = now; time_t diff; then_tm = *localtime( &then); while (modif_p[0] != '\0') { value = atoi(modif_p); if (value == 0) modif_p++; else { if (modif_p[0] == '-' || modif_p[0] == '+') modif_p++; while (modif_p[0] >= '0' && modif_p[0] <= '9') modif_p++; if (modif_p[0] == 's') { then_tm.tm_sec += value; modif_p++; } else if (modif_p[0] == 'n') { then_tm.tm_min += value; modif_p++; } else if (modif_p[0] == 'm' && modif_p[1] == 'n') { then_tm.tm_min += value; modif_p = modif_p + 2; } else if (modif_p[0] == 'h') { then_tm.tm_hour += value; modif_p++; } else if (modif_p[0] == 'd' || modif_p[0] == 'j') { then_tm.tm_yday += value; modif_p++; } else if (modif_p[0] == 'm') { then_tm.tm_mon += value; modif_p++; } else if (modif_p[0] == 'y' || modif_p[0] == 'a') { then_tm.tm_year += value; modif_p++; } else if (modif_p[0] != '\0') { modif_p++; } } } diff = mktime(&then_tm); totaltime = difftime(diff,now); // totaltime = year*12*30*24*60*60 + month*30*24*60*60 + day*24*60*60 + hour*60*60 + minute*60 + second; //in second ShowDebug("totaltime = %d then = %d, now = %d diff= %d then>now=%d\n",totaltime,then,now,diff,then>now); return totaltime; }