rv = nsComponentManager::CreateInstance(kLocaleServiceCID, NULL, nsILocaleService::GetIID(), (void**)&localeService);
NS_WITH_SERVICE(nsILocaleService, localeService, kLocaleServiceCID, &rv);
NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime(                            const PRUnichar *locale,                             nsDateFormatSelector dateFormatSelector,                             nsTimeFormatSelector timeFormatSelector,                             PRInt32 year,                             PRInt32 month,                             PRInt32 day,                             PRInt32 hour,                             PRInt32 minute,                             PRInt32 second,                             PRUnichar **dateTimeString){  nsILocaleService *localeService;  nsILocale* aLocale;   nsString localeName(locale);  nsresult rv;  *dateTimeString = NULL;  // get locale service   rv = nsComponentManager::CreateInstance(kLocaleServiceCID, NULL,                                            nsILocaleService::GetIID(), (void**)&localeService);  if (NS_SUCCEEDED(rv)) {    rv = localeName.Length() ? localeService->NewLocale(localeName.GetUnicode(), &aLocale) :                               localeService->GetApplicationLocale(&aLocale);    localeService->Release();    if (NS_SUCCEEDED(rv) && aLocale) {      nsIDateTimeFormat *aDateTimeFormat;    	rv = nsComponentManager::CreateInstance(kDateTimeFormatCID, NULL,                                              nsIDateTimeFormat::GetIID(), (void **) &aDateTimeFormat);      if (NS_SUCCEEDED(rv) && aDateTimeFormat) {        struct tm tmTime;        time_t  timetTime;        nsCRT::memset( &tmTime, 0, sizeof(tmTime) );        tmTime.tm_year = year - 1900;        tmTime.tm_mon = month - 1;        tmTime.tm_mday = day;        tmTime.tm_hour = hour;        tmTime.tm_min = minute;        tmTime.tm_sec = second;        tmTime.tm_yday = tmTime.tm_wday = 0;        tmTime.tm_isdst = -1;        timetTime = mktime(&tmTime);        if ((time_t)-1 != timetTime) {          rv = aDateTimeFormat->FormatTime(aLocale, dateFormatSelector, timeFormatSelector,                                            timetTime, mStringOut);          if (NS_SUCCEEDED(rv)) {            *dateTimeString = mStringOut.ToNewUnicode();          }        }        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,                                                prtime, mStringOut);            if (NS_SUCCEEDED(rv)) {              *dateTimeString = mStringOut.ToNewUnicode();            }          }        }        NS_RELEASE(aDateTimeFormat);      }      NS_RELEASE(aLocale);    }  }  return rv;}