C Problem for Time Difference

We know that time in digital devices is represented in form of HH:MM:SS.  It is hard to find the difference between two time values.  We have to write code that may find difference between two time values. 


Here, for the purpose, we convert time string into equivalent seconds and after finding the difference, seconds shall be converted into HH:MM:SS format.  See the following codes:

#include <stdio.h>

#include <string.h>


int TimeToNumber(char *s) {

    int n = 0, i = 0, t = 0;

    int tF[3] = {0, 0, 0};

    if (strlen(s) > 8) {

        printf("Invalid time format. Format should be HH:MM:SS.");

        return -1;

    }

    while (s[i] != '\0') {

        if (s[i] == ':') {

            n++;

        }

        if ((s[i] < '0' || s[i] > '9') && (s[i] != ':')) {

            printf("Invalid time format. Should have numeric and : symbols.");

            return -1;

        }

        i++;

    }

    if (n != 2) {

        printf("Invalid time format. Should have only two : symbols.");

        return -1;

    }

    int v = 0;

    for (i = 0; i <= strlen(s); i++) {

        if (s[i] == ':' || s[i] == '\0') {

            tF[v] = t;

            v++;

            t = 0;

        } else {

            t = t * 10 + (s[i] - 48);

        }

    }

    if ((tF[0] < 0 || tF[0] > 23) || \

        (tF[1] < 0 || tF[1] > 59) || \

        (tF[2] < 0 || tF[2] > 59)) {

        printf("Invalid time format.");

        return -1;

    }

    return ((tF[0])*3600 + (tF[1])*60 + (tF[2]));

}


/*Reverse string function.*/

char *revesrStr(char * str) {

    char *rs_val = malloc(5);

    int i = 0;

    for (i = 0; i < strlen(str); i++) {

        rs_val[i] = str[strlen(str) - i - 1];

    }

    rs_val[i] = '\0';

    return rs_val;

    free(rs_val);

}


/*Convert integer into array*/

char *timeIntToStr(int n) {

    char *tim_ita = malloc(5);

    int i = 0;

    int v = 0;

    if (n == 0) {

        return strcpy(tim_ita, "00");

    }

    while (n > 0) {

        tim_ita[i] = (n % 10) + 48;

        n /= 10;

        i++;

    }

    if (i == 1) {

        tim_ita[i] = '0';

        i++;

    }

    tim_ita[i] = '\0';

    return revesrStr(tim_ita);

    free(tim_ita);

}


char *NumberToTime(int n) {

    int tF[3] = {0, 0, 0};

    char *tFS = malloc(10 * sizeof (char));

    if (n < 0 || n > 86399) {

        printf("Invalid time.");

        strcpy(tFS, "MISS");

        return tFS;

        free(tFS);

    }

    tF[2] = (n % 60);

    n /= 60;

    tF[1] = (n % 60);

    n /= 60;

    tF[0] = n;

    strcpy(tFS, timeIntToStr(tF[0]));

    strcat(tFS, ":");

    strcat(tFS, timeIntToStr(tF[1]));

    strcat(tFS, ":");

    strcat(tFS, timeIntToStr(tF[2]));

    return tFS;

    free(tFS);

}


int main() {

    printf("%s\n", NumberToTime((TimeToNumber("17:15:20") - TimeToNumber("8:10:59"))));

    return 0;

}