2007-03-14 02:29:16 +00:00
|
|
|
/*
|
|
|
|
* $Id: SecondsFormatter.m 227 2007-01-21 06:22:13Z stephen_booth $
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 - 2007 Stephen F. Booth <me@sbooth.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "SecondsFormatter.h"
|
|
|
|
|
|
|
|
@implementation SecondsFormatter
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSString *)stringForObjectValue:(id)object {
|
|
|
|
NSString *result = nil;
|
|
|
|
unsigned value;
|
|
|
|
unsigned days = 0;
|
|
|
|
unsigned hours = 0;
|
|
|
|
unsigned minutes = 0;
|
|
|
|
unsigned seconds = 0;
|
|
|
|
|
2021-05-08 22:13:49 +00:00
|
|
|
if(nil == object || NO == [object isKindOfClass:[NSNumber class]] || isnan([object doubleValue])) {
|
|
|
|
return @"";
|
2007-03-14 02:29:16 +00:00
|
|
|
}
|
2007-11-24 20:16:27 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
value = (unsigned)([object doubleValue]);
|
|
|
|
|
|
|
|
seconds = value % 60;
|
|
|
|
minutes = value / 60;
|
|
|
|
|
2021-05-08 22:13:49 +00:00
|
|
|
while(60 <= minutes) {
|
2007-03-14 02:29:16 +00:00
|
|
|
minutes -= 60;
|
|
|
|
++hours;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2021-05-08 22:13:49 +00:00
|
|
|
while(24 <= hours) {
|
2007-03-14 02:29:16 +00:00
|
|
|
hours -= 24;
|
|
|
|
++days;
|
|
|
|
}
|
2021-05-08 22:13:49 +00:00
|
|
|
|
|
|
|
if(0 < days) {
|
|
|
|
result = [NSString stringWithFormat:@"%u:%.2u:%.2u:%.2u", days, hours, minutes, seconds];
|
2022-02-07 05:49:27 +00:00
|
|
|
} else if(0 < hours) {
|
2021-05-08 22:13:49 +00:00
|
|
|
result = [NSString stringWithFormat:@"%u:%.2u:%.2u", hours, minutes, seconds];
|
2022-02-07 05:49:27 +00:00
|
|
|
} else if(0 < minutes) {
|
2021-05-08 22:13:49 +00:00
|
|
|
result = [NSString stringWithFormat:@"%u:%.2u", minutes, seconds];
|
2022-02-07 05:49:27 +00:00
|
|
|
} else {
|
2021-05-08 22:13:49 +00:00
|
|
|
result = [NSString stringWithFormat:@"0:%.2u", seconds];
|
2007-03-14 02:29:16 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-13 07:09:46 +00:00
|
|
|
return result;
|
2007-03-14 02:29:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error {
|
|
|
|
NSScanner *scanner = nil;
|
|
|
|
BOOL result = NO;
|
|
|
|
int value = 0;
|
|
|
|
unsigned seconds = 0;
|
|
|
|
|
|
|
|
scanner = [NSScanner scannerWithString:string];
|
2007-03-14 02:29:16 +00:00
|
|
|
|
2021-05-08 22:13:49 +00:00
|
|
|
while(NO == [scanner isAtEnd]) {
|
|
|
|
// Grab a value
|
|
|
|
if([scanner scanInt:&value]) {
|
2022-02-07 05:49:27 +00:00
|
|
|
seconds *= 60;
|
|
|
|
seconds += value;
|
|
|
|
result = YES;
|
2007-03-14 02:29:16 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2021-05-08 22:13:49 +00:00
|
|
|
// Grab the separator, if present
|
|
|
|
[scanner scanString:@":" intoString:NULL];
|
2007-03-14 02:29:16 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2021-05-08 22:13:49 +00:00
|
|
|
if(result && NULL != object) {
|
|
|
|
*object = [NSNumber numberWithUnsignedInt:seconds];
|
2022-02-07 05:49:27 +00:00
|
|
|
} else if(NULL != error) {
|
2007-03-14 02:29:16 +00:00
|
|
|
*error = @"Couldn't convert value to seconds";
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-03-14 02:29:16 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSAttributedString *)attributedStringForObjectValue:(id)object withDefaultAttributes:(NSDictionary *)attributes {
|
|
|
|
NSAttributedString *result = nil;
|
|
|
|
|
2021-05-08 22:13:49 +00:00
|
|
|
result = [[NSAttributedString alloc] initWithString:[self stringForObjectValue:object] attributes:attributes];
|
2016-05-05 20:05:39 +00:00
|
|
|
return result;
|
2007-03-14 02:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|