6

I am having a terrible time trying to do something that should be easy. I have a NSNumber value of 32025.89 seconds. I need to represent that in Hours, Minutes, Seconds. Is there a method that spits that out? I can't seem to find a proper formatter.

SonnyBurnette
  • 680
  • 3
  • 11
  • 25
  • Duplicate of http://stackoverflow.com/questions/1237778/how-do-i-break-down-an-nstimeinterval-into-year-months-days-hours-minutes-and . – Peter Hosey Oct 07 '09 at 02:14

4 Answers4

9

If you don't want to just divide it out, try this. It may be close enough for your purposes. Note: It doesn't account for sub-second precision. (setSecond takes an NSInteger).

NSDateComponents* c = [[[NSDateComponents alloc] init] autorelease];
[c setSecond:32025.89];

NSCalendar* cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                   autorelease];

NSDate* d = [cal dateFromComponents:c];

NSDateComponents* result = [cal components:NSHourCalendarUnit |
                                           NSMinuteCalendarUnit |
                                           NSSecondCalendarUnit
                                  fromDate:d];

NSLog(@"%d hours, %d minutes, %d seconds",
    [result hour], [result minute], [result second]);
nall
  • 15,899
  • 4
  • 61
  • 65
9

Have you tried creating an NSDate from that and printing that using an NSDateFormatter?

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[theNumber doubleValue]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
NSLog(@"%@", [formatter stringFromDate:date]); 
[formatter release];

If you need the individual values for hours, minutes, and seconds, use NSDateComponents as suggested by nall.

The above won't print the correct amounts if the NSNumber represents more than one day. It could be 1 day and 1 hour, and it would only print 01:00:00. In such a case you should calculate hours, minutes and seconds manually.

Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
Thomas Müller
  • 15,565
  • 6
  • 41
  • 47
  • This is more along the lines of what I was looking for. I know I could od the math but I was trying to find a way that essentially did it for me -- NSDateFormatter was I guess, what I needed. Thanks. – SonnyBurnette Oct 07 '09 at 01:00
  • Using this code I had a user who got an incorrect value - instead of 03:00 for 180 seconds - they got 33:00. They were in a timezone which was 4.5 hours ahead and I wondered if the half hour in their timezone could possibly have caused this... – petenelson Aug 06 '13 at 08:52
4

Here's a way without any libraries except for modf() from math.h:

float fsec = 32025.89f, frac = 0;
int milliseconds = (int)(modf(fsec, &frac) * 1000);
int isec = (int)frac;
int hours = isec / 3600;
int minutes = (isec % 3600) / 60;
int seconds = isec % 60;
Nick Bedford
  • 4,365
  • 30
  • 36
2

NSNumber *valueForDisplay = [NSNumber numberWithDouble: [self valueForDisplay:clockName]];

NSNumber *totalDays = [NSNumber numberWithDouble:
         ([valueForDisplay doubleValue] / 86400)];
NSNumber *totalHours = [NSNumber numberWithDouble:
         (([valueForDisplay doubleValue] / 3600) -
          ([totalDays intValue] * 24))];
NSNumber *totalMinutes = [NSNumber numberWithDouble:
         (([valueForDisplay doubleValue] / 60) -
         ([totalDays intValue] * 24 * 60) -
         ([totalHours intValue] * 60))];
NSNumber *totalSeconds = [NSNumber numberWithInt:
         ([valueForDisplay intValue] % 60)];
taus-iDeveloper
  • 681
  • 2
  • 8
  • 22