2001-12-17 14:31:42 +00:00
|
|
|
|
/** Implementation for NSDate for GNUStep
|
1998-12-02 20:16:23 +00:00
|
|
|
|
Copyright (C) 1995, 1996, 1997, 1998 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
|
1998-12-02 20:16:23 +00:00
|
|
|
|
Modifications by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
1995-04-13 21:57:29 +00:00
|
|
|
|
|
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
|
2005-05-22 03:32:16 +00:00
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
|
|
<title>NSDate class reference</title>
|
|
|
|
|
$Date$ $Revision$
|
1995-03-31 15:39:12 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "Foundation/NSArray.h"
|
|
|
|
|
#include "Foundation/NSDictionary.h"
|
|
|
|
|
#include "Foundation/NSDate.h"
|
|
|
|
|
#include "Foundation/NSCalendarDate.h"
|
|
|
|
|
#include "Foundation/NSTimeZone.h"
|
|
|
|
|
#include "Foundation/NSString.h"
|
|
|
|
|
#include "Foundation/NSCoder.h"
|
|
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
|
#include "Foundation/NSCharacterSet.h"
|
|
|
|
|
#include "Foundation/NSScanner.h"
|
|
|
|
|
#include "Foundation/NSObjCRuntime.h"
|
|
|
|
|
#include "Foundation/NSPortCoder.h"
|
|
|
|
|
#include "Foundation/NSUserDefaults.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/preface.h"
|
|
|
|
|
#include "GNUstepBase/GSObjCRuntime.h"
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2000-06-06 16:50:52 +00:00
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#endif
|
1996-03-12 14:48:07 +00:00
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2002-03-13 09:58:43 +00:00
|
|
|
|
#include "GSPrivate.h"
|
1996-02-13 15:43:30 +00:00
|
|
|
|
|
2005-03-15 16:40:07 +00:00
|
|
|
|
/* These constants seem to be what MacOS-X uses */
|
|
|
|
|
#define DISTANT_FUTURE 63113990400.0
|
|
|
|
|
#define DISTANT_PAST -63113817600.0
|
1996-03-12 14:48:07 +00:00
|
|
|
|
|
2002-11-01 08:11:46 +00:00
|
|
|
|
const NSTimeInterval NSTimeIntervalSince1970 = 978307200.0;
|
|
|
|
|
|
1999-04-23 14:38:03 +00:00
|
|
|
|
|
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
|
static BOOL debug = NO;
|
|
|
|
|
static Class abstractClass = nil;
|
|
|
|
|
static Class concreteClass = nil;
|
|
|
|
|
static Class calendarClass = nil;
|
|
|
|
|
|
2002-08-08 19:25:17 +00:00
|
|
|
|
/**
|
|
|
|
|
* Our concrete base class - NSCalendar date must share the ivar layout.
|
|
|
|
|
*/
|
|
|
|
|
@interface NSGDate : NSDate
|
|
|
|
|
{
|
|
|
|
|
@public
|
|
|
|
|
NSTimeInterval _seconds_since_ref;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
1999-04-23 14:38:03 +00:00
|
|
|
|
@interface GSDateSingle : NSGDate
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface GSDatePast : GSDateSingle
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface GSDateFuture : GSDateSingle
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
static NSDate *_distantPast = nil;
|
|
|
|
|
static NSDate *_distantFuture = nil;
|
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
|
|
1998-12-02 20:16:23 +00:00
|
|
|
|
static NSString*
|
|
|
|
|
findInArray(NSArray *array, unsigned pos, NSString *str)
|
|
|
|
|
{
|
|
|
|
|
unsigned index;
|
|
|
|
|
unsigned limit = [array count];
|
|
|
|
|
|
|
|
|
|
for (index = pos; index < limit; index++)
|
|
|
|
|
{
|
|
|
|
|
NSString *item;
|
|
|
|
|
|
|
|
|
|
item = [array objectAtIndex: index];
|
|
|
|
|
if ([str caseInsensitiveCompare: item] == NSOrderedSame)
|
|
|
|
|
return item;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
|
static inline NSTimeInterval
|
|
|
|
|
otherTime(NSDate* other)
|
|
|
|
|
{
|
2000-10-31 11:05:23 +00:00
|
|
|
|
Class c;
|
1998-12-02 20:16:23 +00:00
|
|
|
|
|
2000-10-31 11:05:23 +00:00
|
|
|
|
if (other == nil)
|
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"other time nil"];
|
2000-10-31 16:17:33 +00:00
|
|
|
|
if (GSObjCIsInstance(other) == NO)
|
2000-10-31 11:05:23 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException format: @"other time bad"];
|
2000-10-31 16:17:33 +00:00
|
|
|
|
c = GSObjCClass(other);
|
1999-04-19 14:29:52 +00:00
|
|
|
|
if (c == concreteClass || c == calendarClass)
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return ((NSGDate*)other)->_seconds_since_ref;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
else
|
|
|
|
|
return [other timeIntervalSinceReferenceDate];
|
|
|
|
|
}
|
1995-03-31 15:39:12 +00:00
|
|
|
|
|
2002-08-08 19:25:17 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the current time (seconds since reference date) as an NSTimeInterval.
|
|
|
|
|
*/
|
1999-04-20 16:28:04 +00:00
|
|
|
|
NSTimeInterval
|
2002-11-09 06:45:31 +00:00
|
|
|
|
GSTimeNow(void)
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
2005-10-11 19:09:26 +00:00
|
|
|
|
#if !defined(__MINGW32__)
|
2001-05-04 04:37:24 +00:00
|
|
|
|
NSTimeInterval interval;
|
1996-03-12 14:48:07 +00:00
|
|
|
|
struct timeval tp;
|
|
|
|
|
|
1997-05-03 18:13:17 +00:00
|
|
|
|
gettimeofday (&tp, NULL);
|
2002-11-01 08:11:46 +00:00
|
|
|
|
interval = -NSTimeIntervalSince1970;
|
1996-03-12 14:48:07 +00:00
|
|
|
|
interval += tp.tv_sec;
|
|
|
|
|
interval += (double)tp.tv_usec / 1000000.0;
|
|
|
|
|
return interval;
|
1996-10-31 17:22:04 +00:00
|
|
|
|
#else
|
|
|
|
|
SYSTEMTIME sys_time;
|
|
|
|
|
NSTimeInterval t;
|
2000-06-30 15:44:30 +00:00
|
|
|
|
#if 0
|
|
|
|
|
NSCalendarDate *d;
|
1996-10-31 17:22:04 +00:00
|
|
|
|
|
|
|
|
|
// 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
|
1999-01-20 13:28:28 +00:00
|
|
|
|
timeZone: [NSTimeZone localTimeZone]];
|
1999-04-19 14:29:52 +00:00
|
|
|
|
t = otherTime(d);
|
|
|
|
|
RELEASE(d);
|
2000-06-30 15:44:30 +00:00
|
|
|
|
#else
|
|
|
|
|
/*
|
|
|
|
|
* Get current GMT time, convert to NSTimeInterval since reference date,
|
|
|
|
|
*/
|
|
|
|
|
GetSystemTime(&sys_time);
|
2000-12-02 11:47:30 +00:00
|
|
|
|
t = GSTime(sys_time.wDay, sys_time.wMonth, sys_time.wYear, sys_time.wHour,
|
2005-02-22 11:22:44 +00:00
|
|
|
|
sys_time.wMinute, sys_time.wSecond, sys_time.wMilliseconds);
|
2000-06-30 15:44:30 +00:00
|
|
|
|
#endif
|
2001-01-30 15:11:08 +00:00
|
|
|
|
return t;
|
2005-10-11 19:09:26 +00:00
|
|
|
|
#endif /* __MINGW32__ */
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
|
* An <code>NSDate</code> object encapsulates a constant date/time to a high
|
|
|
|
|
* resolution represented by the <code>NSTimeInterval</code> typedef.
|
|
|
|
|
* <code>NSDate</code> has methods relating to times and time differences in
|
|
|
|
|
* the abstract, but not calendar dates or time zones. These features are
|
|
|
|
|
* added in the [NSCalendarDate] subclass. The [NSTimeZone] class handles time
|
|
|
|
|
* zone information.
|
2003-05-16 12:45:02 +00:00
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
@implementation NSDate
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [NSDate class])
|
|
|
|
|
{
|
|
|
|
|
[self setVersion: 1];
|
|
|
|
|
abstractClass = self;
|
|
|
|
|
concreteClass = [NSGDate class];
|
|
|
|
|
calendarClass = [NSCalendarDate class];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) alloc
|
|
|
|
|
{
|
|
|
|
|
if (self == abstractClass)
|
|
|
|
|
return NSAllocateObject(concreteClass, 0, NSDefaultMallocZone());
|
|
|
|
|
else
|
|
|
|
|
return NSAllocateObject(self, 0, NSDefaultMallocZone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
if (self == abstractClass)
|
|
|
|
|
return NSAllocateObject(concreteClass, 0, z);
|
|
|
|
|
else
|
|
|
|
|
return NSAllocateObject(self, 0, z);
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the time interval between the current date and the
|
|
|
|
|
* reference date (1 January 2001, GMT).
|
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
+ (NSTimeInterval) timeIntervalSinceReferenceDate
|
|
|
|
|
{
|
1999-04-20 16:28:04 +00:00
|
|
|
|
return GSTimeNow();
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased instance representing the current date/time.
|
|
|
|
|
*/
|
1998-01-21 14:56:24 +00:00
|
|
|
|
+ (id) date
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
1999-04-20 16:28:04 +00:00
|
|
|
|
initWithTimeIntervalSinceReferenceDate: GSTimeNow()]);
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased instance representing the date and time given
|
|
|
|
|
* by string. The value of string may be a 'natural' specification as
|
|
|
|
|
* specified by the preferences in the user defaults database, allowing
|
|
|
|
|
* phrases like 'last tuesday'
|
|
|
|
|
*/
|
1998-12-02 20:16:23 +00:00
|
|
|
|
+ (id) dateWithNaturalLanguageString: (NSString*)string
|
|
|
|
|
{
|
1999-06-24 19:30:29 +00:00
|
|
|
|
return [self dateWithNaturalLanguageString: string
|
|
|
|
|
locale: nil];
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>Returns an autoreleased instance representing the date and time given
|
|
|
|
|
* by string. The value of string may be a 'natural' specification as
|
|
|
|
|
* specified by the preferences in the user defaults database, allowing
|
|
|
|
|
* phrases like 'last tuesday'
|
|
|
|
|
* </p>
|
|
|
|
|
* The locale contains keys such as -
|
|
|
|
|
* <deflist>
|
|
|
|
|
* <term>NSDateTimeOrdering</term>
|
|
|
|
|
* <desc>Controls the use of ambiguous numbers. This is done as a
|
|
|
|
|
* sequence of the letters D(ay), M(onth), Y(ear), and H(our).
|
|
|
|
|
* YMDH means that the first number encountered is assumed to be a
|
|
|
|
|
* year, the second a month, the third a day, and the last an hour.
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>NSEarlierTimeDesignations</term>
|
|
|
|
|
* <desc>An array of strings for times in the past.<br />
|
|
|
|
|
* Defaults are <em>ago</em>, <em>last</em>, <em>past</em>, <em>prior</em>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>NSHourNameDesignations</term>
|
|
|
|
|
* <desc>An array of arrays of strings identifying the time of day.
|
|
|
|
|
* Each array has an hour as its first value, and one or more words
|
|
|
|
|
* as subsequent values.<br />
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* Defaults are: (0, midnight), (10, morning), (12, noon, lunch),
|
2003-05-16 12:45:02 +00:00
|
|
|
|
* (14, afternoon), (19, dinner).
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>NSLaterTimeDesignations</term>
|
|
|
|
|
* <desc>An array of strings for times in the future.<br />
|
|
|
|
|
* Default is <em>next</em>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>NSNextDayDesignations</term>
|
|
|
|
|
* <desc>The day after today. Default is <em>tomorrow.</em>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>NSNextNextDayDesignations</term>
|
|
|
|
|
* <desc>The day after tomorrow. Default is <em>nextday.</em>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>NSPriorDayDesignations</term>
|
|
|
|
|
* <desc>The day before today. Default is <em>yesterday.</em>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>NSThisDayDesignations</term>
|
|
|
|
|
* <desc>Identifies the current day. Default is <em>today.</em>
|
|
|
|
|
* </desc>
|
|
|
|
|
* <term>NSYearMonthWeekDesignations</term>
|
|
|
|
|
* <desc>An array giving the word for year, month, and week.<br />
|
|
|
|
|
* Defaults are <em>year</em>, <em>month</em> and <em>week</em>.
|
|
|
|
|
* </desc>
|
|
|
|
|
* </deflist>
|
|
|
|
|
*/
|
1998-12-02 20:16:23 +00:00
|
|
|
|
+ (id) dateWithNaturalLanguageString: (NSString*)string
|
|
|
|
|
locale: (NSDictionary*)locale
|
|
|
|
|
{
|
|
|
|
|
NSCharacterSet *ws;
|
|
|
|
|
NSCharacterSet *digits;
|
|
|
|
|
NSScanner *scanner;
|
|
|
|
|
NSString *tmp;
|
|
|
|
|
NSString *dto;
|
|
|
|
|
NSArray *ymw;
|
|
|
|
|
NSMutableArray *words;
|
|
|
|
|
unsigned index;
|
|
|
|
|
unsigned length;
|
|
|
|
|
NSCalendarDate *theDate;
|
|
|
|
|
BOOL hadHour = NO;
|
|
|
|
|
BOOL hadMinute = NO;
|
|
|
|
|
BOOL hadSecond = NO;
|
|
|
|
|
BOOL hadDay = NO;
|
|
|
|
|
BOOL hadMonth = NO;
|
|
|
|
|
BOOL hadYear = NO;
|
|
|
|
|
BOOL hadWeekDay = NO;
|
|
|
|
|
int weekDay = 0;
|
|
|
|
|
int dayOfWeek = 0;
|
|
|
|
|
int modMonth = 0;
|
|
|
|
|
int modYear = 0;
|
|
|
|
|
int modDay = 0;
|
|
|
|
|
int D, M, Y;
|
|
|
|
|
int h = 12;
|
|
|
|
|
int m = 0;
|
|
|
|
|
int s = 0;
|
|
|
|
|
unsigned dtoIndex;
|
|
|
|
|
|
|
|
|
|
if (locale == nil)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
locale = GSUserDefaultsDictionaryRepresentation();
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
ws = [NSCharacterSet whitespaceAndNewlineCharacterSet];
|
|
|
|
|
digits = [NSCharacterSet decimalDigitCharacterSet];
|
|
|
|
|
scanner = [NSScanner scannerWithString: string];
|
|
|
|
|
words = [NSMutableArray arrayWithCapacity: 10];
|
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
|
theDate = (NSCalendarDate*)[calendarClass date];
|
1998-12-02 20:16:23 +00:00
|
|
|
|
Y = [theDate yearOfCommonEra];
|
|
|
|
|
M = [theDate monthOfYear];
|
|
|
|
|
D = [theDate dayOfMonth];
|
|
|
|
|
dayOfWeek = [theDate dayOfWeek];
|
|
|
|
|
|
|
|
|
|
[scanner scanCharactersFromSet: ws intoString: 0];
|
|
|
|
|
while ([scanner scanUpToCharactersFromSet: ws intoString: &tmp] == YES)
|
|
|
|
|
{
|
|
|
|
|
[words addObject: tmp];
|
|
|
|
|
[scanner scanCharactersFromSet: ws intoString: 0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Scan the array for day specifications and remove them.
|
|
|
|
|
*/
|
|
|
|
|
if (hadDay == NO)
|
|
|
|
|
{
|
2001-07-10 17:19:18 +00:00
|
|
|
|
NSArray *tdd = [locale objectForKey: NSThisDayDesignations];
|
|
|
|
|
NSArray *ndd = [locale objectForKey: NSNextDayDesignations];
|
|
|
|
|
NSArray *pdd = [locale objectForKey: NSPriorDayDesignations];
|
|
|
|
|
NSArray *nndd = [locale objectForKey: NSNextNextDayDesignations];
|
1998-12-02 20:16:23 +00:00
|
|
|
|
|
|
|
|
|
for (index = 0; hadDay == NO && index < [words count]; index++)
|
|
|
|
|
{
|
|
|
|
|
tmp = [words objectAtIndex: index];
|
|
|
|
|
|
2001-07-10 17:19:18 +00:00
|
|
|
|
if (findInArray(tdd, 0 ,tmp) != nil)
|
1998-12-02 20:16:23 +00:00
|
|
|
|
{
|
|
|
|
|
hadDay = YES;
|
|
|
|
|
}
|
2001-07-10 17:19:18 +00:00
|
|
|
|
else if (findInArray(ndd, 0 ,tmp) != nil)
|
1998-12-02 20:16:23 +00:00
|
|
|
|
{
|
|
|
|
|
modDay++;
|
|
|
|
|
hadDay = YES;
|
|
|
|
|
}
|
2001-07-10 17:19:18 +00:00
|
|
|
|
else if (findInArray(nndd, 0 ,tmp) != nil)
|
1998-12-02 20:16:23 +00:00
|
|
|
|
{
|
|
|
|
|
modDay += 2;
|
|
|
|
|
hadDay = YES;
|
|
|
|
|
}
|
2001-07-10 17:19:18 +00:00
|
|
|
|
else if (findInArray(pdd, 0 ,tmp) != nil)
|
1998-12-02 20:16:23 +00:00
|
|
|
|
{
|
|
|
|
|
modDay--;
|
|
|
|
|
hadDay = YES;
|
|
|
|
|
}
|
|
|
|
|
if (hadDay)
|
|
|
|
|
{
|
|
|
|
|
hadMonth = YES;
|
|
|
|
|
hadYear = YES;
|
|
|
|
|
[words removeObjectAtIndex: index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Scan the array for month specifications and remove them.
|
|
|
|
|
*/
|
|
|
|
|
if (hadMonth == NO)
|
|
|
|
|
{
|
|
|
|
|
NSArray *lm = [locale objectForKey: NSMonthNameArray];
|
|
|
|
|
NSArray *sm = [locale objectForKey: NSShortMonthNameArray];
|
|
|
|
|
|
|
|
|
|
for (index = 0; hadMonth == NO && index < [words count]; index++)
|
|
|
|
|
{
|
|
|
|
|
NSString *mname;
|
|
|
|
|
|
|
|
|
|
tmp = [words objectAtIndex: index];
|
|
|
|
|
|
|
|
|
|
if ((mname = findInArray(lm, 0, tmp)) != nil)
|
|
|
|
|
{
|
2001-07-10 17:43:05 +00:00
|
|
|
|
M = [lm indexOfObjectIdenticalTo: mname] + 1;
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else if ((mname = findInArray(sm, 0, tmp)) != nil)
|
|
|
|
|
{
|
2001-07-10 17:43:05 +00:00
|
|
|
|
M = [sm indexOfObjectIdenticalTo: mname] + 1;
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mname != nil)
|
|
|
|
|
{
|
|
|
|
|
hadMonth = YES;
|
|
|
|
|
[words removeObjectAtIndex: index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Scan the array for weekday specifications and remove them.
|
|
|
|
|
*/
|
|
|
|
|
if (hadWeekDay == NO)
|
|
|
|
|
{
|
|
|
|
|
NSArray *lw = [locale objectForKey: NSWeekDayNameArray];
|
|
|
|
|
NSArray *sw = [locale objectForKey: NSShortWeekDayNameArray];
|
|
|
|
|
|
|
|
|
|
for (index = 0; hadWeekDay == NO && index < [words count]; index++)
|
|
|
|
|
{
|
|
|
|
|
NSString *dname;
|
|
|
|
|
|
|
|
|
|
tmp = [words objectAtIndex: index];
|
|
|
|
|
|
|
|
|
|
if ((dname = findInArray(lw, 0, tmp)) != nil)
|
|
|
|
|
{
|
|
|
|
|
weekDay = [lw indexOfObjectIdenticalTo: dname];
|
|
|
|
|
}
|
|
|
|
|
else if ((dname = findInArray(sw, 0, tmp)) != nil)
|
|
|
|
|
{
|
|
|
|
|
weekDay = [sw indexOfObjectIdenticalTo: dname];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dname != nil)
|
|
|
|
|
{
|
|
|
|
|
hadWeekDay = YES;
|
|
|
|
|
[words removeObjectAtIndex: index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Scan the array for year month week modifiers and remove them.
|
|
|
|
|
* Going by the documentation, these modifiers adjust the date by
|
|
|
|
|
* plus or minus a week, month, or year.
|
|
|
|
|
*/
|
|
|
|
|
ymw = [locale objectForKey: NSYearMonthWeekDesignations];
|
|
|
|
|
if (ymw != nil && [ymw count] > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned c = [ymw count];
|
|
|
|
|
NSString *yname = [ymw objectAtIndex: 0];
|
|
|
|
|
NSString *mname = c > 1 ? [ymw objectAtIndex: 1] : nil;
|
|
|
|
|
NSArray *early = [locale objectForKey: NSEarlierTimeDesignations];
|
|
|
|
|
NSArray *later = [locale objectForKey: NSLaterTimeDesignations];
|
|
|
|
|
|
|
|
|
|
for (index = 0; index < [words count]; index++)
|
|
|
|
|
{
|
|
|
|
|
tmp = [words objectAtIndex: index];
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* See if the current word is a year, month, or week.
|
|
|
|
|
*/
|
|
|
|
|
if (findInArray(ymw, 0, tmp))
|
|
|
|
|
{
|
|
|
|
|
BOOL hadAdjective = NO;
|
|
|
|
|
int adjective = 0;
|
|
|
|
|
NSString *adj = nil;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* See if there is a prefix adjective
|
|
|
|
|
*/
|
|
|
|
|
if (index > 0)
|
|
|
|
|
{
|
|
|
|
|
adj = [words objectAtIndex: index - 1];
|
|
|
|
|
|
|
|
|
|
if (findInArray(early, 0, adj))
|
|
|
|
|
{
|
|
|
|
|
hadAdjective = YES;
|
|
|
|
|
adjective = -1;
|
|
|
|
|
}
|
|
|
|
|
else if (findInArray(later, 0, adj))
|
|
|
|
|
{
|
|
|
|
|
hadAdjective = YES;
|
|
|
|
|
adjective = 1;
|
|
|
|
|
}
|
|
|
|
|
if (hadAdjective)
|
|
|
|
|
{
|
|
|
|
|
[words removeObjectAtIndex: --index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* See if there is a prefix adjective
|
|
|
|
|
*/
|
|
|
|
|
if (hadAdjective == NO && index < [words count] - 1)
|
|
|
|
|
{
|
|
|
|
|
NSString *adj = [words objectAtIndex: index + 1];
|
|
|
|
|
|
|
|
|
|
if (findInArray(early, 0, adj))
|
|
|
|
|
{
|
|
|
|
|
hadAdjective = YES;
|
|
|
|
|
adjective = -1;
|
|
|
|
|
}
|
|
|
|
|
else if (findInArray(later, 0, adj))
|
|
|
|
|
{
|
|
|
|
|
hadAdjective = YES;
|
|
|
|
|
adjective = 1;
|
|
|
|
|
}
|
|
|
|
|
if (hadAdjective)
|
|
|
|
|
{
|
|
|
|
|
[words removeObjectAtIndex: index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Record the adjective information.
|
|
|
|
|
*/
|
|
|
|
|
if (hadAdjective)
|
|
|
|
|
{
|
|
|
|
|
if ([tmp caseInsensitiveCompare: yname] == NSOrderedSame)
|
|
|
|
|
{
|
|
|
|
|
modYear += adjective;
|
|
|
|
|
hadYear = YES;
|
|
|
|
|
}
|
|
|
|
|
else if ([tmp caseInsensitiveCompare: mname] == NSOrderedSame)
|
|
|
|
|
{
|
|
|
|
|
modMonth += adjective;
|
|
|
|
|
hadMonth = YES;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (hadWeekDay)
|
|
|
|
|
{
|
|
|
|
|
modDay += weekDay - dayOfWeek;
|
|
|
|
|
}
|
|
|
|
|
modDay += 7*adjective;
|
|
|
|
|
hadDay = YES;
|
|
|
|
|
hadMonth = YES;
|
|
|
|
|
hadYear = YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Remove from list of words.
|
|
|
|
|
*/
|
|
|
|
|
[words removeObjectAtIndex: index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Scan for hour of the day */
|
|
|
|
|
if (hadHour == NO)
|
|
|
|
|
{
|
|
|
|
|
NSArray *hours = [locale objectForKey: NSHourNameDesignations];
|
|
|
|
|
unsigned hLimit = [hours count];
|
|
|
|
|
unsigned hIndex;
|
|
|
|
|
|
|
|
|
|
for (index = 0; hadHour == NO && index < [words count]; index++)
|
|
|
|
|
{
|
|
|
|
|
tmp = [words objectAtIndex: index];
|
|
|
|
|
|
|
|
|
|
for (hIndex = 0; hadHour == NO && hIndex < hLimit; hIndex++)
|
|
|
|
|
{
|
|
|
|
|
NSArray *names;
|
|
|
|
|
|
|
|
|
|
names = [hours objectAtIndex: hIndex];
|
|
|
|
|
if (findInArray(names, 1, tmp) != nil)
|
|
|
|
|
{
|
|
|
|
|
h = [[names objectAtIndex: 0] intValue];
|
|
|
|
|
hadHour = YES;
|
|
|
|
|
hadMinute = YES;
|
|
|
|
|
hadSecond = YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Now re-scan the string for numeric information.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
dto = [locale objectForKey: NSDateTimeOrdering];
|
|
|
|
|
if (dto == nil)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"no NSDateTimeOrdering - default to DMYH.");
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
dto = @"DMYH";
|
|
|
|
|
}
|
|
|
|
|
length = [dto length];
|
|
|
|
|
if (length > 4)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"too many characters in NSDateTimeOrdering - truncating.");
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
length = 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dtoIndex = 0;
|
|
|
|
|
scanner = [NSScanner scannerWithString: string];
|
|
|
|
|
[scanner scanUpToCharactersFromSet: digits intoString: 0];
|
|
|
|
|
while ([scanner scanCharactersFromSet: digits intoString: &tmp] == YES)
|
|
|
|
|
{
|
|
|
|
|
int num = [tmp intValue];
|
|
|
|
|
|
|
|
|
|
if ([scanner scanUpToCharactersFromSet: digits intoString: &tmp] == NO)
|
|
|
|
|
{
|
|
|
|
|
tmp = nil;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Numbers separated by colons are a time specification.
|
|
|
|
|
*/
|
|
|
|
|
if (tmp && ([tmp characterAtIndex: 0] == (unichar)':'))
|
|
|
|
|
{
|
|
|
|
|
BOOL done = NO;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if (hadHour == NO)
|
|
|
|
|
{
|
|
|
|
|
if (num > 23)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"hour (%d) too large - ignored.", num);
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
else
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
h = num;
|
|
|
|
|
m = 0;
|
|
|
|
|
s = 0;
|
|
|
|
|
hadHour = YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (hadMinute == NO)
|
|
|
|
|
{
|
|
|
|
|
if (num > 59)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"minute (%d) too large - ignored.", num);
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
else
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m = num;
|
|
|
|
|
s = 0;
|
|
|
|
|
hadMinute = YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (hadSecond == NO)
|
|
|
|
|
{
|
|
|
|
|
if (num > 59)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"second (%d) too large - ignored.", num);
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
else
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s = num;
|
|
|
|
|
hadSecond = YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"odd time spec - excess numbers ignored.");
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
done = YES;
|
|
|
|
|
if (tmp && ([tmp characterAtIndex: 0] == (unichar)':'))
|
|
|
|
|
{
|
|
|
|
|
if ([scanner scanCharactersFromSet: digits intoString: &tmp])
|
|
|
|
|
{
|
|
|
|
|
num = [tmp intValue];
|
|
|
|
|
done = NO;
|
|
|
|
|
if ([scanner scanUpToCharactersFromSet: digits
|
|
|
|
|
intoString: &tmp] == NO)
|
|
|
|
|
{
|
|
|
|
|
tmp = nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (done == NO);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
BOOL mustSkip = YES;
|
|
|
|
|
|
|
|
|
|
while ((dtoIndex < [dto length]) && (mustSkip == YES))
|
|
|
|
|
{
|
|
|
|
|
switch ([dto characterAtIndex: dtoIndex])
|
|
|
|
|
{
|
|
|
|
|
case 'D':
|
|
|
|
|
if (hadDay)
|
|
|
|
|
dtoIndex++;
|
|
|
|
|
else
|
|
|
|
|
mustSkip = NO;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
|
if (hadMonth)
|
|
|
|
|
dtoIndex++;
|
|
|
|
|
else
|
|
|
|
|
mustSkip = NO;
|
|
|
|
|
break;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
|
1998-12-02 20:16:23 +00:00
|
|
|
|
case 'Y':
|
|
|
|
|
if (hadYear)
|
|
|
|
|
dtoIndex++;
|
|
|
|
|
else
|
|
|
|
|
mustSkip = NO;
|
|
|
|
|
break;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
|
1998-12-02 20:16:23 +00:00
|
|
|
|
case 'H':
|
|
|
|
|
if (hadHour)
|
|
|
|
|
dtoIndex++;
|
|
|
|
|
else
|
|
|
|
|
mustSkip = NO;
|
|
|
|
|
break;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
|
1998-12-02 20:16:23 +00:00
|
|
|
|
default:
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"odd char (unicode %d) in NSDateTimeOrdering.",
|
|
|
|
|
[dto characterAtIndex: dtoIndex]);
|
|
|
|
|
}
|
1999-09-13 04:11:39 +00:00
|
|
|
|
dtoIndex++;
|
1998-12-02 20:16:23 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (dtoIndex >= [dto length])
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"odd date specification - excess numbers ignored.");
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch ([dto characterAtIndex: dtoIndex])
|
|
|
|
|
{
|
|
|
|
|
case 'D':
|
|
|
|
|
if (num < 1)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"day (0) too small - ignored.");
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
else
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else if (num > 31)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"day (%d) too large - ignored.", num);
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
else
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
D = num;
|
|
|
|
|
hadDay = YES;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'M':
|
|
|
|
|
if (num < 1)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"month (0) too small - ignored.");
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
else
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else if (num > 12)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"month (%d) too large - ignored.", num);
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
else
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
M = num;
|
|
|
|
|
hadMonth = YES;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'Y':
|
|
|
|
|
if (num < 100)
|
|
|
|
|
{
|
|
|
|
|
if (num < 70)
|
|
|
|
|
{
|
|
|
|
|
Y = num + 2000;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Y = num + 1900;
|
|
|
|
|
}
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"year (%d) adjusted to %d.", num, Y);
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Y = num;
|
|
|
|
|
}
|
|
|
|
|
hadYear = YES;
|
|
|
|
|
break;
|
|
|
|
|
case 'H':
|
|
|
|
|
{
|
|
|
|
|
BOOL shouldIgnore = NO;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Check the next text to see if it is an am/pm
|
|
|
|
|
* designation.
|
|
|
|
|
*/
|
|
|
|
|
if (tmp)
|
|
|
|
|
{
|
|
|
|
|
NSArray *ampm;
|
|
|
|
|
NSString *mod;
|
|
|
|
|
|
|
|
|
|
ampm = [locale objectForKey: NSAMPMDesignation];
|
|
|
|
|
mod = findInArray(ampm, 0, tmp);
|
|
|
|
|
if (mod)
|
|
|
|
|
{
|
|
|
|
|
if (num > 11)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"hour (%d) too large - ignored.", num);
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
else
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
shouldIgnore = YES;
|
|
|
|
|
}
|
|
|
|
|
else if (mod == [ampm objectAtIndex: 1])
|
|
|
|
|
{
|
|
|
|
|
num += 12;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (shouldIgnore == NO)
|
|
|
|
|
{
|
|
|
|
|
if (num > 23)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"hour (%d) too large - ignored.", num);
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
else
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hadHour = YES;
|
|
|
|
|
h = num;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"unexpected char (unicode%d) in NSDateTimeOrdering.",
|
|
|
|
|
[dto characterAtIndex: dtoIndex]);
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-04-19 14:29:52 +00:00
|
|
|
|
|
1998-12-02 20:16:23 +00:00
|
|
|
|
/*
|
|
|
|
|
* If we had no date or time information - we give up, otherwise
|
|
|
|
|
* we can use reasonable defaults for any missing info.
|
|
|
|
|
* Missing date => today
|
1999-04-19 14:29:52 +00:00
|
|
|
|
* Missing time => 12: 00
|
1998-12-02 20:16:23 +00:00
|
|
|
|
* If we had a week/month/year modifier without a day, we assume today.
|
|
|
|
|
* If we had a day name without any more day detail - adjust to that
|
|
|
|
|
* day this week.
|
|
|
|
|
*/
|
|
|
|
|
if (hadDay == NO && hadWeekDay == YES)
|
|
|
|
|
{
|
|
|
|
|
modDay += weekDay - dayOfWeek;
|
|
|
|
|
hadDay = YES;
|
|
|
|
|
}
|
|
|
|
|
if (hadDay == NO && hadHour == NO)
|
|
|
|
|
{
|
|
|
|
|
if (modDay == NO && modMonth == NO && modYear == NO)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Build a calendar date we can adjust easily.
|
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
theDate = [calendarClass dateWithYear: Y
|
1998-12-02 20:16:23 +00:00
|
|
|
|
month: M
|
|
|
|
|
day: D
|
|
|
|
|
hour: h
|
|
|
|
|
minute: m
|
|
|
|
|
second: s
|
|
|
|
|
timeZone: [NSTimeZone defaultTimeZone]];
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Adjust the date by year month or days if necessary.
|
|
|
|
|
*/
|
|
|
|
|
if (modYear || modMonth || modDay)
|
|
|
|
|
{
|
|
|
|
|
theDate = [theDate dateByAddingYears: modYear
|
|
|
|
|
months: modMonth
|
|
|
|
|
days: modDay
|
|
|
|
|
hours: 0
|
|
|
|
|
minutes: 0
|
|
|
|
|
seconds: 0];
|
|
|
|
|
}
|
|
|
|
|
if (hadWeekDay && [theDate dayOfWeek] != weekDay)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
2002-10-11 09:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"Date resulted in wrong day of week.");
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
2001-05-18 15:25:48 +00:00
|
|
|
|
if (theDate == nil)
|
|
|
|
|
{
|
|
|
|
|
return theDate;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return [self dateWithTimeIntervalSinceReferenceDate:
|
|
|
|
|
otherTime(theDate)];
|
|
|
|
|
}
|
1998-12-02 20:16:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased instance with the date and time value given
|
|
|
|
|
* by the string using the ISO standard format YYYY-MM-DD HH:MM:SS +/-HHHMM
|
|
|
|
|
* (all the fields of which must be present).
|
|
|
|
|
*/
|
1998-01-21 14:56:24 +00:00
|
|
|
|
+ (id) dateWithString: (NSString*)description
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return AUTORELEASE([[self alloc] initWithString: description]);
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased instance with the offset from the current
|
|
|
|
|
* date/time given by seconds (which may be fractional).
|
|
|
|
|
*/
|
1998-01-21 14:56:24 +00:00
|
|
|
|
+ (id) dateWithTimeIntervalSinceNow: (NSTimeInterval)seconds
|
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return AUTORELEASE([[self alloc] initWithTimeIntervalSinceNow: seconds]);
|
1998-01-21 14:56:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased instance with the offset from the unix system
|
|
|
|
|
* reference date of 1 January 1970, GMT.
|
|
|
|
|
*/
|
2002-09-30 16:54:29 +00:00
|
|
|
|
+ (id) dateWithTimeIntervalSince1970: (NSTimeInterval)seconds
|
1996-10-31 17:14:00 +00:00
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return AUTORELEASE([[self alloc] initWithTimeIntervalSinceReferenceDate:
|
2002-11-01 08:11:46 +00:00
|
|
|
|
-NSTimeIntervalSince1970 + seconds]);
|
1996-10-31 17:14:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased instance with the offset from the OpenStep
|
|
|
|
|
* reference date of 1 January 2001, GMT.
|
|
|
|
|
*/
|
1998-01-21 14:56:24 +00:00
|
|
|
|
+ (id) dateWithTimeIntervalSinceReferenceDate: (NSTimeInterval)seconds
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
2002-09-30 16:54:29 +00:00
|
|
|
|
return AUTORELEASE([[self alloc] initWithTimeIntervalSinceReferenceDate:
|
|
|
|
|
seconds]);
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased instance with th date/time set in the far
|
|
|
|
|
* future.
|
|
|
|
|
*/
|
1998-01-21 14:56:24 +00:00
|
|
|
|
+ (id) distantFuture
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1999-04-23 14:38:03 +00:00
|
|
|
|
if (_distantFuture == nil)
|
|
|
|
|
return [GSDateFuture allocWithZone: 0];
|
|
|
|
|
return _distantFuture;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased instance with th date/time set in the far
|
|
|
|
|
* past.
|
|
|
|
|
*/
|
1998-01-21 14:56:24 +00:00
|
|
|
|
+ (id) distantPast
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1999-04-23 14:38:03 +00:00
|
|
|
|
if (_distantPast == nil)
|
|
|
|
|
return [GSDatePast allocWithZone: 0];
|
|
|
|
|
return _distantPast;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
|
|
|
|
if (NSShouldRetainWithZone(self, zone))
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return RETAIN(self);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
else
|
1998-07-28 17:51:55 +00:00
|
|
|
|
return NSCopyObject(self, 0, zone);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-16 12:35:42 +00:00
|
|
|
|
- (Class) classForCoder
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return abstractClass;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-01-31 05:58:59 +00:00
|
|
|
|
- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
2001-01-31 05:58:59 +00:00
|
|
|
|
if ([aCoder isByref] == NO)
|
|
|
|
|
return self;
|
|
|
|
|
return [super replacementObjectForPortCoder: aCoder];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)coder
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
NSTimeInterval interval = [self timeIntervalSinceReferenceDate];
|
|
|
|
|
|
|
|
|
|
[coder encodeValueOfObjCType: @encode(NSTimeInterval) at: &interval];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (id) initWithCoder: (NSCoder*)coder
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
NSTimeInterval interval;
|
|
|
|
|
id o;
|
|
|
|
|
|
|
|
|
|
[coder decodeValueOfObjCType: @encode(NSTimeInterval) at: &interval];
|
2000-10-16 12:35:42 +00:00
|
|
|
|
if (interval == DISTANT_PAST)
|
|
|
|
|
{
|
|
|
|
|
o = RETAIN([abstractClass distantPast]);
|
|
|
|
|
}
|
|
|
|
|
else if (interval == DISTANT_FUTURE)
|
|
|
|
|
{
|
|
|
|
|
o = RETAIN([abstractClass distantFuture]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
o = [concreteClass allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
o = [o initWithTimeIntervalSinceReferenceDate: interval];
|
|
|
|
|
}
|
|
|
|
|
RELEASE(self);
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return o;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an instance initialised with the current date/time.
|
|
|
|
|
*/
|
1995-03-31 15:39:12 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
1999-04-20 16:28:04 +00:00
|
|
|
|
return [self initWithTimeIntervalSinceReferenceDate: GSTimeNow()];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an instance with the date and time value given
|
|
|
|
|
* by the string using the ISO standard format YYYY-MM-DD HH:MM:SS +/-HHHMM
|
|
|
|
|
* (all the fields of which must be present).
|
|
|
|
|
*/
|
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
|
1999-04-19 14:29:52 +00:00
|
|
|
|
NSCalendarDate *d = [calendarClass alloc];
|
|
|
|
|
|
2000-04-05 21:43:40 +00:00
|
|
|
|
d = [d initWithString: description];
|
2001-05-18 15:25:48 +00:00
|
|
|
|
if (d == nil)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithTimeIntervalSinceReferenceDate: otherTime(d)];
|
|
|
|
|
RELEASE(d);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an instance with the given offset from anotherDate.
|
|
|
|
|
*/
|
1998-01-21 14:56:24 +00:00
|
|
|
|
- (id) initWithTimeInterval: (NSTimeInterval)secsToBeAdded
|
2001-12-17 14:31:42 +00:00
|
|
|
|
sinceDate: (NSDate*)anotherDate
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
2001-05-18 15:25:48 +00:00
|
|
|
|
if (anotherDate == nil)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"initWithTimeInterval:sinceDate: given nil date");
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
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:
|
2001-05-18 15:25:48 +00:00
|
|
|
|
otherTime(anotherDate) + secsToBeAdded];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an instance with the offset from the current date/time.
|
|
|
|
|
*/
|
2001-12-17 14:31:42 +00:00
|
|
|
|
- (id) 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:
|
2000-08-07 22:00:31 +00:00
|
|
|
|
GSTimeNow() + secsToBeAdded];
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an instance with the offset from the unix system
|
|
|
|
|
* reference date of 1 January 1970, GMT.
|
|
|
|
|
*/
|
2002-09-30 16:54:29 +00:00
|
|
|
|
- (id) initWithTimeIntervalSince1970: (NSTimeInterval)seconds
|
1997-03-03 20:07:35 +00:00
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return [self initWithTimeIntervalSinceReferenceDate:
|
2002-11-01 08:11:46 +00:00
|
|
|
|
-NSTimeIntervalSince1970 + seconds];
|
1997-03-03 20:07:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/** <init />
|
|
|
|
|
* Returns an instance with the given offset from the OpenStep
|
|
|
|
|
* reference date of 1 January 2001, GMT.
|
|
|
|
|
*/
|
1996-10-31 17:14:00 +00:00
|
|
|
|
- (id) initWithTimeIntervalSinceReferenceDate: (NSTimeInterval)secs
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1996-10-31 17:14:00 +00:00
|
|
|
|
return self;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased instance of the [NSCalendarDate] class whose
|
|
|
|
|
* date/time value is the same as that of the receiver, and which uses
|
|
|
|
|
* the formatString and timeZone specified.
|
|
|
|
|
*/
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSCalendarDate *) dateWithCalendarFormat: (NSString*)formatString
|
|
|
|
|
timeZone: (NSTimeZone*)timeZone
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
|
NSCalendarDate *d = [calendarClass alloc];
|
|
|
|
|
[d initWithTimeIntervalSinceReferenceDate: otherTime(self)];
|
1996-10-31 17:14:00 +00:00
|
|
|
|
[d setCalendarFormat: formatString];
|
|
|
|
|
[d setTimeZone: timeZone];
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return AUTORELEASE(d);
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
1995-08-08 15:46:04 +00:00
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a string representation of the receiver formatted according
|
|
|
|
|
* to the default format string, time zone, and locale.
|
|
|
|
|
*/
|
1995-03-31 15:39:12 +00:00
|
|
|
|
- (NSString*) description
|
|
|
|
|
{
|
1996-10-31 17:14:00 +00:00
|
|
|
|
// Easiest to just have NSCalendarDate do the work for us
|
|
|
|
|
NSString *s;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
NSCalendarDate *d = [calendarClass alloc];
|
|
|
|
|
[d initWithTimeIntervalSinceReferenceDate: otherTime(self)];
|
1996-10-31 17:14:00 +00:00
|
|
|
|
s = [d description];
|
1999-04-19 14:29:52 +00:00
|
|
|
|
RELEASE(d);
|
1996-10-31 17:14:00 +00:00
|
|
|
|
return s;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
1995-08-08 15:46:04 +00:00
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a string representation of the receiver formatted according
|
|
|
|
|
* to the specified format string, time zone, and locale.
|
|
|
|
|
*/
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSString*) descriptionWithCalendarFormat: (NSString*)format
|
|
|
|
|
timeZone: (NSTimeZone*)aTimeZone
|
1998-11-30 10:04:24 +00:00
|
|
|
|
locale: (NSDictionary*)l
|
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;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
NSCalendarDate *d = [calendarClass alloc];
|
1998-11-30 10:04:24 +00:00
|
|
|
|
id f;
|
1996-10-31 17:14:00 +00:00
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
|
[d initWithTimeIntervalSinceReferenceDate: otherTime(self)];
|
1996-10-31 17:14:00 +00:00
|
|
|
|
if (!format)
|
|
|
|
|
f = [d calendarFormat];
|
|
|
|
|
else
|
|
|
|
|
f = format;
|
1998-11-30 10:04:24 +00:00
|
|
|
|
if (aTimeZone)
|
|
|
|
|
[d setTimeZone: aTimeZone];
|
1996-10-31 17:14:00 +00:00
|
|
|
|
|
1998-11-30 10:04:24 +00:00
|
|
|
|
s = [d descriptionWithCalendarFormat: f locale: l];
|
1999-04-19 14:29:52 +00:00
|
|
|
|
RELEASE(d);
|
1996-10-31 17:14:00 +00:00
|
|
|
|
return s;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
1995-08-08 15:46:04 +00:00
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a string representation of the receiver formatted according
|
|
|
|
|
* to the default format string and time zone, but using the given locale.
|
|
|
|
|
*/
|
1996-10-31 17:14:00 +00:00
|
|
|
|
- (NSString *) descriptionWithLocale: (NSDictionary *)locale
|
|
|
|
|
{
|
|
|
|
|
// Easiest to just have NSCalendarDate do the work for us
|
|
|
|
|
NSString *s;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
NSCalendarDate *d = [calendarClass alloc];
|
|
|
|
|
[d initWithTimeIntervalSinceReferenceDate: otherTime(self)];
|
1996-10-31 17:14:00 +00:00
|
|
|
|
s = [d descriptionWithLocale: locale];
|
1999-04-19 14:29:52 +00:00
|
|
|
|
RELEASE(d);
|
1996-10-31 17:14:00 +00:00
|
|
|
|
return s;
|
|
|
|
|
}
|
1995-03-31 15:39:12 +00:00
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an autoreleased NSDate instance whose value if offset from
|
|
|
|
|
* that of the receiver by seconds.
|
|
|
|
|
*/
|
1998-01-21 14:56:24 +00:00
|
|
|
|
- (id) 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:
|
2001-05-18 15:25:48 +00:00
|
|
|
|
otherTime(self) + seconds];
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the time interval between the receivers value and the
|
|
|
|
|
* unix system reference date of 1 January 1970, GMT.
|
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (NSTimeInterval) timeIntervalSince1970
|
|
|
|
|
{
|
2002-11-01 08:11:46 +00:00
|
|
|
|
return otherTime(self) + NSTimeIntervalSince1970;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the time interval between the receivers value and that of the
|
|
|
|
|
* otherDate argument. If otherDate is earlier than the receiver, the
|
|
|
|
|
* returned value will be positive, if it is later it will be negative.
|
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (NSTimeInterval) timeIntervalSinceDate: (NSDate*)otherDate
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (otherDate == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"nil argument for timeIntervalSinceDate:"];
|
|
|
|
|
}
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return otherTime(self) - otherTime(otherDate);
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the time interval between the receivers value and the
|
|
|
|
|
* current date/time. If the receiver represents a date/time in
|
|
|
|
|
* the past this will be negative, if it is in the future the
|
|
|
|
|
* returned value will be positive.
|
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (NSTimeInterval) timeIntervalSinceNow
|
|
|
|
|
{
|
1999-04-20 16:28:04 +00:00
|
|
|
|
return otherTime(self) - GSTimeNow();
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the time interval between the receivers value and the
|
|
|
|
|
* OpenStep reference date of 1 Jan 2001 GMT.
|
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (NSTimeInterval) timeIntervalSinceReferenceDate
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns NSOrderedDescending if the receiver is later than otherDate,
|
|
|
|
|
* Returns NSOrderedAscending if the receiver is earlier than otherDate,
|
|
|
|
|
* Otherwise, returns NSOrderedSame.
|
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (NSComparisonResult) compare: (NSDate*)otherDate
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (otherDate == self)
|
|
|
|
|
{
|
|
|
|
|
return NSOrderedSame;
|
|
|
|
|
}
|
|
|
|
|
if (otherDate == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"nil argument for compare:"];
|
|
|
|
|
}
|
1999-04-19 14:29:52 +00:00
|
|
|
|
if (otherTime(self) > otherTime(otherDate))
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
return NSOrderedDescending;
|
|
|
|
|
}
|
1999-04-19 14:29:52 +00:00
|
|
|
|
if (otherTime(self) < otherTime(otherDate))
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
return NSOrderedAscending;
|
|
|
|
|
}
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return NSOrderedSame;
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the earlier of the receiver and otherDate.<br />
|
|
|
|
|
* If the two represent identical date/time values, returns the receiver.
|
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (NSDate*) earlierDate: (NSDate*)otherDate
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (otherDate == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"nil argument for earlierDate:"];
|
|
|
|
|
}
|
1999-04-19 14:29:52 +00:00
|
|
|
|
if (otherTime(self) > otherTime(otherDate))
|
|
|
|
|
return otherDate;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-24 15:30:11 +00:00
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
|
|
|
|
return (unsigned)[self timeIntervalSinceReferenceDate];
|
|
|
|
|
}
|
|
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (BOOL) isEqual: (id)other
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (other == nil)
|
|
|
|
|
return NO;
|
2005-06-17 14:51:57 +00:00
|
|
|
|
if ([other isKindOfClass: abstractClass]
|
2005-07-22 19:36:33 +00:00
|
|
|
|
&& otherTime(self) == otherTime(other))
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return YES;
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
|
/**
|
2005-07-22 19:36:33 +00:00
|
|
|
|
* Returns whether the receiver is exactly equal to other, to the limit
|
|
|
|
|
* of the NSTimeInterval precision.<br />
|
|
|
|
|
* This is the behavior of the current MacOS-X system, not that of the
|
2005-11-06 13:53:40 +00:00
|
|
|
|
* OpenStep specification (which counted two dates within a second of
|
2005-07-22 19:36:33 +00:00
|
|
|
|
* each other as being equal).<br />
|
|
|
|
|
* The old behavior meant that two dates equal to a third date were not
|
|
|
|
|
* necessarily equal to each other (confusing), and meant that there was
|
|
|
|
|
* no reasonable way to use a date as a dictionary key or store dates
|
|
|
|
|
* in a set.
|
2004-06-22 22:40:40 +00:00
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (BOOL) isEqualToDate: (NSDate*)other
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (other == nil)
|
|
|
|
|
return NO;
|
2005-07-22 19:36:33 +00:00
|
|
|
|
if (otherTime(self) == otherTime(other))
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return YES;
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-16 12:45:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the earlier of the receiver and otherDate.<br />
|
|
|
|
|
* If the two represent identical date/time values, returns the receiver.
|
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
- (NSDate*) laterDate: (NSDate*)otherDate
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (otherDate == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"nil argument for laterDate:"];
|
|
|
|
|
}
|
2001-05-18 15:25:48 +00:00
|
|
|
|
if (otherTime(self) < otherTime(otherDate))
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return otherDate;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation NSGDate
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [NSDate class])
|
|
|
|
|
{
|
|
|
|
|
[self setVersion: 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)coder
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
[coder encodeValueOfObjCType: @encode(NSTimeInterval)
|
|
|
|
|
at: &_seconds_since_ref];
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)coder
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
[coder decodeValueOfObjCType: @encode(NSTimeInterval)
|
|
|
|
|
at: &_seconds_since_ref];
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithTimeIntervalSinceReferenceDate: (NSTimeInterval)secs
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_seconds_since_ref = secs;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Adding and getting intervals
|
|
|
|
|
|
1997-03-03 20:07:35 +00:00
|
|
|
|
- (NSTimeInterval) timeIntervalSince1970
|
|
|
|
|
{
|
2002-11-01 08:11:46 +00:00
|
|
|
|
return _seconds_since_ref + NSTimeIntervalSince1970;
|
1997-03-03 20:07:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSTimeInterval) timeIntervalSinceDate: (NSDate*)otherDate
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (otherDate == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"nil argument for timeIntervalSinceDate:"];
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return _seconds_since_ref - otherTime(otherDate);
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSTimeInterval) timeIntervalSinceNow
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return _seconds_since_ref - GSTimeNow();
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSTimeInterval) timeIntervalSinceReferenceDate
|
|
|
|
|
{
|
1999-09-16 07:21:34 +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
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (otherDate == self)
|
|
|
|
|
{
|
|
|
|
|
return NSOrderedSame;
|
|
|
|
|
}
|
|
|
|
|
if (otherDate == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"nil argument for compare:"];
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_seconds_since_ref > otherTime(otherDate))
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
return NSOrderedDescending;
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_seconds_since_ref < otherTime(otherDate))
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
|
|
|
|
return NSOrderedAscending;
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (otherDate == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"nil argument for earlierDate:"];
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_seconds_since_ref > otherTime(otherDate))
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return otherDate;
|
|
|
|
|
return self;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-05-24 15:30:11 +00:00
|
|
|
|
- (unsigned) hash
|
|
|
|
|
{
|
|
|
|
|
return (unsigned)_seconds_since_ref;
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (BOOL) isEqual: (id)other
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (other == nil)
|
|
|
|
|
return NO;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
if ([other isKindOfClass: abstractClass]
|
2005-07-22 19:36:33 +00:00
|
|
|
|
&& _seconds_since_ref == otherTime(other))
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return YES;
|
|
|
|
|
return NO;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
1995-03-31 15:39:12 +00:00
|
|
|
|
|
1998-11-30 10:04:24 +00:00
|
|
|
|
- (BOOL) isEqualToDate: (NSDate*)other
|
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (other == nil)
|
|
|
|
|
return NO;
|
2005-07-22 19:36:33 +00:00
|
|
|
|
if (_seconds_since_ref == otherTime(other))
|
1998-11-30 10:04:24 +00:00
|
|
|
|
return YES;
|
|
|
|
|
return NO;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
1998-11-30 10:04:24 +00:00
|
|
|
|
|
1995-04-13 21:57:29 +00:00
|
|
|
|
- (NSDate*) laterDate: (NSDate*)otherDate
|
1995-03-31 15:39:12 +00:00
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (otherDate == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"nil argument for laterDate:"];
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_seconds_since_ref < otherTime(otherDate))
|
1995-04-13 21:57:29 +00:00
|
|
|
|
return otherDate;
|
|
|
|
|
return self;
|
1995-03-31 15:39:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
1999-04-19 14:29:52 +00:00
|
|
|
|
|
1999-04-23 14:38:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This abstract class represents a date of which there can be only
|
|
|
|
|
* one instance.
|
|
|
|
|
*/
|
|
|
|
|
@implementation GSDateSingle
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [GSDateSingle class])
|
|
|
|
|
{
|
|
|
|
|
[self setVersion: 1];
|
2002-11-27 12:52:29 +00:00
|
|
|
|
GSObjCAddClassBehavior(self, [NSGDate class]);
|
1999-04-23 14:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-04-24 09:28:32 +00:00
|
|
|
|
- (id) autorelease
|
1999-04-23 14:38:03 +00:00
|
|
|
|
{
|
1999-04-24 09:28:32 +00:00
|
|
|
|
return self;
|
1999-04-23 14:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) release
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) retain
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1999-06-24 19:30:29 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1999-04-23 14:38:03 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Attempt to allocate fixed date"];
|
1999-06-24 19:30:29 +00:00
|
|
|
|
return nil;
|
1999-04-23 14:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Attempt to deallocate fixed date"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithTimeIntervalSinceReferenceDate: (NSTimeInterval)secs
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSDatePast
|
|
|
|
|
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
if (_distantPast == nil)
|
|
|
|
|
{
|
|
|
|
|
id obj = NSAllocateObject(self, 0, NSDefaultMallocZone());
|
|
|
|
|
|
|
|
|
|
_distantPast = [obj init];
|
|
|
|
|
}
|
|
|
|
|
return _distantPast;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithTimeIntervalSinceReferenceDate: (NSTimeInterval)secs
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_seconds_since_ref = DISTANT_PAST;
|
1999-04-23 14:38:03 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSDateFuture
|
|
|
|
|
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
if (_distantFuture == nil)
|
|
|
|
|
{
|
|
|
|
|
id obj = NSAllocateObject(self, 0, NSDefaultMallocZone());
|
|
|
|
|
|
|
|
|
|
_distantFuture = [obj init];
|
|
|
|
|
}
|
|
|
|
|
return _distantFuture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithTimeIntervalSinceReferenceDate: (NSTimeInterval)secs
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_seconds_since_ref = DISTANT_FUTURE;
|
1999-04-23 14:38:03 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|