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

Asked

Viewed 284 times

3

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

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?

1 answer

2


There is a this struct used to manipulate dates.

I think what you want is something like that:

#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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.