mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-31 16:50:58 +00:00
Implement other formatting methods in terms of the main one
This commit is contained in:
parent
71dd282a1c
commit
996b40ad34
2 changed files with 102 additions and 8 deletions
|
@ -91,7 +91,7 @@ typedef NSUInteger NSDateComponentsFormatterZeroFormattingBehavior;
|
||||||
|
|
||||||
- (NSString *) stringFromDateComponents: (NSDateComponents *)components;
|
- (NSString *) stringFromDateComponents: (NSDateComponents *)components;
|
||||||
|
|
||||||
- (NSString *) stringFromDate: (NSDate *)startDate toDate:(NSDate *)endDate;
|
- (NSString *) stringFromDate: (NSDate *)startDate toDate: (NSDate *)endDate;
|
||||||
|
|
||||||
- (NSString *) stringFromTimeInterval: (NSTimeInterval)ti;
|
- (NSString *) stringFromTimeInterval: (NSTimeInterval)ti;
|
||||||
|
|
||||||
|
|
|
@ -23,27 +23,107 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <Foundation/NSDateComponentsFormatter.h>
|
#include <Foundation/NSDateComponentsFormatter.h>
|
||||||
|
#include <Foundation/NSDate.h>
|
||||||
|
#include <Foundation/NSString.h>
|
||||||
|
#include <Foundation/NSValue.h>
|
||||||
|
#include <Foundation/NSException.h>
|
||||||
|
|
||||||
@implementation NSDateComponentsFormatter
|
@implementation NSDateComponentsFormatter
|
||||||
|
|
||||||
|
- (instancetype) init
|
||||||
|
{
|
||||||
|
self = [super init];
|
||||||
|
if(self != nil)
|
||||||
|
{
|
||||||
|
_calendar = nil;
|
||||||
|
_referenceDate = nil;
|
||||||
|
_allowsFractionalUnits = NO;
|
||||||
|
_collapsesLargestUnit = NO;
|
||||||
|
_includesApproximationPhrase = NO;
|
||||||
|
_formattingContext = NSFormattingContextUnknown;
|
||||||
|
_maximumUnitCount = 0;
|
||||||
|
_zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorNone;
|
||||||
|
_allowedUnits = NSCalendarUnitYear |
|
||||||
|
NSCalendarUnitMonth |
|
||||||
|
NSCalendarUnitDay |
|
||||||
|
NSCalendarUnitHour |
|
||||||
|
NSCalendarUnitMinute |
|
||||||
|
NSCalendarUnitSecond;
|
||||||
|
_unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void) dealloc
|
||||||
|
{
|
||||||
|
RELEASE(_calendar);
|
||||||
|
RELEASE(_referenceDate);
|
||||||
|
[super dealloc];
|
||||||
|
}
|
||||||
|
|
||||||
- (NSString *) stringForObjectValue: (id)obj
|
- (NSString *) stringForObjectValue: (id)obj
|
||||||
{
|
{
|
||||||
return nil;
|
NSString *result = nil;
|
||||||
|
|
||||||
|
if([obj isKindOfClass: [NSDateComponents class]])
|
||||||
|
{
|
||||||
|
result = [self stringFromDateComponents: obj];
|
||||||
|
}
|
||||||
|
else if([obj isKindOfClass: [NSNumber class]])
|
||||||
|
{
|
||||||
|
NSTimeInterval ti = [obj longLongValue];
|
||||||
|
result = [self stringFromTimeInterval: ti];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *) stringFromDateComponents: (NSDateComponents *)components
|
- (NSString *) stringFromDateComponents: (NSDateComponents *)components
|
||||||
{
|
{
|
||||||
return nil;
|
NSString *result = @"";
|
||||||
|
|
||||||
|
if(_allowedUnits | NSCalendarUnitYear)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
if(_allowedUnits | NSCalendarUnitMonth)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
if(_allowedUnits | NSCalendarUnitDay)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
if(_allowedUnits | NSCalendarUnitHour)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
if(_allowedUnits | NSCalendarUnitMinute)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
if(_allowedUnits | NSCalendarUnitSecond)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
if(_allowedUnits | NSCalendarUnitWeekOfMonth)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *) stringFromDate: (NSDate *)startDate toDate:(NSDate *)endDate
|
- (NSString *) stringFromDate: (NSDate *)startDate
|
||||||
|
toDate: (NSDate *)endDate
|
||||||
{
|
{
|
||||||
return nil;
|
NSDateComponents *dc = nil;
|
||||||
|
NSCalendar *calendar = ( _calendar != nil ) ? _calendar : [NSCalendar currentCalendar];
|
||||||
|
dc = [calendar components: _allowedUnits
|
||||||
|
fromDate: startDate
|
||||||
|
toDate: endDate
|
||||||
|
options: NSCalendarMatchStrictly];
|
||||||
|
return [self stringFromDateComponents: dc];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *) stringFromTimeInterval: (NSTimeInterval)ti
|
- (NSString *) stringFromTimeInterval: (NSTimeInterval)ti
|
||||||
{
|
{
|
||||||
return nil;
|
NSDate *startDate = [NSDate date];
|
||||||
|
NSDate *endDate = [startDate dateByAddingTimeInterval: (ti > 0) ? ti : -ti];
|
||||||
|
return [self stringFromDate: startDate toDate: endDate];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSDateComponentsFormatterUnitsStyle) unitsStyle
|
- (NSDateComponentsFormatterUnitsStyle) unitsStyle
|
||||||
|
@ -63,6 +143,17 @@
|
||||||
|
|
||||||
- (void) setAllowedUnits: (NSCalendarUnit)units
|
- (void) setAllowedUnits: (NSCalendarUnit)units
|
||||||
{
|
{
|
||||||
|
if(units | NSCalendarUnitYear &&
|
||||||
|
units | NSCalendarUnitMonth &&
|
||||||
|
units | NSCalendarUnitDay &&
|
||||||
|
units | NSCalendarUnitHour &&
|
||||||
|
units | NSCalendarUnitMinute &&
|
||||||
|
units | NSCalendarUnitSecond &&
|
||||||
|
units | NSCalendarUnitWeekOfMonth)
|
||||||
|
{
|
||||||
|
[NSException raise: NSInvalidArgumentException
|
||||||
|
format: @"Passed invalid unit into allowedUnits"];
|
||||||
|
}
|
||||||
_allowedUnits = units;
|
_allowedUnits = units;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,7 +245,10 @@
|
||||||
+ (NSString *) localizedStringFromDateComponents: (NSDateComponents *)components
|
+ (NSString *) localizedStringFromDateComponents: (NSDateComponents *)components
|
||||||
unitsStyle: (NSDateComponentsFormatterUnitsStyle)unitsStyle
|
unitsStyle: (NSDateComponentsFormatterUnitsStyle)unitsStyle
|
||||||
{
|
{
|
||||||
return nil;
|
NSDateComponentsFormatter *fmt = [[NSDateComponentsFormatter alloc] init];
|
||||||
|
[fmt setUnitsStyle: unitsStyle];
|
||||||
|
AUTORELEASE(fmt);
|
||||||
|
return [fmt stringFromDateComponents: components];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue