Last active
September 4, 2015 13:21
-
-
Save srmds/a0e3854386fd6d702179 to your computer and use it in GitHub Desktop.
Obj-C - Convert milliseconds to a formatted output string of hours:minutes:seconds with applied padding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TimeFormatter.h | |
// ElectroDeluxe | |
// | |
// Convert milliseconds to a formatted output string containing hours:minutes:seconds | |
// with padding zero. | |
// | |
// Copyright © 2015 srmds. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface TimeFormatter : NSObject | |
-(NSString *) getFormattedDuration:(NSNumber*)trackDuration; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TimeFormatter.m | |
// ElectroDeluxe | |
// | |
// Convert milliseconds to a formatted output string containing hours:minutes:seconds | |
// with padding zero. | |
// | |
// Copyright © 2015 srmds. All rights reserved. | |
// | |
#import "TimeFormatter.h" | |
@implementation TimeFormatter | |
-(NSString *) getFormattedDuration:(NSNumber*)trackDuration{ | |
if([trackDuration intValue] < 1000) | |
return @"00:00:00"; | |
unsigned long milliseconds = [trackDuration floatValue]; | |
unsigned long seconds = milliseconds / 1000; | |
unsigned long minutes = seconds / 60; | |
seconds %= 60; | |
unsigned long hours = minutes / 60; | |
minutes %= 60; | |
NSMutableDictionary *timeComponents = [[NSMutableDictionary alloc]initWithCapacity:3]; | |
[timeComponents setObject:[NSNumber numberWithInteger:[@(hours) integerValue]] forKey:@"hours"]; | |
[timeComponents setObject:[NSNumber numberWithInteger:[@(minutes) integerValue]] forKey:@"minutes"]; | |
[timeComponents setObject:[NSNumber numberWithInteger:[@(seconds) integerValue]] forKey:@"seconds"]; | |
return [self durationStringBuilder: timeComponents]; | |
} | |
-(NSString *)durationStringBuilder:(NSDictionary *)timeComponents { | |
int hours = [[timeComponents valueForKey:@"hours"] intValue]; | |
int minutes = [[timeComponents valueForKey:@"minutes"] intValue]; | |
int seconds = [[timeComponents valueForKey:@"seconds"] intValue]; | |
NSMutableString * result = [NSMutableString new]; | |
[result appendString:[self setZeroPadding:hours delimiter:false]]; | |
[result appendString:[self setZeroPadding:minutes delimiter:false]]; | |
[result appendString:[self setZeroPadding:seconds delimiter:true]]; | |
return result; | |
} | |
-(NSString*)setZeroPadding:(int)timeComponent delimiter:(BOOL)delimiter { | |
NSMutableString * result = [NSMutableString new]; | |
if (timeComponent) { | |
if(timeComponent < 10){ | |
if(!delimiter) | |
[result appendFormat: @"0%d:", timeComponent]; | |
else | |
[result appendFormat: @"0%d", timeComponent]; | |
} | |
else{ | |
if(!delimiter) | |
[result appendFormat: @"%d:", timeComponent]; | |
else | |
[result appendFormat: @"%d", timeComponent]; | |
} | |
} else { | |
if (!delimiter) | |
[result appendString:@"00:"]; | |
else | |
[result appendString:@"00"]; | |
} | |
return result; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment