Merge pull request #76 from gnustep/UnitsOfMeasure

Implement Units of measure changes into GNUstep
This commit is contained in:
Gregory Casamento 2019-10-24 20:20:33 -04:00 committed by GitHub
commit 040c553063
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 3823 additions and 37 deletions

View file

@ -55,12 +55,15 @@
#import <Foundation/NSConnection.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDateFormatter.h>
#import <Foundation/NSDateInterval.h>
#import <Foundation/NSDateIntervalFormatter.h>
#import <Foundation/NSDate.h>
#import <Foundation/NSDecimalNumber.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSDistantObject.h>
#import <Foundation/NSDistributedLock.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import <Foundation/NSEnergyFormatter.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSError.h>
#import <Foundation/NSException.h>
@ -86,9 +89,12 @@
#import <Foundation/NSKeyedArchiver.h>
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSKeyValueObserving.h>
#import <Foundation/NSLengthFormatter.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSLocale.h>
#import <Foundation/NSMapTable.h>
#import <Foundation/NSMeasurement.h>
#import <Foundation/NSMeasurementFormatter.h>
#import <Foundation/NSMetadata.h>
#import <Foundation/NSMethodSignature.h>
#import <Foundation/NSNotification.h>
@ -138,6 +144,7 @@
#import <Foundation/NSTimeZone.h>
#import <Foundation/NSUbiquitousKeyValueStore.h>
#import <Foundation/NSUndoManager.h>
#import <Foundation/NSUnit.h>
#import <Foundation/NSURLAuthenticationChallenge.h>
#import <Foundation/NSURLCache.h>
#import <Foundation/NSURLConnection.h>

View file

@ -20,9 +20,6 @@ NSAppleEventDescriptor.h
NSAppleEventManager.h
NSBackgroundActivityScheduler.h
NSDateComponentsFormatter.h
NSDateIntervalFormatter.h
NSDateInterval.h
NSEnergyFormatter.h
NSExtensionContext.h
NSExtensionItem.h
NSExtensionRequestHandling.h
@ -30,44 +27,10 @@ NSHFSFileTypes.h
NSISO8601DateFormatter.h
NSItemProvider.h
NSItemProviderReadingWriting.h
NSLengthFormatter.h
NSLinguisticTagger.h
NSMassFormatter.h
NSMeasurementFormatter.h
NSMeasurement.h
NSMetadataAttributes.h
NSObjectScripting.h
NSOrthography.h
NSUnit.h
NSUserActivity.h
NSXPCConnection.h
-------------------------------------------------------------
Foundation:
==
FoundationLegacySwiftCompatibility.h
NSAppleEventDescriptor.h
NSAppleEventManager.h
NSBackgroundActivityScheduler.h
NSDateComponentsFormatter.h
NSDateIntervalFormatter.h
NSDateInterval.h
NSEnergyFormatter.h
NSExtensionContext.h
NSExtensionItem.h
NSExtensionRequestHandling.h
NSHFSFileTypes.h
NSISO8601DateFormatter.h
NSItemProvider.h
NSItemProviderReadingWriting.h
NSLengthFormatter.h
NSLinguisticTagger.h
NSMassFormatter.h
NSMeasurementFormatter.h
NSMeasurement.h
NSMetadataAttributes.h
NSObjectScripting.h
NSOrthography.h
NSUnit.h
NSUserActivity.h
NSXPCConnection.h

View file

@ -0,0 +1,84 @@
/* Definition 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.
*/
#ifndef _NSDateInterval_h_GNUSTEP_BASE_INCLUDE
#define _NSDateInterval_h_GNUSTEP_BASE_INCLUDE
#include <Foundation/NSObject.h>
#include <Foundation/NSDate.h>
#if defined(__cplusplus)
extern "C" {
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_12, GS_API_LATEST)
@interface NSDateInterval : NSObject <NSCoding, NSCopying>
{
NSTimeInterval _duration;
NSDate *_startDate;
}
// Init
- (instancetype) init;
- (instancetype) initWithStartDate: (NSDate *)startDate
duration: (NSTimeInterval)duration;
- (instancetype) initWithStartDate: (NSDate *)startDate
endDate: (NSDate *)endDate;
// Access
- (NSDate *) startDate;
- (void) setStartDate: (NSDate *)startDate;
- (NSDate *) endDate;
- (void) setEndDate: (NSDate *)endDate;
- (NSTimeInterval)duration;
- (void) setDuration: (NSTimeInterval)duration;
// Compare
- (NSComparisonResult) compare: (NSDateInterval *)dateInterval;
- (BOOL) isEqualToDateInterval: (NSDateInterval *)dateInterval;
// Determine
- (BOOL) intersectsDateInterval: (NSDateInterval *)dateInterval;
- (NSDateInterval *) intersectionWithDateInterval: (NSDateInterval *)dateInterval;
// Contain
- (BOOL) containsDate: (NSDate *)date;
@end
#if defined(__cplusplus)
}
#endif
#endif /* GS_API_MACOSX */
#endif /* _NSDateInterval_h_GNUSTEP_BASE_INCLUDE */

View file

@ -0,0 +1,91 @@
/* Definition 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.
*/
#ifndef _NSDateIntervalFormatter_h_GNUSTEP_BASE_INCLUDE
#define _NSDateIntervalFormatter_h_GNUSTEP_BASE_INCLUDE
#include <Foundation/NSFormatter.h>
#if defined(__cplusplus)
extern "C" {
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
enum {
NSDateIntervalFormatterNoStyle = 0,
NSDateIntervalFormatterShortStyle = 1,
NSDateIntervalFormatterMediumStyle = 2,
NSDateIntervalFormatterLongStyle = 3,
NSDateIntervalFormatterFullStyle = 4
};
typedef NSUInteger NSDateIntervalFormatterStyle;
@class NSCalendar, NSLocale, NSDateInterval;
@interface NSDateIntervalFormatter : NSFormatter
{
NSLocale *_locale;
NSCalendar *_calendar;
NSTimeZone *_timeZone;
NSString *_dateTemplate;
NSDateIntervalFormatterStyle _dateStyle;
NSDateIntervalFormatterStyle _timeStyle;
}
// Properties
- (NSLocale *) locale;
- (void) setLocale: (NSLocale *)locale;
- (NSCalendar *) calendar;
- (void) setCalendar: (NSCalendar *)calendar;
- (NSTimeZone *) timeZone;
- (void) setTimeZone: (NSTimeZone *)timeZone;
- (NSString *) dateTemplate;
- (void) setDateTemplate: (NSString *)dateTemplate;
- (NSDateIntervalFormatterStyle) dateStyle;
- (void) setDateStyle: (NSDateIntervalFormatterStyle)dateStyle;
- (NSDateIntervalFormatterStyle) timeStyle;
- (void) setTimeStyle: (NSDateIntervalFormatterStyle)timeStyle;
// Create strings
- (NSString *)stringFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;
- (NSString *)stringFromDateInterval:(NSDateInterval *)dateInterval;
@end
#if defined(__cplusplus)
}
#endif
#endif /* GS_API_MACOSX */
#endif /* _NSDateIntervalFormatter_h_GNUSTEP_BASE_INCLUDE */

View file

@ -0,0 +1,81 @@
/* Definition 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.
*/
#ifndef _NSEnergyFormatter_h_GNUSTEP_BASE_INCLUDE
#define _NSEnergyFormatter_h_GNUSTEP_BASE_INCLUDE
#include <Foundation/NSFormatter.h>
#if defined(__cplusplus)
extern "C" {
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
@class NSNumberFormatter, NSString;
enum {
NSEnergyFormatterUnitJoule = 11,
NSEnergyFormatterUnitKilojoule = 14,
NSEnergyFormatterUnitCalorie = (7 << 8) + 1,
NSEnergyFormatterUnitKilocalorie = (7 << 8) + 2,
};
typedef NSInteger NSEnergyFormatterUnit;
@interface NSEnergyFormatter : NSFormatter
{
BOOL _isForFoodEnergyUse;
NSNumberFormatter *_numberFormatter;
NSFormattingUnitStyle _unitStyle;
}
- (NSNumberFormatter *) numberFormatter;
- (void) setNumberFormatter: (NSNumberFormatter *)formatter;
- (NSFormattingUnitStyle) unitStyle;
- (void) setUnitStyle: (NSFormattingUnitStyle)style;
- (BOOL) isForFoodEnergyUse;
- (void) setForFoodEnergyUse: (BOOL)flag;
- (NSString *) stringFromValue: (double)value unit: (NSEnergyFormatterUnit)unit;
- (NSString *) stringFromJoules: (double)numberInJoules;
- (NSString *) unitStringFromValue: (double)value unit: (NSEnergyFormatterUnit)unit;
- (NSString *) unitStringFromJoules: (double)numberInJoules usedUnit: (NSEnergyFormatterUnit *)unitp;
- (BOOL) getObjectValue:(id *)obj forString: (NSString *)string errorDescription: (NSString **)error;
@end
#if defined(__cplusplus)
}
#endif
#endif /* GS_API_MACOSX */
#endif /* _NSEnergyFormatter_h_GNUSTEP_BASE_INCLUDE */

View file

@ -0,0 +1,84 @@
/* Definition of class NSLengthFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: Gregory John Casamento <greg.casamento@gmail.com>
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.
*/
#ifndef _NSLengthFormatter_h_GNUSTEP_BASE_INCLUDE
#define _NSLengthFormatter_h_GNUSTEP_BASE_INCLUDE
#include <Foundation/NSFormatter.h>
#if defined(__cplusplus)
extern "C" {
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
enum {
NSLengthFormatterUnitMillimeter = 8,
NSLengthFormatterUnitCentimeter = 9,
NSLengthFormatterUnitMeter = 11,
NSLengthFormatterUnitKilometer = 14,
NSLengthFormatterUnitInch = (5 << 8) + 1,
NSLengthFormatterUnitFoot = (5 << 8) + 2,
NSLengthFormatterUnitYard = (5 << 8) + 3,
NSLengthFormatterUnitMile = (5 << 8) + 4,
};
typedef NSInteger NSLengthFormatterUnit;
@class NSNumberFormatter, NSString;
@interface NSLengthFormatter : NSFormatter
{
BOOL _isForPersonHeightUse;
NSNumberFormatter *_numberFormatter;
NSFormattingUnitStyle _unitStyle;
}
- (NSNumberFormatter *) numberFormatter;
- (void) setNumberFormatter: (NSNumberFormatter *)formatter;
- (NSFormattingUnitStyle) unitStyle;
- (void) setUnitStyle: (NSFormattingUnitStyle)style;
- (BOOL) isForPersonHeightUse;
- (void) setForPersonHeightUse: (BOOL)flag;
- (NSString *) stringFromValue: (double)value unit: (NSLengthFormatterUnit)unit;
- (NSString *) stringFromMeters: (double)numberInMeters;
- (NSString *) unitStringFromValue: (double)value unit: (NSLengthFormatterUnit)unit;
- (NSString *) unitStringFromMeters: (double)numberInMeters usedUnit: (NSLengthFormatterUnit *)unit;
- (BOOL)getObjectValue: (id *)obj forString: (NSString *)string errorDescription: (NSString **)error;
@end
#if defined(__cplusplus)
}
#endif
#endif /* GS_API_MACOSX */
#endif /* _NSLengthFormatter_h_GNUSTEP_BASE_INCLUDE */

View file

@ -0,0 +1,83 @@
/* Definition of class NSMassFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: heron
Date: Mon Sep 30 15:58:21 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.
*/
#ifndef _NSMassFormatter_h_GNUSTEP_BASE_INCLUDE
#define _NSMassFormatter_h_GNUSTEP_BASE_INCLUDE
#include <Foundation/NSFormatter.h>
#if defined(__cplusplus)
extern "C" {
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
enum {
NSMassFormatterUnitGram = 11,
NSMassFormatterUnitKilogram = 14,
NSMassFormatterUnitOunce = (6 << 8) + 1,
NSMassFormatterUnitPound = (6 << 8) + 2,
NSMassFormatterUnitStone = (6 << 8) + 3,
};
typedef NSInteger NSMassFormatterUnit;
@class NSNumberFormatter;
@interface NSMassFormatter : NSObject
{
NSNumberFormatter *_numberFormatter;
BOOL _isForPersonMassUse;
NSFormattingUnitStyle _unitStyle;
}
- (NSNumberFormatter *) numberFormatter;
- (void) setNumberFormatter: (NSNumberFormatter *)formatter;
- (NSFormattingUnitStyle) unitStyle;
- (void) setUnitStyle: (NSFormattingUnitStyle)style;
- (BOOL) isForPersonMassUse;
- (void) setForPersonMassUse: (BOOL)flag;
- (NSString *)stringFromValue: (double)value unit: (NSMassFormatterUnit)unit;
- (NSString *)stringFromKilograms: (double)numberInKilograms;
- (NSString *)unitStringFromValue: (double)value unit: (NSMassFormatterUnit)unit;
- (NSString *)unitStringFromKilograms: (double)numberInKilograms usedUnit: (NSMassFormatterUnit *)unitp;
- (BOOL)getObjectValue: (id*)obj forString: (NSString *)string errorDescription: (NSString **)error;
@end
#if defined(__cplusplus)
}
#endif
#endif /* GS_API_MACOSX */
#endif /* _NSMassFormatter_h_GNUSTEP_BASE_INCLUDE */

View file

@ -0,0 +1,74 @@
/* Definition of class NSMeasurement
Copyright (C) 2019 Free Software Foundation, Inc.
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.
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.
*/
#ifndef _NSMeasurement_h_GNUSTEP_BASE_INCLUDE
#define _NSMeasurement_h_GNUSTEP_BASE_INCLUDE
#include <Foundation/NSObject.h>
#include <Foundation/NSUnit.h>
#if defined(__cplusplus)
extern "C" {
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_0, GS_API_LATEST)
@class NSUnit;
@interface NSMeasurement : NSObject <NSCopying, NSCoding>
{
NSUnit *_unit;
double _doubleValue;
}
// Creating Measurements
- (instancetype) initWithDoubleValue: (double)doubleValue
unit: (NSUnit *)unit;
// Accessing unit and value
- (NSUnit *) unit;
- (double) doubleValue;
// Conversion
- (BOOL) canBeConvertedToUnit: (NSUnit *)unit;
- (NSMeasurement *) measurementByConvertingToUnit: (NSUnit *)unit;
// Operating
- (NSMeasurement *) measurementByAddingMeasurement: (NSMeasurement *)measurement;
- (NSMeasurement *) measurementBySubtractingMeasurement: (NSMeasurement *)measurement;
@end
#if defined(__cplusplus)
}
#endif
#endif /* GS_API_MACOSX */
#endif /* _NSMeasurement_h_GNUSTEP_BASE_INCLUDE */

View file

@ -0,0 +1,80 @@
/* Definition of class NSMeasurementFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: heron
Date: Mon Sep 30 15:58:21 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.
*/
#ifndef _NSMeasurementFormatter_h_GNUSTEP_BASE_INCLUDE
#define _NSMeasurementFormatter_h_GNUSTEP_BASE_INCLUDE
#if defined(__cplusplus)
extern "C" {
#endif
#include <Foundation/NSObject.h>
#include <Foundation/NSFormatter.h>
@class NSLocale, NSMeasurement, NSNumberFormatter, NSUnit;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_12, GS_API_LATEST)
enum {
NSMeasurementFormatterUnitOptionsProvidedUnit = (1UL << 0),
NSMeasurementFormatterUnitOptionsNaturalScale = (1UL << 1),
NSMeasurementFormatterUnitOptionsTemperatureWithoutUnit = (1UL << 2),
};
typedef NSUInteger NSMeasurementFormatterUnitOptions;
@interface NSMeasurementFormatter : NSFormatter <NSCoding>
{
NSMeasurementFormatterUnitOptions _unitOptions;
NSFormattingUnitStyle _unitStyle;
NSLocale *_locale;
NSNumberFormatter *_numberFormatter;
}
- (NSMeasurementFormatterUnitOptions) unitOptions;
- (void) setUnitOptions: (NSMeasurementFormatterUnitOptions) unitOptions;
- (NSFormattingUnitStyle) unitStyle;
- (void) setUnitStyle: (NSFormattingUnitStyle)style;
- (NSLocale *) locale;
- (void) setLocale: (NSLocale *)locale;
- (NSNumberFormatter *) numberFormatter;
- (void) setNumberFormatter: (NSNumberFormatter *)numberFormatter;
- (NSString *)stringFromMeasurement: (NSMeasurement *)measurement;
- (NSString *)stringFromUnit: (NSUnit *)unit;
@end
#if defined(__cplusplus)
}
#endif
#endif /* GS_API_MACOSX */
#endif /* _NSMeasurementFormatter_h_GNUSTEP_BASE_INCLUDE */

420
Headers/Foundation/NSUnit.h Normal file
View file

@ -0,0 +1,420 @@
/* Definition of class NSUnit
Copyright (C) 2019 Free Software Foundation, Inc.
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.
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.
*/
#ifndef _NSUnit_h_GNUSTEP_BASE_INCLUDE
#define _NSUnit_h_GNUSTEP_BASE_INCLUDE
#include <Foundation/NSObject.h>
#if defined(__cplusplus)
extern "C" {
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_12, GS_API_LATEST)
// Unit converter
@interface NSUnitConverter : NSObject
- (double) baseUnitValueFromValue: (double)value;
- (double) valueFromBaseUnitValue: (double)baseUnitValue;
@end
// Linea converter... for things like C <-> F conversion...
@interface NSUnitConverterLinear : NSUnitConverter <NSCoding>
{
double _coefficient;
double _constant;
}
- (instancetype) initWithCoefficient: (double)coefficient;
- (instancetype) initWithCoefficient: (double)coefficient
constant: (double)constant;
- (double) coefficient;
- (double) constant;
@end
// Units... abstract...
@interface NSUnit : NSObject <NSCopying, NSCoding>
{
NSString *_symbol;
}
- (instancetype) init;
- (instancetype) initWithSymbol: (NSString *)symbol;
- (NSString *) symbol;
@end
// Dimension using units....
@interface NSDimension : NSUnit <NSCoding>
{
double _value;
NSUnitConverter *_converter;
}
- (NSUnitConverter *) converter;
- (instancetype) initWithSymbol: (NSString *)symbol converter: (NSUnitConverter *)converter ;
+ (instancetype) baseUnit;
@end
// Predefined....
@interface NSUnitAcceleration : NSDimension
/*
Base unit - metersPerSecondSquared
*/
+ (NSUnitAcceleration *) metersPerSecondSquared;
+ (NSUnitAcceleration *) gravity;
@end
@interface NSUnitAngle : NSDimension
/*
Base unit - degrees
*/
+ (NSUnitAngle *) degrees;
+ (NSUnitAngle *) arcMinutes;
+ (NSUnitAngle *) arcSeconds;
+ (NSUnitAngle *) radians;
+ (NSUnitAngle *) gradians;
+ (NSUnitAngle *) revolutions;
@end
@interface NSUnitArea : NSDimension
/*
Base unit - squareMeters
*/
+ (NSUnitArea *) squareMegameters;
+ (NSUnitArea *) squareKilometers;
+ (NSUnitArea *) squareMeters;
+ (NSUnitArea *) squareCentimeters;
+ (NSUnitArea *) squareMillimeters;
+ (NSUnitArea *) squareMicrometers;
+ (NSUnitArea *) squareNanometers;
+ (NSUnitArea *) squareInches;
+ (NSUnitArea *) squareFeet;
+ (NSUnitArea *) squareYards;
+ (NSUnitArea *) squareMiles;
+ (NSUnitArea *) acres;
+ (NSUnitArea *) ares;
+ (NSUnitArea *) hectares;
@end
@interface NSUnitConcentrationMass : NSDimension
/*
Base unit - gramsPerLiter
*/
+ (NSUnitConcentrationMass *) gramsPerLiter;
+ (NSUnitConcentrationMass *) milligramsPerDeciliter;
+ (NSUnitConcentrationMass *) millimolesPerLiterWithGramsPerMole: (double)gramsPerMole;
@end
@interface NSUnitDispersion : NSDimension
/*
Base unit - partsPerMillion
*/
+ (NSUnitDispersion *) partsPerMillion;
@end
@interface NSUnitDuration : NSDimension
/*
Base unit - seconds
*/
+ (NSUnitDuration *) seconds;
+ (NSUnitDuration *) minutes;
+ (NSUnitDuration *) hours;
@end
@interface NSUnitElectricCharge : NSDimension
/*
Base unit - coulombs
*/
+ (NSUnitElectricCharge *) coulombs;
+ (NSUnitElectricCharge *) megaampereHours;
+ (NSUnitElectricCharge *) kiloampereHours;
+ (NSUnitElectricCharge *) ampereHours;
+ (NSUnitElectricCharge *) milliampereHours;
+ (NSUnitElectricCharge *) microampereHours;
@end
@interface NSUnitElectricCurrent : NSDimension
/*
Base unit - amperes
*/
+ (NSUnitElectricCurrent *) megaamperes;
+ (NSUnitElectricCurrent *) kiloamperes;
+ (NSUnitElectricCurrent *) amperes;
+ (NSUnitElectricCurrent *) milliamperes;
+ (NSUnitElectricCurrent *) microamperes;
@end
@interface NSUnitElectricPotentialDifference : NSDimension
/*
Base unit - volts
*/
+ (NSUnitElectricPotentialDifference *) megavolts;
+ (NSUnitElectricPotentialDifference *) kilovolts;
+ (NSUnitElectricPotentialDifference *) volts;
+ (NSUnitElectricPotentialDifference *) millivolts;
+ (NSUnitElectricPotentialDifference *) microvolts;
@end
@interface NSUnitElectricResistance : NSDimension
/*
Base unit - ohms
*/
+ (NSUnitElectricResistance *) megaohms;
+ (NSUnitElectricResistance *) kiloohms;
+ (NSUnitElectricResistance *) ohms;
+ (NSUnitElectricResistance *) milliohms;
+ (NSUnitElectricResistance *) microohms;
@end
@interface NSUnitEnergy : NSDimension
/*
Base unit - joules
*/
+ (NSUnitEnergy *) kilojoules;
+ (NSUnitEnergy *) joules;
+ (NSUnitEnergy *) kilocalories;
+ (NSUnitEnergy *) calories;
+ (NSUnitEnergy *) kilowattHours;
@end
@interface NSUnitFrequency : NSDimension
/*
Base unit - hertz
*/
+ (NSUnitFrequency *) terahertz;
+ (NSUnitFrequency *) gigahertz;
+ (NSUnitFrequency *) megahertz;
+ (NSUnitFrequency *) kilohertz;
+ (NSUnitFrequency *) hertz;
+ (NSUnitFrequency *) millihertz;
+ (NSUnitFrequency *) microhertz;
+ (NSUnitFrequency *) nanohertz;
@end
@interface NSUnitFuelEfficiency : NSDimension
/*
Base unit - litersPer100Kilometers
*/
+ (NSUnitFuelEfficiency *) litersPer100Kilometers;
+ (NSUnitFuelEfficiency *) milesPerImperialGallon;
+ (NSUnitFuelEfficiency *) milesPerGallon;
@end
@interface NSUnitLength : NSDimension
/*
Base unit - meters
*/
+ (NSUnitLength *) megameters;
+ (NSUnitLength *) kilometers;
+ (NSUnitLength *) hectometers;
+ (NSUnitLength *) decameters;
+ (NSUnitLength *) meters;
+ (NSUnitLength *) decimeters;
+ (NSUnitLength *) centimeters;
+ (NSUnitLength *) millimeters;
+ (NSUnitLength *) micrometers;
+ (NSUnitLength *) nanometers;
+ (NSUnitLength *) picometers;
+ (NSUnitLength *) inches;
+ (NSUnitLength *) feet;
+ (NSUnitLength *) yards;
+ (NSUnitLength *) miles;
+ (NSUnitLength *) scandinavianMiles;
+ (NSUnitLength *) lightyears;
+ (NSUnitLength *) nauticalMiles;
+ (NSUnitLength *) fathoms;
+ (NSUnitLength *) furlongs;
+ (NSUnitLength *) astronomicalUnits;
+ (NSUnitLength *) parsecs;
@end
@interface NSUnitIlluminance : NSDimension
/*
Base unit - lux
*/
+ (NSUnitIlluminance *) lux;
@end
@interface NSUnitMass : NSDimension
/*
Base unit - kilograms
*/
+ (NSUnitMass *) kilograms;
+ (NSUnitMass *) grams;
+ (NSUnitMass *) decigrams;
+ (NSUnitMass *) centigrams;
+ (NSUnitMass *) milligrams;
+ (NSUnitMass *) micrograms;
+ (NSUnitMass *) nanograms;
+ (NSUnitMass *) picograms;
+ (NSUnitMass *) ounces;
+ (NSUnitMass *) pounds;
+ (NSUnitMass *) stones;
+ (NSUnitMass *) metricTons;
+ (NSUnitMass *) shortTons;
+ (NSUnitMass *) carats;
+ (NSUnitMass *) ouncesTroy;
+ (NSUnitMass *) slugs;
@end
@interface NSUnitPower : NSDimension
/*
Base unit - watts
*/
+ (NSUnitPower *) terawatts;
+ (NSUnitPower *) gigawatts;
+ (NSUnitPower *) megawatts;
+ (NSUnitPower *) kilowatts;
+ (NSUnitPower *) watts;
+ (NSUnitPower *) milliwatts;
+ (NSUnitPower *) microwatts;
+ (NSUnitPower *) nanowatts;
+ (NSUnitPower *) picowatts;
+ (NSUnitPower *) femtowatts;
+ (NSUnitPower *) horsepower;
@end
@interface NSUnitPressure : NSDimension
/*
Base unit - newtonsPerMetersSquared (equivalent to 1 pascal)
*/
+ (NSUnitPressure *) newtonsPerMetersSquared;
+ (NSUnitPressure *) gigapascals;
+ (NSUnitPressure *) megapascals;
+ (NSUnitPressure *) kilopascals;
+ (NSUnitPressure *) hectopascals;
+ (NSUnitPressure *) inchesOfMercury;
+ (NSUnitPressure *) bars;
+ (NSUnitPressure *) millibars;
+ (NSUnitPressure *) millimetersOfMercury;
+ (NSUnitPressure *) poundsForcePerSquareInch;
@end
@interface NSUnitSpeed : NSDimension
/*
Base unit - metersPerSecond
*/
+ (NSUnitSpeed *) metersPerSecond;
+ (NSUnitSpeed *) kilometersPerHour;
+ (NSUnitSpeed *) milesPerHour;
+ (NSUnitSpeed *) knots;
@end
@interface NSUnitTemperature : NSDimension
/*
Base unit - kelvin
*/
+ (NSUnitTemperature *) kelvin;
+ (NSUnitTemperature *) celsius;
+ (NSUnitTemperature *) fahrenheit;
@end
@interface NSUnitVolume : NSDimension
/*
Base unit - liters
*/
+ (NSUnitVolume *) megaliters;
+ (NSUnitVolume *) kiloliters;
+ (NSUnitVolume *) liters;
+ (NSUnitVolume *) deciliters;
+ (NSUnitVolume *) centiliters;
+ (NSUnitVolume *) milliliters;
+ (NSUnitVolume *) cubicKilometers;
+ (NSUnitVolume *) cubicMeters;
+ (NSUnitVolume *) cubicDecimeters;
+ (NSUnitVolume *) cubicCentimeters;
+ (NSUnitVolume *) cubicMillimeters;
+ (NSUnitVolume *) cubicInches;
+ (NSUnitVolume *) cubicFeet;
+ (NSUnitVolume *) cubicYards;
+ (NSUnitVolume *) cubicMiles;
+ (NSUnitVolume *) acreFeet;
+ (NSUnitVolume *) bushels;
+ (NSUnitVolume *) teaspoons;
+ (NSUnitVolume *) tablespoons;
+ (NSUnitVolume *) fluidOunces;
+ (NSUnitVolume *) cups;
+ (NSUnitVolume *) pints;
+ (NSUnitVolume *) quarts;
+ (NSUnitVolume *) gallons;
+ (NSUnitVolume *) imperialTeaspoons;
+ (NSUnitVolume *) imperialTablespoons;
+ (NSUnitVolume *) imperialFluidOunces;
+ (NSUnitVolume *) imperialPints;
+ (NSUnitVolume *) imperialQuarts;
+ (NSUnitVolume *) imperialGallons;
+ (NSUnitVolume *) metricCups;
@end
#if defined(__cplusplus)
}
#endif
#endif /* GS_API_MACOSX */
#endif /* _NSUnit_h_GNUSTEP_BASE_INCLUDE */

View file

@ -213,6 +213,8 @@ NSConnection.m \
NSData.m \
NSDate.m \
NSDateFormatter.m \
NSDateInterval.m \
NSDateIntervalFormatter.m \
NSDebug.m \
NSDecimal.m \
NSDecimalNumber.m \
@ -220,6 +222,7 @@ NSDictionary.m \
NSDistantObject.m \
NSDistributedLock.m \
NSDistributedNotificationCenter.m \
NSEnergyFormatter.m \
NSEnumerator.m \
NSError.m \
NSException.m \
@ -244,10 +247,14 @@ NSKeyedArchiver.m \
NSKeyedUnarchiver.m \
NSKeyValueCoding.m \
NSKeyValueObserving.m \
NSLengthFormatter.m \
NSLocale.m \
NSLock.m \
NSLog.m \
NSMapTable.m \
NSMassFormatter.m \
NSMeasurementFormatter.m \
NSMeasurement.m \
NSMetadata.m \
NSMethodSignature.m \
NSNotification.m \
@ -292,6 +299,7 @@ NSScriptKeyValueCoding.m \
NSScriptObjectSpecifiers.m \
NSScriptStandardSuiteCommands.m \
NSScriptSuiteRegistry.m \
NSUnit.m \
NSUserScriptTask.m \
NSSerializer.m \
NSSet.m \
@ -401,6 +409,8 @@ NSCompoundPredicate.h \
NSConnection.h \
NSData.h \
NSDateFormatter.h \
NSDateInterval.h \
NSDateIntervalFormatter.h \
NSDate.h \
NSDebug.h \
NSDecimal.h \
@ -409,6 +419,7 @@ NSDictionary.h \
NSDistantObject.h \
NSDistributedLock.h \
NSDistributedNotificationCenter.h \
NSEnergyFormatter.h \
NSEnumerator.h \
NSError.h \
NSErrorRecoveryAttempting.h \
@ -435,9 +446,13 @@ NSJSONSerialization.h \
NSKeyedArchiver.h \
NSKeyValueCoding.h \
NSKeyValueObserving.h \
NSLengthFormtter.h \
NSLocale.h \
NSLock.h \
NSMapTable.h \
NSMassFormatter.h \
NSMeasurementFormatter.h \
NSMeasurement.h \
NSMetadata.h \
NSMethodSignature.h \
NSNetServices.h \
@ -477,6 +492,7 @@ NSScriptKeyValueCoding.h \
NSScriptObjectSpecifiers.h \
NSScriptStandardSuiteCommands.h \
NSScriptSuiteRegistry.h \
NSUnit.h \
NSUserScriptTask.h \
NSScriptWhoseTests.h \
NSSerialization.h \

221
Source/NSDateInterval.m Normal file
View file

@ -0,0 +1,221 @@
/* 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>
#include <Foundation/NSException.h>
@implementation NSDateInterval
// Init
- (instancetype)init
{
self = [super init];
if(self != nil)
{
_startDate = [NSDate date];
_duration = 0.0;
RETAIN(_startDate);
}
return self;
}
- (instancetype)initWithStartDate:(NSDate *)startDate
duration:(NSTimeInterval)duration
{
self = [super init];
if(self != nil)
{
ASSIGNCOPY(_startDate, startDate);
if(duration < 0)
{
[NSException raise: NSInvalidArgumentException
format: @"Duration %f is less than zero", duration];
}
_duration = duration;
}
return self;
}
- (instancetype)initWithStartDate:(NSDate *)startDate
endDate:(NSDate *)endDate
{
return [self initWithStartDate: startDate
duration: [endDate timeIntervalSinceDate: startDate]];
}
- (instancetype) initWithCoder: (NSCoder *)coder
{
// TODO: Implement encoding
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 [_startDate dateByAddingTimeInterval: _duration];
}
- (void) setEndDate: (NSDate *)endDate
{
_duration = [endDate timeIntervalSinceDate: _startDate];
}
- (NSTimeInterval) duration
{
return _duration;
}
- (void) setDuration: (NSTimeInterval)duration
{
_duration = duration;
}
// Compare
- (NSComparisonResult) compare: (NSDateInterval *)dateInterval
{
NSComparisonResult result = NSOrderedSame;
if([_startDate isEqualToDate: [dateInterval startDate]] &&
_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;
}
return result;
}
- (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;
NSDateInterval *first = self; //[sortedArray firstObject];
NSDateInterval *last = dateInterval; // [sortedArray lastObject];
NSDate *intersectStartDate = nil;
NSDate *intersectEndDate = nil;
// 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;
}
// Contain
- (BOOL) containsDate: (NSDate *)date
{
NSDate *endDate = [self endDate];
return ([_startDate compare: date] == NSOrderedSame ||
[endDate compare: date] == NSOrderedSame ||
([_startDate compare: date] == NSOrderedAscending &&
[endDate compare: date] == NSOrderedDescending));
}
@end

View file

@ -0,0 +1,115 @@
/* Implementation of class NSDateIntervalFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: Gregory John Casamento <greg.casamento@gmail.com>
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>
#include <Foundation/NSLocale.h>
#include <Foundation/NSCalendar.h>
#include <Foundation/NSTimeZone.h>
#include <Foundation/NSString.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSDateInterval.h>
@implementation NSDateIntervalFormatter
// Properties
- (NSLocale *) locale
{
return _locale;
}
- (void) setLocale: (NSLocale *)locale
{
ASSIGNCOPY(_locale, locale);
}
- (NSCalendar *) calendar
{
return _calendar;
}
- (void) setCalendar: (NSCalendar *)calendar
{
ASSIGNCOPY(_calendar, calendar);
}
- (NSTimeZone *) timeZone
{
return _timeZone;
}
- (void) setTimeZone: (NSTimeZone *)timeZone
{
ASSIGNCOPY(_timeZone, timeZone);
}
- (NSString *) dateTemplate
{
return _dateTemplate;
}
- (void) setDateTemplate: (NSString *)dateTemplate
{
ASSIGNCOPY(_dateTemplate, dateTemplate);
}
- (NSDateIntervalFormatterStyle) dateStyle
{
return _dateStyle;
}
- (void) setDateStyle: (NSDateIntervalFormatterStyle)dateStyle
{
_dateStyle = dateStyle;
}
- (NSDateIntervalFormatterStyle) timeStyle
{
return _timeStyle;
}
- (void) setTimeStyle: (NSDateIntervalFormatterStyle)timeStyle
{
_timeStyle = timeStyle;
}
// Create strings
- (NSString *) stringFromDate: (NSDate *)fromDate toDate: (NSDate *)toDate
{
NSDateInterval *interval = [[NSDateInterval alloc] initWithStartDate: fromDate
endDate: toDate];
AUTORELEASE(interval);
return [self stringFromDateInterval: interval];
}
- (NSString *) stringFromDateInterval: (NSDateInterval *)dateInterval
{
NSDate *fromDate = [dateInterval startDate];
NSDate *toDate = [dateInterval endDate];
// Add formatting of NSDate here.
return [NSString stringWithFormat: @"%@ - %@", fromDate, toDate];
}
@end

128
Source/NSEnergyFormatter.m Normal file
View file

@ -0,0 +1,128 @@
/* Implementation of class NSEnergyFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: Gregory John Casamento <greg.casamento@gmail.com>
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>
#include <Foundation/NSMeasurement.h>
#include <Foundation/NSMeasurementFormatter.h>
#include <Foundation/NSUnit.h>
@implementation NSEnergyFormatter
- (instancetype) init
{
self = [super init];
if(self != nil)
{
_numberFormatter = nil;
_unitStyle = NSFormattingUnitStyleMedium;
_isForFoodEnergyUse = NO;
}
return self;
}
- (NSNumberFormatter *) numberFormatter
{
return _numberFormatter;
}
- (void) setNumberFormatter: (NSNumberFormatter *)formatter
{
ASSIGN(_numberFormatter, formatter);
}
- (NSFormattingUnitStyle) unitStyle
{
return _unitStyle;
}
- (void) setUnitStyle: (NSFormattingUnitStyle)style
{
_unitStyle = style;
}
- (BOOL) isForFoodEnergyUse
{
return _isForFoodEnergyUse;
}
- (void) setForFoodEnergyUse: (BOOL)flag
{
_isForFoodEnergyUse = flag;
}
- (NSString *) stringFromValue: (double)value unit: (NSEnergyFormatterUnit)unit
{
NSUnit *u = nil;
NSMeasurement *m = nil;
NSMeasurementFormatter *mf = nil;
switch(unit)
{
case NSEnergyFormatterUnitJoule:
u = [NSUnitEnergy joules];
break;
case NSEnergyFormatterUnitKilojoule:
u = [NSUnitEnergy kilojoules];
break;
case NSEnergyFormatterUnitCalorie:
u = [NSUnitEnergy calories];
break;
case NSEnergyFormatterUnitKilocalorie:
u = [NSUnitEnergy kilocalories];
break;
}
m = [[NSMeasurement alloc] initWithDoubleValue: value
unit: u];
AUTORELEASE(m);
mf = [[NSMeasurementFormatter alloc] init];
AUTORELEASE(mf);
[mf setUnitStyle: _unitStyle];
[mf setNumberFormatter: _numberFormatter];
return [mf stringFromMeasurement: m];
}
- (NSString *) stringFromJoules: (double)numberInJoules
{
return [self stringFromValue: numberInJoules unit: NSEnergyFormatterUnitJoule];
}
- (NSString *) unitStringFromValue: (double)value unit: (NSEnergyFormatterUnit)unit
{
return [self stringFromValue: value unit: unit];
}
- (NSString *) unitStringFromJoules: (double)numberInJoules usedUnit: (NSEnergyFormatterUnit *)unit
{
*unit = NSEnergyFormatterUnitJoule;
return [self stringFromValue: numberInJoules unit: *unit];
}
- (BOOL)getObjectValue: (id *)obj forString: (NSString *)string errorDescription: (NSString **)error
{
return NO;
}
@end

148
Source/NSLengthFormatter.m Normal file
View file

@ -0,0 +1,148 @@
/* 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>
#include <Foundation/NSUnit.h>
#include <Foundation/NSMeasurement.h>
#include <Foundation/NSMeasurementFormatter.h>
@implementation NSLengthFormatter
- (instancetype) init
{
self = [super init];
if(self != nil)
{
_numberFormatter = nil;
_unitStyle = NSFormattingUnitStyleMedium;
_isForPersonHeightUse = NO;
}
return self;
}
- (void) dealloc
{
RELEASE(_numberFormatter);
[super dealloc];
}
- (NSNumberFormatter *) numberFormatter
{
return _numberFormatter;
}
- (void) setNumberFormatter: (NSNumberFormatter *)formatter
{
ASSIGN(_numberFormatter, formatter);
}
- (NSFormattingUnitStyle) unitStyle
{
return _unitStyle;
}
- (void) setUnitStyle: (NSFormattingUnitStyle)style
{
_unitStyle = style;
}
- (BOOL) isForPersonHeightUse
{
return _isForPersonHeightUse;
}
- (void) setForPersonHeightUse: (BOOL)flag
{
_isForPersonHeightUse = flag;
}
- (NSString *) stringFromValue: (double)value unit: (NSLengthFormatterUnit)unit
{
NSUnit *u = nil;
NSMeasurement *m = nil;
NSMeasurementFormatter *mf = nil;
switch(unit)
{
case NSLengthFormatterUnitMillimeter:
u = [NSUnitLength millimeters];
break;
case NSLengthFormatterUnitCentimeter:
u = [NSUnitLength centimeters];
break;
case NSLengthFormatterUnitMeter:
u = [NSUnitLength meters];
break;
case NSLengthFormatterUnitKilometer:
u = [NSUnitLength kilometers];
break;
case NSLengthFormatterUnitInch:
u = [NSUnitLength inches];
break;
case NSLengthFormatterUnitFoot:
u = [NSUnitLength feet];
break;
case NSLengthFormatterUnitYard:
u = [NSUnitLength yards];
break;
case NSLengthFormatterUnitMile:
u = [NSUnitLength miles];
break;
}
m = [[NSMeasurement alloc] initWithDoubleValue: value
unit: u];
AUTORELEASE(m);
mf = [[NSMeasurementFormatter alloc] init];
AUTORELEASE(mf);
[mf setUnitStyle: _unitStyle];
[mf setNumberFormatter: _numberFormatter];
return [mf stringFromMeasurement: m];
}
- (NSString *) stringFromMeters: (double)numberInMeters
{
return [self stringFromValue: numberInMeters unit: NSLengthFormatterUnitMeter];
}
- (NSString *) unitStringFromValue: (double)value unit: (NSLengthFormatterUnit)unit
{
return [self stringFromValue: value unit: unit];
}
- (NSString *) unitStringFromMeters: (double)numberInMeters usedUnit: (NSLengthFormatterUnit *)unit
{
*unit = NSLengthFormatterUnitMeter;
return [self stringFromValue: numberInMeters unit: *unit];
}
- (BOOL)getObjectValue: (id *)obj forString: (NSString *)string errorDescription: (NSString **)error
{
return NO;
}
@end

133
Source/NSMassFormatter.m Normal file
View file

@ -0,0 +1,133 @@
/* Implementation of class NSMassFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: heron
Date: Mon Sep 30 15:58:21 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/NSMassFormatter.h>
#include <Foundation/NSMeasurement.h>
#include <Foundation/NSMeasurementFormatter.h>
#include <Foundation/NSUnit.h>
@implementation NSMassFormatter
- (instancetype) init
{
self = [super init];
if(self != nil)
{
_numberFormatter = nil;
_unitStyle = NSFormattingUnitStyleMedium;
_isForPersonMassUse = NO;
}
return self;
}
- (NSNumberFormatter *) numberFormatter
{
return _numberFormatter;
}
- (void) setNumberFormatter: (NSNumberFormatter *)formatter
{
ASSIGN(_numberFormatter, formatter);
}
- (NSFormattingUnitStyle) unitStyle
{
return _unitStyle;
}
- (void) setUnitStyle: (NSFormattingUnitStyle)style;
{
_unitStyle = style;
}
- (BOOL) isForPersonMassUse;
{
return _isForPersonMassUse;
}
- (void) setForPersonMassUse: (BOOL)flag;
{
_isForPersonMassUse = flag;
}
- (NSString *)stringFromValue: (double)value unit: (NSMassFormatterUnit)unit;
{
NSUnit *u = nil;
NSMeasurement *m = nil;
NSMeasurementFormatter *mf = nil;
switch(unit)
{
case NSMassFormatterUnitGram:
u = [NSUnitMass grams];
break;
case NSMassFormatterUnitKilogram:
u = [NSUnitMass kilograms];
break;
case NSMassFormatterUnitOunce:
u = [NSUnitMass ounces];
break;
case NSMassFormatterUnitPound:
u = [NSUnitMass pounds];
break;
case NSMassFormatterUnitStone:
u = [NSUnitMass stones];
break;
}
m = [[NSMeasurement alloc] initWithDoubleValue: value
unit: u];
AUTORELEASE(m);
mf = [[NSMeasurementFormatter alloc] init];
AUTORELEASE(mf);
[mf setUnitStyle: _unitStyle];
[mf setNumberFormatter: _numberFormatter];
return [mf stringFromMeasurement: m];
}
- (NSString *)stringFromKilograms: (double)numberInKilograms;
{
return [self stringFromValue: numberInKilograms unit: NSMassFormatterUnitKilogram];
}
- (NSString *)unitStringFromValue: (double)value unit: (NSMassFormatterUnit)unit;
{
return [self stringFromValue: value unit: unit];
}
- (NSString *)unitStringFromKilograms: (double)numberInKilograms usedUnit: (NSMassFormatterUnit *)unit
{
*unit = NSMassFormatterUnitKilogram;
return [self stringFromValue: numberInKilograms unit: *unit];
}
- (BOOL)getObjectValue: (id*)obj forString: (NSString *)string errorDescription: (NSString **)error
{
return NO;
}
@end

157
Source/NSMeasurement.m Normal file
View file

@ -0,0 +1,157 @@
/* Implementation of class NSMeasurement
Copyright (C) 2019 Free Software Foundation, Inc.
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.
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/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])
{
// TODO: Verify that this is the correct encoding...
[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

@ -0,0 +1,130 @@
/* Implementation of class NSMeasurementFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
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.
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/NSMeasurementFormatter.h>
#include <Foundation/NSLocale.h>
#include <Foundation/NSMeasurement.h>
#include <Foundation/NSNumberFormatter.h>
#include <Foundation/NSUnit.h>
@implementation NSMeasurementFormatter
- (instancetype) init
{
self = [super init];
if(self != nil)
{
_unitOptions = NSMeasurementFormatterUnitOptionsProvidedUnit;
_unitStyle = NSFormattingUnitStyleMedium;
_locale = RETAIN([NSLocale currentLocale]);
}
return self;
}
- (void) dealloc
{
RELEASE(_locale);
[super dealloc];
}
- (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
{
NSString *result = nil;
NSNumber *num = [NSNumber numberWithDouble: [measurement doubleValue]];
NSUnit *u = [measurement unit];
result = [_numberFormatter stringForObjectValue: num];
switch(_unitStyle)
{
case NSFormattingUnitStyleShort:
case NSFormattingUnitStyleMedium:
case NSFormattingUnitStyleLong:
result = [result stringByAppendingString: [self stringFromUnit: u]];
break;
}
return result;
}
- (NSString *)stringFromUnit: (NSUnit *)unit
{
return [unit symbol];
}
- (NSString *)stringForObjectValue: (id)obj
{
NSString *result = nil;
if([obj isKindOfClass: [NSMeasurement class]])
{
result = [self stringFromMeasurement: obj];
}
else if([obj isKindOfClass: [NSUnit class]])
{
result = [self stringFromUnit: obj];
}
return result;
}
@end

1771
Source/NSUnit.m Normal file

File diff suppressed because it is too large Load diff