1995-03-31 15:39:12 +00:00
|
|
|
|
/* Implementation for NSDate for GNUStep
|
1996-03-12 14:48:07 +00:00
|
|
|
|
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
|
1995-03-31 15:39:12 +00:00
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
Written by: Jeremy Bettis <jeremy@hksys.com>
|
1996-10-31 17:14:00 +00:00
|
|
|
|
Rewritten by: Scott Christley <scottc@net-community.com>
|
1995-04-13 21:57:29 +00:00
|
|
|
|
Date: March 1995
|
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
|
This file is part of the GNUstep Base Library.
|
1995-03-31 15:39:12 +00:00
|
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
1995-04-13 21:57:29 +00:00
|
|
|
|
1995-03-31 02:41:00 -0600 Jeremy Bettis <jeremy@hksys.com>
|
|
|
|
|
Release the first draft of NSDate.
|
|
|
|
|
Three methods not implemented, and NSCalendarDate/NSTimeZone don't exist.
|
1995-03-31 15:39:12 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1995-04-17 21:13:20 +00:00
|
|
|
|
#include <Foundation/NSDate.h>
|
1995-08-02 15:21:54 +00:00
|
|
|
|
#include <Foundation/NSString.h>
|
1996-07-15 18:41:44 +00:00
|
|
|
|
#ifndef __WIN32__
|
1996-03-12 14:48:07 +00:00
|
|
|
|
#include <time.h>
|
1996-07-15 18:41:44 +00:00
|
|
|
|
#endif /* !__WIN32__ */
|
1996-03-12 14:48:07 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
1996-07-15 18:41:44 +00:00
|
|
|
|
#ifndef __WIN32__
|
1995-03-31 15:39:12 +00:00
|
|
|
|
#include <sys/time.h>
|
1996-07-15 18:41:44 +00:00
|
|
|
|
#endif /* !__WIN32__ */
|
1996-02-13 15:43:30 +00:00
|
|
|
|
|
1996-03-12 14:48:07 +00:00
|
|
|
|
/* The number of seconds between 1/1/2001 and 1/1/1970 = -978307200. */
|
|
|
|
|
/* This number comes from:
|
|
|
|
|
-(((31 years * 365 days) + 8 days for leap years) =total number of days
|
|
|
|
|
* 24 hours
|
|
|
|
|
* 60 minutes
|
|
|
|
|
* 60 seconds)
|
|
|
|
|
This ignores leap-seconds. */
|
|
|
|
|
#define UNIX_REFERENCE_INTERVAL -978307200.0
|
|
|
|
|
|
|
|
|
|
/* I hope 100,000 years is distant enough. */
|
|
|
|
|
#define DISTANT_YEARS 100000.0
|
|
|
|
|
#define DISTANT_FUTURE (DISTANT_YEARS * 365.0 * 24 * 60 * 60)
|
|
|
|
|
#define DISTANT_PAST (-DISTANT_FUTURE)
|
|
|
|
|
|
|
|
|
|
|
1996-10-31 17:14:00 +00:00
|
|
|
|
/* The implementation of NSDate. */
|
1995-04-13 21:57:29 +00:00
|
|
|
|
|
1995-03-31 15:39:12 +00:00
|
|
|
|
@implementation NSDate
|
|
|
|
|
|
|
|
|
|
// Getting current time
|
|
|
|
|
|
|
|
|
|
+ (NSTimeInterval) timeIntervalSinceReferenceDate
|
|
|
|
|
{
|
1996-10-31 17:22:04 +00:00
|
|
|
|
#ifndef __WIN32__
|
1996-03-12 14:48:07 +00:00
|
|
|
|
volatile NSTimeInterval interval;
|
|
|
|
|
struct timeval tp;
|
|
|
|
|
struct timezone tzp;
|
|
|
|
|
|
|
|
|
|
interval = UNIX_REFERENCE_INTERVAL;
|
|
|
|
|
gettimeofday (&tp, &tzp);
|
|
|
|
|
interval += tp.tv_sec;
|
|
|
|
|
interval += (double)tp.tv_usec / 1000000.0;
|
|
|
|
|
|
|
|
|
|
/* There seems to be a problem with bad double arithmetic... */
|
|
|
|
|
assert (interval < 0);
|
|
|
|
|
|
|
|
|
|
return interval;
|
1996-10-31 17:22:04 +00:00
|
|
|
|
#else
|
|
|
|
|
TIME_ZONE_INFORMATION sys_time_zone;
|
|
|
|
|
SYSTEMTIME sys_time;
|
|
|
|
|
NSCalendarDate *d;
|
|
|
|
|
NSTimeInterval t;
|
|
|
|
|
|
|
|
|
|
// Get the time zone information
|
|
|
|
|
GetTimeZoneInformation(&sys_time_zone);
|
|
|
|
|
|
|
|
|
|
// Get the system time
|
|
|
|
|
GetLocalTime(&sys_time);
|
|
|
|
|
|
|
|
|
|
// Use an NSCalendar object to make it easier
|
|
|
|
|
d = [NSCalendarDate alloc];
|
|
|
|
|
[d initWithYear: sys_time.wYear
|
|
|
|
|
month: sys_time.wMonth
|
|
|
|
|
day: sys_time.wDay
|
|
|
|
|
hour: sys_time.wHour
|
|
|
|
|
minute: sys_time.wMinute
|
|
|
|
|
second: sys_time.wSecond
|
|
|
|
|
timeZone: [NSTimeZone defaultTimeZone]];
|
|
|
|
|
t = [d timeIntervalSinceReferenceDate];
|
|
|
|
|
[d release];
|
|
|
|
|
return t;
|
|
|
|
|
#endif /* __WIN32__ */
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocation and initializing
|
|
|
|
|
|
|
|
|
|
+ (NSDate*) date
|
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
return [[[self alloc] init] autorelease];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
+ (NSDate*) dateWithTimeIntervalSinceNow: (NSTimeInterval)seconds
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-03-12 14:48:07 +00:00
|
|
|
|
return [[[self alloc] initWithTimeIntervalSinceNow: seconds]
|
1995-04-13 21:57:29 +00:00
|
|
|
|
autorelease];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-10-31 17:14:00 +00:00
|
|
|
|
+ (NSDate *)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds
|
|
|
|
|
{
|
|
|
|
|
return [[[self alloc] initWithTimeIntervalSinceReferenceDate:
|
|
|
|
|
UNIX_REFERENCE_INTERVAL + seconds] autorelease];
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
+ (NSDate*) dateWithTimeIntervalSinceReferenceDate: (NSTimeInterval)seconds
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-03-12 14:48:07 +00:00
|
|
|
|
return [[[self alloc] initWithTimeIntervalSinceReferenceDate: seconds]
|
|
|
|
|
autorelease];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSDate*) distantFuture
|
|
|
|
|
{
|
1996-03-18 18:31:37 +00:00
|
|
|
|
static id df = nil;
|
|
|
|
|
if (!df)
|
|
|
|
|
df = [[self alloc] initWithTimeIntervalSinceReferenceDate: DISTANT_FUTURE];
|
|
|
|
|
return df;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSDate*) distantPast
|
|
|
|
|
{
|
1996-03-18 18:31:37 +00:00
|
|
|
|
static id dp = nil;
|
|
|
|
|
if (!dp)
|
|
|
|
|
dp = [[self alloc] initWithTimeIntervalSinceReferenceDate: DISTANT_PAST];
|
|
|
|
|
return dp;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
return [self initWithTimeIntervalSinceReferenceDate:
|
|
|
|
|
[[self class] timeIntervalSinceReferenceDate]];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (id) initWithString: (NSString*)description
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
// Easiest to just have NSCalendarDate do the work for us
|
|
|
|
|
NSCalendarDate *d = [NSCalendarDate alloc];
|
|
|
|
|
[d initWithString: description];
|
|
|
|
|
[self initWithTimeIntervalSinceReferenceDate:
|
|
|
|
|
[d timeIntervalSinceReferenceDate]];
|
|
|
|
|
[d release];
|
|
|
|
|
return self;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSDate*) initWithTimeInterval: (NSTimeInterval)secsToBeAdded
|
|
|
|
|
sinceDate: (NSDate*)anotherDate;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
// Get the other date's time, add the secs and init thyself
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return [self initWithTimeIntervalSinceReferenceDate:
|
1996-10-31 17:14:00 +00:00
|
|
|
|
[anotherDate timeIntervalSinceReferenceDate] + secsToBeAdded];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSDate*) initWithTimeIntervalSinceNow: (NSTimeInterval)secsToBeAdded;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
// Get the current time, add the secs and init thyself
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return [self initWithTimeIntervalSinceReferenceDate:
|
|
|
|
|
[[self class] timeIntervalSinceReferenceDate] + secsToBeAdded];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-10-31 17:14:00 +00:00
|
|
|
|
- (id) initWithTimeIntervalSinceReferenceDate: (NSTimeInterval)secs
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
[super init];
|
|
|
|
|
seconds_since_ref = secs;
|
|
|
|
|
return self;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Converting to NSCalendar
|
1995-08-08 15:46:04 +00:00
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSCalendarDate *) dateWithCalendarFormat: (NSString*)formatString
|
|
|
|
|
timeZone: (NSTimeZone*)timeZone
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
NSCalendarDate *d = [NSCalendarDate alloc];
|
|
|
|
|
[d initWithTimeIntervalSinceReferenceDate: seconds_since_ref];
|
|
|
|
|
[d setCalendarFormat: formatString];
|
|
|
|
|
[d setTimeZone: timeZone];
|
|
|
|
|
return [d autorelease];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
1995-08-08 15:46:04 +00:00
|
|
|
|
|
1995-03-31 15:39:12 +00:00
|
|
|
|
// Representing dates
|
|
|
|
|
|
|
|
|
|
- (NSString*) description
|
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
// Easiest to just have NSCalendarDate do the work for us
|
|
|
|
|
NSString *s;
|
|
|
|
|
NSCalendarDate *d = [NSCalendarDate alloc];
|
|
|
|
|
[d initWithTimeIntervalSinceReferenceDate: seconds_since_ref];
|
|
|
|
|
s = [d description];
|
|
|
|
|
[d release];
|
|
|
|
|
return s;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
1995-08-08 15:46:04 +00:00
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSString*) descriptionWithCalendarFormat: (NSString*)format
|
|
|
|
|
timeZone: (NSTimeZone*)aTimeZone
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
// Easiest to just have NSCalendarDate do the work for us
|
|
|
|
|
NSString *s;
|
|
|
|
|
NSCalendarDate *d = [NSCalendarDate alloc];
|
|
|
|
|
id f, t;
|
|
|
|
|
|
|
|
|
|
[d initWithTimeIntervalSinceReferenceDate: seconds_since_ref];
|
|
|
|
|
if (!format)
|
|
|
|
|
f = [d calendarFormat];
|
|
|
|
|
else
|
|
|
|
|
f = format;
|
|
|
|
|
if (!aTimeZone)
|
|
|
|
|
t = [d timeZoneDetail];
|
|
|
|
|
else
|
|
|
|
|
t = aTimeZone;
|
|
|
|
|
|
|
|
|
|
s = [d descriptionWithCalendarFormat: f timeZone: t];
|
|
|
|
|
[d release];
|
|
|
|
|
return s;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
1995-08-08 15:46:04 +00:00
|
|
|
|
|
1996-10-31 17:14:00 +00:00
|
|
|
|
- (NSString *) descriptionWithLocale: (NSDictionary *)locale
|
|
|
|
|
{
|
|
|
|
|
// Easiest to just have NSCalendarDate do the work for us
|
|
|
|
|
NSString *s;
|
|
|
|
|
NSCalendarDate *d = [NSCalendarDate alloc];
|
|
|
|
|
[d initWithTimeIntervalSinceReferenceDate: seconds_since_ref];
|
|
|
|
|
s = [d descriptionWithLocale: locale];
|
|
|
|
|
[d release];
|
|
|
|
|
return s;
|
|
|
|
|
}
|
1995-03-31 15:39:12 +00:00
|
|
|
|
|
|
|
|
|
// Adding and getting intervals
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSDate*) addTimeInterval: (NSTimeInterval)seconds
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-03-12 14:48:07 +00:00
|
|
|
|
/* xxx We need to check for overflow? */
|
1995-08-02 15:21:54 +00:00
|
|
|
|
return [[self class] dateWithTimeIntervalSinceReferenceDate:
|
1996-10-31 17:14:00 +00:00
|
|
|
|
seconds_since_ref + seconds];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSTimeInterval) timeIntervalSinceDate: (NSDate*)otherDate
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
return seconds_since_ref - [otherDate timeIntervalSinceReferenceDate];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSTimeInterval) timeIntervalSinceNow
|
|
|
|
|
{
|
1996-03-12 14:48:07 +00:00
|
|
|
|
NSTimeInterval now = [[self class] timeIntervalSinceReferenceDate];
|
1996-10-31 17:14:00 +00:00
|
|
|
|
return seconds_since_ref - now;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSTimeInterval) timeIntervalSinceReferenceDate
|
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
return seconds_since_ref;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Comparing dates
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSComparisonResult) compare: (NSDate*)otherDate
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
if (seconds_since_ref > [otherDate timeIntervalSinceReferenceDate])
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return NSOrderedDescending;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
|
1996-10-31 17:14:00 +00:00
|
|
|
|
if (seconds_since_ref < [otherDate timeIntervalSinceReferenceDate])
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return NSOrderedAscending;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return NSOrderedSame;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSDate*) earlierDate: (NSDate*)otherDate
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
if (seconds_since_ref > [otherDate timeIntervalSinceReferenceDate])
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return otherDate;
|
|
|
|
|
return self;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (BOOL) isEqual: (id)other
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1995-08-02 15:21:54 +00:00
|
|
|
|
if ([other isKindOf: [NSDate class]]
|
1996-10-31 17:14:00 +00:00
|
|
|
|
&& 1.0 > (seconds_since_ref - [other timeIntervalSinceReferenceDate]))
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return YES;
|
|
|
|
|
return NO;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSDate*) laterDate: (NSDate*)otherDate
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
if (seconds_since_ref < [otherDate timeIntervalSinceReferenceDate])
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return otherDate;
|
|
|
|
|
return self;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|