Added locale sensitive hanlding for 12/24 hour format, bug 27640, written by chjung@netscape.com, r=nhotta.

Changed to use do_GetService instead of CreateInstance, bug 30203, r=cata.


git-svn-id: svn://10.0.0.236/trunk@63179 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
nhotta%netscape.com
2000-03-16 22:59:43 +00:00
parent 4c51b7d5db
commit 5a83b0ef42
2 changed files with 39 additions and 8 deletions

View File

@@ -93,16 +93,12 @@ nsresult nsDateTimeFormatUnix::Initialize(nsILocale* locale)
mLocale.SetString(aLocaleUnichar); // cache locale name
nsAllocator::Free(aLocaleUnichar);
nsCOMPtr <nsIPosixLocale> posixLocale;
res = nsComponentManager::CreateInstance(kPosixLocaleFactoryCID, NULL,
NS_GET_IID(nsIPosixLocale), getter_AddRefs(posixLocale));
nsCOMPtr <nsIPosixLocale> posixLocale = do_GetService(kPosixLocaleFactoryCID, &res);
if (NS_SUCCEEDED(res)) {
res = posixLocale->GetPlatformLocale(&mLocale, mPlatformLocale, kPlatformLocaleLength+1);
}
nsCOMPtr <nsIPlatformCharset> platformCharset;
res = nsComponentManager::CreateInstance(kPlatformCharsetCID, NULL,
NS_GET_IID(nsIPlatformCharset), getter_AddRefs(platformCharset));
nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_PROGID, &res);
if (NS_SUCCEEDED(res)) {
PRUnichar* mappedCharset = NULL;
res = platformCharset->GetDefaultCharsetForLocale(mLocale.GetUnicode(), &mappedCharset);
@@ -113,9 +109,37 @@ nsresult nsDateTimeFormatUnix::Initialize(nsILocale* locale)
}
}
mLocalePreferred24hour = LocalePreferred24hour();
return res;
}
PRBool nsDateTimeFormatUnix::LocalePreferred24hour()
{
char str[100];
time_t tt;
struct tm *tmc;
int i;
tt = time((time_t)NULL);
tmc = localtime(&tt);
tmc->tm_hour=22; // put the test sample hour to 22:00 which is 10PM
tmc->tm_min=0; // set the min & sec other number than '2'
tmc->tm_sec=0;
char *temp = setlocale(LC_TIME, mPlatformLocale);
strftime(str, (size_t)99, "%X", (struct tm *)tmc);
(void) setlocale(LC_TIME, temp);
for (i=0; str[i]; i++)
if (str[i] == '2') { // if there is any '2', that locale use 0-23 time format
return PR_TRUE;
}
return PR_FALSE;
}
nsresult nsDateTimeFormatUnix::FormatTime(nsILocale* locale,
const nsDateFormatSelector dateFormatSelector,
const nsTimeFormatSelector timeFormatSelector,
@@ -168,10 +192,14 @@ nsresult nsDateTimeFormatUnix::FormatTMTime(nsILocale* locale,
PL_strncpy(fmtT, "", NSDATETIME_FORMAT_BUFFER_LEN);
break;
case kTimeFormatSeconds:
PL_strncpy(fmtT, "%I:%M:%S %p", NSDATETIME_FORMAT_BUFFER_LEN);
PL_strncpy(fmtT,
mLocalePreferred24hour ? "%H:%M:%S" : "%I:%M:%S %p",
NSDATETIME_FORMAT_BUFFER_LEN);
break;
case kTimeFormatNoSeconds:
PL_strncpy(fmtT, "%I:%M %p", NSDATETIME_FORMAT_BUFFER_LEN);
PL_strncpy(fmtT,
mLocalePreferred24hour ? "%H:%M" : "%I:%M %p",
NSDATETIME_FORMAT_BUFFER_LEN);
break;
case kTimeFormatSecondsForce24Hour:
PL_strncpy(fmtT, "%H:%M:%S", NSDATETIME_FORMAT_BUFFER_LEN);

View File

@@ -71,10 +71,13 @@ private:
// init this interface to a specified locale
NS_IMETHOD Initialize(nsILocale* locale);
PRBool LocalePreferred24hour();
nsString mLocale;
nsString mAppLocale;
nsString mCharset; // in order to convert API result to unicode
char mPlatformLocale[kPlatformLocaleLength+1]; // for setlocale
PRBool mLocalePreferred24hour; // true if 24 hour format is preferred by current locale
};
#endif /* nsDateTimeFormatUnix_h__ */