TimeIntervalFormatter.m
author Jens Alfke <jens@mooseyard.com>
Wed Apr 22 16:46:38 2009 -0700 (2009-04-22)
changeset 26 252c13061ee5
parent 1 e55a17cdabd2
permissions -rw-r--r--
Fixed a few compiler warnings.
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
jens@11
   111
jens@11
   112
jens@11
   113
/*
jens@11
   114
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@11
   115
 
jens@11
   116
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@11
   117
 provided that the following conditions are met:
jens@11
   118
 
jens@11
   119
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@11
   120
 and the following disclaimer.
jens@11
   121
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@11
   122
 and the following disclaimer in the documentation and/or other materials provided with the
jens@11
   123
 distribution.
jens@11
   124
 
jens@11
   125
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@11
   126
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@11
   127
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@11
   128
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@11
   129
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@11
   130
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@11
   131
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@11
   132
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@11
   133
 */