TimeIntervalFormatter.m
author Jens Alfke <jens@mooseyard.com>
Thu Mar 20 09:05:58 2008 -0700 (2008-03-20)
changeset 1 e55a17cdabd2
parent 0 d84d25d6cdbb
child 11 e5976864dfe9
permissions -rw-r--r--
Configurable logging (LogTo).
Added "my_" prefix to category method names.
Added MurmurHash.
Added UniqueWindowController.
Bug fixes.
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@1
    14
- (id) init
jens@1
    15
{
jens@1
    16
    self = [super init];
jens@1
    17
    if (self != nil) {
jens@1
    18
        _showsMinutes = YES;
jens@1
    19
    }
jens@1
    20
    return self;
jens@1
    21
}
jens@1
    22
jens@0
    23
- (void) awakeFromNib
jens@0
    24
{
jens@0
    25
    _showsMinutes = YES;
jens@0
    26
}
jens@0
    27
jens@0
    28
- (void) setShowsMinutes: (BOOL)show                {_showsMinutes = show;}
jens@0
    29
- (void) setShowsFractionalSeconds: (BOOL)show      {_showsFractionalSeconds = show;}
jens@0
    30
jens@1
    31
+ (NSString*) formatTimeInterval: (NSTimeInterval)interval
jens@1
    32
{
jens@1
    33
    TimeIntervalFormatter *fmt = [[self alloc] init];
jens@1
    34
    NSString *result = [fmt stringForObjectValue: [NSNumber numberWithDouble: interval]];
jens@1
    35
    [fmt release];
jens@1
    36
    return result;
jens@1
    37
}
jens@1
    38
jens@0
    39
jens@0
    40
- (NSString*) stringForObjectValue: (id)object
jens@0
    41
{
jens@0
    42
    if (![object isKindOfClass:[NSNumber class]])
jens@0
    43
        return nil;
jens@0
    44
    NSTimeInterval time = [object doubleValue];
jens@0
    45
    NSString *sign;
jens@0
    46
    if( time==0.0 )
jens@0
    47
        return nil;
jens@0
    48
    else if( time < 0.0 ) {
jens@0
    49
        sign = @"-";
jens@0
    50
        time = -time;
jens@0
    51
    } else
jens@0
    52
        sign = @"";
jens@0
    53
    if( ! _showsFractionalSeconds )
jens@0
    54
        time = floor(time);
jens@0
    55
    int minutes = (int)floor(time / 60.0);
jens@0
    56
    if( _showsMinutes || minutes>0 ) {
jens@0
    57
        double seconds = time - 60.0*minutes;
jens@0
    58
        return [NSString stringWithFormat: (_showsFractionalSeconds ?@"%@%d:%06.3lf" :@"%@%d:%02.0lf"),
jens@0
    59
                                           sign,minutes,seconds];
jens@0
    60
    } else {
jens@0
    61
        return [NSString stringWithFormat: (_showsFractionalSeconds ?@"%@%.3lf" :@"%@%.0lf"),
jens@0
    62
                                           sign,time];
jens@0
    63
    }
jens@0
    64
}
jens@0
    65
jens@0
    66
jens@0
    67
- (BOOL)getObjectValue:(id *)anObject
jens@0
    68
             forString:(NSString *)string 
jens@0
    69
      errorDescription:(NSString **)error
jens@0
    70
{
jens@0
    71
    NSScanner *scanner = [NSScanner scannerWithString: string];
jens@0
    72
    [scanner setCharactersToBeSkipped: [NSCharacterSet whitespaceCharacterSet]];
jens@0
    73
    double seconds;
jens@0
    74
    if( [scanner isAtEnd] ) {
jens@0
    75
        seconds = 0.0;
jens@0
    76
    } else {
jens@0
    77
        if( ! [scanner scanDouble: &seconds] || seconds<0.0 ) goto error;
jens@0
    78
        if( [scanner scanString: @":" intoString: NULL] ) {
jens@0
    79
            double minutes = seconds;
jens@0
    80
            if( ! [scanner scanDouble: &seconds] || seconds<0.0 ) goto error;
jens@0
    81
            seconds += 60*minutes;
jens@0
    82
        }
jens@0
    83
        if( ! [scanner isAtEnd] ) goto error;
jens@0
    84
    }
jens@0
    85
    *anObject = [NSNumber numberWithDouble: seconds];
jens@0
    86
    return YES;
jens@0
    87
    
jens@0
    88
error:
jens@0
    89
    *anObject = nil;
jens@0
    90
    if( error )
jens@0
    91
        *error = @"Not a valid time interval";
jens@0
    92
    return NO;
jens@0
    93
}
jens@0
    94
jens@0
    95
jens@0
    96
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr 
jens@0
    97
       proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
jens@0
    98
              originalString:(NSString *)origString 
jens@0
    99
       originalSelectedRange:(NSRange)origSelRange 
jens@0
   100
            errorDescription:(NSString **)error
jens@0
   101
{
jens@0
   102
    static NSCharacterSet *sIllegalChars;
jens@0
   103
    if( ! sIllegalChars )
jens@0
   104
        sIllegalChars = [[[NSCharacterSet characterSetWithCharactersInString: @"0123456789.:"] 
jens@0
   105
                                invertedSet] retain];
jens@0
   106
    return [*partialStringPtr rangeOfCharacterFromSet: sIllegalChars].length == 0;
jens@0
   107
}
jens@0
   108
jens@0
   109
jens@0
   110
@end