TimeIntervalFormatter.m
author Jens Alfke <jens@mooseyard.com>
Sat Mar 08 21:04:41 2008 -0800 (2008-03-08)
changeset 0 d84d25d6cdbb
child 1 e55a17cdabd2
permissions -rw-r--r--
Initial checkin.
jens@0
     1
//
jens@0
     2
//  TimeIntervalFormatter.m
jens@0
     3
//  MYUtilities
jens@0
     4
//
jens@0
     5
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@0
     6
//
jens@0
     7
jens@0
     8
#import "TimeIntervalFormatter.h"
jens@0
     9
jens@0
    10
jens@0
    11
@implementation TimeIntervalFormatter
jens@0
    12
jens@0
    13
jens@0
    14
- (void) awakeFromNib
jens@0
    15
{
jens@0
    16
    _showsMinutes = YES;
jens@0
    17
}
jens@0
    18
jens@0
    19
- (void) setShowsMinutes: (BOOL)show                {_showsMinutes = show;}
jens@0
    20
- (void) setShowsFractionalSeconds: (BOOL)show      {_showsFractionalSeconds = show;}
jens@0
    21
jens@0
    22
jens@0
    23
- (NSString*) stringForObjectValue: (id)object
jens@0
    24
{
jens@0
    25
    if (![object isKindOfClass:[NSNumber class]])
jens@0
    26
        return nil;
jens@0
    27
    NSTimeInterval time = [object doubleValue];
jens@0
    28
    NSString *sign;
jens@0
    29
    if( time==0.0 )
jens@0
    30
        return nil;
jens@0
    31
    else if( time < 0.0 ) {
jens@0
    32
        sign = @"-";
jens@0
    33
        time = -time;
jens@0
    34
    } else
jens@0
    35
        sign = @"";
jens@0
    36
    if( ! _showsFractionalSeconds )
jens@0
    37
        time = floor(time);
jens@0
    38
    int minutes = (int)floor(time / 60.0);
jens@0
    39
    if( _showsMinutes || minutes>0 ) {
jens@0
    40
        double seconds = time - 60.0*minutes;
jens@0
    41
        return [NSString stringWithFormat: (_showsFractionalSeconds ?@"%@%d:%06.3lf" :@"%@%d:%02.0lf"),
jens@0
    42
                                           sign,minutes,seconds];
jens@0
    43
    } else {
jens@0
    44
        return [NSString stringWithFormat: (_showsFractionalSeconds ?@"%@%.3lf" :@"%@%.0lf"),
jens@0
    45
                                           sign,time];
jens@0
    46
    }
jens@0
    47
}
jens@0
    48
jens@0
    49
jens@0
    50
- (BOOL)getObjectValue:(id *)anObject
jens@0
    51
             forString:(NSString *)string 
jens@0
    52
      errorDescription:(NSString **)error
jens@0
    53
{
jens@0
    54
    NSScanner *scanner = [NSScanner scannerWithString: string];
jens@0
    55
    [scanner setCharactersToBeSkipped: [NSCharacterSet whitespaceCharacterSet]];
jens@0
    56
    double seconds;
jens@0
    57
    if( [scanner isAtEnd] ) {
jens@0
    58
        seconds = 0.0;
jens@0
    59
    } else {
jens@0
    60
        if( ! [scanner scanDouble: &seconds] || seconds<0.0 ) goto error;
jens@0
    61
        if( [scanner scanString: @":" intoString: NULL] ) {
jens@0
    62
            double minutes = seconds;
jens@0
    63
            if( ! [scanner scanDouble: &seconds] || seconds<0.0 ) goto error;
jens@0
    64
            seconds += 60*minutes;
jens@0
    65
        }
jens@0
    66
        if( ! [scanner isAtEnd] ) goto error;
jens@0
    67
    }
jens@0
    68
    *anObject = [NSNumber numberWithDouble: seconds];
jens@0
    69
    return YES;
jens@0
    70
    
jens@0
    71
error:
jens@0
    72
    *anObject = nil;
jens@0
    73
    if( error )
jens@0
    74
        *error = @"Not a valid time interval";
jens@0
    75
    return NO;
jens@0
    76
}
jens@0
    77
jens@0
    78
jens@0
    79
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr 
jens@0
    80
       proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
jens@0
    81
              originalString:(NSString *)origString 
jens@0
    82
       originalSelectedRange:(NSRange)origSelRange 
jens@0
    83
            errorDescription:(NSString **)error
jens@0
    84
{
jens@0
    85
    static NSCharacterSet *sIllegalChars;
jens@0
    86
    if( ! sIllegalChars )
jens@0
    87
        sIllegalChars = [[[NSCharacterSet characterSetWithCharactersInString: @"0123456789.:"] 
jens@0
    88
                                invertedSet] retain];
jens@0
    89
    return [*partialStringPtr rangeOfCharacterFromSet: sIllegalChars].length == 0;
jens@0
    90
}
jens@0
    91
jens@0
    92
jens@0
    93
@end