/** Implementation of NSDateFormatter class
Copyright (C) 1998 Free Software Foundation, Inc.

Written by:  Richard Frith-Macdonald <richard@brainstorm.co.uk>
Created: December 1998

This file is part of the GNUstep Base Library.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.

<title>NSDateFormatter class reference</title>
$Date: 2004/09/14 03:34:37 $ $Revision: 1.10 $

adapted for mySTEP

*/

#include "Foundation/NSDate.h"
#include "Foundation/NSCalendarDate.h"
#include "Foundation/NSTimeZone.h"
#include "Foundation/NSFormatter.h"
#include "Foundation/NSDateFormatter.h"
#include "Foundation/NSString.h"
#include "Foundation/NSCoder.h"

@implementation NSDateFormatter

static NSDateFormatterBehavior _defaultFormatterBehavior=NSDateFormatterBehaviorDefault;

+ (NSDateFormatterBehavior) defaultFormatterBehavior; { return _defaultFormatterBehavior; }
+ (void) setDefaultFormatterBehavior:(NSDateFormatterBehavior) behavior; { _defaultFormatterBehavior=behavior; }

- (BOOL) allowsNaturalLanguage
{
	return _allowsNaturalLanguage;
}

#if 0	// not available - so don't override default implementation
- (NSAttributedString*) attributedStringForObjectValue: (id)anObject
								 withDefaultAttributes: (NSDictionary*)attr
{
	return NIMP;
}
#endif

- (id) copyWithZone: (NSZone*)zone
{
	NSDateFormatter	*other = [super copyWithZone:zone];
	RETAIN(other->_dateFormat);
	return other;
}

- (NSString*) dateFormat
{
	return _dateFormat;
}

- (id) init;
{
	if((self=[super init]))
		{
		// should we set a default format???
		}
	return self;
}

- (void) dealloc
{
	RELEASE(_dateFormat);
	[super dealloc];
}

- (NSString*) editingStringForObjectValue: (id)anObject
{
	return [self stringForObjectValue: anObject];
}

- (void) encodeWithCoder: (NSCoder*)aCoder
{
	[aCoder encodeValuesOfObjCTypes: "@C", &_dateFormat, &_allowsNaturalLanguage];
}

- (BOOL) getObjectValue: (id*)anObject
			  forString: (NSString*)string
       errorDescription: (NSString**)error
{
	NSCalendarDate	*d;
	
	d = [NSCalendarDate dateWithString: string calendarFormat: _dateFormat];
	if (d == nil)
		{
		if (_allowsNaturalLanguage)
			{
			//			d = [NSCalendarDate dateWithNaturalLanguageString: string];
			// FIXME: read from LANGUAGE dictionary
			if([string isEqualToString:@"now"])
				d=[NSCalendarDate calendarDate];
			else if([string isEqualToString:@"today"])
				d=[NSCalendarDate calendarDate];
			else if([string isEqualToString:@"tomorrow"])
				d=[[NSCalendarDate calendarDate] dateByAddingYears:0 months:0 days:1 hours:0 minutes:0 seconds:0];
			else if([string isEqualToString:@"yesterday"])
				d=[[NSCalendarDate calendarDate] dateByAddingYears:0 months:0 days:-1 hours:0 minutes:0 seconds:0];
			}
		if (d == nil)
			{
			if (error)
				{
				*error = @"Couldn't convert to date";
				}
			return NO;
			}
		}
	if (anObject)
		{
		*anObject = d;
		}
	return YES;
}

- (id) initWithCoder: (NSCoder*)aCoder
{
	if([aCoder allowsKeyedCoding])
		{
		_dateFormat=[[aCoder decodeObjectForKey:@"NS.format"] retain];
		_allowsNaturalLanguage=[aCoder decodeBoolForKey:@"NS.natural"];
		_attributes=[[aCoder decodeObjectForKey:@"NS.attributes"] retain];
#if 0
		NSLog(@"attributes=%@", _attributes);
		/* e.g.
			dateFormat_10_0 = "%b %d, %Y %H:%M:%S";
		formatterBehavior = 1000;
		lenient = 0;
			*/
#endif
		return self;
		}
	[aCoder decodeValuesOfObjCTypes: "@C", &_dateFormat, &_allowsNaturalLanguage];
	return self;
}

- (id) initWithDateFormat: (NSString *)format
     allowNaturalLanguage: (BOOL)flag
{
	_dateFormat = [format copy];
	_allowsNaturalLanguage = flag;
	return self;
}

- (BOOL) isPartialStringValid: (NSString*)partialString
			 newEditingString: (NSString**)newString
			 errorDescription: (NSString**)error
{
	// FIXME!
	if(newString)
		*newString = nil;
	if(error)
		*error = nil;
	return YES;
}

- (NSString*) stringForObjectValue: (id)anObject
{
#if 0
	NSLog(@"stringForObjectValue: %@", anObject);
#endif
	if(![anObject isKindOfClass: [NSDate class]])
		return nil;
	if([anObject isKindOfClass: [NSCalendarDate class]])
		return [anObject descriptionWithCalendarFormat: _dateFormat locale: nil];
	return [anObject descriptionWithCalendarFormat: _dateFormat timeZone: [NSTimeZone defaultTimeZone] locale: nil];
}

/*
 
 -AMSymbol
 -calendar
 -dateFromString:
 -dateStyle
 -defaultDate
 -eraSymbols
 -formatterBehavior
 -generatesCalendarDates
 -getObjectValue:forString:range:error:
 -isLenient
 -locale
 -monthSymbols
 -PMSymbol
 -setAMSymbol:
 -setCalendar:
 -setDateFormat:
 -setDateStyle:
 -setDefaultDate:
 -setEraSymbols:
 -setFormatterBehavior:
 -setGeneratesCalendarDates:
 -setLenient:
  -setLocale:
 -setMonthSymbols:
 -setPMSymbol:
 -setShortMonthSymbols:
 -setShortWeekdaySymbols:
 -setTimeStyle:
 -setTimeZone:
 -setTwoDigitStartDate:
 -setWeekdaySymbols:
 -shortMonthSymbols
 -shortWeekdaySymbols
 -stringFromDate:
 -timeStyle
 -timeZone
 -twoDigitStartDate
 -weekdaySymbols

*/

@end

