Merge branch 'UnitsOfMeasure' of github.com:gnustep/libs-base into UnitsOfMeasure

This commit is contained in:
Gregory John Casamento 2019-10-19 06:43:04 -04:00
commit 9f36d5107e
17 changed files with 2429 additions and 439 deletions

View file

@ -213,6 +213,7 @@ NSConnection.m \
NSData.m \
NSDate.m \
NSDateFormatter.m \
NSDateInterval.m \
NSDebug.m \
NSDecimal.m \
NSDecimalNumber.m \
@ -220,6 +221,7 @@ NSDictionary.m \
NSDistantObject.m \
NSDistributedLock.m \
NSDistributedNotificationCenter.m \
NSEnergyFormatter.m \
NSEnumerator.m \
NSError.m \
NSException.m \
@ -244,6 +246,7 @@ NSKeyedArchiver.m \
NSKeyedUnarchiver.m \
NSKeyValueCoding.m \
NSKeyValueObserving.m \
NSLengthFormatter.m \
NSLocale.m \
NSLock.m \
NSLog.m \
@ -405,6 +408,7 @@ NSCompoundPredicate.h \
NSConnection.h \
NSData.h \
NSDateFormatter.h \
NSDateInterval.h \
NSDate.h \
NSDebug.h \
NSDecimal.h \
@ -413,6 +417,7 @@ NSDictionary.h \
NSDistantObject.h \
NSDistributedLock.h \
NSDistributedNotificationCenter.h \
NSEnergyFormatter.h \
NSEnumerator.h \
NSError.h \
NSErrorRecoveryAttempting.h \
@ -439,6 +444,7 @@ NSJSONSerialization.h \
NSKeyedArchiver.h \
NSKeyValueCoding.h \
NSKeyValueObserving.h \
NSLengthFormtter.h \
NSLocale.h \
NSLock.h \
NSMapTable.h \

217
Source/NSDateInterval.m Normal file
View file

@ -0,0 +1,217 @@
/* Implementation of class NSDateInterval
Copyright (C) 2019 Free Software Foundation, Inc.
By: Gregory Casamento <greg.casamento@gmail.com>
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
Library General Public License for more details.
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#include <Foundation/NSDateInterval.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSArray.h>
@implementation NSDateInterval
// Init
- (instancetype)init
{
self = [super init];
if(self != nil)
{
_startDate = nil;
_duration = 0.0;
}
return self;
}
- (instancetype)initWithStartDate:(NSDate *)startDate
duration:(NSTimeInterval)duration
{
self = [super init];
if(self != nil)
{
ASSIGNCOPY(_startDate, startDate);
ASSIGNCOPY(_endDate, [startDate dateByAddingTimeInterval: duration]);
_duration = duration;
}
return self;
}
- (instancetype)initWithStartDate:(NSDate *)startDate
endDate:(NSDate *)endDate
{
self = [super init];
if(self != nil)
{
ASSIGNCOPY(_startDate, startDate);
ASSIGNCOPY(_endDate, endDate);
_duration = [endDate timeIntervalSinceDate: startDate];
}
return self;
}
- (instancetype) initWithCoder: (NSCoder *)coder
{
return nil;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
}
- (id) copyWithZone: (NSZone *)zone
{
return [[[self class] allocWithZone: zone]
initWithStartDate: _startDate
duration: _duration];
}
- (void) dealloc
{
RELEASE(_startDate);
[super dealloc];
}
// Access
- (NSDate *) startDate
{
return _startDate;
}
- (void) setStartDate: (NSDate *)startDate
{
ASSIGNCOPY(_startDate, startDate);
}
- (NSDate *) endDate
{
return _endDate;
}
- (void) setEndDate: (NSDate *)endDate
{
ASSIGNCOPY(_endDate, endDate);
_duration = [endDate timeIntervalSinceDate: _startDate];
}
- (NSTimeInterval) duration
{
return _duration;
}
- (void) setDuration: (NSTimeInterval)duration
{
NSDate *newEndDate = [_startDate dateByAddingTimeInterval: duration];
_duration = duration;
[self setEndDate: newEndDate];
}
// Compare
- (NSComparisonResult) compare: (NSDateInterval *)dateInterval
{
// NSOrderedAscending
if([_startDate isEqualToDate: [dateInterval startDate]] &&
_duration < [dateInterval duration])
return NSOrderedAscending;
if([_startDate compare: [dateInterval startDate]] == NSOrderedAscending)
return NSOrderedAscending;
// NSOrderedSame
if([self isEqualToDateInterval: dateInterval])
return NSOrderedSame;
// NSOrderedDescending
if([_startDate isEqualToDate: [dateInterval startDate]] &&
_duration > [dateInterval duration])
return NSOrderedDescending;
if([_startDate compare: [dateInterval startDate]] == NSOrderedDescending)
return NSOrderedDescending;
return 0;
}
- (BOOL) isEqualToDateInterval: (NSDateInterval *)dateInterval
{
return ([_startDate isEqualToDate: [dateInterval startDate]] &&
_duration == [dateInterval duration]);
}
// Determine
- (BOOL) intersectsDateInterval: (NSDateInterval *)dateInterval
{
return [self intersectionWithDateInterval: dateInterval] != nil;
}
- (NSDateInterval *) intersectionWithDateInterval: (NSDateInterval *)dateInterval
{
NSDateInterval *result = nil;
NSArray *array = [NSArray arrayWithObjects: self, dateInterval, nil];
NSArray *sortedArray = [array sortedArrayUsingSelector: @selector(compare:)];
NSDateInterval *first = [sortedArray firstObject];
NSDateInterval *last = [sortedArray lastObject];
NSDate *intersectStartDate = nil;
NSDate *intersectEndDate = nil;
// 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;
}
// Contain
- (BOOL) containsDate: (NSDate *)date
{
return ([_startDate compare: date] == NSOrderedSame ||
[_endDate compare: date] == NSOrderedSame ||
([_startDate compare: date] == NSOrderedAscending &&
[_endDate compare: date] == NSOrderedDescending));
}
@end

View file

@ -0,0 +1,31 @@
/* Implementation of class NSDateIntervalFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: heron
Date: Wed Oct 9 16:23:55 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
Library General Public License for more details.
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#include <Foundation/NSDateIntervalFormatter.h>
@implementation NSDateIntervalFormatter
@end

View file

@ -0,0 +1,31 @@
/* Implementation of class NSEnergyFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: heron
Date: Tue Oct 8 13:30:10 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
Library General Public License for more details.
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#include <Foundation/NSEnergyFormatter.h>
@implementation NSEnergyFormatter
@end

View file

@ -0,0 +1,31 @@
/* Implementation of class NSLengthFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: heron
Date: Tue Oct 8 13:30:33 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
Library General Public License for more details.
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#include <Foundation/NSLengthFormatter.h>
@implementation NSLengthFormatter
@end

View file

@ -2,7 +2,7 @@
/* Implementation of class NSMeasurement
Copyright (C) 2019 Free Software Foundation, Inc.
By: heron
By: Gregory John Casamento <greg.casamento@gmail.com>
Date: Mon Sep 30 15:58:21 EDT 2019
This file is part of the GNUstep Library.
@ -24,8 +24,134 @@
*/
#include <Foundation/NSMeasurement.h>
#include <Foundation/NSUnit.h>
#include <Foundation/NSException.h>
#include <Foundation/NSArchiver.h>
#include <Foundation/NSKeyedArchiver.h>
@implementation NSMeasurement
// Creating Measurements
- (instancetype)initWithDoubleValue: (double)doubleValue
unit: (NSUnit *)unit
{
self = [super init];
if(self != nil)
{
ASSIGNCOPY(_unit, unit);
_doubleValue = doubleValue;
}
return self;
}
- (void) dealloc
{
RELEASE(_unit);
[super dealloc];
}
// Accessing unit and value
- (NSUnit *) unit
{
return _unit;
}
- (double) doubleValue
{
return _doubleValue;
}
// Conversion
- (BOOL) canBeConvertedToUnit: (NSUnit *)unit
{
return ([unit isKindOfClass: [_unit class]] &&
[unit respondsToSelector: @selector(converter)]);
}
- (NSMeasurement *)measurementByConvertingToUnit:(NSUnit *)unit
{
NSMeasurement *result = nil;
double val = 0.0;
if([self canBeConvertedToUnit: unit])
{
// Do conversion...
NSUnitConverter *c = [(NSDimension *)unit converter];
val = [c baseUnitValueFromValue: _doubleValue];
result = [[NSMeasurement alloc] initWithDoubleValue: val unit: unit];
AUTORELEASE(result);
}
else
{
[NSException raise: NSInvalidArgumentException
format: @"Cannot convert from %@ to %@", _unit, unit];
}
return result;
}
// Operating
- (NSMeasurement *)measurementByAddingMeasurement:(NSMeasurement *)measurement
{
NSMeasurement *newMeasurement = [measurement measurementByConvertingToUnit: _unit];
NSMeasurement *result = nil;
double v = 0.0;
v = _doubleValue + [newMeasurement doubleValue];
result = [[NSMeasurement alloc] initWithDoubleValue: v unit: _unit];
AUTORELEASE(result);
return result;
}
- (NSMeasurement *)measurementBySubtractingMeasurement:(NSMeasurement *)measurement
{
NSMeasurement *newMeasurement = [measurement measurementByConvertingToUnit: _unit];
NSMeasurement *result = nil;
double v = 0.0;
v = _doubleValue - [newMeasurement doubleValue];
result = [[NSMeasurement alloc] initWithDoubleValue: v unit: _unit];
AUTORELEASE(result);
return result;
}
// NSCopying
- (id) copyWithZone: (NSZone *)zone
{
return [[[self class] allocWithZone: zone] initWithDoubleValue: _doubleValue
unit: _unit];
}
// NSCoding
- (void) encodeWithCoder: (NSCoder *)coder
{
if([coder allowsKeyedCoding])
{
[coder encodeObject: _unit forKey: @"unit"];
[coder encodeDouble: _doubleValue forKey: @"doubleValue"];
}
else
{
[coder encodeObject: _unit];
[coder encodeValueOfObjCType: @encode(double) at: &_doubleValue];
}
}
- (id) initWithCoder: (NSCoder *)coder
{
if((self = [super init]) != nil)
{
if([coder allowsKeyedCoding])
{
_unit = [coder decodeObjectForKey: @"unit"];
_doubleValue = [coder decodeDoubleForKey: @"doubleValue"];
}
else
{
_unit = [coder decodeObject];
[coder decodeValueOfObjCType: @encode(double) at: &_doubleValue];
}
}
return self;
}
@end

View file

@ -24,8 +24,63 @@
*/
#include <Foundation/NSMeasurementFormatter.h>
#include <Foundation/NSLocale.h>
#include <Foundation/NSMeasurement.h>
#include <Foundation/NSNumberFormatter.h>
#include <Foundation/NSUnit.h>
@implementation NSMeasurementFormatter
- (NSMeasurementFormatterUnitOptions) unitOptions
{
return _unitOptions;
}
- (void) setUnitOptions: (NSMeasurementFormatterUnitOptions) unitOptions
{
_unitOptions = unitOptions;
}
- (NSFormattingUnitStyle) unitStyle
{
return _unitStyle;
}
- (void) setUnitStyle: (NSFormattingUnitStyle)style
{
_unitStyle = style;
}
- (NSLocale *) locale
{
return _locale;
}
- (void) setLocale: (NSLocale *)locale
{
ASSIGNCOPY(_locale, locale);
}
- (NSNumberFormatter *) numberFormatter
{
return _numberFormatter;
}
- (void) setNumberFormatter: (NSNumberFormatter *)numberFormatter
{
ASSIGNCOPY(_numberFormatter, numberFormatter);
}
- (NSString *)stringFromMeasurement: (NSMeasurement *)measurement
{
return nil;
}
- (NSString *)stringFromUnit: (NSUnit *)unit
{
return nil;
}
@end

File diff suppressed because it is too large Load diff