michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsILocaleService.h" michael@0: #include "nsDateTimeFormatCID.h" michael@0: #include "nsIDateTimeFormat.h" michael@0: #include "nsIScriptableDateFormat.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: michael@0: static NS_DEFINE_CID(kLocaleServiceCID, NS_LOCALESERVICE_CID); michael@0: static NS_DEFINE_CID(kDateTimeFormatCID, NS_DATETIMEFORMAT_CID); michael@0: michael@0: class nsScriptableDateFormat : public nsIScriptableDateFormat { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: NS_IMETHOD FormatDateTime(const char16_t *locale, michael@0: nsDateFormatSelector dateFormatSelector, michael@0: nsTimeFormatSelector timeFormatSelector, michael@0: int32_t year, michael@0: int32_t month, michael@0: int32_t day, michael@0: int32_t hour, michael@0: int32_t minute, michael@0: int32_t second, michael@0: char16_t **dateTimeString); michael@0: michael@0: NS_IMETHOD FormatDate(const char16_t *locale, michael@0: nsDateFormatSelector dateFormatSelector, michael@0: int32_t year, michael@0: int32_t month, michael@0: int32_t day, michael@0: char16_t **dateString) michael@0: {return FormatDateTime(locale, dateFormatSelector, kTimeFormatNone, michael@0: year, month, day, 0, 0, 0, dateString);} michael@0: michael@0: NS_IMETHOD FormatTime(const char16_t *locale, michael@0: nsTimeFormatSelector timeFormatSelector, michael@0: int32_t hour, michael@0: int32_t minute, michael@0: int32_t second, michael@0: char16_t **timeString) michael@0: {return FormatDateTime(locale, kDateFormatNone, timeFormatSelector, michael@0: 1999, 1, 1, hour, minute, second, timeString);} michael@0: michael@0: nsScriptableDateFormat() {} michael@0: virtual ~nsScriptableDateFormat() {} michael@0: private: michael@0: nsString mStringOut; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsScriptableDateFormat, nsIScriptableDateFormat) michael@0: michael@0: NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime( michael@0: const char16_t *aLocale, michael@0: nsDateFormatSelector dateFormatSelector, michael@0: nsTimeFormatSelector timeFormatSelector, michael@0: int32_t year, michael@0: int32_t month, michael@0: int32_t day, michael@0: int32_t hour, michael@0: int32_t minute, michael@0: int32_t second, michael@0: char16_t **dateTimeString) michael@0: { michael@0: // We can't have a valid date with the year, month or day michael@0: // being lower than 1. michael@0: if (year < 1 || month < 1 || day < 1) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsresult rv; michael@0: nsAutoString localeName(aLocale); michael@0: *dateTimeString = nullptr; michael@0: michael@0: nsCOMPtr locale; michael@0: // re-initialise locale pointer only if the locale was given explicitly michael@0: if (!localeName.IsEmpty()) { michael@0: // get locale service michael@0: nsCOMPtr localeService(do_GetService(kLocaleServiceCID, &rv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: // get locale michael@0: rv = localeService->NewLocale(localeName, getter_AddRefs(locale)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: nsCOMPtr dateTimeFormat(do_CreateInstance(kDateTimeFormatCID, &rv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: tm tmTime; michael@0: time_t timetTime; michael@0: michael@0: memset(&tmTime, 0, sizeof(tmTime)); michael@0: tmTime.tm_year = year - 1900; michael@0: tmTime.tm_mon = month - 1; michael@0: tmTime.tm_mday = day; michael@0: tmTime.tm_hour = hour; michael@0: tmTime.tm_min = minute; michael@0: tmTime.tm_sec = second; michael@0: tmTime.tm_yday = tmTime.tm_wday = 0; michael@0: tmTime.tm_isdst = -1; michael@0: timetTime = mktime(&tmTime); michael@0: michael@0: if ((time_t)-1 != timetTime) { michael@0: rv = dateTimeFormat->FormatTime(locale, dateFormatSelector, timeFormatSelector, michael@0: timetTime, mStringOut); michael@0: } michael@0: else { michael@0: // if mktime fails (e.g. year <= 1970), then try NSPR. michael@0: PRTime prtime; michael@0: char string[32]; michael@0: sprintf(string, "%.2d/%.2d/%d %.2d:%.2d:%.2d", month, day, year, hour, minute, second); michael@0: if (PR_SUCCESS != PR_ParseTimeString(string, false, &prtime)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: rv = dateTimeFormat->FormatPRTime(locale, dateFormatSelector, timeFormatSelector, michael@0: prtime, mStringOut); michael@0: } michael@0: if (NS_SUCCEEDED(rv)) michael@0: *dateTimeString = ToNewUnicode(mStringOut); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: nsresult michael@0: NS_NewScriptableDateFormat(nsISupports* aOuter, REFNSIID aIID, void** aResult) michael@0: { michael@0: if (aOuter) michael@0: return NS_ERROR_NO_AGGREGATION; michael@0: michael@0: nsScriptableDateFormat* result = new nsScriptableDateFormat(); michael@0: if (! result) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: NS_ADDREF(result); michael@0: nsresult rv = result->QueryInterface(aIID, aResult); michael@0: NS_RELEASE(result); michael@0: michael@0: return rv; michael@0: }