views:

844

answers:

4

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.

A: 

I don't know Objective C, but it's a darn poor programming language that doesn't have the tools you need to split them out yourself using basic arithmetic.

Robert L
Just because someone doesn't yet know how to do some specific task in a language doesn't mean the language can't perform that task. You're welcome to point out flaws in Objective-C, but please learn about the language first.
Thomas Müller
@Thomas I think you missed his point, which was to state in a roundbout way "split them yourself using basic arithmetic".
phoebus
Does C "have the tools you need to split them out"? Then so does Objective-C. I'm not sure answers beginning "I don't know [the language this question pertains to", but.." are likely to be very helpful. Might as well keep'em to yourself.
Felixyz
Objective-C has plenty of tools for you to do all sorts of date and time manipulation. standard C doesn't.
pxl
In any case it was unhelpful. And a bit snippy.
willc2
+3  A: 

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
+1  A: 

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.

Thomas Müller
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
A: 

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
I just noticed that you divide 0 by 3600 to get hours. That's a typo, right? You means to say "fsec / 3600" right?
SonnyBurnette
And what does `frexp` have to do with this ??
Robert L
um... it extracts the fraction part and the integer part of a floatingh pointer number. In this case, the milliseconds and seconds.
Nick Bedford
Nick Bedford
@Nick wrong function, read the docs.
Robert L
Ahh my mistake. `modf` was the one I meant! Thanks
Nick Bedford