C: How to generate a date structure

Asked

Viewed 87 times

1

good afternoon.

I’m having a hard time with the C-date manipulation. I’m using the time. h library to mess with dates.

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

Excerpt from the code:

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

I was not having problems with this solution for recent dates.
What might be going on?

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

EDIT 2: Doing a few more tests, I noticed that the value that mktime() returns is the number of seconds since 31/12/1969 22:00. Any date before that I can’t create with that structure.

1 answer

0

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

Browser other questions tagged

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