Search code examples
iosobjective-cnsdatensdateformatternstimeinterval

Display a negative time in Obj-C


It may sound simple however I am unsure how to do this. How do I display time in the following format?:

-00:00:00

I have tried using float and int values of the interval difference between two times however neither give a consistent display in the 00:00:00 format. I have also tried converting the time difference into date and then display as String.

This is the code I have used to convert my intervals :

NSDate * now = [NSDate date];
NSTimeInterval totalTime1 = [now timeIntervalSinceDate: timeEntry1];
NSTimeInterval totalTime2 = [now timeIntervalSinceDate: timeEntry2];

//must always be this way
int adjustedTime = totalTime1 - totalTime2;

int hours = adjustedTime / 3600;
int minutes = (adjustedTime / 60) % 60;
int seconds = adjustedTime % 60;

NSString * newTime = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, minutes, seconds];

The above works fine for displaying positive time differences. However presents a variety of 00:423456:978098 and so on when it goes negative in both the NSLog and the Label.

When I convert and save as a type of NSDate I get (null) in NSLog and nothing in my Label.

When I use float it works but does not consistently display in the 00:00:00 format.

NOTE

The code I am using works immaculately for positive time differences. I need negative time differences to also display.

I also need to be able to save the negative time to CoreData. If this is not possible then I will work around, but displaying negative time formatted correctly is the main issue.

EDIT

My new revised code:

NSDate * now = [NSDate date];
NSTimeInterval totalTime1 = [now timeIntervalSinceDate: timeEntry1];
NSTimeInterval totalTime2 = [now timeIntervalSinceDate: timeEntry2];

//must always be this way
int adjustedTime = (int) (totalTime1 - totalTime2);
NSLog (@"What is the adjustedTime? %d", adjustedTime);

int hours = adjustedTime / 3600;
int minutes = (adjustedTime / 60) % 60;
int seconds = adjustedTime % 60;

NSString * newTime = [NSString stringWithFormat:@"%@%02d:%02d:%02d", adjustedTime < 0 ?@"-":@"", hours, minutes, seconds];
NSLog(@"What is the newTime? %@", newTime);

It is closer as now it displays negative numbers however the display is still incorrect when negative.

EDIT 2

A person who answered below suggested I try checking for negative if it is a boolean. Displaying did not change. Below are more log statements to demonstrate. NOTE I stopped using an updated seconds for sake of working out whether it affected the display and stored seconds separate to test, which is why there is no - sign or alteration to the seconds.

2015-01-09 09:30:14.526 App2.0[8398:498707] What is time 2 : 720
2015-01-09 09:30:14.526 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:14.526 App2.0[8398:498707] What is the adjusted time? 51
2015-01-09 09:30:14.527 App2.0[8398:498707] New Time: 00:00:51

2015-01-09 09:30:18.249 App2.0[8398:498707] What is time 2 : 900
2015-01-09 09:30:18.249 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:18.249 App2.0[8398:498707] What is the adjusted time? -129
2015-01-09 09:30:18.249 App2.0[8398:498707] New Time: -00:-2:51

2015-01-09 09:30:20.281 App2.0[8398:498707] What is time 2 : 840
2015-01-09 09:30:20.281 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:20.281 App2.0[8398:498707] What is the adjusted time? -69
2015-01-09 09:30:20.281 App2.0[8398:498707] New Time: -00:-1:51

2015-01-09 09:30:21.725 App2.0[8398:498707] What is time 2 : 780
2015-01-09 09:30:21.726 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:21.726 App2.0[8398:498707] What is the adjusted time? -9
2015-01-09 09:30:21.726 App2.0[8398:498707] New Time: -00:00:51

2015-01-09 09:30:30.161 App2.0[8398:498707] What is time 2 : 1080
2015-01-09 09:30:30.161 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:30.162 App2.0[8398:498707] What is the adjusted time? -309
2015-01-09 09:30:30.162 App2.0[8398:498707] New Time: -00:-5:51

2015-01-09 09:30:33.389 App2.0[8398:498707] What is time 2 : 4680
2015-01-09 09:30:33.389 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:33.390 App2.0[8398:498707] What is the adjusted time? -3909
2015-01-09 09:30:33.390 App2.0[8398:498707] New Time: --1:-5:51

2015-01-09 09:30:36.186 App2.0[8398:498707] What is time 2 : 8280
2015-01-09 09:30:36.187 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:36.187 App2.0[8398:498707] What is the adjusted time? -7509
2015-01-09 09:30:36.187 App2.0[8398:498707] New Time: --2:-5:51

2015-01-09 09:30:43.918 App2.0[8398:498707] What is time 2 : 660
2015-01-09 09:30:43.918 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:43.919 App2.0[8398:498707] What is the adjusted time? 111
2015-01-09 09:30:43.919 App2.0[8398:498707] New Time: 00:01:51

Solution

  • That should do the trick:

      NSDate *now = [NSDate date];
      NSTimeInterval totalTime1 = [now timeIntervalSinceDate: timeEntry1];
      NSTimeInterval totalTime2 = [now timeIntervalSinceDate: timeEntry2];
    
      NSLog(@"totalTime1: %f", totalTime1); // =>  -60.00;
      NSLog(@"totalTime2: %f", totalTime2); // => 6023.00;
    
      //must always be this way
      int timeOffset = (int) (totalTime1 - totalTime2);
      NSLog(@"timeOffset: %d", timeOffset); // => -6083;
    
      BOOL showNegativePrefix = timeOffset < 0;
    
      int hours   = abs(timeOffset / 3600);       // => 01
      int minutes = abs((timeOffset / 60 ) % 60); // => 41
      int seconds = abs(timeOffset % 60);         // => 23
    
      NSString * newTime = [NSString stringWithFormat:@"%@%02d:%02d:%02d", showNegativePrefix ? @"-" : @"", hours, minutes, seconds];
      NSLog(@"%@", newTime); // => -01:41:23