diff --git a/Foundation/CPDateFormatter.j b/Foundation/CPDateFormatter.j new file mode 100644 index 000000000..661beac79 --- /dev/null +++ b/Foundation/CPDateFormatter.j @@ -0,0 +1,136 @@ +/* + * CPDateFormatter.j + * Foundation + * + * Created by Alexander Ljungberg. + * Copyright 2012, SlevenBits Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#import "Ref.h" + +@import +@import +@import + +CPDateFormatterNoStyle = 0; +CPDateFormatterShortStyle = 1; +CPDateFormatterMediumStyle = 2; +CPDateFormatterLongStyle = 3; +CPDateFormatterFullStyle = 4; + +/*! + @ingroup foundation + @class CPDateFormatter + + * Not yet implemented. This is a stub class. * + + CPDateFormatter takes a CPDate value and formats it as text for + display. It also supports the converse, taking text and interpreting it as a + CPDate by configurable formatting rules. +*/ +@implementation CPDateFormatter : CPFormatter +{ + CPDateFormatterStyle _dateStyle @accessors(property=dateStyle); +} + +- (id)init +{ + if (self = [super init]) + { + _dateStyle = CPDateFormatterShortStyle; + } + + return self; +} + +- (CPString)stringFromDate:(CPDate)aDate +{ + // TODO Add locale support. + switch (_dateStyle) + { + case CPDateFormatterShortStyle: + var format = "d/m/Y"; + return aDate.dateFormat(format); + default: + return [aDate description]; + } +} + +- (CPDate)dateFromString:(CPString)aString +{ + if (!aString) + return nil; + switch (_dateStyle) + { + case CPDateFormatterShortStyle: + var format = "d/m/Y"; + return Date.parseDate(string, format); + default: + return Date.parseDate(string); + } +} + +- (CPString)stringForObjectValue:(id)anObject +{ + if ([anObject isKindOfClass:[CPDate class]]) + return [self stringFromDate:anObject]; + else + return [anObject description]; +} + +- (CPString)editingStringForObjectValue:(id)anObject +{ + return [self stringForObjectValue:anObject]; +} + +- (BOOL)getObjectValue:(id)anObject forString:(CPString)aString errorDescription:(CPString)anError +{ + console.log("getObjectValue: forString:" + aString); + // TODO Error handling. + var value = [self dateFromString:aString]; + AT_DEREF(anObject, value); + + console.log("got " + value); + return YES; +} + +@end + +var CPDateFormatterStyleKey = "CPDateFormatterStyle"; + +@implementation CPDateFormatter (CPCoding) + +- (id)initWithCoder:(CPCoder)aCoder +{ + self = [super initWithCoder:aCoder]; + + if (self) + { + _dateStyle = [aCoder decodeIntForKey:CPDateFormatterStyleKey]; + } + + return self; +} + +- (void)encodeWithCoder:(CPCoder)aCoder +{ + [super encodeWithCoder:aCoder]; + + [aCoder encodeInt:_dateStyle forKey:CPDateFormatterStyleKey]; +} + +@end diff --git a/Foundation/Foundation.j b/Foundation/Foundation.j index e3f17d3ec..d26bfbb1f 100755 --- a/Foundation/Foundation.j +++ b/Foundation/Foundation.j @@ -28,6 +28,7 @@ @import "CPCompoundPredicate.j" @import "CPData.j" @import "CPDate.j" +@import "CPDateFormatter.j" @import "CPDecimal.j" @import "CPDecimalNumber.j" @import "CPDictionary.j" diff --git a/Tools/nib2cib/NSDateFormatter.j b/Tools/nib2cib/NSDateFormatter.j new file mode 100644 index 000000000..f744a7abb --- /dev/null +++ b/Tools/nib2cib/NSDateFormatter.j @@ -0,0 +1,55 @@ +/* + * NSDateFormatter.j + * nib2cib + * + * Created by Alexander Ljungberg. + * Copyright 2012, SlevenBits Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +@import + +@implementation CPDateFormatter (CPCoding) + +- (id)NS_initWithCoder:(CPCoder)aCoder +{ + self = [super init]; + + if (self) + { + _dateStyle = CPDateFormatterShortStyle; + } + + return self; +} + +@end + +@implementation NSDateFormatter : CPDateFormatter +{ +} + +- (id)initWithCoder:(CPCoder)aCoder +{ + return [self NS_initWithCoder:aCoder]; +} + +- (Class)classForKeyedArchiver +{ + return [CPDateFormatter class]; +} + +@end diff --git a/Tools/nib2cib/NSFoundation.j b/Tools/nib2cib/NSFoundation.j index 1c312408f..06699e3b8 100644 --- a/Tools/nib2cib/NSFoundation.j +++ b/Tools/nib2cib/NSFoundation.j @@ -22,10 +22,10 @@ @import "NSArray.j" @import "NSAttributedString.j" +@import "NSDateFormatter.j" @import "NSDictionary.j" @import "NSExpression.j" @import "NSFormatter.j" @import "NSMutableString.j" @import "NSNumberFormatter.j" @import "NSSet.j" -