/* 
   NSDate.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <sys/time.h>
#include <time.h>

#if HAVE_LIBC_H
# include <libc.h>
#else
# include <unistd.h>
#endif

#include <Foundation/NSString.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSCoder.h>

#include "NSConcreteDate.h"
#include "NSCalendarDate.h"

@implementation NSDate

+ allocWithZone:(NSZone*)zone
{
    return NSAllocateObject( (self == [NSDate class]) ? 
	    [NSConcreteDate class] : self, 0, zone);
}

+ (NSDate*)date
{
    return [[[self alloc] init] autorelease];
}

+ (NSDate*)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs
{
    return [[[self alloc] initWithTimeIntervalSinceNow:secs] autorelease];
}

+ (NSDate*)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs
{
    return [[[self alloc] initWithTimeIntervalSinceReferenceDate:secs] 
	    autorelease];
}

+ (NSDate*)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds
{
    return [[[self alloc] initWithTimeIntervalSince1970:seconds] autorelease];
}

+ (NSDate*)distantFuture
{
    return [[[self alloc] 
	    initWithTimeIntervalSinceReferenceDate:DISTANT_FUTURE] 
	    autorelease];
}

+ (NSDate*)distantPast
{
    return [[[self alloc] 
	    initWithTimeIntervalSinceReferenceDate:DISTANT_PAST] 
	    autorelease];
}

- init
{
    return [super init];
}

- initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secsToBeAdded
{
    [self subclassResponsibility:_cmd];
    return self;
}

- (id)initWithString:(NSString*)description
{
    id cal = [[NSCalendarDate alloc] initWithString:description];
    [self initWithTimeIntervalSinceReferenceDate:
	    [cal timeIntervalSinceReferenceDate]];
    [cal release];
    return self;
}

- (NSDate*)initWithTimeInterval:(NSTimeInterval)secsToBeAdded 
	sinceDate:(NSDate*)anotherDate
{
    return [self initWithTimeIntervalSinceReferenceDate:
	    (secsToBeAdded + [anotherDate timeIntervalSinceReferenceDate])];
}

- (NSDate*)initWithTimeIntervalSinceNow:(NSTimeInterval)secsToBeAddedToNow
{
    [self initWithTimeIntervalSinceReferenceDate:
	(secsToBeAddedToNow + [NSDate timeIntervalSinceReferenceDate])];
    return self;
}

- (id)initWithTimeIntervalSince1970:(NSTimeInterval)seconds
{
    [self initWithTimeIntervalSinceReferenceDate: seconds + UNIX_OFFSET];
    return self;
}

/* Copying */

- (id)copyWithZone:(NSZone*)zone
{
    if (NSShouldRetainWithZone(self, zone))
	return [self retain];
    else
	return [[[self class] alloc] initWithTimeIntervalSinceReferenceDate:
		[self timeIntervalSinceReferenceDate]];
}

/* Representing Dates */

- (NSString*)description
{
    return [[NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
		[self timeIntervalSinceReferenceDate]] description];
}	

- (NSString*)descriptionWithCalendarFormat:(NSString*)formatString
	timeZone:(NSTimeZone*)aTimeZone
	locale:(NSDictionary*)locale	
{
    id calendar = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
	[self timeIntervalSinceReferenceDate]];
    return [calendar descriptionWithCalendarFormat:formatString
	timeZone:aTimeZone locale:locale];
} 

- (NSString*)descriptionWithLocale:(NSDictionary*)locale
{
    id calendar = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
	[self timeIntervalSinceReferenceDate]];
    return [calendar descriptionWithLocale:locale];
}

/* Adding and Getting Intervals */

+ (NSTimeInterval)timeIntervalSinceReferenceDate
{
    NSTimeInterval	theTime = UNIX_OFFSET;
    struct timeval	tp;
    struct timezone	tzp = { 0, 0 };

    gettimeofday(&tp, &tzp);
    theTime += tp.tv_sec;
    theTime += (double)tp.tv_usec / 1000000.0;
    theTime -= tzp.tz_minuteswest * 60 + (tzp.tz_dsttime ? 3600 : 0);

    return theTime;
}

- (NSTimeInterval)timeIntervalSinceReferenceDate
{
    [self subclassResponsibility:_cmd];
    return 0;
}

- (NSTimeInterval)timeIntervalSinceDate:(NSDate*)anotherDate;
{
    return [self timeIntervalSinceReferenceDate] -
	    [anotherDate timeIntervalSinceReferenceDate];
}

- (NSTimeInterval)timeIntervalSinceNow
{
    return [self timeIntervalSinceReferenceDate] -
	    [NSDate timeIntervalSinceReferenceDate];
}

- (id)addTimeInterval:(NSTimeInterval)seconds
{
    return [[[[self class] alloc] initWithTimeInterval:seconds sinceDate:self]
	    autorelease];
}

- (NSTimeInterval)timeIntervalSince1970
{
    return [self timeIntervalSinceReferenceDate] - UNIX_OFFSET;
}

/* Comparing Dates */

- (NSDate*)earlierDate:(NSDate*)anotherDate
{
    return [self compare:anotherDate] == NSOrderedAscending?
	    self : anotherDate;
}

- (NSDate*)laterDate:(NSDate*)anotherDate
{
    return [self compare:anotherDate] == NSOrderedAscending ?
	    anotherDate : self;
}

- (NSComparisonResult)compare:(NSDate*)other
{
    [self subclassResponsibility:_cmd];
    return NSOrderedSame;
}

- (BOOL)isEqual:other
{
    return [other isKindOfClass:[NSDate class]] &&
	   [self isEqualToDate:other];
}

- (BOOL)isEqualToDate:other
{
    return [self compare:other] == NSOrderedSame;
}

/* Converting to an NSCalendar Object */
- (NSCalendarDate*)dateWithCalendarFormat:(NSString*)aFormatString
	timeZone:(NSTimeZone*)aTimeZone
{
    id new = [[[NSCalendarDate alloc] 
	    initWithTimeIntervalSinceReferenceDate:
		    [self timeIntervalSinceReferenceDate]]
	    autorelease];
    [new setCalendarFormat:aFormatString];
    [new setTimeZone:aTimeZone];
    return new;
}

/* Encoding */
- (Class)classForCoder
{
    return [NSDate class];
}

- (void)encodeWithCoder:(NSCoder*)aCoder
{
    NSTimeInterval timeDiff = [self timeIntervalSinceReferenceDate];

    [aCoder encodeValueOfObjCType:@encode(NSTimeInterval) at:&timeDiff];
}

- (id)initWithCoder:(NSCoder*)aDecoder
{
    NSTimeInterval timeDiff;

    [aDecoder decodeValueOfObjCType:@encode(NSTimeInterval) at:&timeDiff];
    [self initWithTimeIntervalSinceReferenceDate:timeDiff];
    return self;
}

@end
