Created
August 15, 2012 02:04
-
-
Save codeswimmer/3354849 to your computer and use it in GitHub Desktop.
NSString+OVSSTimeAdditions - A Category on NSString that properly formats durations in hours, minutes, and seconds
This file contains hidden or 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
| // | |
| // NSString+OVSSTimeAdditions.h | |
| // | |
| // | |
| // Created by Owen Voorhees on 3/22/11. | |
| // Copyright 2011 Owen Voorhees. All rights reserved. | |
| // | |
| #import <Foundation/Foundation.h> | |
| @interface NSString (NSString_OVSSTimeAdditions) | |
| +(NSString *)OVSSStringWithHours:(int)hours Minutes:(int)minutes Seconds:(int)seconds; | |
| @end |
This file contains hidden or 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
| #import "NSString+OVSSTimeAdditions.h" | |
| @implementation NSString (NSString_OVSSTimeAdditions) | |
| +(NSString *)OVSSStringWithHours:(int)hours Minutes:(int)minutes Seconds:(int)seconds{ | |
| NSString *hourstring; | |
| NSString *minutestring; | |
| NSString *secondstring; | |
| NSString *final; | |
| if (hours<0 | minutes<0 | seconds<0) { | |
| return @"error: invalid time value entered."; | |
| } | |
| if (hours==0 & minutes==0 & seconds==0) { | |
| return @""; | |
| } | |
| if (hours==0) { | |
| hourstring=[NSString stringWithFormat:@""]; | |
| } | |
| if (hours>0) { | |
| hourstring=[NSString stringWithFormat:@"%d:",hours]; | |
| } | |
| if (minutes==0) { | |
| minutestring=[NSString stringWithFormat:@""]; | |
| } | |
| if (minutes>0 & hours==0) { | |
| minutestring=[NSString stringWithFormat:@"%d:",minutes]; | |
| } | |
| if (minutes>0 & hours>0) { | |
| minutestring=[NSString stringWithFormat:@"0%d:",minutes]; | |
| } | |
| if (minutes>10) { | |
| minutestring=[NSString stringWithFormat:@"%d:",minutes]; | |
| } | |
| if (seconds<10) { | |
| secondstring=[NSString stringWithFormat:@"0%d",seconds]; | |
| } | |
| if (seconds>=10) { | |
| secondstring=[NSString stringWithFormat:@"%d",seconds]; | |
| } | |
| if (hours==0 & minutes==0 & seconds!=0) { | |
| return [NSString stringWithFormat:@"00:%@",secondstring]; | |
| } | |
| if (hours==0) { | |
| final=[NSString stringWithFormat:@"%@%@",minutestring,secondstring]; | |
| return final; | |
| } | |
| if (hours!=0 & seconds!=0 & minutes==0) { | |
| return [NSString stringWithFormat:@"%@00:%@",hourstring,secondstring]; | |
| } | |
| if (minutes==0 & seconds==0) { | |
| return [NSString stringWithFormat:@"%@00:00",hourstring]; | |
| } | |
| final=[NSString stringWithFormat:@"%@%@%@",hourstring,minutestring,secondstring]; | |
| return final; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment