viewing paste Unknown #5878 | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*
 * 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;
}
Viewed 1228 times, submitted by lighta.