C: How to generate a date structure

1

Good afternoon.

I'm having difficulty with handling dates in C. I'm using the time.h library to tinker with dates.

I need to create a struct tm that contains the date of the day (example) 1/29/1950, and then store its time_t value in a variable. The problem is that I create the structure, I assign the day / month / year values to their respective variables ( tm_mday , tm_mon and tm_year ), but when I use the mktime() function, it returns -1.

Code snippet:

data->tm_mday = dia;
data->tm_mon = mes - 1;
data->tm_year = ano - 1900;
segs = mktime(data);

I was not having any problems with this solution for recent dates. What could be happening?

EDIT 1 : Running a loop to see how far mktime () works, I noticed that it works until the value of tm_year equals 70. Below that it returns -1 ...

EDIT 2 : Doing some more tests, I noticed that the value that mktime () returns is the number of seconds since 12/31/1969 10:00 PM. Any date before that I can not create with this structure.

    
asked by anonymous 10.12.2017 / 16:42

1 answer

0

mktime () uses the "Unix era", this is a way to represent dates by means of seconds. Where a date (along with hour, minutes, and seconds) is represented by the number of seconds between January 1, 1970 at 00:00:00 (UTC) and the date you want. Therefore any date before this will return an error.

    
11.12.2017 / 04:01