source: trunk/Cocoa/Pester/Source/NJRDateFormatter.m@ 22

Last change on this file since 22 was 21, checked in by Nicholas Riley, 22 years ago

Pester 1.0d1

File size: 1.8 KB
Line 
1//
2// NJRDateFormatter.m
3// Pester
4//
5// Created by Nicholas Riley on Wed Oct 09 2002.
6// Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9#import "NJRDateFormatter.h"
10
11
12@implementation NJRDateFormatter
13
14// workaround for bug in Jaguar (and earlier?) NSCalendarDate dateWithNaturalLanguageString:
15NSString * stringByRemovingSurroundingWhitespace(NSString *string) {
16 static NSCharacterSet *nonWhitespace = nil;
17 NSRange firstValidCharacter, lastValidCharacter;
18
19 if (!nonWhitespace) {
20 nonWhitespace = [[[NSCharacterSet characterSetWithCharactersInString:
21 @" \t\r\n"] invertedSet] retain];
22 }
23
24 firstValidCharacter = [string rangeOfCharacterFromSet:nonWhitespace];
25 if (firstValidCharacter.length == 0)
26 return @"";
27 lastValidCharacter = [string rangeOfCharacterFromSet:nonWhitespace options:NSBackwardsSearch];
28
29 if (firstValidCharacter.location == 0 && lastValidCharacter.location == [string length] - 1)
30 return string;
31 else
32 return [string substringWithRange:NSUnionRange(firstValidCharacter, lastValidCharacter)];
33}
34
35
36- (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string errorDescription:(NSString **)error
37{
38 NSCalendarDate *date;
39 if (![self allowsNaturalLanguage])
40 return [super getObjectValue: anObject forString: string errorDescription: error];
41 if (string == nil) return nil;
42 NS_DURING // dateWithNaturalLanguageString: can throw an exception
43 date = [NSCalendarDate dateWithNaturalLanguageString: stringByRemovingSurroundingWhitespace(string)];
44 NS_HANDLER
45 if (error != nil) *error = [localException reason];
46 NS_VALUERETURN(NO, BOOL);
47 NS_ENDHANDLER
48 if (date == nil) return [super getObjectValue: anObject forString: string errorDescription: error];
49 *anObject = date;
50 return YES;
51}
52
53@end
Note: See TracBrowser for help on using the repository browser.