Reputation: 2607
Why a difftime between now and now throws one hour as a result? Shouldn't it be 0?
time_t diffe = difftime(now, now);
CCLOG("wow %i", localtime(&diffe)->tm_hour);
Result of log = "wow 1"
thks
Upvotes: 1
Views: 336
Reputation: 16253
difftime
returns a double
(number of seconds), not a time_t
.
localtime
, on the other hand, takes a time_t*
argument. Passing a double*
would not make much sense.
Upvotes: 1
Reputation: 74028
The difference
is a double with the value 0
.
When you use a time_t
with value 0
, this is the epoch
, which is 1970-01-01 00:00:00. When you interpret this with localtime
and a timezone one hour ahead of GMT
, you get 1970-01-01 01:00:00, which is why you see
1 hour
"difference".
Upvotes: 5
Reputation: 70929
difftime
returns the difference in seconds(double). localtime
on the other hand initializes a tm
structure using the value provided. So my guess is that you are executing the code at GMT+1
timezone. To get the difference in hours simply print difftime(now, now) / (60.0 * 60.0)
Upvotes: 0