diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index 380d33ea3d2f..663a529ee1d9 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -131,8 +131,6 @@ #include "nsIParser.h" #include "nsIContentSink.h" -#include "nsDateTimeFormatCID.h" -#include "nsIDateTimeFormat.h" #include "mozilla/EventDispatcher.h" #include "mozilla/EventStates.h" #include "mozilla/InternalMutationEvent.h" diff --git a/dom/xul/templates/nsXULContentUtils.cpp b/dom/xul/templates/nsXULContentUtils.cpp index 5e128b42ed1c..a5f00473c9a1 100644 --- a/dom/xul/templates/nsXULContentUtils.cpp +++ b/dom/xul/templates/nsXULContentUtils.cpp @@ -27,6 +27,7 @@ #include "mozilla/ArrayUtils.h" +#include "DateTimeFormat.h" #include "nsCOMPtr.h" #include "nsIContent.h" #include "nsIDocument.h" @@ -48,7 +49,6 @@ #include "prtime.h" #include "rdf.h" #include "nsContentUtils.h" -#include "nsIDateTimeFormat.h" #include "nsIScriptableDateFormat.h" #include "nsICollation.h" #include "nsCollationCID.h" @@ -62,7 +62,6 @@ using namespace mozilla; //------------------------------------------------------------------------ nsIRDFService* nsXULContentUtils::gRDF; -nsIDateTimeFormat* nsXULContentUtils::gFormat; nsICollation *nsXULContentUtils::gCollation; extern LazyLogModule gXULTemplateLog; @@ -102,11 +101,6 @@ nsXULContentUtils::Init() #undef XUL_RESOURCE #undef XUL_LITERAL - gFormat = nsIDateTimeFormat::Create().take(); - if (!gFormat) { - return NS_ERROR_FAILURE; - } - return NS_OK; } @@ -122,7 +116,6 @@ nsXULContentUtils::Finish() #undef XUL_RESOURCE #undef XUL_LITERAL - NS_IF_RELEASE(gFormat); NS_IF_RELEASE(gCollation); return NS_OK; @@ -214,11 +207,11 @@ nsXULContentUtils::GetTextForNode(nsIRDFNode* aNode, nsAString& aResult) if (NS_FAILED(rv)) return rv; nsAutoString str; - rv = gFormat->FormatPRTime(nullptr /* nsILocale* locale */, - kDateFormatShort, - kTimeFormatSeconds, - value, - str); + rv = DateTimeFormat::FormatPRTime(kDateFormatShort, + kTimeFormatSeconds, + value, + str); + aResult.Assign(str); if (NS_FAILED(rv)) return rv; diff --git a/dom/xul/templates/nsXULContentUtils.h b/dom/xul/templates/nsXULContentUtils.h index c106fd91ec6e..dad0af1d488e 100644 --- a/dom/xul/templates/nsXULContentUtils.h +++ b/dom/xul/templates/nsXULContentUtils.h @@ -21,7 +21,6 @@ class nsIRDFNode; class nsIRDFResource; class nsIRDFLiteral; class nsIRDFService; -class nsIDateTimeFormat; class nsICollation; // errors to pass to LogTemplateError @@ -86,7 +85,6 @@ class nsXULContentUtils { protected: static nsIRDFService* gRDF; - static nsIDateTimeFormat* gFormat; static nsICollation *gCollation; static bool gDisableXULCache; diff --git a/intl/build/nsI18nModule.cpp b/intl/build/nsI18nModule.cpp index 9959d3d6fb37..7512763917bd 100644 --- a/intl/build/nsI18nModule.cpp +++ b/intl/build/nsI18nModule.cpp @@ -61,15 +61,12 @@ NS_DEFINE_NAMED_CID(NS_LANGUAGEATOMSERVICE_CID); NS_DEFINE_NAMED_CID(NS_PLATFORMCHARSET_CID); #ifdef XP_WIN NS_DEFINE_NAMED_CID(NS_COLLATION_CID); -NS_DEFINE_NAMED_CID(NS_DATETIMEFORMAT_CID); #endif #ifdef USE_UNIX_LOCALE NS_DEFINE_NAMED_CID(NS_COLLATION_CID); -NS_DEFINE_NAMED_CID(NS_DATETIMEFORMAT_CID); #endif #ifdef USE_MAC_LOCALE NS_DEFINE_NAMED_CID(NS_COLLATION_CID); -NS_DEFINE_NAMED_CID(NS_DATETIMEFORMAT_CID); #endif static const mozilla::Module::CIDEntry kIntlCIDs[] = { @@ -90,15 +87,12 @@ static const mozilla::Module::CIDEntry kIntlCIDs[] = { { &kNS_PLATFORMCHARSET_CID, false, nullptr, nsPlatformCharsetConstructor }, #ifdef XP_WIN { &kNS_COLLATION_CID, false, nullptr, nsCollationWinConstructor }, - { &kNS_DATETIMEFORMAT_CID, false, nullptr, nsDateTimeFormatWinConstructor }, #endif #ifdef USE_UNIX_LOCALE { &kNS_COLLATION_CID, false, nullptr, nsCollationUnixConstructor }, - { &kNS_DATETIMEFORMAT_CID, false, nullptr, nsDateTimeFormatUnixConstructor }, #endif #ifdef USE_MAC_LOCALE { &kNS_COLLATION_CID, false, nullptr, nsCollationMacUCConstructor }, - { &kNS_DATETIMEFORMAT_CID, false, nullptr, nsDateTimeFormatMacConstructor }, #endif { nullptr } }; @@ -121,15 +115,12 @@ static const mozilla::Module::ContractIDEntry kIntlContracts[] = { { NS_PLATFORMCHARSET_CONTRACTID, &kNS_PLATFORMCHARSET_CID }, #ifdef XP_WIN { NS_COLLATION_CONTRACTID, &kNS_COLLATION_CID }, - { NS_DATETIMEFORMAT_CONTRACTID, &kNS_DATETIMEFORMAT_CID }, #endif #ifdef USE_UNIX_LOCALE { NS_COLLATION_CONTRACTID, &kNS_COLLATION_CID }, - { NS_DATETIMEFORMAT_CONTRACTID, &kNS_DATETIMEFORMAT_CID }, #endif #ifdef USE_MAC_LOCALE { NS_COLLATION_CONTRACTID, &kNS_COLLATION_CID }, - { NS_DATETIMEFORMAT_CONTRACTID, &kNS_DATETIMEFORMAT_CID }, #endif { nullptr } }; diff --git a/intl/locale/DateTimeFormat.h b/intl/locale/DateTimeFormat.h new file mode 100644 index 000000000000..afb142110cbc --- /dev/null +++ b/intl/locale/DateTimeFormat.h @@ -0,0 +1,68 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_DateTimeFormat_h +#define mozilla_DateTimeFormat_h + +#include +#include "nsIScriptableDateFormat.h" +#include "nsStringGlue.h" +#include "prtime.h" + +#ifndef ENABLE_INTL_API +#include "nsCOMPtr.h" +#include "nsIUnicodeDecoder.h" +#endif + +namespace mozilla { + +class DateTimeFormat { +public: + // performs a locale sensitive date formatting operation on the time_t parameter + static nsresult FormatTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const time_t aTimetTime, + nsAString& aStringOut); + + // performs a locale sensitive date formatting operation on the PRTime parameter + static nsresult FormatPRTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const PRTime aPrTime, + nsAString& aStringOut); + + // performs a locale sensitive date formatting operation on the PRExplodedTime parameter + static nsresult FormatPRExplodedTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const PRExplodedTime* aExplodedTime, + nsAString& aStringOut); + + static void Shutdown(); + +private: + DateTimeFormat() = delete; + + static nsresult Initialize(); + +#ifdef ENABLE_INTL_API + static nsCString* mLocale; +#else + // performs a locale sensitive date formatting operation on the struct tm parameter + static nsresult FormatTMTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const struct tm* aTmTime, + nsAString& aStringOut); + + static void LocalePreferred24hour(); + + static bool mLocalePreferred24hour; // true if 24 hour format is preferred by current locale + static bool mLocaleAMPMfirst; // true if AM/PM string is preferred before the time + static nsCOMPtr mDecoder; +#endif +}; + +} + +#endif /* mozilla_DateTimeFormat_h */ diff --git a/intl/locale/DateTimeFormatICU.cpp b/intl/locale/DateTimeFormatICU.cpp new file mode 100644 index 000000000000..b07a8b0b74d1 --- /dev/null +++ b/intl/locale/DateTimeFormatICU.cpp @@ -0,0 +1,161 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "DateTimeFormat.h" +#include "nsCOMPtr.h" +#include "nsIServiceManager.h" +#include "nsILocaleService.h" +#include "unicode/udat.h" + +namespace mozilla { + +nsCString* DateTimeFormat::mLocale = nullptr; + +/*static*/ nsresult +DateTimeFormat::Initialize() +{ + nsAutoString localeStr; + nsresult rv = NS_OK; + + if (!mLocale) { + mLocale = new nsCString(); + } else if (!mLocale->IsEmpty()) { + return NS_OK; + } + + nsCOMPtr localeService = + do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv); + if (NS_SUCCEEDED(rv)) { + nsCOMPtr appLocale; + rv = localeService->GetApplicationLocale(getter_AddRefs(appLocale)); + if (NS_SUCCEEDED(rv)) { + rv = appLocale->GetCategory(NS_LITERAL_STRING("NSILOCALE_TIME"), localeStr); + if (NS_SUCCEEDED(rv) && !localeStr.IsEmpty()) { + *mLocale = NS_LossyConvertUTF16toASCII(localeStr); // cache locale name + } + } + } + + return rv; +} + +// performs a locale sensitive date formatting operation on the time_t parameter +/*static*/ nsresult +DateTimeFormat::FormatTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const time_t aTimetTime, + nsAString& aStringOut) +{ + return FormatPRTime(aDateFormatSelector, aTimeFormatSelector, (aTimetTime * PR_USEC_PER_SEC), aStringOut); +} + +// performs a locale sensitive date formatting operation on the PRTime parameter +/*static*/ nsresult +DateTimeFormat::FormatPRTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const PRTime aPrTime, + nsAString& aStringOut) +{ +#define DATETIME_FORMAT_INITIAL_LEN 127 + int32_t dateTimeLen = 0; + nsresult rv = NS_OK; + + // return, nothing to format + if (aDateFormatSelector == kDateFormatNone && aTimeFormatSelector == kTimeFormatNone) { + aStringOut.Truncate(); + return NS_OK; + } + + // set up locale data + rv = Initialize(); + + if (NS_FAILED(rv)) { + return rv; + } + + UDate timeUDate = aPrTime / PR_USEC_PER_MSEC; + + // Get the date style for the formatter: + UDateFormatStyle dateStyle; + switch (aDateFormatSelector) { + case kDateFormatLong: + dateStyle = UDAT_LONG; + break; + case kDateFormatShort: + dateStyle = UDAT_SHORT; + break; + case kDateFormatNone: + dateStyle = UDAT_NONE; + break; + default: + NS_ERROR("Unknown nsDateFormatSelector"); + return NS_ERROR_ILLEGAL_VALUE; + } + + // Get the time style for the formatter: + UDateFormatStyle timeStyle; + switch (aTimeFormatSelector) { + case kTimeFormatSeconds: + timeStyle = UDAT_MEDIUM; + break; + case kTimeFormatNoSeconds: + timeStyle = UDAT_SHORT; + break; + case kTimeFormatNone: + timeStyle = UDAT_NONE; + break; + default: + NS_ERROR("Unknown nsTimeFormatSelector"); + return NS_ERROR_ILLEGAL_VALUE; + } + + // generate date/time string + + UErrorCode status = U_ZERO_ERROR; + + UDateFormat* dateTimeFormat = udat_open(timeStyle, dateStyle, mLocale->get(), nullptr, -1, nullptr, -1, &status); + + if (U_SUCCESS(status) && dateTimeFormat) { + aStringOut.SetLength(DATETIME_FORMAT_INITIAL_LEN); + dateTimeLen = udat_format(dateTimeFormat, timeUDate, reinterpret_cast(aStringOut.BeginWriting()), DATETIME_FORMAT_INITIAL_LEN, nullptr, &status); + aStringOut.SetLength(dateTimeLen); + + if (status == U_BUFFER_OVERFLOW_ERROR) { + status = U_ZERO_ERROR; + udat_format(dateTimeFormat, timeUDate, reinterpret_cast(aStringOut.BeginWriting()), dateTimeLen, nullptr, &status); + } + } + + if (U_FAILURE(status)) { + rv = NS_ERROR_FAILURE; + } + + if (dateTimeFormat) { + udat_close(dateTimeFormat); + } + + return rv; +} + +// performs a locale sensitive date formatting operation on the PRExplodedTime parameter +/*static*/ nsresult +DateTimeFormat::FormatPRExplodedTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const PRExplodedTime* aExplodedTime, + nsAString& aStringOut) +{ + return FormatPRTime(aDateFormatSelector, aTimeFormatSelector, PR_ImplodeTime(aExplodedTime), aStringOut); +} + +/*static*/ void +DateTimeFormat::Shutdown() +{ + if (mLocale) { + delete mLocale; + } +} + +} diff --git a/intl/locale/DateTimeFormatUnix.cpp b/intl/locale/DateTimeFormatUnix.cpp new file mode 100644 index 000000000000..4516f850e806 --- /dev/null +++ b/intl/locale/DateTimeFormatUnix.cpp @@ -0,0 +1,239 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "DateTimeFormat.h" +#include "plstr.h" +#include "nsIServiceManager.h" +#include "nsILocaleService.h" +#include "nsIPlatformCharset.h" +#include "mozilla/dom/EncodingUtils.h" + +using mozilla::dom::EncodingUtils; + +namespace mozilla { + +bool DateTimeFormat::mLocalePreferred24hour; +bool DateTimeFormat::mLocaleAMPMfirst; +nsCOMPtr DateTimeFormat::mDecoder; + +/*static*/ nsresult +DateTimeFormat::Initialize() +{ + nsAutoString localeStr; + nsAutoCString charset; + nsresult rv = NS_OK; + + if (mDecoder) { + return NS_OK; + } + + charset.AssignLiteral("windows-1252"); + + nsCOMPtr localeService = + do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv); + if (NS_SUCCEEDED(rv)) { + nsCOMPtr appLocale; + rv = localeService->GetApplicationLocale(getter_AddRefs(appLocale)); + if (NS_SUCCEEDED(rv)) { + rv = appLocale->GetCategory(NS_LITERAL_STRING("NSILOCALE_TIME##PLATFORM"), localeStr); + NS_ASSERTION(NS_SUCCEEDED(rv), "failed to get app locale info"); + } + } + + if (NS_SUCCEEDED(rv) && !localeStr.IsEmpty()) { + nsCOMPtr platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv); + if (NS_SUCCEEDED(rv)) { + nsAutoCString mappedCharset; + rv = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset); + if (NS_SUCCEEDED(rv)) { + charset = mappedCharset; + } + } + } + + mDecoder = EncodingUtils::DecoderForEncoding(charset); + + LocalePreferred24hour(); + + return rv; +} + +/*static*/ void +DateTimeFormat::LocalePreferred24hour() +{ + char str[100]; + time_t tt; + struct tm *tmc; + int i; + + tt = time(nullptr); + 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; + + strftime(str, (size_t)99, "%X", (struct tm *)tmc); + + mLocalePreferred24hour = false; + for (i=0; str[i]; i++) { + if (str[i] == '2') { // if there is any '2', that locale use 0-23 time format + mLocalePreferred24hour = true; + break; + } + } + + mLocaleAMPMfirst = true; + if (mLocalePreferred24hour == false) { + if (str[0] && str[0] == '1') { // if the first character is '1' of 10:00, + // AMPM string is located after 10:00 + mLocaleAMPMfirst = false; + } + } +} + +/*static*/ nsresult +DateTimeFormat::FormatTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const time_t aTimetTime, + nsAString& aStringOut) +{ + struct tm tmTime; + memcpy(&tmTime, localtime(&aTimetTime), sizeof(struct tm)); + return FormatTMTime(aDateFormatSelector, aTimeFormatSelector, &tmTime, aStringOut); +} + +// performs a locale sensitive date formatting operation on the struct tm parameter +/*static*/ nsresult +DateTimeFormat::FormatTMTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const struct tm* aTmTime, + nsAString& aStringOut) +{ +#define NSDATETIME_FORMAT_BUFFER_LEN 80 + char strOut[NSDATETIME_FORMAT_BUFFER_LEN*2]; // buffer for date and time + char fmtD[NSDATETIME_FORMAT_BUFFER_LEN], fmtT[NSDATETIME_FORMAT_BUFFER_LEN]; + nsresult rv; + + // set up locale data + (void) Initialize(); + NS_ENSURE_TRUE(mDecoder, NS_ERROR_NOT_INITIALIZED); + + // set date format + if (aDateFormatSelector == kDateFormatLong && aTimeFormatSelector == kTimeFormatSeconds) { + PL_strncpy(fmtD, "%c", NSDATETIME_FORMAT_BUFFER_LEN); + PL_strncpy(fmtT, "", NSDATETIME_FORMAT_BUFFER_LEN); + } else { + + switch (aDateFormatSelector) { + case kDateFormatNone: + PL_strncpy(fmtD, "", NSDATETIME_FORMAT_BUFFER_LEN); + break; + case kDateFormatLong: + case kDateFormatShort: + PL_strncpy(fmtD, "%x", NSDATETIME_FORMAT_BUFFER_LEN); + break; + default: + PL_strncpy(fmtD, "", NSDATETIME_FORMAT_BUFFER_LEN); + } + + // set time format + switch (aTimeFormatSelector) { + case kTimeFormatNone: + PL_strncpy(fmtT, "", NSDATETIME_FORMAT_BUFFER_LEN); + break; + case kTimeFormatSeconds: + PL_strncpy(fmtT, "%X", NSDATETIME_FORMAT_BUFFER_LEN); + break; + case kTimeFormatNoSeconds: + PL_strncpy(fmtT, + mLocalePreferred24hour ? "%H:%M" : mLocaleAMPMfirst ? "%p %I:%M" : "%I:%M %p", + NSDATETIME_FORMAT_BUFFER_LEN); + break; + default: + PL_strncpy(fmtT, "", NSDATETIME_FORMAT_BUFFER_LEN); + } + } + + // generate date/time string + if (strlen(fmtD) && strlen(fmtT)) { + PL_strncat(fmtD, " ", NSDATETIME_FORMAT_BUFFER_LEN); + PL_strncat(fmtD, fmtT, NSDATETIME_FORMAT_BUFFER_LEN); + strftime(strOut, NSDATETIME_FORMAT_BUFFER_LEN, fmtD, aTmTime); + } else if (strlen(fmtD) && !strlen(fmtT)) { + strftime(strOut, NSDATETIME_FORMAT_BUFFER_LEN, fmtD, aTmTime); + } else if (!strlen(fmtD) && strlen(fmtT)) { + strftime(strOut, NSDATETIME_FORMAT_BUFFER_LEN, fmtT, aTmTime); + } else { + PL_strncpy(strOut, "", NSDATETIME_FORMAT_BUFFER_LEN); + } + + // convert result to unicode + int32_t srcLength = (int32_t) strlen(strOut); + int32_t unicharLength = NSDATETIME_FORMAT_BUFFER_LEN*2; + char16_t unichars[NSDATETIME_FORMAT_BUFFER_LEN*2]; // buffer for date and time + + rv = mDecoder->Convert(strOut, &srcLength, unichars, &unicharLength); + if (NS_FAILED(rv)) { + return rv; + } + aStringOut.Assign(unichars, unicharLength); + + return rv; +} + +// performs a locale sensitive date formatting operation on the PRTime parameter +/*static*/ nsresult +DateTimeFormat::FormatPRTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const PRTime aPrTime, + nsAString& aStringOut) +{ + PRExplodedTime explodedTime; + PR_ExplodeTime(aPrTime, PR_LocalTimeParameters, &explodedTime); + + return FormatPRExplodedTime(aDateFormatSelector, aTimeFormatSelector, &explodedTime, aStringOut); +} + +// performs a locale sensitive date formatting operation on the PRExplodedTime parameter +/*static*/ nsresult +DateTimeFormat::FormatPRExplodedTime(const nsDateFormatSelector aDateFormatSelector, + const nsTimeFormatSelector aTimeFormatSelector, + const PRExplodedTime* aExplodedTime, + nsAString& aStringOut) +{ + struct tm tmTime; + /* be safe and set all members of struct tm to zero + * + * there are other fields in the tm struct that we aren't setting + * (tm_isdst, tm_gmtoff, tm_zone, should we set these?) and since + * tmTime is on the stack, it may be filled with garbage, but + * the garbage may vary. (this may explain why some saw bug #10412, and + * others did not. + * + * when tmTime is passed to strftime() with garbage bad things may happen. + * see bug #10412 + */ + memset( &tmTime, 0, sizeof(tmTime) ); + + tmTime.tm_yday = aExplodedTime->tm_yday; + tmTime.tm_wday = aExplodedTime->tm_wday; + tmTime.tm_year = aExplodedTime->tm_year; + tmTime.tm_year -= 1900; + tmTime.tm_mon = aExplodedTime->tm_month; + tmTime.tm_mday = aExplodedTime->tm_mday; + tmTime.tm_hour = aExplodedTime->tm_hour; + tmTime.tm_min = aExplodedTime->tm_min; + tmTime.tm_sec = aExplodedTime->tm_sec; + + return FormatTMTime(aDateFormatSelector, aTimeFormatSelector, &tmTime, aStringOut); +} + +/*static*/ void +DateTimeFormat::Shutdown() +{ +} + +} diff --git a/intl/locale/mac/moz.build b/intl/locale/mac/moz.build index beda4fa260b0..99535d83cfb6 100644 --- a/intl/locale/mac/moz.build +++ b/intl/locale/mac/moz.build @@ -6,7 +6,6 @@ UNIFIED_SOURCES += [ 'nsCollationMacUC.cpp', - 'nsDateTimeFormatMac.cpp', 'nsMacCharset.cpp', ] diff --git a/intl/locale/mac/nsDateTimeFormatMac.cpp b/intl/locale/mac/nsDateTimeFormatMac.cpp deleted file mode 100644 index 6ee73292d7a5..000000000000 --- a/intl/locale/mac/nsDateTimeFormatMac.cpp +++ /dev/null @@ -1,266 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include -#include "nsIServiceManager.h" -#include "nsDateTimeFormatMac.h" -#include -#include "nsIComponentManager.h" -#include "nsILocaleService.h" -#include "nsCRT.h" -#include "plstr.h" -#include "nsUnicharUtils.h" -#include "nsTArray.h" - - -NS_IMPL_ISUPPORTS(nsDateTimeFormatMac, nsIDateTimeFormat) - -nsresult nsDateTimeFormatMac::Initialize(nsILocale* locale) -{ - nsAutoString localeStr; - nsAutoString category(NS_LITERAL_STRING("NSILOCALE_TIME")); - nsresult res; - - // use cached info if match with stored locale - if (nullptr == locale) { - if (!mLocale.IsEmpty() && - mLocale.Equals(mAppLocale, nsCaseInsensitiveStringComparator())) { - return NS_OK; - } - } - else { - res = locale->GetCategory(category, localeStr); - if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { - if (!mLocale.IsEmpty() && - mLocale.Equals(localeStr, - nsCaseInsensitiveStringComparator())) { - return NS_OK; - } - } - } - - // get application locale - nsCOMPtr localeService = - do_GetService(NS_LOCALESERVICE_CONTRACTID, &res); - if (NS_SUCCEEDED(res)) { - nsCOMPtr appLocale; - res = localeService->GetApplicationLocale(getter_AddRefs(appLocale)); - if (NS_SUCCEEDED(res)) { - res = appLocale->GetCategory(category, localeStr); - if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { - mAppLocale = localeStr; // cache app locale name - } - } - } - - // use app default if no locale specified - if (nullptr == locale) { - mUseDefaultLocale = true; - } - else { - mUseDefaultLocale = false; - res = locale->GetCategory(category, localeStr); - } - - if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { - mLocale.Assign(localeStr); // cache locale name - } - - return res; -} - -// performs a locale sensitive date formatting operation on the time_t parameter -nsresult nsDateTimeFormatMac::FormatTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const time_t timetTime, - nsAString& stringOut) -{ - struct tm tmTime; - return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, localtime_r(&timetTime, &tmTime), stringOut); -} - -// performs a locale sensitive date formatting operation on the struct tm parameter -nsresult nsDateTimeFormatMac::FormatTMTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const struct tm* tmTime, - nsAString& stringOut) -{ - nsresult res = NS_OK; - - // set up locale data - (void) Initialize(locale); - - // return, nothing to format - if (dateFormatSelector == kDateFormatNone && timeFormatSelector == kTimeFormatNone) { - stringOut.Truncate(); - return NS_OK; - } - - NS_ASSERTION(tmTime->tm_mon >= 0, "tm is not set correctly"); - NS_ASSERTION(tmTime->tm_mday >= 1, "tm is not set correctly"); - NS_ASSERTION(tmTime->tm_hour >= 0, "tm is not set correctly"); - NS_ASSERTION(tmTime->tm_min >= 0, "tm is not set correctly"); - NS_ASSERTION(tmTime->tm_sec >= 0, "tm is not set correctly"); - NS_ASSERTION(tmTime->tm_wday >= 0, "tm is not set correctly"); - - // Got the locale for the formatter: - CFLocaleRef formatterLocale; - if (!locale) { - formatterLocale = CFLocaleCopyCurrent(); - } else { - CFStringRef localeStr = CFStringCreateWithCharacters(nullptr, - reinterpret_cast(mLocale.get()), - mLocale.Length()); - formatterLocale = CFLocaleCreate(nullptr, localeStr); - CFRelease(localeStr); - } - - // Get the date style for the formatter: - CFDateFormatterStyle dateStyle; - switch (dateFormatSelector) { - case kDateFormatLong: - dateStyle = kCFDateFormatterLongStyle; - break; - case kDateFormatShort: - dateStyle = kCFDateFormatterShortStyle; - break; - case kDateFormatYearMonth: - case kDateFormatWeekday: - dateStyle = kCFDateFormatterNoStyle; // formats handled below - break; - case kDateFormatNone: - dateStyle = kCFDateFormatterNoStyle; - break; - default: - NS_ERROR("Unknown nsDateFormatSelector"); - res = NS_ERROR_FAILURE; - dateStyle = kCFDateFormatterNoStyle; - } - - // Get the time style for the formatter: - CFDateFormatterStyle timeStyle; - switch (timeFormatSelector) { - case kTimeFormatSeconds: - case kTimeFormatSecondsForce24Hour: // 24 hour part fixed below - timeStyle = kCFDateFormatterMediumStyle; - break; - case kTimeFormatNoSeconds: - case kTimeFormatNoSecondsForce24Hour: // 24 hour part fixed below - timeStyle = kCFDateFormatterShortStyle; - break; - case kTimeFormatNone: - timeStyle = kCFDateFormatterNoStyle; - break; - default: - NS_ERROR("Unknown nsTimeFormatSelector"); - res = NS_ERROR_FAILURE; - timeStyle = kCFDateFormatterNoStyle; - } - - // Create the formatter and fix up its formatting as necessary: - CFDateFormatterRef formatter = - CFDateFormatterCreate(nullptr, formatterLocale, dateStyle, timeStyle); - - CFRelease(formatterLocale); - - if (dateFormatSelector == kDateFormatYearMonth || - dateFormatSelector == kDateFormatWeekday) { - CFStringRef dateFormat = - dateFormatSelector == kDateFormatYearMonth ? CFSTR("yyyy/MM ") : CFSTR("EEE "); - - CFStringRef oldFormat = CFDateFormatterGetFormat(formatter); - CFMutableStringRef newFormat = CFStringCreateMutableCopy(nullptr, 0, oldFormat); - CFStringInsert(newFormat, 0, dateFormat); - CFDateFormatterSetFormat(formatter, newFormat); - CFRelease(newFormat); // note we don't own oldFormat - } - - if (timeFormatSelector == kTimeFormatSecondsForce24Hour || - timeFormatSelector == kTimeFormatNoSecondsForce24Hour) { - // Replace "h" with "H", and remove "a": - CFStringRef oldFormat = CFDateFormatterGetFormat(formatter); - CFMutableStringRef newFormat = CFStringCreateMutableCopy(nullptr, 0, oldFormat); - CFIndex replaceCount = CFStringFindAndReplace(newFormat, - CFSTR("h"), CFSTR("H"), - CFRangeMake(0, CFStringGetLength(newFormat)), - 0); - NS_ASSERTION(replaceCount <= 2, "Unexpected number of \"h\" occurrences"); - replaceCount = CFStringFindAndReplace(newFormat, - CFSTR("a"), CFSTR(""), - CFRangeMake(0, CFStringGetLength(newFormat)), - 0); - NS_ASSERTION(replaceCount <= 1, "Unexpected number of \"a\" occurrences"); - CFDateFormatterSetFormat(formatter, newFormat); - CFRelease(newFormat); // note we don't own oldFormat - } - - // Now get the formatted date: - CFGregorianDate date; - date.second = tmTime->tm_sec; - date.minute = tmTime->tm_min; - date.hour = tmTime->tm_hour; - date.day = tmTime->tm_mday; // Mac is 1-based, tm is 1-based - date.month = tmTime->tm_mon + 1; // Mac is 1-based, tm is 0-based - date.year = tmTime->tm_year + 1900; - - CFTimeZoneRef timeZone = CFTimeZoneCopySystem(); // tmTime is in local time - CFAbsoluteTime absTime = CFGregorianDateGetAbsoluteTime(date, timeZone); - CFRelease(timeZone); - - CFStringRef formattedDate = CFDateFormatterCreateStringWithAbsoluteTime(nullptr, - formatter, - absTime); - - CFIndex stringLen = CFStringGetLength(formattedDate); - - AutoTArray stringBuffer; - stringBuffer.SetLength(stringLen + 1); - CFStringGetCharacters(formattedDate, CFRangeMake(0, stringLen), stringBuffer.Elements()); - stringOut.Assign(reinterpret_cast(stringBuffer.Elements()), stringLen); - - CFRelease(formattedDate); - CFRelease(formatter); - - return res; -} - -// performs a locale sensitive date formatting operation on the PRTime parameter -nsresult nsDateTimeFormatMac::FormatPRTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRTime prTime, - nsAString& stringOut) -{ - PRExplodedTime explodedTime; - PR_ExplodeTime(prTime, PR_LocalTimeParameters, &explodedTime); - - return FormatPRExplodedTime(locale, dateFormatSelector, timeFormatSelector, &explodedTime, stringOut); -} - -// performs a locale sensitive date formatting operation on the PRExplodedTime parameter -nsresult nsDateTimeFormatMac::FormatPRExplodedTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRExplodedTime* explodedTime, - nsAString& stringOut) -{ - struct tm tmTime; - memset( &tmTime, 0, sizeof(tmTime) ); - - tmTime.tm_yday = explodedTime->tm_yday; - tmTime.tm_wday = explodedTime->tm_wday; - tmTime.tm_year = explodedTime->tm_year; - tmTime.tm_year -= 1900; - tmTime.tm_mon = explodedTime->tm_month; - tmTime.tm_mday = explodedTime->tm_mday; - tmTime.tm_hour = explodedTime->tm_hour; - tmTime.tm_min = explodedTime->tm_min; - tmTime.tm_sec = explodedTime->tm_sec; - - return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, &tmTime, stringOut); -} - diff --git a/intl/locale/mac/nsDateTimeFormatMac.h b/intl/locale/mac/nsDateTimeFormatMac.h deleted file mode 100644 index dfdf70378025..000000000000 --- a/intl/locale/mac/nsDateTimeFormatMac.h +++ /dev/null @@ -1,61 +0,0 @@ - -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef nsDateTimeFormatMac_h__ -#define nsDateTimeFormatMac_h__ - - -#include "nsCOMPtr.h" -#include "nsIDateTimeFormat.h" - - -class nsDateTimeFormatMac : public nsIDateTimeFormat { - -public: - NS_DECL_THREADSAFE_ISUPPORTS - - // performs a locale sensitive date formatting operation on the time_t parameter - NS_IMETHOD FormatTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const time_t timetTime, - nsAString& stringOut) override; - - // performs a locale sensitive date formatting operation on the struct tm parameter - NS_IMETHOD FormatTMTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const struct tm* tmTime, - nsAString& stringOut) override; - // performs a locale sensitive date formatting operation on the PRTime parameter - NS_IMETHOD FormatPRTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRTime prTime, - nsAString& stringOut) override; - - // performs a locale sensitive date formatting operation on the PRExplodedTime parameter - NS_IMETHOD FormatPRExplodedTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRExplodedTime* explodedTime, - nsAString& stringOut) override; - - nsDateTimeFormatMac() {} - -protected: - virtual ~nsDateTimeFormatMac() {} - -private: - // init this interface to a specified locale - NS_IMETHOD Initialize(nsILocale* locale); - - nsString mLocale; - nsString mAppLocale; - bool mUseDefaultLocale; -}; - -#endif /* nsDateTimeFormatMac_h__ */ diff --git a/intl/locale/moz.build b/intl/locale/moz.build index 2da1c2dd543f..a6ebd52bf880 100644 --- a/intl/locale/moz.build +++ b/intl/locale/moz.build @@ -25,10 +25,9 @@ XPIDL_SOURCES += [ XPIDL_MODULE = 'locale' EXPORTS += [ + 'DateTimeFormat.h', 'nsCollation.h', 'nsCollationCID.h', - 'nsDateTimeFormatCID.h', - 'nsIDateTimeFormat.h', 'nsILanguageAtomService.h', 'nsIPlatformCharset.h', 'nsPosixLocale.h', @@ -38,7 +37,6 @@ EXPORTS += [ UNIFIED_SOURCES += [ 'nsCollation.cpp', - 'nsIDateTimeFormat.cpp', 'nsLanguageAtomService.cpp', 'nsLocale.cpp', 'nsLocaleService.cpp', @@ -46,6 +44,15 @@ UNIFIED_SOURCES += [ 'nsUConvPropertySearch.cpp', ] +if CONFIG['ENABLE_INTL_API']: + UNIFIED_SOURCES += [ + 'DateTimeFormatICU.cpp', + ] +else: + UNIFIED_SOURCES += [ + 'DateTimeFormatUnix.cpp', + ] + EXTRA_JS_MODULES += [ 'PluralForm.jsm', ] diff --git a/intl/locale/nsDateTimeFormatCID.h b/intl/locale/nsDateTimeFormatCID.h deleted file mode 100644 index 31115bc4d273..000000000000 --- a/intl/locale/nsDateTimeFormatCID.h +++ /dev/null @@ -1,20 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef nsDateTimeFormatCID_h__ -#define nsDateTimeFormatCID_h__ - - -#include "nscore.h" -#include "nsISupports.h" - -#define NS_DATETIMEFORMAT_CONTRACTID "@mozilla.org/intl/datetimeformat;1" - -// {0704E7C0-A758-11d2-9119-006008A6EDF6} -#define NS_DATETIMEFORMAT_CID \ -{ 0x704e7c0, 0xa758, 0x11d2, \ -{ 0x91, 0x19, 0x0, 0x60, 0x8, 0xa6, 0xed, 0xf6 } } - -#endif // nsDateTimeFormatCID_h__ - diff --git a/intl/locale/nsIDateTimeFormat.cpp b/intl/locale/nsIDateTimeFormat.cpp deleted file mode 100644 index 263b3abb4e98..000000000000 --- a/intl/locale/nsIDateTimeFormat.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "nsIDateTimeFormat.h" -#include "mozilla/RefPtr.h" - -#if defined(XP_MACOSX) -#define USE_MAC_LOCALE -#elif defined(XP_UNIX) -#define USE_UNIX_LOCALE -#endif - -#ifdef XP_WIN -#include "windows/nsDateTimeFormatWin.h" -#endif -#ifdef USE_UNIX_LOCALE -#include "unix/nsDateTimeFormatUnix.h" -#endif -#ifdef USE_MAC_LOCALE -#include "mac/nsDateTimeFormatMac.h" -#endif - -using mozilla::MakeAndAddRef; - -/*static*/ already_AddRefed -nsIDateTimeFormat::Create() -{ -#ifdef XP_WIN - return MakeAndAddRef(); -#elif defined(USE_UNIX_LOCALE) - return MakeAndAddRef(); -#elif defined(USE_MAC_LOCALE) - return MakeAndAddRef(); -#else - return nullptr; -#endif -} diff --git a/intl/locale/nsIDateTimeFormat.h b/intl/locale/nsIDateTimeFormat.h deleted file mode 100644 index 37ee9de461eb..000000000000 --- a/intl/locale/nsIDateTimeFormat.h +++ /dev/null @@ -1,69 +0,0 @@ - -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef nsIDateTimeFormat_h__ -#define nsIDateTimeFormat_h__ - - -#include "nsISupports.h" -#include "nscore.h" -#include "nsStringGlue.h" -#include "nsILocale.h" -#include "nsIScriptableDateFormat.h" -#include "prtime.h" -#include - - -// {2BBAA0B0-A591-11d2-9119-006008A6EDF6} -#define NS_IDATETIMEFORMAT_IID \ -{ 0x2bbaa0b0, 0xa591, 0x11d2, \ -{ 0x91, 0x19, 0x0, 0x60, 0x8, 0xa6, 0xed, 0xf6 } } - - -// Locale sensitive date and time format interface -// -class nsIDateTimeFormat : public nsISupports { -protected: - nsIDateTimeFormat() {} - virtual ~nsIDateTimeFormat() {} - -public: - NS_DECLARE_STATIC_IID_ACCESSOR(NS_IDATETIMEFORMAT_IID) - - static already_AddRefed Create(); - - // performs a locale sensitive date formatting operation on the time_t parameter - NS_IMETHOD FormatTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const time_t timetTime, - nsAString& stringOut) = 0; - - // performs a locale sensitive date formatting operation on the struct tm parameter - NS_IMETHOD FormatTMTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const struct tm* tmTime, - nsAString& stringOut) = 0; - - // performs a locale sensitive date formatting operation on the PRTime parameter - NS_IMETHOD FormatPRTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRTime prTime, - nsAString& stringOut) = 0; - - // performs a locale sensitive date formatting operation on the PRExplodedTime parameter - NS_IMETHOD FormatPRExplodedTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRExplodedTime* explodedTime, - nsAString& stringOut) = 0; -}; - -NS_DEFINE_STATIC_IID_ACCESSOR(nsIDateTimeFormat, NS_IDATETIMEFORMAT_IID) - -#endif /* nsIDateTimeFormat_h__ */ diff --git a/intl/locale/nsLocaleConstructors.h b/intl/locale/nsLocaleConstructors.h index 2d2e579740fc..a4443ab10db7 100644 --- a/intl/locale/nsLocaleConstructors.h +++ b/intl/locale/nsLocaleConstructors.h @@ -7,7 +7,6 @@ #define nsLocaleConstructors_h__ #include "nsCollationCID.h" -#include "nsDateTimeFormatCID.h" #include "mozilla/ModuleUtils.h" #include "nsILocaleService.h" #include "nsIScriptableDateFormat.h" @@ -25,17 +24,14 @@ #ifdef XP_WIN #include "windows/nsCollationWin.h" -#include "windows/nsDateTimeFormatWin.h" #endif #ifdef USE_MAC_LOCALE #include "mac/nsCollationMacUC.h" -#include "mac/nsDateTimeFormatMac.h" #endif #ifdef USE_UNIX_LOCALE #include "unix/nsCollationUnix.h" -#include "unix/nsDateTimeFormatUnix.h" #endif #define NSLOCALE_MAKE_CTOR(ctor_, iface_, func_) \ @@ -63,17 +59,14 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPlatformCharset, Init) #ifdef XP_WIN NS_GENERIC_FACTORY_CONSTRUCTOR(nsCollationWin) -NS_GENERIC_FACTORY_CONSTRUCTOR(nsDateTimeFormatWin) #endif #ifdef USE_UNIX_LOCALE NS_GENERIC_FACTORY_CONSTRUCTOR(nsCollationUnix) -NS_GENERIC_FACTORY_CONSTRUCTOR(nsDateTimeFormatUnix) #endif #ifdef USE_MAC_LOCALE NS_GENERIC_FACTORY_CONSTRUCTOR(nsCollationMacUC) -NS_GENERIC_FACTORY_CONSTRUCTOR(nsDateTimeFormatMac) #endif #endif diff --git a/intl/locale/nsScriptableDateFormat.cpp b/intl/locale/nsScriptableDateFormat.cpp index a75cdaf29f1d..3a64e3cfc3cf 100644 --- a/intl/locale/nsScriptableDateFormat.cpp +++ b/intl/locale/nsScriptableDateFormat.cpp @@ -3,16 +3,14 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "DateTimeFormat.h" #include "mozilla/Sprintf.h" #include "nsILocaleService.h" -#include "nsDateTimeFormatCID.h" -#include "nsIDateTimeFormat.h" #include "nsIScriptableDateFormat.h" #include "nsCOMPtr.h" #include "nsServiceManagerUtils.h" static NS_DEFINE_CID(kLocaleServiceCID, NS_LOCALESERVICE_CID); -static NS_DEFINE_CID(kDateTimeFormatCID, NS_DATETIMEFORMAT_CID); class nsScriptableDateFormat : public nsIScriptableDateFormat { public: @@ -89,9 +87,6 @@ NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime( NS_ENSURE_SUCCESS(rv, rv); } - nsCOMPtr dateTimeFormat(do_CreateInstance(kDateTimeFormatCID, &rv)); - NS_ENSURE_SUCCESS(rv, rv); - tm tmTime; time_t timetTime; @@ -107,8 +102,8 @@ NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime( timetTime = mktime(&tmTime); if ((time_t)-1 != timetTime) { - rv = dateTimeFormat->FormatTime(locale, dateFormatSelector, timeFormatSelector, - timetTime, mStringOut); + rv = mozilla::DateTimeFormat::FormatTime(dateFormatSelector, timeFormatSelector, + timetTime, mStringOut); } else { // if mktime fails (e.g. year <= 1970), then try NSPR. @@ -118,8 +113,8 @@ NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime( if (PR_SUCCESS != PR_ParseTimeString(string, false, &prtime)) return NS_ERROR_INVALID_ARG; - rv = dateTimeFormat->FormatPRTime(locale, dateFormatSelector, timeFormatSelector, - prtime, mStringOut); + rv = mozilla::DateTimeFormat::FormatPRTime(dateFormatSelector, timeFormatSelector, + prtime, mStringOut); } if (NS_SUCCEEDED(rv)) *dateTimeString = ToNewUnicode(mStringOut); diff --git a/intl/locale/unix/moz.build b/intl/locale/unix/moz.build index 7685d0a01c14..858fce229756 100644 --- a/intl/locale/unix/moz.build +++ b/intl/locale/unix/moz.build @@ -6,7 +6,6 @@ SOURCES += [ 'nsCollationUnix.cpp', - 'nsDateTimeFormatUnix.cpp', 'nsPosixLocale.cpp', ] diff --git a/intl/locale/unix/nsDateTimeFormatUnix.cpp b/intl/locale/unix/nsDateTimeFormatUnix.cpp deleted file mode 100644 index a70b54102c77..000000000000 --- a/intl/locale/unix/nsDateTimeFormatUnix.cpp +++ /dev/null @@ -1,284 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include -#include "plstr.h" -#include "nsIServiceManager.h" -#include "nsDateTimeFormatUnix.h" -#include "nsIComponentManager.h" -#include "nsILocaleService.h" -#include "nsIPlatformCharset.h" -#include "nsPosixLocale.h" -#include "nsCRT.h" -#include "nsReadableUtils.h" -#include "nsUnicharUtils.h" -#include "mozilla/dom/EncodingUtils.h" - -using mozilla::dom::EncodingUtils; - -NS_IMPL_ISUPPORTS(nsDateTimeFormatUnix, nsIDateTimeFormat) - -// init this interface to a specified locale -nsresult nsDateTimeFormatUnix::Initialize(nsILocale* locale) -{ - nsAutoString localeStr; - NS_NAMED_LITERAL_STRING(aCategory, "NSILOCALE_TIME##PLATFORM"); - nsresult res = NS_OK; - - // use cached info if match with stored locale - if (!locale) { - if (!mLocale.IsEmpty() && - mLocale.Equals(mAppLocale, nsCaseInsensitiveStringComparator())) { - return NS_OK; - } - } - else { - res = locale->GetCategory(aCategory, localeStr); - if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { - if (!mLocale.IsEmpty() && - mLocale.Equals(localeStr, - nsCaseInsensitiveStringComparator())) { - return NS_OK; - } - } - } - - mCharset.AssignLiteral("windows-1252"); - mPlatformLocale.AssignLiteral("en_US"); - - // get locale name string, use app default if no locale specified - if (!locale) { - nsCOMPtr localeService = - do_GetService(NS_LOCALESERVICE_CONTRACTID, &res); - if (NS_SUCCEEDED(res)) { - nsCOMPtr appLocale; - res = localeService->GetApplicationLocale(getter_AddRefs(appLocale)); - if (NS_SUCCEEDED(res)) { - res = appLocale->GetCategory(aCategory, localeStr); - if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { - NS_ASSERTION(NS_SUCCEEDED(res), "failed to get app locale info"); - mAppLocale = localeStr; // cache app locale name - } - } - } - } - else { - res = locale->GetCategory(aCategory, localeStr); - NS_ASSERTION(NS_SUCCEEDED(res), "failed to get locale info"); - } - - if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { - mLocale = localeStr; // cache locale name - - nsPosixLocale::GetPlatformLocale(mLocale, mPlatformLocale); - - nsCOMPtr platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res); - if (NS_SUCCEEDED(res)) { - nsAutoCString mappedCharset; - res = platformCharset->GetDefaultCharsetForLocale(mLocale, mappedCharset); - if (NS_SUCCEEDED(res)) { - mCharset = mappedCharset; - } - } - } - - mDecoder = EncodingUtils::DecoderForEncoding(mCharset); - - LocalePreferred24hour(); - - return res; -} - -void nsDateTimeFormatUnix::LocalePreferred24hour() -{ - char str[100]; - time_t tt; - struct tm *tmc; - int i; - - tt = time(nullptr); - 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.get()); - strftime(str, (size_t)99, "%X", (struct tm *)tmc); - - (void) setlocale(LC_TIME, temp); - - mLocalePreferred24hour = false; - for (i=0; str[i]; i++) { - if (str[i] == '2') { // if there is any '2', that locale use 0-23 time format - mLocalePreferred24hour = true; - break; - } - } - - mLocaleAMPMfirst = true; - if (mLocalePreferred24hour == false) { - if (str[0] && str[0] == '1') { // if the first character is '1' of 10:00, - // AMPM string is located after 10:00 - mLocaleAMPMfirst = false; - } - } -} - -nsresult nsDateTimeFormatUnix::FormatTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const time_t timetTime, - nsAString& stringOut) -{ - struct tm tmTime; - memcpy(&tmTime, localtime(&timetTime), sizeof(struct tm)); - return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, &tmTime, stringOut); -} - -// performs a locale sensitive date formatting operation on the struct tm parameter -nsresult nsDateTimeFormatUnix::FormatTMTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const struct tm* tmTime, - nsAString& stringOut) -{ -#define NSDATETIME_FORMAT_BUFFER_LEN 80 - char strOut[NSDATETIME_FORMAT_BUFFER_LEN*2]; // buffer for date and time - char fmtD[NSDATETIME_FORMAT_BUFFER_LEN], fmtT[NSDATETIME_FORMAT_BUFFER_LEN]; - nsresult rv; - - - // set up locale data - (void) Initialize(locale); - NS_ENSURE_TRUE(mDecoder, NS_ERROR_NOT_INITIALIZED); - - // set date format - if (dateFormatSelector == kDateFormatLong && timeFormatSelector == kTimeFormatSeconds) { - PL_strncpy(fmtD, "%c", NSDATETIME_FORMAT_BUFFER_LEN); - PL_strncpy(fmtT, "", NSDATETIME_FORMAT_BUFFER_LEN); - } else { - - switch (dateFormatSelector) { - case kDateFormatNone: - PL_strncpy(fmtD, "", NSDATETIME_FORMAT_BUFFER_LEN); - break; - case kDateFormatLong: - case kDateFormatShort: - PL_strncpy(fmtD, "%x", NSDATETIME_FORMAT_BUFFER_LEN); - break; - case kDateFormatYearMonth: - PL_strncpy(fmtD, "%Y/%m", NSDATETIME_FORMAT_BUFFER_LEN); - break; - case kDateFormatWeekday: - PL_strncpy(fmtD, "%a", NSDATETIME_FORMAT_BUFFER_LEN); - break; - default: - PL_strncpy(fmtD, "", NSDATETIME_FORMAT_BUFFER_LEN); - } - - // set time format - switch (timeFormatSelector) { - case kTimeFormatNone: - PL_strncpy(fmtT, "", NSDATETIME_FORMAT_BUFFER_LEN); - break; - case kTimeFormatSeconds: - PL_strncpy(fmtT, "%X", NSDATETIME_FORMAT_BUFFER_LEN); - break; - case kTimeFormatNoSeconds: - PL_strncpy(fmtT, - mLocalePreferred24hour ? "%H:%M" : mLocaleAMPMfirst ? "%p %I:%M" : "%I:%M %p", - NSDATETIME_FORMAT_BUFFER_LEN); - break; - case kTimeFormatSecondsForce24Hour: - PL_strncpy(fmtT, "%H:%M:%S", NSDATETIME_FORMAT_BUFFER_LEN); - break; - case kTimeFormatNoSecondsForce24Hour: - PL_strncpy(fmtT, "%H:%M", NSDATETIME_FORMAT_BUFFER_LEN); - break; - default: - PL_strncpy(fmtT, "", NSDATETIME_FORMAT_BUFFER_LEN); - } - } - - // generate data/time string - char *old_locale = setlocale(LC_TIME, nullptr); - (void) setlocale(LC_TIME, mPlatformLocale.get()); - if (strlen(fmtD) && strlen(fmtT)) { - PL_strncat(fmtD, " ", NSDATETIME_FORMAT_BUFFER_LEN); - PL_strncat(fmtD, fmtT, NSDATETIME_FORMAT_BUFFER_LEN); - strftime(strOut, NSDATETIME_FORMAT_BUFFER_LEN, fmtD, tmTime); - } - else if (strlen(fmtD) && !strlen(fmtT)) { - strftime(strOut, NSDATETIME_FORMAT_BUFFER_LEN, fmtD, tmTime); - } - else if (!strlen(fmtD) && strlen(fmtT)) { - strftime(strOut, NSDATETIME_FORMAT_BUFFER_LEN, fmtT, tmTime); - } - else { - PL_strncpy(strOut, "", NSDATETIME_FORMAT_BUFFER_LEN); - } - (void) setlocale(LC_TIME, old_locale); - - // convert result to unicode - int32_t srcLength = (int32_t) strlen(strOut); - int32_t unicharLength = NSDATETIME_FORMAT_BUFFER_LEN*2; - char16_t unichars[NSDATETIME_FORMAT_BUFFER_LEN*2]; // buffer for date and time - - rv = mDecoder->Convert(strOut, &srcLength, unichars, &unicharLength); - if (NS_FAILED(rv)) - return rv; - stringOut.Assign(unichars, unicharLength); - - return rv; -} - -// performs a locale sensitive date formatting operation on the PRTime parameter -nsresult nsDateTimeFormatUnix::FormatPRTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRTime prTime, - nsAString& stringOut) -{ - PRExplodedTime explodedTime; - PR_ExplodeTime(prTime, PR_LocalTimeParameters, &explodedTime); - - return FormatPRExplodedTime(locale, dateFormatSelector, timeFormatSelector, &explodedTime, stringOut); -} - -// performs a locale sensitive date formatting operation on the PRExplodedTime parameter -nsresult nsDateTimeFormatUnix::FormatPRExplodedTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRExplodedTime* explodedTime, - nsAString& stringOut) -{ - struct tm tmTime; - /* be safe and set all members of struct tm to zero - * - * there are other fields in the tm struct that we aren't setting - * (tm_isdst, tm_gmtoff, tm_zone, should we set these?) and since - * tmTime is on the stack, it may be filled with garbage, but - * the garbage may vary. (this may explain why some saw bug #10412, and - * others did not. - * - * when tmTime is passed to strftime() with garbage bad things may happen. - * see bug #10412 - */ - memset( &tmTime, 0, sizeof(tmTime) ); - - tmTime.tm_yday = explodedTime->tm_yday; - tmTime.tm_wday = explodedTime->tm_wday; - tmTime.tm_year = explodedTime->tm_year; - tmTime.tm_year -= 1900; - tmTime.tm_mon = explodedTime->tm_month; - tmTime.tm_mday = explodedTime->tm_mday; - tmTime.tm_hour = explodedTime->tm_hour; - tmTime.tm_min = explodedTime->tm_min; - tmTime.tm_sec = explodedTime->tm_sec; - - return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, &tmTime, stringOut); -} - diff --git a/intl/locale/unix/nsDateTimeFormatUnix.h b/intl/locale/unix/nsDateTimeFormatUnix.h deleted file mode 100644 index 6c305556f6f8..000000000000 --- a/intl/locale/unix/nsDateTimeFormatUnix.h +++ /dev/null @@ -1,70 +0,0 @@ - -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef nsDateTimeFormatUnix_h__ -#define nsDateTimeFormatUnix_h__ - - -#include "nsCOMPtr.h" -#include "nsIDateTimeFormat.h" -#include "nsIUnicodeDecoder.h" - -#define kPlatformLocaleLength 64 - -class nsDateTimeFormatUnix : public nsIDateTimeFormat { - -public: - NS_DECL_THREADSAFE_ISUPPORTS - - // performs a locale sensitive date formatting operation on the time_t parameter - NS_IMETHOD FormatTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const time_t timetTime, - nsAString& stringOut) override; - - // performs a locale sensitive date formatting operation on the struct tm parameter - NS_IMETHOD FormatTMTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const struct tm* tmTime, - nsAString& stringOut) override; - - // performs a locale sensitive date formatting operation on the PRTime parameter - NS_IMETHOD FormatPRTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRTime prTime, - nsAString& stringOut) override; - - // performs a locale sensitive date formatting operation on the PRExplodedTime parameter - NS_IMETHOD FormatPRExplodedTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRExplodedTime* explodedTime, - nsAString& stringOut) override; - - - nsDateTimeFormatUnix() {mLocale.Truncate();mAppLocale.Truncate();} - -private: - virtual ~nsDateTimeFormatUnix() {} - - // init this interface to a specified locale - NS_IMETHOD Initialize(nsILocale* locale); - - void LocalePreferred24hour(); - - nsString mLocale; - nsString mAppLocale; - nsCString mCharset; // in order to convert API result to unicode - nsCString mPlatformLocale; // for setlocale - bool mLocalePreferred24hour; // true if 24 hour format is preferred by current locale - bool mLocaleAMPMfirst; // true if AM/PM string is preferred before the time - nsCOMPtr mDecoder; -}; - -#endif /* nsDateTimeFormatUnix_h__ */ diff --git a/intl/locale/windows/moz.build b/intl/locale/windows/moz.build index 5bde38b89c4e..fb0b2e7540dd 100644 --- a/intl/locale/windows/moz.build +++ b/intl/locale/windows/moz.build @@ -6,7 +6,6 @@ SOURCES += [ 'nsCollationWin.cpp', - 'nsDateTimeFormatWin.cpp', 'nsWin32Locale.cpp', 'nsWinCharset.cpp', ] diff --git a/intl/locale/windows/nsDateTimeFormatWin.cpp b/intl/locale/windows/nsDateTimeFormatWin.cpp deleted file mode 100644 index f65868e1bcce..000000000000 --- a/intl/locale/windows/nsDateTimeFormatWin.cpp +++ /dev/null @@ -1,253 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "nsDateTimeFormatWin.h" -#include "nsIServiceManager.h" -#include "nsIComponentManager.h" -#include "nsILocaleService.h" -#include "nsWin32Locale.h" -#include "nsUnicharUtils.h" -#include "nsCRT.h" -#include "nsCOMPtr.h" - - -#define NSDATETIMEFORMAT_BUFFER_LEN 80 - -NS_IMPL_ISUPPORTS(nsDateTimeFormatWin, nsIDateTimeFormat) - - -// init this interface to a specified locale -nsresult nsDateTimeFormatWin::Initialize(nsILocale* locale) -{ - nsAutoString localeStr; - nsresult res = NS_OK; - - // use cached info if match with stored locale - if (!locale) { - if (!mLocale.IsEmpty() && - mLocale.Equals(mAppLocale, nsCaseInsensitiveStringComparator())) { - return NS_OK; - } - } - else { - res = locale->GetCategory(NS_LITERAL_STRING("NSILOCALE_TIME"), localeStr); - if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { - if (!mLocale.IsEmpty() && - mLocale.Equals(localeStr, nsCaseInsensitiveStringComparator())) { - return NS_OK; - } - } - } - - // default LCID (en-US) - mLCID = 1033; - - // get locale string, use app default if no locale specified - if (!locale) { - nsCOMPtr localeService = - do_GetService(NS_LOCALESERVICE_CONTRACTID); - if (localeService) { - nsCOMPtr appLocale; - res = localeService->GetApplicationLocale(getter_AddRefs(appLocale)); - if (NS_SUCCEEDED(res)) { - res = appLocale->GetCategory(NS_LITERAL_STRING("NSILOCALE_TIME"), - localeStr); - if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { - mAppLocale.Assign(localeStr); // cache app locale name - } - } - } - } - else { - res = locale->GetCategory(NS_LITERAL_STRING("NSILOCALE_TIME"), localeStr); - } - - // Get LCID and charset name from locale, if available - if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { - mLocale.Assign(localeStr); // cache locale name - res = nsWin32Locale::GetPlatformLocale(mLocale, (LCID *) &mLCID); - } - - return res; -} - -// performs a locale sensitive date formatting operation on the time_t parameter -nsresult nsDateTimeFormatWin::FormatTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const time_t timetTime, - nsAString& stringOut) -{ - return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, localtime( &timetTime ), stringOut); -} - -// performs a locale sensitive date formatting operation on the struct tm parameter -nsresult nsDateTimeFormatWin::FormatTMTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const struct tm* tmTime, - nsAString& stringOut) -{ - SYSTEMTIME system_time; - DWORD dwFlags_Date = 0, dwFlags_Time = 0; - int dateLen, timeLen; - char16_t dateBuffer[NSDATETIMEFORMAT_BUFFER_LEN], timeBuffer[NSDATETIMEFORMAT_BUFFER_LEN]; - - // set up locale data - (void) Initialize(locale); - - // Map tm to SYSTEMTIME - system_time.wYear = 1900 + tmTime->tm_year; - system_time.wMonth = tmTime->tm_mon + 1; - system_time.wDayOfWeek = tmTime->tm_wday; - system_time.wDay = tmTime->tm_mday; - system_time.wHour = tmTime->tm_hour; - system_time.wMinute = tmTime->tm_min; - system_time.wSecond = tmTime->tm_sec; - system_time.wMilliseconds = 0; - - // Map to WinAPI date format - switch (dateFormatSelector) { - case kDateFormatLong: - dwFlags_Date = DATE_LONGDATE; - break; - case kDateFormatShort: - dwFlags_Date = DATE_SHORTDATE; - break; - case kDateFormatWeekday: - dwFlags_Date = 0; - break; - case kDateFormatYearMonth: - dwFlags_Date = 0; // TODO:only availabe NT5 - break; - } - - // Map to WinAPI time format - switch (timeFormatSelector) { - case kTimeFormatSeconds: - dwFlags_Time = 0; - break; - case kTimeFormatNoSeconds: - dwFlags_Time = TIME_NOSECONDS; - break; - case kTimeFormatSecondsForce24Hour: - dwFlags_Time = TIME_FORCE24HOURFORMAT; - break; - case kTimeFormatNoSecondsForce24Hour: - dwFlags_Time = TIME_NOSECONDS + TIME_FORCE24HOURFORMAT; - break; - } - - // Call GetDateFormatW - if (dateFormatSelector == kDateFormatNone) { - dateLen = 0; - } - else { - if (dateFormatSelector == kDateFormatYearMonth) { - dateLen = nsGetDateFormatW(0, &system_time, "yyyy/MM", - dateBuffer, NSDATETIMEFORMAT_BUFFER_LEN); - } - else if (dateFormatSelector == kDateFormatWeekday) { - dateLen = nsGetDateFormatW(0, &system_time, "ddd", - dateBuffer, NSDATETIMEFORMAT_BUFFER_LEN); - } - else { - dateLen = nsGetDateFormatW(dwFlags_Date, &system_time, nullptr, - dateBuffer, NSDATETIMEFORMAT_BUFFER_LEN); - } - if (dateLen != 0) { - dateLen--; // Since the count includes the terminating null. - } - } - - // Call GetTimeFormatW - if (timeFormatSelector == kTimeFormatNone) { - timeLen = 0; - } - else { - timeLen = nsGetTimeFormatW(dwFlags_Time, &system_time, nullptr, - timeBuffer, NSDATETIMEFORMAT_BUFFER_LEN); - if (timeLen != 0) { - timeLen--; // Since the count includes the terminating null. - } - } - - NS_ASSERTION(NSDATETIMEFORMAT_BUFFER_LEN >= (uint32_t) (dateLen + 1), "internal date buffer is not large enough"); - NS_ASSERTION(NSDATETIMEFORMAT_BUFFER_LEN >= (uint32_t) (timeLen + 1), "internal time buffer is not large enough"); - - // Copy the result - stringOut.Truncate(); - if (dateLen != 0 && timeLen != 0) { - stringOut.Assign(dateBuffer, dateLen); - stringOut.Append((char16_t *)(L" "), 1); - stringOut.Append(timeBuffer, timeLen); - } - else if (dateLen != 0 && timeLen == 0) { - stringOut.Assign(dateBuffer, dateLen); - } - else if (dateLen == 0 && timeLen != 0) { - stringOut.Assign(timeBuffer, timeLen); - } - - return NS_OK; -} - -// performs a locale sensitive date formatting operation on the PRTime parameter -nsresult nsDateTimeFormatWin::FormatPRTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRTime prTime, - nsAString& stringOut) -{ - PRExplodedTime explodedTime; - PR_ExplodeTime(prTime, PR_LocalTimeParameters, &explodedTime); - - return FormatPRExplodedTime(locale, dateFormatSelector, timeFormatSelector, &explodedTime, stringOut); -} - -// performs a locale sensitive date formatting operation on the PRExplodedTime parameter -nsresult nsDateTimeFormatWin::FormatPRExplodedTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRExplodedTime* explodedTime, - nsAString& stringOut) -{ - struct tm tmTime; - memset( &tmTime, 0, sizeof(tmTime) ); - - tmTime.tm_yday = explodedTime->tm_yday; - tmTime.tm_wday = explodedTime->tm_wday; - tmTime.tm_year = explodedTime->tm_year; - tmTime.tm_year -= 1900; - tmTime.tm_mon = explodedTime->tm_month; - tmTime.tm_mday = explodedTime->tm_mday; - tmTime.tm_hour = explodedTime->tm_hour; - tmTime.tm_min = explodedTime->tm_min; - tmTime.tm_sec = explodedTime->tm_sec; - - return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, &tmTime, stringOut); -} - -int nsDateTimeFormatWin::nsGetTimeFormatW(DWORD dwFlags, const SYSTEMTIME *lpTime, - const char* format, char16_t *timeStr, int cchTime) -{ - int len = 0; - len = GetTimeFormatW(mLCID, dwFlags, lpTime, - format ? - NS_ConvertASCIItoUTF16(format).get() : - nullptr, - (LPWSTR) timeStr, cchTime); - return len; -} - -int nsDateTimeFormatWin::nsGetDateFormatW(DWORD dwFlags, const SYSTEMTIME *lpDate, - const char* format, char16_t *dateStr, int cchDate) -{ - int len = 0; - len = GetDateFormatW(mLCID, dwFlags, lpDate, - format ? NS_ConvertASCIItoUTF16(format).get() : nullptr, - (LPWSTR) dateStr, cchDate); - return len; -} diff --git a/intl/locale/windows/nsDateTimeFormatWin.h b/intl/locale/windows/nsDateTimeFormatWin.h deleted file mode 100644 index 1c84492ee50f..000000000000 --- a/intl/locale/windows/nsDateTimeFormatWin.h +++ /dev/null @@ -1,71 +0,0 @@ - -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef nsDateTimeFormatWin_h__ -#define nsDateTimeFormatWin_h__ - - -#include "nsIDateTimeFormat.h" -#include - - -// Locale sensitive date and time format interface -// -class nsDateTimeFormatWin : public nsIDateTimeFormat { - virtual ~nsDateTimeFormatWin() {} - -public: - NS_DECL_THREADSAFE_ISUPPORTS - - // performs a locale sensitive date formatting operation on the time_t parameter - NS_IMETHOD FormatTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const time_t timetTime, - nsAString& stringOut); - - // performs a locale sensitive date formatting operation on the struct tm parameter - NS_IMETHOD FormatTMTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const struct tm* tmTime, - nsAString& stringOut); - - // performs a locale sensitive date formatting operation on the PRTime parameter - NS_IMETHOD FormatPRTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRTime prTime, - nsAString& stringOut); - - // performs a locale sensitive date formatting operation on the PRExplodedTime parameter - NS_IMETHOD FormatPRExplodedTime(nsILocale* locale, - const nsDateFormatSelector dateFormatSelector, - const nsTimeFormatSelector timeFormatSelector, - const PRExplodedTime* explodedTime, - nsAString& stringOut); - - nsDateTimeFormatWin() {mLocale.SetLength(0);mAppLocale.SetLength(0);} - - -private: - // init this interface to a specified locale - NS_IMETHOD Initialize(nsILocale* locale); - - // call GetTimeFormatW or TimeFormatA - int nsGetTimeFormatW(DWORD dwFlags, const SYSTEMTIME *lpTime, - const char* format, char16_t *timeStr, int cchTime); - - // call GetDateFormatW or GetDateFormatA - int nsGetDateFormatW(DWORD dwFlags, const SYSTEMTIME *lpDate, - const char* format, char16_t *dateStr, int cchDate); - - nsString mLocale; - nsString mAppLocale; - uint32_t mLCID; // Windows platform locale ID -}; - -#endif /* nsDateTimeFormatWin_h__ */ diff --git a/layout/build/nsLayoutStatics.cpp b/layout/build/nsLayoutStatics.cpp index 96891ef8551d..1c448f65c88f 100644 --- a/layout/build/nsLayoutStatics.cpp +++ b/layout/build/nsLayoutStatics.cpp @@ -7,6 +7,7 @@ #include "nsLayoutStatics.h" #include "nscore.h" +#include "DateTimeFormat.h" #include "nsAttrValue.h" #include "nsAutoCopyListener.h" #include "nsColorNames.h" @@ -423,6 +424,8 @@ nsLayoutStatics::Shutdown() nsHyphenationManager::Shutdown(); nsDOMMutationObserver::Shutdown(); + DateTimeFormat::Shutdown(); + ContentParent::ShutDown(); DisplayItemClip::Shutdown(); diff --git a/layout/generic/nsSimplePageSequenceFrame.cpp b/layout/generic/nsSimplePageSequenceFrame.cpp index b56d3e7022da..6898716aa9f8 100644 --- a/layout/generic/nsSimplePageSequenceFrame.cpp +++ b/layout/generic/nsSimplePageSequenceFrame.cpp @@ -5,6 +5,7 @@ #include "nsSimplePageSequenceFrame.h" +#include "DateTimeFormat.h" #include "nsCOMPtr.h" #include "nsDeviceContext.h" #include "nsPresContext.h" @@ -22,7 +23,6 @@ #include "nsHTMLCanvasFrame.h" #include "mozilla/dom/HTMLCanvasElement.h" #include "nsICanvasRenderingContextInternal.h" -#include "nsIDateTimeFormat.h" #include "nsServiceManagerUtils.h" #include @@ -299,18 +299,10 @@ nsSimplePageSequenceFrame::Reflow(nsPresContext* aPresContext, pageNum++; } - // Create current Date/Time String - if (!mDateFormatter) { - mDateFormatter = nsIDateTimeFormat::Create(); - } - if (!mDateFormatter) { - return; - } nsAutoString formattedDateString; time_t ltime; time( <ime ); - if (NS_SUCCEEDED(mDateFormatter->FormatTime(nullptr /* nsILocale* locale */, - kDateFormatShort, + if (NS_SUCCEEDED(DateTimeFormat::FormatTime(kDateFormatShort, kTimeFormatNoSeconds, ltime, formattedDateString))) { diff --git a/layout/generic/nsSimplePageSequenceFrame.h b/layout/generic/nsSimplePageSequenceFrame.h index c4e1e84b63d6..885678e70e54 100644 --- a/layout/generic/nsSimplePageSequenceFrame.h +++ b/layout/generic/nsSimplePageSequenceFrame.h @@ -10,8 +10,6 @@ #include "nsContainerFrame.h" #include "nsIPrintSettings.h" -class nsIDateTimeFormat; - namespace mozilla { namespace dom { @@ -139,9 +137,6 @@ protected: nsMargin mMargin; - // I18N date formatter service which we'll want to cache locally. - nsCOMPtr mDateFormatter; - nsSize mSize; nsSharedPageData* mPageData; // data shared by all the nsPageFrames diff --git a/netwerk/streamconv/converters/nsIndexedToHTML.cpp b/netwerk/streamconv/converters/nsIndexedToHTML.cpp index f75069850bd2..ecc68723b77b 100644 --- a/netwerk/streamconv/converters/nsIndexedToHTML.cpp +++ b/netwerk/streamconv/converters/nsIndexedToHTML.cpp @@ -3,6 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "DateTimeFormat.h" #include "nsIndexedToHTML.h" #include "mozilla/dom/EncodingUtils.h" #include "nsNetUtil.h" @@ -18,7 +19,6 @@ #include "nsIPrefBranch.h" #include "nsIPrefLocalizedString.h" #include "nsIChromeRegistry.h" -#include "nsIDateTimeFormat.h" #include "nsIStringBundle.h" #include "nsITextToSubURI.h" #include "nsXPIDLString.h" @@ -69,10 +69,6 @@ nsIndexedToHTML::Init(nsIStreamListener* aListener) { mListener = aListener; - mDateTime = nsIDateTimeFormat::Create(); - if (!mDateTime) - return NS_ERROR_FAILURE; - nsCOMPtr sbs = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; @@ -835,18 +831,16 @@ nsIndexedToHTML::OnIndexAvailable(nsIRequest *aRequest, pushBuffer.AppendInt(static_cast(t)); pushBuffer.AppendLiteral("\">"); nsAutoString formatted; - mDateTime->FormatPRTime(nullptr, - kDateFormatShort, - kTimeFormatNone, - t, - formatted); + mozilla::DateTimeFormat::FormatPRTime(kDateFormatShort, + kTimeFormatNone, + t, + formatted); AppendNonAsciiToNCR(formatted, pushBuffer); pushBuffer.AppendLiteral("\n "); - mDateTime->FormatPRTime(nullptr, - kDateFormatNone, - kTimeFormatSeconds, - t, - formatted); + mozilla::DateTimeFormat::FormatPRTime(kDateFormatNone, + kTimeFormatSeconds, + t, + formatted); // use NCR to show date in any doc charset AppendNonAsciiToNCR(formatted, pushBuffer); } diff --git a/netwerk/streamconv/converters/nsIndexedToHTML.h b/netwerk/streamconv/converters/nsIndexedToHTML.h index 6f0fe9479cc0..f5610188fac4 100644 --- a/netwerk/streamconv/converters/nsIndexedToHTML.h +++ b/netwerk/streamconv/converters/nsIndexedToHTML.h @@ -14,7 +14,6 @@ #define NS_NSINDEXEDTOHTMLCONVERTER_CID \ { 0xcf0f71fd, 0xfafd, 0x4e2b, {0x9f, 0xdc, 0x13, 0x4d, 0x97, 0x2e, 0x16, 0xe2} } -class nsIDateTimeFormat; class nsIStringBundle; class nsITextToSubURI; @@ -47,7 +46,6 @@ protected: nsCOMPtr mParser; nsCOMPtr mListener; // final listener (consumer) - nsCOMPtr mDateTime; nsCOMPtr mBundle; nsCOMPtr mTextToSubURI; diff --git a/security/manager/ssl/TransportSecurityInfo.cpp b/security/manager/ssl/TransportSecurityInfo.cpp index cebc0e9a2895..0bada23f2aa7 100644 --- a/security/manager/ssl/TransportSecurityInfo.cpp +++ b/security/manager/ssl/TransportSecurityInfo.cpp @@ -6,12 +6,12 @@ #include "TransportSecurityInfo.h" +#include "DateTimeFormat.h" #include "PSMRunnable.h" #include "mozilla/Casting.h" #include "nsComponentManagerUtils.h" #include "nsIArray.h" #include "nsICertOverrideService.h" -#include "nsIDateTimeFormat.h" #include "nsIObjectInputStream.h" #include "nsIObjectOutputStream.h" #include "nsIWebProgressListener.h" @@ -798,14 +798,9 @@ GetDateBoundary(nsIX509Cert* ix509, trueExpired_falseNotYetValid = false; } - nsCOMPtr dateTimeFormat = nsIDateTimeFormat::Create(); - if (!dateTimeFormat) { - return; - } - - dateTimeFormat->FormatPRTime(nullptr, kDateFormatLong, kTimeFormatNoSeconds, + DateTimeFormat::FormatPRTime(kDateFormatLong, kTimeFormatNoSeconds, timeToUse, formattedDate); - dateTimeFormat->FormatPRTime(nullptr, kDateFormatLong, kTimeFormatNoSeconds, + DateTimeFormat::FormatPRTime(kDateFormatLong, kTimeFormatNoSeconds, now, nowDate); } diff --git a/security/manager/ssl/nsNSSCertHelper.cpp b/security/manager/ssl/nsNSSCertHelper.cpp index 8ca8c14b6046..0a5515dec53b 100644 --- a/security/manager/ssl/nsNSSCertHelper.cpp +++ b/security/manager/ssl/nsNSSCertHelper.cpp @@ -6,6 +6,7 @@ #include +#include "DateTimeFormat.h" #include "ScopedNSSTypes.h" #include "mozilla/Assertions.h" #include "mozilla/Casting.h" @@ -14,8 +15,6 @@ #include "mozilla/UniquePtr.h" #include "nsCOMPtr.h" #include "nsComponentManagerUtils.h" -#include "nsDateTimeFormatCID.h" -#include "nsIDateTimeFormat.h" #include "nsNSSASN1Object.h" #include "nsNSSCertTrust.h" #include "nsNSSCertValidity.h" @@ -1615,20 +1614,15 @@ static nsresult ProcessTime(PRTime dispTime, const char16_t* displayName, nsIASN1Sequence* parentSequence) { - nsCOMPtr dateFormatter = nsIDateTimeFormat::Create(); - if (!dateFormatter) { - return NS_ERROR_FAILURE; - } - nsString text; nsString tempString; PRExplodedTime explodedTime; PR_ExplodeTime(dispTime, PR_LocalTimeParameters, &explodedTime); - dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong, - kTimeFormatSeconds, &explodedTime, - tempString); + DateTimeFormat::FormatPRExplodedTime(kDateFormatLong, + kTimeFormatSeconds, &explodedTime, + tempString); text.Append(tempString); text.AppendLiteral("\n("); @@ -1636,9 +1630,9 @@ ProcessTime(PRTime dispTime, const char16_t* displayName, PRExplodedTime explodedTimeGMT; PR_ExplodeTime(dispTime, PR_GMTParameters, &explodedTimeGMT); - dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong, - kTimeFormatSeconds, &explodedTimeGMT, - tempString); + DateTimeFormat::FormatPRExplodedTime(kDateFormatLong, + kTimeFormatSeconds, &explodedTimeGMT, + tempString); text.Append(tempString); text.AppendLiteral(" GMT)"); diff --git a/security/manager/ssl/nsNSSCertValidity.cpp b/security/manager/ssl/nsNSSCertValidity.cpp index 536aafc8f456..bf9c56aafd2e 100644 --- a/security/manager/ssl/nsNSSCertValidity.cpp +++ b/security/manager/ssl/nsNSSCertValidity.cpp @@ -62,16 +62,11 @@ nsX509CertValidity::FormatTime(const PRTime& aTimeDate, if (!mTimesInitialized) return NS_ERROR_FAILURE; - nsCOMPtr dateFormatter = nsIDateTimeFormat::Create(); - if (!dateFormatter) { - return NS_ERROR_FAILURE; - } - PRExplodedTime explodedTime; PR_ExplodeTime(const_cast(aTimeDate), aParamFn, &explodedTime); - return dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong, - aTimeFormatSelector, - &explodedTime, aFormattedTimeDate); + return mozilla::DateTimeFormat::FormatPRExplodedTime(kDateFormatLong, + aTimeFormatSelector, + &explodedTime, aFormattedTimeDate); } NS_IMETHODIMP diff --git a/security/manager/ssl/nsNSSCertValidity.h b/security/manager/ssl/nsNSSCertValidity.h index 79c95e383bac..e8f337a02616 100644 --- a/security/manager/ssl/nsNSSCertValidity.h +++ b/security/manager/ssl/nsNSSCertValidity.h @@ -5,8 +5,8 @@ #ifndef nsNSSCertValidity_h #define nsNSSCertValidity_h +#include "DateTimeFormat.h" #include "ScopedNSSTypes.h" -#include "nsIDateTimeFormat.h" #include "nsIX509CertValidity.h" #include "nsNSSShutDown.h" diff --git a/toolkit/components/filepicker/nsFileView.cpp b/toolkit/components/filepicker/nsFileView.cpp index 2110a92d2cfa..257bfdec3c33 100644 --- a/toolkit/components/filepicker/nsFileView.cpp +++ b/toolkit/components/filepicker/nsFileView.cpp @@ -3,6 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "DateTimeFormat.h" #include "nsIFileView.h" #include "nsITreeView.h" #include "mozilla/ModuleUtils.h" @@ -14,7 +15,6 @@ #include "nsReadableUtils.h" #include "nsCRT.h" #include "nsPrintfCString.h" -#include "nsIDateTimeFormat.h" #include "nsQuickSort.h" #include "nsIAtom.h" #include "nsIAutoCompleteResult.h" @@ -234,7 +234,6 @@ protected: nsCOMPtr mDirectoryPath; nsCOMPtr mTree; nsCOMPtr mSelection; - nsCOMPtr mDateFormatter; int16_t mSortType; int32_t mTotalRows; @@ -291,10 +290,6 @@ nsFileView::~nsFileView() nsresult nsFileView::Init() { - mDateFormatter = nsIDateTimeFormat::Create(); - if (!mDateFormatter) - return NS_ERROR_OUT_OF_MEMORY; - return NS_OK; } @@ -730,8 +725,8 @@ nsFileView::GetCellText(int32_t aRow, nsITreeColumn* aCol, curFile->GetLastModifiedTime(&lastModTime); // XXX FormatPRTime could take an nsAString& nsAutoString temp; - mDateFormatter->FormatPRTime(nullptr, kDateFormatShort, kTimeFormatSeconds, - lastModTime * 1000, temp); + mozilla::DateTimeFormat::FormatPRTime(kDateFormatShort, kTimeFormatSeconds, + lastModTime * 1000, temp); aCellText = temp; } else { // file size