Pages

September 16, 2013

Get time format from seconds

I have an amount of seconds that I would convert to a human readable string. What can I do?
Simply use my Objective-C method to accomplish it!
You need:
- http://objective-c-functions.blogspot.com/2013/08/add-zeros-to-digit-number.html


/** Converts from seconds to time string.

 Returned time format is "HH:mm:ss".

 @param sec Number of seconds as NSNumber.
 @return Returns the string time as "HH:mm:ss".
 @see secondsInTimeString:

 */
+ (NSString *)stringTimeFromSeconds:(NSNumber *)sec {
    NSString *ret;
    if ([sec longValue] < 0) {
        LogError(@"Seconds are negative: %ld", sec.longValue);
        ret = @"Undefined";
    } else {
        NSInteger h = floorf( [sec longValue] / 3600 );
        NSInteger m = floorf( ([sec longValue]/60) % 60 );
        NSInteger s = floorf( [sec longValue] % 60 );
        ret = [NSString stringWithFormat:@"%@:%@:%@",
               [LMFunctions twoDigits:h], [LMFunctions twoDigits:m], [LMFunctions twoDigits:s]];
    }
    return ret;
}



Download the ready-for-use source file (.m) of Get time format from seconds

No comments:

Post a Comment