gecko-dev/intl/locale/nsScriptableDateFormat.cpp
Birunthan Mohanathas aef3149a23 Bug 1038537 - Part 3: Flatten intl/locale/src/ directory. r=smontagu
--HG--
rename : intl/locale/src/Makefile.in => intl/locale/Makefile.in
rename : intl/locale/src/PluralForm.jsm => intl/locale/PluralForm.jsm
rename : intl/locale/src/langGroups.properties => intl/locale/langGroups.properties
rename : intl/locale/src/language.properties => intl/locale/language.properties
rename : intl/locale/src/mac/moz.build => intl/locale/mac/moz.build
rename : intl/locale/src/mac/nsCollationMacUC.cpp => intl/locale/mac/nsCollationMacUC.cpp
rename : intl/locale/src/mac/nsCollationMacUC.h => intl/locale/mac/nsCollationMacUC.h
rename : intl/locale/src/mac/nsDateTimeFormatMac.cpp => intl/locale/mac/nsDateTimeFormatMac.cpp
rename : intl/locale/src/mac/nsDateTimeFormatMac.h => intl/locale/mac/nsDateTimeFormatMac.h
rename : intl/locale/src/mac/nsMacCharset.cpp => intl/locale/mac/nsMacCharset.cpp
rename : intl/locale/src/nsCollation.cpp => intl/locale/nsCollation.cpp
rename : intl/locale/src/nsCollation.h => intl/locale/nsCollation.h
rename : intl/locale/src/nsLanguageAtomService.cpp => intl/locale/nsLanguageAtomService.cpp
rename : intl/locale/src/nsLanguageAtomService.h => intl/locale/nsLanguageAtomService.h
rename : intl/locale/src/nsLocale.cpp => intl/locale/nsLocale.cpp
rename : intl/locale/src/nsLocale.h => intl/locale/nsLocale.h
rename : intl/locale/src/nsLocaleConstructors.h => intl/locale/nsLocaleConstructors.h
rename : intl/locale/src/nsLocaleService.cpp => intl/locale/nsLocaleService.cpp
rename : intl/locale/src/nsPlatformCharset.h => intl/locale/nsPlatformCharset.h
rename : intl/locale/src/nsScriptableDateFormat.cpp => intl/locale/nsScriptableDateFormat.cpp
rename : intl/locale/src/nsUConvPropertySearch.cpp => intl/locale/nsUConvPropertySearch.cpp
rename : intl/locale/src/nsUConvPropertySearch.h => intl/locale/nsUConvPropertySearch.h
rename : intl/locale/src/props2arrays.py => intl/locale/props2arrays.py
rename : intl/locale/src/unix/Makefile.in => intl/locale/unix/Makefile.in
rename : intl/locale/src/unix/moz.build => intl/locale/unix/moz.build
rename : intl/locale/src/unix/nsAndroidCharset.cpp => intl/locale/unix/nsAndroidCharset.cpp
rename : intl/locale/src/unix/nsCollationUnix.cpp => intl/locale/unix/nsCollationUnix.cpp
rename : intl/locale/src/unix/nsCollationUnix.h => intl/locale/unix/nsCollationUnix.h
rename : intl/locale/src/unix/nsDateTimeFormatUnix.cpp => intl/locale/unix/nsDateTimeFormatUnix.cpp
rename : intl/locale/src/unix/nsDateTimeFormatUnix.h => intl/locale/unix/nsDateTimeFormatUnix.h
rename : intl/locale/src/unix/nsPosixLocale.cpp => intl/locale/unix/nsPosixLocale.cpp
rename : intl/locale/src/unix/nsUNIXCharset.cpp => intl/locale/unix/nsUNIXCharset.cpp
rename : intl/locale/src/unix/unixcharset.properties => intl/locale/unix/unixcharset.properties
rename : intl/locale/src/windows/Makefile.in => intl/locale/windows/Makefile.in
rename : intl/locale/src/windows/moz.build => intl/locale/windows/moz.build
rename : intl/locale/src/windows/nsCollationWin.cpp => intl/locale/windows/nsCollationWin.cpp
rename : intl/locale/src/windows/nsCollationWin.h => intl/locale/windows/nsCollationWin.h
rename : intl/locale/src/windows/nsDateTimeFormatWin.cpp => intl/locale/windows/nsDateTimeFormatWin.cpp
rename : intl/locale/src/windows/nsDateTimeFormatWin.h => intl/locale/windows/nsDateTimeFormatWin.h
rename : intl/locale/src/windows/nsWin32Locale.cpp => intl/locale/windows/nsWin32Locale.cpp
rename : intl/locale/src/windows/nsWinCharset.cpp => intl/locale/windows/nsWinCharset.cpp
rename : intl/locale/src/windows/wincharset.properties => intl/locale/windows/wincharset.properties
2014-07-24 10:56:38 -07:00

145 lines
5.0 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 "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:
NS_DECL_ISUPPORTS
NS_IMETHOD FormatDateTime(const char16_t *locale,
nsDateFormatSelector dateFormatSelector,
nsTimeFormatSelector timeFormatSelector,
int32_t year,
int32_t month,
int32_t day,
int32_t hour,
int32_t minute,
int32_t second,
char16_t **dateTimeString);
NS_IMETHOD FormatDate(const char16_t *locale,
nsDateFormatSelector dateFormatSelector,
int32_t year,
int32_t month,
int32_t day,
char16_t **dateString)
{return FormatDateTime(locale, dateFormatSelector, kTimeFormatNone,
year, month, day, 0, 0, 0, dateString);}
NS_IMETHOD FormatTime(const char16_t *locale,
nsTimeFormatSelector timeFormatSelector,
int32_t hour,
int32_t minute,
int32_t second,
char16_t **timeString)
{return FormatDateTime(locale, kDateFormatNone, timeFormatSelector,
1999, 1, 1, hour, minute, second, timeString);}
nsScriptableDateFormat() {}
private:
nsString mStringOut;
virtual ~nsScriptableDateFormat() {}
};
NS_IMPL_ISUPPORTS(nsScriptableDateFormat, nsIScriptableDateFormat)
NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime(
const char16_t *aLocale,
nsDateFormatSelector dateFormatSelector,
nsTimeFormatSelector timeFormatSelector,
int32_t year,
int32_t month,
int32_t day,
int32_t hour,
int32_t minute,
int32_t second,
char16_t **dateTimeString)
{
// We can't have a valid date with the year, month or day
// being lower than 1.
if (year < 1 || month < 1 || day < 1)
return NS_ERROR_INVALID_ARG;
nsresult rv;
nsAutoString localeName(aLocale);
*dateTimeString = nullptr;
nsCOMPtr<nsILocale> locale;
// re-initialise locale pointer only if the locale was given explicitly
if (!localeName.IsEmpty()) {
// get locale service
nsCOMPtr<nsILocaleService> localeService(do_GetService(kLocaleServiceCID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
// get locale
rv = localeService->NewLocale(localeName, getter_AddRefs(locale));
NS_ENSURE_SUCCESS(rv, rv);
}
nsCOMPtr<nsIDateTimeFormat> dateTimeFormat(do_CreateInstance(kDateTimeFormatCID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
tm tmTime;
time_t timetTime;
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 = dateTimeFormat->FormatTime(locale, dateFormatSelector, timeFormatSelector,
timetTime, 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, false, &prtime))
return NS_ERROR_INVALID_ARG;
rv = dateTimeFormat->FormatPRTime(locale, dateFormatSelector, timeFormatSelector,
prtime, mStringOut);
}
if (NS_SUCCEEDED(rv))
*dateTimeString = ToNewUnicode(mStringOut);
return rv;
}
nsresult
NS_NewScriptableDateFormat(nsISupports* aOuter, REFNSIID aIID, void** aResult)
{
if (aOuter)
return NS_ERROR_NO_AGGREGATION;
nsScriptableDateFormat* result = new nsScriptableDateFormat();
if (! result)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(result);
nsresult rv = result->QueryInterface(aIID, aResult);
NS_RELEASE(result);
return rv;
}