Make sure UCalendar is open before trying to use it.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@31962 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Stefan Bidigaray 2011-01-28 00:23:57 +00:00
parent d80a4a55f2
commit 351e9eb29a
2 changed files with 37 additions and 40 deletions

View file

@ -1,3 +1,7 @@
2011-01-27 Stefan Bidigaray <stefanbidi@gmail.com>
* Source/NSCalendar.m: Open UCalendar before use.
2011-01-27 Stefan Bidigaray <stefanbidi@gmail.com> 2011-01-27 Stefan Bidigaray <stefanbidi@gmail.com>
* Source/NSNumberFormatter.m: Set some sensible defaults. * Source/NSNumberFormatter.m: Set some sensible defaults.

View file

@ -69,8 +69,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
#endif /* GS_USE_ICU */ #endif /* GS_USE_ICU */
@interface NSCalendar (PrivateMethods) @interface NSCalendar (PrivateMethods)
- (void) _openCalendar; - (void) _resetCalendar;
- (void) _closeCalendar;
- (NSString *) _localeIdWithLocale: (NSLocale *) locale; - (NSString *) _localeIdWithLocale: (NSLocale *) locale;
- (NSString *)_localeIdentifier; - (NSString *)_localeIdentifier;
- (void) _setLocaleIdentifier: (NSString *) identifier; - (void) _setLocaleIdentifier: (NSString *) identifier;
@ -79,23 +78,24 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
#define TZ_NAME_LENGTH 1024 #define TZ_NAME_LENGTH 1024
@implementation NSCalendar (PrivateMethods) @implementation NSCalendar (PrivateMethods)
- (void) _openCalendar - (void) _resetCalendar
{ {
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
if (_cal == NULL) NSString *tzName;
{ NSUInteger tzLen;
NSString *tzName; unichar cTzId[TZ_NAME_LENGTH];
NSUInteger tzLen; const char *cLocaleId;
unichar cTzId[TZ_NAME_LENGTH]; UErrorCode err = U_ZERO_ERROR;
const char *cLocaleId;
UErrorCode err = U_ZERO_ERROR; if (_cal != NULL)
ucal_close (_cal);
cLocaleId = [_localeId UTF8String];
tzName = [_tz name]; cLocaleId = [_localeId UTF8String];
tzLen = [tzName length]; tzName = [_tz name];
if (tzLen > TZ_NAME_LENGTH) tzLen = [tzName length];
tzLen = TZ_NAME_LENGTH; if (tzLen > TZ_NAME_LENGTH)
[tzName getCharacters: cTzId range: NSMakeRange(0, tzLen)]; tzLen = TZ_NAME_LENGTH;
[tzName getCharacters: cTzId range: NSMakeRange(0, tzLen)];
#ifndef UCAL_DEFAULT #ifndef UCAL_DEFAULT
/* /*
@ -104,17 +104,8 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
*/ */
#define UCAL_DEFAULT UCAL_TRADITIONAL #define UCAL_DEFAULT UCAL_TRADITIONAL
#endif #endif
_cal = _cal =
ucal_open ((const UChar *)cTzId, tzLen, cLocaleId, UCAL_DEFAULT, &err); ucal_open ((const UChar *)cTzId, tzLen, cLocaleId, UCAL_DEFAULT, &err);
}
#endif
}
- (void) _closeCalendar
{
#if GS_USE_ICU == 1
ucal_close (_cal);
_cal = NULL;
#endif #endif
} }
@ -144,9 +135,9 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
if ([identifier isEqualToString: _localeId]) if ([identifier isEqualToString: _localeId])
return; return;
[self _closeCalendar];
RELEASE(_localeId); RELEASE(_localeId);
_localeId = RETAIN(identifier); _localeId = RETAIN(identifier);
[self _resetCalendar];
} }
@end @end
@ -201,13 +192,15 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
_localeId = RETAIN([self _localeIdWithLocale: [NSLocale currentLocale]]); _localeId = RETAIN([self _localeIdWithLocale: [NSLocale currentLocale]]);
_tz = RETAIN([NSTimeZone defaultTimeZone]); _tz = RETAIN([NSTimeZone defaultTimeZone]);
[self _openCalendar]; [self _resetCalendar];
return self; return self;
} }
- (void) dealloc - (void) dealloc
{ {
[self _closeCalendar]; #if GS_USE_ICU == 1
ucal_close (_cal);
#endif
RELEASE(_identifier); RELEASE(_identifier);
RELEASE(_localeId); RELEASE(_localeId);
RELEASE(_tz); RELEASE(_tz);
@ -279,7 +272,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
UErrorCode err = U_ZERO_ERROR; UErrorCode err = U_ZERO_ERROR;
UDate udate; UDate udate;
[self _openCalendar]; [self _resetCalendar];
udate = (UDate)([date timeIntervalSince1970] * 1000.0); udate = (UDate)([date timeIntervalSince1970] * 1000.0);
ucal_setMillis (_cal, udate, &err); ucal_setMillis (_cal, udate, &err);
@ -339,7 +332,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
UDate udate; UDate udate;
UErrorCode err = U_ZERO_ERROR; UErrorCode err = U_ZERO_ERROR;
[self _openCalendar]; [self _resetCalendar];
ucal_clear (_cal); ucal_clear (_cal);
if ((amount = (int32_t)[comps day]) != NSUndefinedDateComponent) if ((amount = (int32_t)[comps day]) != NSUndefinedDateComponent)
@ -398,7 +391,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
- (NSUInteger) firstWeekday - (NSUInteger) firstWeekday
{ {
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
[self _openCalendar]; [self _resetCalendar];
return ucal_getAttribute (_cal, UCAL_FIRST_DAY_OF_WEEK); return ucal_getAttribute (_cal, UCAL_FIRST_DAY_OF_WEEK);
#else #else
return 0; return 0;
@ -408,7 +401,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
- (void) setFirstWeekday: (NSUInteger) weekday - (void) setFirstWeekday: (NSUInteger) weekday
{ {
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
[self _openCalendar]; [self _resetCalendar];
ucal_setAttribute (_cal, UCAL_FIRST_DAY_OF_WEEK, (int32_t)weekday); ucal_setAttribute (_cal, UCAL_FIRST_DAY_OF_WEEK, (int32_t)weekday);
#else #else
return; return;
@ -418,7 +411,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
- (NSUInteger) minimumDaysInFirstWeek - (NSUInteger) minimumDaysInFirstWeek
{ {
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
[self _openCalendar]; [self _resetCalendar];
return ucal_getAttribute (_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK); return ucal_getAttribute (_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);
#else #else
return 1; return 1;
@ -428,7 +421,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
- (void) setMinimumDaysInFirstWeek: (NSUInteger) mdw - (void) setMinimumDaysInFirstWeek: (NSUInteger) mdw
{ {
#if GS_USE_ICU == 1 #if GS_USE_ICU == 1
[self _openCalendar]; [self _resetCalendar];
ucal_setAttribute (_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, (int32_t)mdw); ucal_setAttribute (_cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK, (int32_t)mdw);
#else #else
return; return;
@ -445,9 +438,9 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
if ([tz isEqual: _tz]) if ([tz isEqual: _tz])
return; return;
[self _closeCalendar];
RELEASE(_tz); RELEASE(_tz);
_tz = RETAIN(tz); _tz = RETAIN(tz);
[self _resetCalendar];
} }
@ -458,7 +451,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
NSRange result; NSRange result;
UErrorCode err = U_ZERO_ERROR; UErrorCode err = U_ZERO_ERROR;
[self _openCalendar]; [self _resetCalendar];
dateField = _NSCalendarUnitToDateField (unit); dateField = _NSCalendarUnitToDateField (unit);
// We really don't care if there are any errors... // We really don't care if there are any errors...
result.location = result.location =
@ -483,7 +476,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit)
NSRange result; NSRange result;
UErrorCode err = U_ZERO_ERROR; UErrorCode err = U_ZERO_ERROR;
[self _openCalendar]; [self _resetCalendar];
dateField = _NSCalendarUnitToDateField (unit); dateField = _NSCalendarUnitToDateField (unit);
// We really don't care if there are any errors... // We really don't care if there are any errors...
result.location = result.location =