libs-base/Source/NSDateInterval.m

221 lines
5.6 KiB
Mathematica
Raw Permalink Normal View History

/** Implementation of class NSDateInterval
2019-10-12 16:42:18 +00:00
Copyright (C) 2019 Free Software Foundation, Inc.
2019-10-12 16:44:15 +00:00
By: Gregory Casamento <greg.casamento@gmail.com>
2019-10-12 16:42:18 +00:00
Date: Wed Oct 9 16:24:13 EDT 2019
This file is part of the GNUstep Library.
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 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.
2019-10-12 16:42:18 +00:00
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., 31 Milk Street #960789 Boston, MA 02196 USA.
2019-10-12 16:42:18 +00:00
*/
#import "Foundation/NSDateInterval.h"
#import "Foundation/NSDate.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSException.h"
2019-10-12 16:42:18 +00:00
@implementation NSDateInterval
// Init
- (instancetype)init
{
2019-10-13 03:03:28 +00:00
self = [super init];
if(self != nil)
{
_startDate = [NSDate date];
2019-10-13 03:03:28 +00:00
_duration = 0.0;
RETAIN(_startDate);
2019-10-13 03:03:28 +00:00
}
return self;
2019-10-12 16:42:18 +00:00
}
- (instancetype)initWithStartDate:(NSDate *)startDate
duration:(NSTimeInterval)duration
{
2019-10-13 03:03:28 +00:00
self = [super init];
if(self != nil)
{
ASSIGNCOPY(_startDate, startDate);
2019-10-24 18:34:26 +00:00
if(duration < 0)
{
[NSException raise: NSInvalidArgumentException
format: @"Duration %f is less than zero", duration];
}
2019-10-13 03:03:28 +00:00
_duration = duration;
2019-10-24 18:34:26 +00:00
2019-10-13 03:03:28 +00:00
}
return self;
2019-10-12 16:42:18 +00:00
}
- (instancetype)initWithStartDate:(NSDate *)startDate
endDate:(NSDate *)endDate
{
2019-10-24 22:22:10 +00:00
return [self initWithStartDate: startDate
duration: [endDate timeIntervalSinceDate: startDate]];
2019-10-12 16:42:18 +00:00
}
- (instancetype) initWithCoder: (NSCoder *)coder
{
2019-10-24 22:22:10 +00:00
// TODO: Implement encoding
2019-10-13 03:03:28 +00:00
return nil;
2019-10-12 16:42:18 +00:00
}
- (void) encodeWithCoder: (NSCoder *)coder
{
}
- (id) copyWithZone: (NSZone *)zone
{
2019-10-13 03:03:28 +00:00
return [[[self class] allocWithZone: zone]
initWithStartDate: _startDate
duration: _duration];
}
- (void) dealloc
{
RELEASE(_startDate);
[super dealloc];
2019-10-12 16:42:18 +00:00
}
// Access
- (NSDate *) startDate
{
2019-10-13 03:03:28 +00:00
return _startDate;
2019-10-12 16:42:18 +00:00
}
- (void) setStartDate: (NSDate *)startDate
{
2019-10-13 03:03:28 +00:00
ASSIGNCOPY(_startDate, startDate);
2019-10-12 16:42:18 +00:00
}
- (NSDate *) endDate
{
2019-10-21 20:55:51 +00:00
return [_startDate dateByAddingTimeInterval: _duration];
2019-10-12 16:42:18 +00:00
}
- (void) setEndDate: (NSDate *)endDate
{
2019-10-13 03:03:28 +00:00
_duration = [endDate timeIntervalSinceDate: _startDate];
2019-10-12 16:42:18 +00:00
}
- (NSTimeInterval) duration
{
2019-10-13 03:03:28 +00:00
return _duration;
2019-10-12 16:42:18 +00:00
}
- (void) setDuration: (NSTimeInterval)duration
{
2019-10-13 03:03:28 +00:00
_duration = duration;
2019-10-12 16:42:18 +00:00
}
// Compare
- (NSComparisonResult) compare: (NSDateInterval *)dateInterval
{
2019-10-24 18:58:43 +00:00
NSComparisonResult result = NSOrderedSame;
2019-10-13 03:03:28 +00:00
if([_startDate isEqualToDate: [dateInterval startDate]] &&
2019-10-24 18:58:43 +00:00
_duration < [dateInterval duration])
{
result = NSOrderedAscending;
}
else if([_startDate compare: [dateInterval startDate]] == NSOrderedAscending)
{
result = NSOrderedAscending;
}
else if([self isEqualToDateInterval: dateInterval])
{
result = NSOrderedSame;
}
else if([_startDate isEqualToDate: [dateInterval startDate]] &&
_duration > [dateInterval duration])
{
result = NSOrderedDescending;
}
else if([_startDate compare: [dateInterval startDate]] == NSOrderedDescending)
{
result = NSOrderedDescending;
}
2019-10-24 18:58:43 +00:00
return result;
2019-10-12 16:42:18 +00:00
}
- (BOOL) isEqualToDateInterval: (NSDateInterval *)dateInterval
{
2019-10-13 03:03:28 +00:00
return ([_startDate isEqualToDate: [dateInterval startDate]] &&
_duration == [dateInterval duration]);
2019-10-12 16:42:18 +00:00
}
// Determine
- (BOOL) intersectsDateInterval: (NSDateInterval *)dateInterval
{
2019-10-13 03:03:28 +00:00
return [self intersectionWithDateInterval: dateInterval] != nil;
2019-10-12 16:42:18 +00:00
}
- (NSDateInterval *) intersectionWithDateInterval: (NSDateInterval *)dateInterval
{
NSDateInterval *result = nil;
2019-10-24 23:38:19 +00:00
NSDateInterval *first = self; //[sortedArray firstObject];
NSDateInterval *last = dateInterval; // [sortedArray lastObject];
NSDate *intersectStartDate = nil;
NSDate *intersectEndDate = nil;
2019-10-24 23:38:19 +00:00
// NSArray *array = [NSArray arrayWithObjects: self, dateInterval, nil];
// NSArray *sortedArray = [array sortedArrayUsingSelector: @selector(compare:)];
// Max of start date....
if([[first startDate] compare: [last startDate]] == NSOrderedAscending ||
[[first startDate] isEqualToDate: [last startDate]])
{
intersectStartDate = [last startDate];
}
if([[first startDate] compare: [last startDate]] == NSOrderedDescending)
{
intersectStartDate = [first startDate];
}
// Min of end date...
if([[first endDate] compare: [last endDate]] == NSOrderedDescending ||
[[first endDate] isEqualToDate: [last endDate]])
{
intersectEndDate = [last endDate];
}
if([[first endDate] compare: [last endDate]] == NSOrderedAscending)
{
intersectEndDate = [first endDate];
}
if([intersectStartDate compare: intersectEndDate] == NSOrderedAscending)
{
result = [[NSDateInterval alloc] initWithStartDate: intersectStartDate
endDate: intersectEndDate];
AUTORELEASE(result);
}
return result;
2019-10-12 16:42:18 +00:00
}
// Contain
- (BOOL) containsDate: (NSDate *)date
{
2019-10-21 20:55:51 +00:00
NSDate *endDate = [self endDate];
2019-10-13 03:03:28 +00:00
return ([_startDate compare: date] == NSOrderedSame ||
2019-10-21 20:55:51 +00:00
[endDate compare: date] == NSOrderedSame ||
2019-10-13 03:03:28 +00:00
([_startDate compare: date] == NSOrderedAscending &&
2019-10-21 20:55:51 +00:00
[endDate compare: date] == NSOrderedDescending));
2019-10-13 03:03:28 +00:00
2019-10-12 16:42:18 +00:00
}
@end