xus
xus

Reputation: 2607

Difftime between now and now throws 1hr difference (C++)

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

Answers (3)

us2012
us2012

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

Olaf Dietsche
Olaf Dietsche

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

Ivaylo Strandjev
Ivaylo Strandjev

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

Related Questions