How do I get the time in seconds of a date in the string format?

3

I know that the time(0) function returns me the seconds from the first of January 1970, in which case the user would enter a date (dd-mm-yyyy or in any format due to system limitation) check which date is the oldest.

Are there libraries that work with this? What would be the best way to pass a date to the system to turn it into seconds?

    
asked by anonymous 15.11.2016 / 21:23

1 answer

2

There is this struct that is used to manipulate dates.

I think what you want is something like this:

#include <stdio.h>
#include <time.h>

int main()
{
    struct tm tm = {};  // inicializa com zeros
    int dia, mes, ano;

    scanf(" %2d-%2d-%4d", &dia, &mes, &ano);
    tm.tm_mday = dia;
    tm.tm_mon  = mes - 1;
    tm.tm_year = ano - 1900;

    time_t tempo = mktime(&tm);
    puts(asctime(&tm));
    printf("%d\n", tempo);
}
    
15.11.2016 / 21:48