Reputation: 6555
timeStruct @0x13b123b0 tm
tm_hour 0 int
tm_isdst 0 int
tm_mday 1 int
tm_min 0 int
tm_mon 0 int
tm_sec 33 int
tm_wday 4 int
tm_yday 0 int
tm_year 70 int
This is my struct as represented by QTcreators memory watch.
with this as parameter to a call of mktime()
struct tm *timeStruct;
//[...]
time_t timeOrigin = mktime(timeStruct);
timeOrigin
becomes -1.
There are questions out here asking for the same.
But they all were solved by the hint, that the tm_year field is not years since 1970 but years since 1900. I'm aware of this and also respecting it.
What confuses me further is:
man pages like: http://linux.die.net/man/3/mktime
explain that mktime doesn't change the structs members if it returns -1. In my case it did adjust in previous cases tm_wday
and tm_yday
, while returning -1.
I can't find any error regarding this by reading stderr, after mktime returning -1
aswell.
So whats going on here?
Upvotes: 2
Views: 772
Reputation: 144695
mktime
returns -1 because the tm
structure values, interpreted as a time and date expressed in local time, fall outside the valid range. As quoted from the man page: If the specified broken-down time cannot be represented as calendar time (seconds since the Epoch), mktime()
returns (time_t)-1
and does not alter the members of the broken-down time structure.
You are probably located east of Greenwich, meaning that 1/1/1970 0:00:00 local time falls before the beginning of the Unix Epoch that started on 1/1/1970 0:00:00 UTC.
Upvotes: 3
Reputation: 1204
mktime
expects a pointer of type struct tm
as it's first argument.
Here is an example, how you can use it:
#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm tm = *localtime(&(time_t){time(NULL)});
printf("Today is %s", asctime(&tm));
tm.tm_mon -= 100; // tm_mon is now outside its normal range
time_t t = mktime(&tm); // recalculate tm
printf("100 months ago was %s", asctime(&tm));
}
Upvotes: 0