Question

NSTimeInterval expirationTime = (secondsSinceUnixEpoch*1000)+120000;
expirationTime = ceil(expirationTime/2);
int expirationInt = (int)expirationTime;
NSLog(@"%d", expirationInt);

The log output is always negative, even though before I convert it to an int it's positive... I tried just multiplying it by -1 to make it positive again and it's just staying negative! I'm totally perplexed.... don't know much about C, am I just doing something silly??

Was it helpful?

Solution

The number (secondsSinceUnixEpoch*1000)+120000 looks to me like it's going to be way too large to fit in an int. Chances are the integer is overflowing and becoming negative.

OTHER TIPS

Converting to long long is one solution. As you stated in a comment, you want to show a whole number for use in a URL. Just do this:

NSTimeInterval expirationTime = (secondsSinceUnixEpoch*1000)+120000;
expirationTime = ceil(expirationTime/2);
NSString *urlString = [NSString stringWithFormat:@"http://example.com?time=%.0f", expirationTime];

This will format the decimal number as a whole number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top