From e40d187b015e7b4967a8a6cb95b4d019aa370bca Mon Sep 17 00:00:00 2001 From: stefanbidi Date: Sun, 23 Jan 2011 17:10:21 +0000 Subject: [PATCH] Fixed initialization bug in NSDateFormatter and implement NSCalendar-components:fromDate: git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@31933 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 +++++ Source/NSCalendar.m | 36 ++++++++++++++++++++++++++++++++++++ Source/NSDateFormatter.m | 4 ++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index d064657cc..2be232bb6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,11 @@ * libs/base/trunk/Source/NSNumberFormatter.m: Some small fixes in NSNumberFormatter. Mostly stop using == to compare C strings. +2011-01-22 Stefan Bidigaray + + * Source/NSDateFormatter.m: Initialize UDateFormat before using it. + * Source/NSCalendar.m: Implement -components:fromDate:. + 2011-01-22 Stefan Bidigaray * Source/NSDateFormatter.m: Implemented a few more methods. diff --git a/Source/NSCalendar.m b/Source/NSCalendar.m index c0df8b48b..697773f99 100644 --- a/Source/NSCalendar.m +++ b/Source/NSCalendar.m @@ -203,6 +203,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit) _localeId = RETAIN([self _localeIdWithLocale: [NSLocale currentLocale]]); _tz = RETAIN([NSTimeZone defaultTimeZone]); + [self _openCalendar]; return self; } @@ -225,7 +226,42 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit) - (NSDateComponents *) components: (NSUInteger) unitFlags fromDate: (NSDate *) date { +#if GS_USE_ICU == 1 + NSDateComponents *comps; + UErrorCode err = U_ZERO_ERROR; + UDate udate; + + udate = (UDate)floor([date timeIntervalSince1970] * 1000.0); + ucal_setMillis (_cal, udate, &err); + if (U_FAILURE(err)) + return nil; + + comps = [[NSDateComponents alloc] init]; + if (unitFlags & NSEraCalendarUnit) + [comps setEra: ucal_get (_cal, UCAL_ERA, &err)]; + if (unitFlags & NSYearCalendarUnit) + [comps setYear: ucal_get (_cal, UCAL_YEAR, &err)]; + if (unitFlags & NSMonthCalendarUnit) + [comps setMonth: ucal_get (_cal, UCAL_MONTH, &err)]; + if (unitFlags & NSDayCalendarUnit) + [comps setDay: ucal_get (_cal, UCAL_DAY_OF_MONTH, &err)]; + if (unitFlags & NSHourCalendarUnit) + [comps setHour: ucal_get (_cal, UCAL_HOUR_OF_DAY, &err)]; + if (unitFlags & NSMinuteCalendarUnit) + [comps setMinute: ucal_get (_cal, UCAL_MINUTE, &err)]; + if (unitFlags & NSSecondCalendarUnit) + [comps setSecond: ucal_get (_cal, UCAL_SECOND, &err)]; + if (unitFlags & NSWeekCalendarUnit) + [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)]; + + return AUTORELEASE(comps); +#else return nil; +#endif } - (NSDateComponents *) components: (NSUInteger) unitFlags diff --git a/Source/NSDateFormatter.m b/Source/NSDateFormatter.m index 46614d07d..69d860f79 100644 --- a/Source/NSDateFormatter.m +++ b/Source/NSDateFormatter.m @@ -88,6 +88,8 @@ static NSDateFormatterBehavior _defaultBehavior = 0; _locale = RETAIN([NSLocale currentLocale]); _tz = RETAIN([NSTimeZone defaultTimeZone]); + [self _resetUDateFormat]; + /* According to Apple docs, default behavior is NSDateFormatterBehavior10_4 on 10.5 and later. Yeah, go figure. */ #if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST) && GS_USE_ICU == 1 @@ -107,8 +109,6 @@ static NSDateFormatterBehavior _defaultBehavior = 0; NSZoneFree ([self zone], value); #endif - [self _resetUDateFormat]; - return self; }