Bug 163665 cleanup nsScriptableDateFormat::FormatDateTime

r=jshin sr=roc
This commit is contained in:
timeless%mozdev.org 2004-02-25 15:58:18 +00:00
Родитель 415f6f0546
Коммит 48b04aa18e
1 изменённых файлов: 48 добавлений и 50 удалений

Просмотреть файл

@ -92,7 +92,7 @@ private:
NS_IMPL_ISUPPORTS1(nsScriptableDateFormat, nsIScriptableDateFormat)
NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime(
const PRUnichar *locale,
const PRUnichar *aLocale,
nsDateFormatSelector dateFormatSelector,
nsTimeFormatSelector timeFormatSelector,
PRInt32 year,
@ -103,23 +103,27 @@ NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime(
PRInt32 second,
PRUnichar **dateTimeString)
{
nsCOMPtr<nsILocale> aLocale;
nsresult rv;
*dateTimeString = NULL;
nsAutoString localeName(aLocale);
*dateTimeString = nsnull;
// get locale service
nsCOMPtr<nsILocaleService> localeService =
do_GetService(kLocaleServiceCID, &rv);
if (NS_SUCCEEDED(rv)) {
rv = (locale && *locale) ?
localeService->NewLocale(nsDependentString(locale), getter_AddRefs(aLocale)) :
localeService->GetApplicationLocale(getter_AddRefs(aLocale));
nsCOMPtr<nsILocaleService> localeService = do_GetService(kLocaleServiceCID);
if (!localeService)
return NS_ERROR_NOT_AVAILABLE;
if (NS_SUCCEEDED(rv) && aLocale) {
nsCOMPtr<nsIDateTimeFormat> aDateTimeFormat = do_CreateInstance(kDateTimeFormatCID);
nsCOMPtr<nsILocale> locale;
if (localeName.IsEmpty())
localeService->GetApplicationLocale(getter_AddRefs(locale));
else
localeService->NewLocale(localeName, getter_AddRefs(locale));
if (aDateTimeFormat) {
struct tm tmTime;
if (!locale)
return NS_ERROR_NOT_AVAILABLE;
nsCOMPtr<nsIDateTimeFormat> dateTimeFormat = do_CreateInstance(kDateTimeFormatCID);
if (!dateTimeFormat)
return NS_ERROR_NOT_AVAILABLE;
tm tmTime;
time_t timetTime;
memset(&tmTime, 0, sizeof(tmTime));
@ -132,32 +136,26 @@ NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime(
tmTime.tm_yday = tmTime.tm_wday = 0;
tmTime.tm_isdst = -1;
timetTime = mktime(&tmTime);
nsresult rv;
if ((time_t)-1 != timetTime) {
rv = aDateTimeFormat->FormatTime(aLocale, dateFormatSelector, timeFormatSelector,
rv = dateTimeFormat->FormatTime(locale, dateFormatSelector, timeFormatSelector,
timetTime, mStringOut);
if (NS_SUCCEEDED(rv)) {
*dateTimeString = ToNewUnicode(mStringOut);
}
}
else {
// if mktime fails (e.g. year <= 1970), then try NSPR.
PRTime prtime;
char string[32];
sprintf(string, "%.2d/%.2d/%d %.2d:%.2d:%.2d", month, day, year, hour, minute, second);
if (PR_SUCCESS != PR_ParseTimeString(string, PR_FALSE, &prtime)) {
rv = NS_ERROR_ILLEGAL_VALUE; // invalid arg value
}
else {
rv = aDateTimeFormat->FormatPRTime(aLocale, dateFormatSelector, timeFormatSelector,
if (PR_SUCCESS != PR_ParseTimeString(string, PR_FALSE, &prtime))
return NS_ERROR_ILLEGAL_VALUE; // invalid arg value
rv = dateTimeFormat->FormatPRTime(locale, dateFormatSelector, timeFormatSelector,
prtime, mStringOut);
if (NS_SUCCEEDED(rv)) {
}
if (NS_SUCCEEDED(rv))
*dateTimeString = ToNewUnicode(mStringOut);
}
}
}
}
}
}
return rv;
}