new methods supplied by Lubos Dolezel

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@36391 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2013-03-20 17:37:16 +00:00
parent deaa8b820e
commit 081cb7670c
3 changed files with 205 additions and 32 deletions

View file

@ -1,3 +1,11 @@
2012-03-20 Richard Frith-Macdonald <rfm@gnu.org>
* Tests/base/NSCalendar/features-10-7.m:
* Source/NSCalendar.m:
* Headers/Foundation/NSCalendar.h:
New OSX 10.7 methods supplied by Lobos Dolezel and rewritten somewhat
to avoid breaking binary compatibility.
2012-03-17 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSSet.m: Some optimisation for set intersection.

View file

@ -56,7 +56,12 @@ enum
NSWeekdayCalendarUnit = (1UL << 9),
NSWeekdayOrdinalCalendarUnit = (1UL << 10),
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
NSQuarterCalendarUnit = (1UL << 11)
NSQuarterCalendarUnit = (1UL << 11),
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_7, GS_API_LATEST)
NSWeekOfMonthCalendarUnit = (1UL << 12),
NSWeekOfYearCalendarUnit = (1UL << 13),
NSYearForWeekOfYearCalendarUnit = (1UL << 14),
#endif
};
@ -74,19 +79,22 @@ enum
@interface NSDateComponents : NSObject <NSCopying>
{
NSInteger _era;
NSInteger _year;
NSInteger _month;
NSInteger _day;
NSInteger _hour;
NSInteger _minute;
NSInteger _second;
NSInteger _week;
NSInteger _weekday;
NSInteger _weekdayOrdinal;
NSInteger _quarter;
NSCalendar *_cal;
NSTimeZone *_tz;
@private
void *_NSDateComponentsInternal;
/* FIXME ... remove dummy fields at next binary incompatible release
*/
void *_dummy1;
void *_dummy2;
void *_dummy3;
void *_dummy4;
void *_dummy5;
void *_dummy6;
void *_dummy7;
void *_dummy8;
void *_dummy9;
void *_dummy10;
void *_dummy11;
void *_dummy12;
}
- (NSInteger) day;
@ -121,6 +129,48 @@ enum
- (NSTimeZone *) timeZone;
- (void) setCalendar: (NSCalendar *) cal;
- (void) setTimeZone: (NSTimeZone *) tz;
/**
* <p>
* Computes a date by using the components set in this NSDateComponents
* instance.
* </p>
* <p>
* A calendar (and optionally a time zone) must be set prior to
* calling this method.
* </p>
*/
- (NSDate *) date;
/** Returns the number of the week in this month. */
- (NSInteger) weekOfMonth;
/**
* Returns the number of the week in this year.
* Identical to calling <code>week</code>. */
- (NSInteger) weekOfYear;
/**
* The year corresponding to the current week.
* This value may differ from year around the end of the year.
*
* For example, for 2012-12-31, the year number is 2012, but
* yearForWeekOfYear is 2013, since it's already week 1 in 2013.
*/
- (NSInteger) yearForWeekOfYear;
/** Sets the number of the week in this month. */
- (void) setWeekOfMonth: (NSInteger) v;
/**
* Sets the number of the week in this year.
* Identical to calling <code>-setWeek</code>. */
- (void) setWeekOfYear: (NSInteger) v;
/**
* Sets the year number for the current week.
* See the explanation at <code>-yearForWeekOfYear</code>.
*/
- (void) setYearForWeekOfYear: (NSInteger) v;
#endif
@end

View file

@ -41,7 +41,6 @@
#endif
#if GS_USE_ICU == 1
static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
{
@ -287,7 +286,7 @@ static NSRecursiveLock *classLock = nil;
if (unitFlags & NSYearCalendarUnit)
[comps setYear: ucal_get (_cal, UCAL_YEAR, &err)];
if (unitFlags & NSMonthCalendarUnit)
[comps setMonth: ucal_get (_cal, UCAL_MONTH, &err)];
[comps setMonth: ucal_get (_cal, UCAL_MONTH, &err)+1];
if (unitFlags & NSDayCalendarUnit)
[comps setDay: ucal_get (_cal, UCAL_DAY_OF_MONTH, &err)];
if (unitFlags & NSHourCalendarUnit)
@ -296,12 +295,16 @@ static NSRecursiveLock *classLock = nil;
[comps setMinute: ucal_get (_cal, UCAL_MINUTE, &err)];
if (unitFlags & NSSecondCalendarUnit)
[comps setSecond: ucal_get (_cal, UCAL_SECOND, &err)];
if (unitFlags & NSWeekCalendarUnit)
if (unitFlags & (NSWeekCalendarUnit|NSWeekOfYearCalendarUnit))
[comps setWeek: ucal_get (_cal, UCAL_WEEK_OF_YEAR, &err)];
if (unitFlags & NSWeekdayCalendarUnit)
[comps setWeekday: ucal_get (_cal, UCAL_DAY_OF_WEEK, &err)];
if (unitFlags & NSWeekdayOrdinalCalendarUnit)
[comps setWeekdayOrdinal: ucal_get (_cal, UCAL_WEEK_OF_MONTH, &err)];
[comps setWeekdayOrdinal: ucal_get (_cal, UCAL_DAY_OF_WEEK_IN_MONTH, &err)];
if (unitFlags & NSWeekOfMonthCalendarUnit)
[comps setWeekOfMonth: ucal_get (_cal, UCAL_WEEK_OF_MONTH, &err)];
if (unitFlags & NSYearForWeekOfYearCalendarUnit)
[comps setYearForWeekOfYear: ucal_get (_cal, UCAL_YEAR_WOY, &err)];
return AUTORELEASE(comps);
#else
@ -371,6 +374,14 @@ static NSRecursiveLock *classLock = nil;
{
_ADD_COMPONENT(UCAL_DAY_OF_WEEK, amount);
}
if ((amount = (int32_t)[comps weekOfMonth]) != NSUndefinedDateComponent)
{
_ADD_COMPONENT(UCAL_WEEK_OF_MONTH, amount);
}
if ((amount = (int32_t)[comps yearForWeekOfYear]) != NSUndefinedDateComponent)
{
_ADD_COMPONENT(UCAL_YEAR_WOY, amount);
}
#undef _ADD_COMPONENT
udate = ucal_getMillis (_cal, &err);
@ -403,7 +414,7 @@ static NSRecursiveLock *classLock = nil;
}
if ((amount = (int32_t)[comps month]) != NSUndefinedDateComponent)
{
ucal_set (_cal, UCAL_MONTH, amount);
ucal_set (_cal, UCAL_MONTH, amount-1);
}
if ((amount = (int32_t)[comps day]) != NSUndefinedDateComponent)
{
@ -429,6 +440,14 @@ static NSRecursiveLock *classLock = nil;
{
ucal_set (_cal, UCAL_DAY_OF_WEEK, amount);
}
if ((amount = (int32_t)[comps weekOfMonth]) != NSUndefinedDateComponent)
{
ucal_set (_cal, UCAL_WEEK_OF_MONTH, amount);
}
if ((amount = (int32_t)[comps yearForWeekOfYear]) != NSUndefinedDateComponent)
{
ucal_set (_cal, UCAL_YEAR_WOY, amount);
}
udate = ucal_getMillis (_cal, &err);
if (U_FAILURE(err))
@ -660,20 +679,70 @@ static NSRecursiveLock *classLock = nil;
@implementation NSDateComponents
typedef struct {
NSInteger era;
NSInteger year;
NSInteger month;
NSInteger day;
NSInteger hour;
NSInteger minute;
NSInteger second;
NSInteger week;
NSInteger weekday;
NSInteger weekdayOrdinal;
NSInteger quarter;
NSInteger weekOfMonth;
NSInteger yearForWeekOfYear;
NSCalendar *cal;
NSTimeZone *tz;
} DateComp;
#define _era ((DateComp*)_NSDateComponentsInternal)->era
#define _year ((DateComp*)_NSDateComponentsInternal)->year
#define _month ((DateComp*)_NSDateComponentsInternal)->month
#define _day ((DateComp*)_NSDateComponentsInternal)->day
#define _hour ((DateComp*)_NSDateComponentsInternal)->hour
#define _minute ((DateComp*)_NSDateComponentsInternal)->minute
#define _second ((DateComp*)_NSDateComponentsInternal)->second
#define _week ((DateComp*)_NSDateComponentsInternal)->week
#define _weekday ((DateComp*)_NSDateComponentsInternal)->weekday
#define _weekdayOrdinal ((DateComp*)_NSDateComponentsInternal)->weekdayOrdinal
#define _quarter ((DateComp*)_NSDateComponentsInternal)->quarter
#define _weekOfMonth ((DateComp*)_NSDateComponentsInternal)->weekOfMonth
#define _yearForWeekOfYear ((DateComp*)_NSDateComponentsInternal)->yearForWeekOfYear
#define _cal ((DateComp*)_NSDateComponentsInternal)->cal
#define _tz ((DateComp*)_NSDateComponentsInternal)->tz
- (void) dealloc
{
if (0 != _NSDateComponentsInternal)
{
free(_NSDateComponentsInternal);
}
[super dealloc];
}
- (id) init
{
_era = NSUndefinedDateComponent;
_year = NSUndefinedDateComponent;
_month = NSUndefinedDateComponent;
_day = NSUndefinedDateComponent;
_hour = NSUndefinedDateComponent;
_minute = NSUndefinedDateComponent;
_second = NSUndefinedDateComponent;
_week = NSUndefinedDateComponent;
_weekday = NSUndefinedDateComponent;
_weekdayOrdinal = NSUndefinedDateComponent;
_quarter = NSUndefinedDateComponent;
if (nil != (self = [super init]))
{
_NSDateComponentsInternal = malloc(sizeof(DateComp));
_era = NSUndefinedDateComponent;
_year = NSUndefinedDateComponent;
_month = NSUndefinedDateComponent;
_day = NSUndefinedDateComponent;
_hour = NSUndefinedDateComponent;
_minute = NSUndefinedDateComponent;
_second = NSUndefinedDateComponent;
_week = NSUndefinedDateComponent;
_weekday = NSUndefinedDateComponent;
_weekdayOrdinal = NSUndefinedDateComponent;
_quarter = NSUndefinedDateComponent;
_weekOfMonth = NSUndefinedDateComponent;
_yearForWeekOfYear = NSUndefinedDateComponent;
_cal = NULL;
_tz = NULL;
}
return self;
}
@ -732,6 +801,21 @@ static NSRecursiveLock *classLock = nil;
return _year;
}
- (NSInteger) weekOfMonth
{
return _weekOfMonth;
}
- (NSInteger) weekOfYear
{
return _week;
}
- (NSInteger) yearForWeekOfYear
{
return _yearForWeekOfYear;
}
- (NSCalendar *) calendar
{
return _cal;
@ -742,6 +826,16 @@ static NSRecursiveLock *classLock = nil;
return _tz;
}
- (NSDate *) date
{
NSCalendar* cal = [self calendar];
NSTimeZone* zone = [self timeZone];
if (zone != NULL)
[cal setTimeZone: zone];
return [cal dateFromComponents: self];
}
- (void) setDay: (NSInteger) v
@ -799,6 +893,21 @@ static NSRecursiveLock *classLock = nil;
_year = v;
}
- (void) setWeekOfYear: (NSInteger) v
{
_week = v;
}
- (void) setWeekOfMonth: (NSInteger) v
{
_weekOfMonth = v;
}
- (void) setYearForWeekOfYear: (NSInteger) v
{
_yearForWeekOfYear = v;
}
- (void) setCalendar: (NSCalendar *) cal
{
if (_cal)
@ -820,7 +929,13 @@ static NSRecursiveLock *classLock = nil;
if (NSShouldRetainWithZone(self, zone))
return RETAIN(self);
else
return NSCopyObject(self, 0, zone);
{
NSDateComponents *c = [[NSDateComponents allocWithZone: zone] init];
memcpy(c->_NSDateComponentsInternal, _NSDateComponentsInternal,
sizeof(DateComp));
return c;
}
}
@end