Implemented [NSTimeZone-localizedName:locale:].

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@32025 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Stefan Bidigaray 2011-02-10 02:20:57 +00:00
parent ac4a764b44
commit 88e468e8bf
2 changed files with 66 additions and 0 deletions

View file

@ -1,3 +1,7 @@
2011-02-09 Stefan Bidigaray <stefanbidi@gmail.com>
* Source/NSTimeZone.m: ([-localizedName:locale:]) Implemented method.
2011-02-09 Stefan Bidigaray <stefanbidi@gmail.com>
* configure:

View file

@ -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 <unicode/ucal.h>
#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