From 1814320b35feaf42ead1b9bc2958c1d82bcb945b Mon Sep 17 00:00:00 2001 From: "nhotta%netscape.com" Date: Thu, 28 Jan 1999 00:24:18 +0000 Subject: [PATCH] Changed to call WinAPI for date time format. --- .../src/windows/nsDateTimeFormatWin.cpp | 126 +++++++++++++++--- intl/locale/src/windows/nsDateTimeFormatWin.h | 1 + 2 files changed, 107 insertions(+), 20 deletions(-) diff --git a/intl/locale/src/windows/nsDateTimeFormatWin.cpp b/intl/locale/src/windows/nsDateTimeFormatWin.cpp index 705f28ff27ad..3e9f79555e36 100644 --- a/intl/locale/src/windows/nsDateTimeFormatWin.cpp +++ b/intl/locale/src/windows/nsDateTimeFormatWin.cpp @@ -18,6 +18,9 @@ #include "nsDateTimeFormatWin.h" + +#define NSDATETIMEFORMAT_BUFFER_LEN 80 + NS_DEFINE_IID(kIDateTimeFormatIID, NS_IDATETIMEFORMAT_IID); NS_IMPL_ISUPPORTS(nsDateTimeFormatWin, kIDateTimeFormatIID); @@ -29,20 +32,7 @@ nsresult nsDateTimeFormatWin::FormatTime(const nsString& locale, PRUnichar *stringOut, PRUint32 *outLen) { - // Temporary implementation, real implementation to be done by FE. - // - - struct tm *today; - char *str; - - today = localtime( &timetTime ); - str = asctime(today); - - nsString aString(str); - *outLen = aString.Length(); - memcpy((void *) stringOut, (void *) aString.GetUnicode(), aString.Length() * sizeof(PRUnichar)); - - return NS_OK; + return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, localtime( &timetTime ), stringOut, outLen); } // performs a locale sensitive date formatting operation on the struct tm parameter @@ -54,14 +44,110 @@ nsresult nsDateTimeFormatWin::FormatTMTime(const nsString& locale, PRUnichar *stringOut, PRUint32 *outLen) { - // Temporary implementation, real implementation to be done by FE. - // + SYSTEMTIME system_time; + DWORD dwFlags_Date = 0, dwFlags_Time = 0; + int dateLen, timeLen; + PRUnichar dateBuffer[NSDATETIMEFORMAT_BUFFER_LEN], timeBuffer[NSDATETIMEFORMAT_BUFFER_LEN]; - char *str = asctime(tmTime); + // 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; - nsString aString(str); - *outLen = aString.Length(); - memcpy((void *) stringOut, (void *) aString.GetUnicode(), aString.Length() * sizeof(PRUnichar)); + // 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 = GetDateFormatW(GetUserDefaultLCID(), 0, &system_time, L"yy/MM", + dateBuffer, NSDATETIMEFORMAT_BUFFER_LEN); + } + else if (dateFormatSelector == kDateFormatWeekday) { + dateLen = GetDateFormatW(GetUserDefaultLCID(), 0, &system_time, L"ddd", + dateBuffer, NSDATETIMEFORMAT_BUFFER_LEN); + } + else { + dateLen = GetDateFormatW(GetUserDefaultLCID(), dwFlags_Date, &system_time, NULL, + dateBuffer, NSDATETIMEFORMAT_BUFFER_LEN); + } + if (dateLen != 0) { + dateLen--; // Since the count includes the terminating null. + } + } + + // Call GetTimeFormatW + if (timeFormatSelector == kTimeFormatNone) { + timeLen = 0; + } + else { + timeLen = GetTimeFormatW(GetUserDefaultLCID(), dwFlags_Time, &system_time, NULL, + timeBuffer, NSDATETIMEFORMAT_BUFFER_LEN); + if (timeLen != 0) { + timeLen--; // Since the count includes the terminating null. + } + } + + NS_ASSERTION(NSDATETIMEFORMAT_BUFFER_LEN >= (PRUint32) (dateLen + 1), "internal date buffer is not large enough"); + NS_ASSERTION(NSDATETIMEFORMAT_BUFFER_LEN >= (PRUint32) (timeLen + 1), "internal time buffer is not large enough"); + NS_ASSERTION(*outLen >= (PRUint32) (dateLen + timeLen + 1 + 1), "input buffer is not large enough"); + + // Copy the result + stringOut[0] = 0; + if (dateLen != 0 && timeLen != 0) { + (void) wcscpy((wchar_t *) stringOut, (const wchar_t *) dateBuffer); + (void) wcscat((wchar_t *) stringOut, (const wchar_t *) L" "); + (void) wcscat((wchar_t *) stringOut, (const wchar_t *) timeBuffer); + *outLen = dateLen + timeLen + 1; + } + else if (timeLen == 0) { + (void) wcscpy((wchar_t *) stringOut, (const wchar_t *) dateBuffer); + *outLen = dateLen; + } + else if (dateLen == 0) { + (void) wcscpy((wchar_t *) stringOut, (const wchar_t *) timeBuffer); + *outLen = timeLen; + } + else { + *outLen = 0; + } return NS_OK; } diff --git a/intl/locale/src/windows/nsDateTimeFormatWin.h b/intl/locale/src/windows/nsDateTimeFormatWin.h index 76bed5c9d69b..6f97c0f47b64 100644 --- a/intl/locale/src/windows/nsDateTimeFormatWin.h +++ b/intl/locale/src/windows/nsDateTimeFormatWin.h @@ -21,6 +21,7 @@ #include "nsIDateTimeFormat.h" +#include class nsDateTimeFormatWin : public nsIDateTimeFormat {