diff --git a/ChangeLog b/ChangeLog index 38d096b8f..1378cae75 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2011-02-09 Stefan Bidigaray + + * Source/NSTimeZone.m: ([-localizedName:locale:]) Implemented method. + 2011-02-09 Stefan Bidigaray * configure: diff --git a/Source/NSTimeZone.m b/Source/NSTimeZone.m index 3fdfb613a..05ff3f4e5 100644 --- a/Source/NSTimeZone.m +++ b/Source/NSTimeZone.m @@ -99,6 +99,7 @@ #import "Foundation/NSPortCoder.h" #import "Foundation/NSTimeZone.h" #import "Foundation/NSByteOrder.h" +#import "Foundation/NSLocale.h" #import "GNUstepBase/GSConfig.h" #import "GNUstepBase/NSObject+GNUstepBase.h" #import "GNUstepBase/NSString+GNUstepBase.h" @@ -111,6 +112,10 @@ #include "nstzfile.h" #endif +#if defined(HAVE_UNICODE_UCAL_H) +#include +#endif + NSString * const NSSystemTimeZoneDidChangeNotification = @"NSSystemTimeZoneDidChangeNotification"; @@ -152,6 +157,28 @@ NSString * const NSSystemTimeZoneDidChangeNotification #define POSIX_TZONES @"posix/" #endif +static inline int +_NSToICUTZDisplayStyle(NSTimeZoneNameStyle style) +{ +#if GS_USE_ICU == 1 + switch (style) + { + case NSTimeZoneNameStyleStandard: + return UCAL_STANDARD; + case NSTimeZoneNameStyleShortStandard: + return UCAL_SHORT_STANDARD; + case NSTimeZoneNameStyleDaylightSaving: + return UCAL_DST; + case NSTimeZoneNameStyleShortDaylightSaving: + return UCAL_SHORT_DST; + default: + return -1; + } +#else + return -1; +#endif +} + /* Possible location of system time zone files */ static NSString *tzdir = nil; @@ -2092,7 +2119,42 @@ localZoneString, [zone name], sign, s/3600, (s/60)%60); - (NSString *)localizedName: (NSTimeZoneNameStyle)style locale: (NSLocale *)locale { +#if GS_USE_ICU == 1 +#define BUFFER_SIZE 512 + NSString *tzStr; + int32_t tzLen; + int32_t len; + const char *cLocale; + UChar *result; + UChar tzName[BUFFER_SIZE]; + UCalendar *cal; + UErrorCode err = U_ZERO_ERROR; + + tzStr = [self name]; + if ((tzLen = [tzStr length]) > BUFFER_SIZE) + tzLen = BUFFER_SIZE; + [tzStr getCharacters: tzName range: NSMakeRange(0, tzLen)]; + cLocale = [[locale localeIdentifier] UTF8String]; + + cal = ucal_open(tzName, tzLen, cLocale, UCAL_TRADITIONAL, &err); + if (U_FAILURE(err)) + return nil; + + result = NSZoneMalloc ([self zone], BUFFER_SIZE * sizeof(UChar)); + len = ucal_getTimeZoneDisplayName (cal, _NSToICUTZDisplayStyle(style), + cLocale, result, BUFFER_SIZE, &err); + if (len > BUFFER_SIZE) + { + result = NSZoneRealloc ([self zone], result, len * sizeof(UChar)); + ucal_getTimeZoneDisplayName (cal, _NSToICUTZDisplayStyle(style), + cLocale, result, len, &err); + } + + return AUTORELEASE([[NSString alloc] initWithCharactersNoCopy: result + length: len freeWhenDone: YES]); +#else return nil; // FIXME; +#endif } @end