Added -reopenWith:.
2 // TimeIntervalFormatter.m
5 // Copyright 2008 Jens Alfke. All rights reserved.
8 #import "TimeIntervalFormatter.h"
11 @implementation TimeIntervalFormatter
28 - (void) setShowsMinutes: (BOOL)show {_showsMinutes = show;}
29 - (void) setShowsFractionalSeconds: (BOOL)show {_showsFractionalSeconds = show;}
31 + (NSString*) formatTimeInterval: (NSTimeInterval)interval
33 TimeIntervalFormatter *fmt = [[self alloc] init];
34 NSString *result = [fmt stringForObjectValue: [NSNumber numberWithDouble: interval]];
40 - (NSString*) stringForObjectValue: (id)object
42 if (![object isKindOfClass:[NSNumber class]])
44 NSTimeInterval time = [object doubleValue];
48 else if( time < 0.0 ) {
53 if( ! _showsFractionalSeconds )
55 int minutes = (int)floor(time / 60.0);
56 if( _showsMinutes || minutes>0 ) {
57 double seconds = time - 60.0*minutes;
58 return [NSString stringWithFormat: (_showsFractionalSeconds ?@"%@%d:%06.3lf" :@"%@%d:%02.0lf"),
59 sign,minutes,seconds];
61 return [NSString stringWithFormat: (_showsFractionalSeconds ?@"%@%.3lf" :@"%@%.0lf"),
67 - (BOOL)getObjectValue:(id *)anObject
68 forString:(NSString *)string
69 errorDescription:(NSString **)error
71 NSScanner *scanner = [NSScanner scannerWithString: string];
72 [scanner setCharactersToBeSkipped: [NSCharacterSet whitespaceCharacterSet]];
74 if( [scanner isAtEnd] ) {
77 if( ! [scanner scanDouble: &seconds] || seconds<0.0 ) goto error;
78 if( [scanner scanString: @":" intoString: NULL] ) {
79 double minutes = seconds;
80 if( ! [scanner scanDouble: &seconds] || seconds<0.0 ) goto error;
81 seconds += 60*minutes;
83 if( ! [scanner isAtEnd] ) goto error;
85 *anObject = [NSNumber numberWithDouble: seconds];
91 *error = @"Not a valid time interval";
96 - (BOOL)isPartialStringValid:(NSString **)partialStringPtr
97 proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
98 originalString:(NSString *)origString
99 originalSelectedRange:(NSRange)origSelRange
100 errorDescription:(NSString **)error
102 static NSCharacterSet *sIllegalChars;
103 if( ! sIllegalChars )
104 sIllegalChars = [[[NSCharacterSet characterSetWithCharactersInString: @"0123456789.:"]
105 invertedSet] retain];
106 return [*partialStringPtr rangeOfCharacterFromSet: sIllegalChars].length == 0;