PDA

View Full Version : Confused By the Output of My Program




Stunner
Nov 11, 2009, 07:31 AM
Ey guys, I am a little confused by the output I am getting from timeIntervalSinceDate. Here is the code I have:

NSDate *prevDate;
for (NSDate *thisDate in self.arrayOfTimes) {
NSLog(@"item: %@",thisDate);
if (counter > 1) {
NSLog(@"item: %d",[thisDate timeIntervalSinceDate:prevDate]);
}
prevDate = thisDate;
}

And here is my output in console:

2009-11-11 05:25:39.740 WTC[82810:20b] counter: 1
2009-11-11 05:25:39.742 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
2009-11-11 05:25:41.087 WTC[82810:20b] counter: 2
2009-11-11 05:25:41.088 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
2009-11-11 05:25:41.089 WTC[82810:20b] item: 0
2009-11-11 05:25:41.089 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
2009-11-11 05:25:41.089 WTC[82810:20b] item: -1610612736
2009-11-11 05:25:43.481 WTC[82810:20b] counter: 3
2009-11-11 05:25:43.482 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
2009-11-11 05:25:43.484 WTC[82810:20b] item: 0
2009-11-11 05:25:43.485 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
2009-11-11 05:25:43.485 WTC[82810:20b] item: -1610612736
2009-11-11 05:25:43.486 WTC[82810:20b] item: 2009-11-11 05:25:43 -0800
2009-11-11 05:25:43.486 WTC[82810:20b] item: -402653184
2009-11-11 05:25:49.757 WTC[82810:20b] counter: 4
2009-11-11 05:25:49.758 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
2009-11-11 05:25:49.759 WTC[82810:20b] item: 0
2009-11-11 05:25:49.760 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
2009-11-11 05:25:49.761 WTC[82810:20b] item: -1610612736
2009-11-11 05:25:49.761 WTC[82810:20b] item: 2009-11-11 05:25:43 -0800
2009-11-11 05:25:49.762 WTC[82810:20b] item: -402653184
2009-11-11 05:25:49.762 WTC[82810:20b] item: 2009-11-11 05:25:49 -0800
2009-11-11 05:25:49.763 WTC[82810:20b] item: 671088640
2009-11-11 05:25:53.821 WTC[82810:20b] counter: 5
2009-11-11 05:25:53.822 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
2009-11-11 05:25:53.822 WTC[82810:20b] item: 0
2009-11-11 05:25:53.823 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
2009-11-11 05:25:53.824 WTC[82810:20b] item: -1610612736
2009-11-11 05:25:53.824 WTC[82810:20b] item: 2009-11-11 05:25:43 -0800
2009-11-11 05:25:53.825 WTC[82810:20b] item: -402653184
2009-11-11 05:25:53.826 WTC[82810:20b] item: 2009-11-11 05:25:49 -0800
2009-11-11 05:25:53.826 WTC[82810:20b] item: 671088640
2009-11-11 05:25:53.826 WTC[82810:20b] item: 2009-11-11 05:25:53 -0800
2009-11-11 05:25:53.827 WTC[82810:20b] item: -1073741824
2009-11-11 05:26:00.567 WTC[82810:20b] counter: 6
2009-11-11 05:26:00.568 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
2009-11-11 05:26:00.569 WTC[82810:20b] item: 0
2009-11-11 05:26:00.569 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
2009-11-11 05:26:00.570 WTC[82810:20b] item: -1610612736
2009-11-11 05:26:00.570 WTC[82810:20b] item: 2009-11-11 05:25:43 -0800
2009-11-11 05:26:00.571 WTC[82810:20b] item: -402653184
2009-11-11 05:26:00.571 WTC[82810:20b] item: 2009-11-11 05:25:49 -0800
2009-11-11 05:26:00.572 WTC[82810:20b] item: 671088640
2009-11-11 05:26:00.573 WTC[82810:20b] item: 2009-11-11 05:25:53 -0800
2009-11-11 05:26:00.574 WTC[82810:20b] item: -1073741824
2009-11-11 05:26:00.575 WTC[82810:20b] item: 2009-11-11 05:26:00 -0800
2009-11-11 05:26:00.575 WTC[82810:20b] item: -671088640


I am wondering why the program is printing out positive and negative numbers such as: -671088640 and 671088640. And how would I go about converting these values into seconds? Thanks!



ChOas
Nov 11, 2009, 08:14 AM
timeIntervalSinceDate returns an NSTimeInterval, NSTimeInterval is a double, you are trying to print an integer... Overflow is biting you.

Try:


NSLog(@"interval: %g",[thisDate timeIntervalSinceDate:prevDate]);

Stunner
Nov 11, 2009, 09:00 PM
timeIntervalSinceDate returns an NSTimeInterval, NSTimeInterval is a double, you are trying to print an integer... Overflow is biting you.

Try:


NSLog(@"interval: %g",[thisDate timeIntervalSinceDate:prevDate]);


Awesome, thank you. By the way, does there happen to be a page that lists what should be used when printing different values? Such as %d for integers, %g for time values, etc. Thanks again.

ChOas
Nov 12, 2009, 01:37 AM
Awesome, thank you. By the way, does there happen to be a page that lists what should be used when printing different values? Such as %d for integers, %g for time values, etc. Thanks again.

After some searching (you can do this too, really :) ) I found these for you:

http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1

http://www.opengroup.org/onlinepubs/009695399/functions/printf.html

Stunner
Nov 12, 2009, 04:09 AM
Awesome, thanks.