Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Member Functions | Private Attributes
nsDateTimeFormatWin Class Reference

#include <nsDateTimeFormatWin.h>

Inheritance diagram for nsDateTimeFormatWin:
Inheritance graph
[legend]
Collaboration diagram for nsDateTimeFormatWin:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS NS_IMETHOD FormatTime (nsILocale *locale, const nsDateFormatSelector dateFormatSelector, const nsTimeFormatSelector timeFormatSelector, const time_t timetTime, nsString &stringOut)
NS_IMETHOD FormatTMTime (nsILocale *locale, const nsDateFormatSelector dateFormatSelector, const nsTimeFormatSelector timeFormatSelector, const struct tm *tmTime, nsString &stringOut)
NS_IMETHOD FormatPRTime (nsILocale *locale, const nsDateFormatSelector dateFormatSelector, const nsTimeFormatSelector timeFormatSelector, const PRTime prTime, nsString &stringOut)
NS_IMETHOD FormatPRExplodedTime (nsILocale *locale, const nsDateFormatSelector dateFormatSelector, const nsTimeFormatSelector timeFormatSelector, const PRExplodedTime *explodedTime, nsString &stringOut)
 nsDateTimeFormatWin ()
virtual ~nsDateTimeFormatWin ()

Private Member Functions

NS_IMETHOD Initialize (nsILocale *locale)
int nsGetTimeFormatW (DWORD dwFlags, const SYSTEMTIME *lpTime, const char *format, PRUnichar *timeStr, int cchTime)
int nsGetDateFormatW (DWORD dwFlags, const SYSTEMTIME *lpDate, const char *format, PRUnichar *dateStr, int cchDate)

Private Attributes

nsString mLocale
nsString mAppLocale
PRBool mW_API
PRUint32 mLCID

Detailed Description

Definition at line 49 of file nsDateTimeFormatWin.h.


Constructor & Destructor Documentation

Definition at line 82 of file nsDateTimeFormatWin.h.

{mLocale.SetLength(0);mAppLocale.SetLength(0);}
virtual nsDateTimeFormatWin::~nsDateTimeFormatWin ( ) [inline, virtual]

Definition at line 85 of file nsDateTimeFormatWin.h.

{}

Member Function Documentation

nsresult nsDateTimeFormatWin::FormatPRExplodedTime ( nsILocale locale,
const nsDateFormatSelector  dateFormatSelector,
const nsTimeFormatSelector  timeFormatSelector,
const PRExplodedTime explodedTime,
nsString stringOut 
) [virtual]

Implements nsIDateTimeFormat.

Definition at line 261 of file nsDateTimeFormatWin.cpp.

{
  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);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsDateTimeFormatWin::FormatPRTime ( nsILocale locale,
const nsDateFormatSelector  dateFormatSelector,
const nsTimeFormatSelector  timeFormatSelector,
const PRTime  prTime,
nsString stringOut 
) [virtual]

Implements nsIDateTimeFormat.

Definition at line 248 of file nsDateTimeFormatWin.cpp.

{
  PRExplodedTime explodedTime;
  PR_ExplodeTime(prTime, PR_LocalTimeParameters, &explodedTime);

  return FormatPRExplodedTime(locale, dateFormatSelector, timeFormatSelector, &explodedTime, stringOut);
}

Here is the call graph for this function:

nsresult nsDateTimeFormatWin::FormatTime ( nsILocale locale,
const nsDateFormatSelector  dateFormatSelector,
const nsTimeFormatSelector  timeFormatSelector,
const time_t  timetTime,
nsString stringOut 
) [virtual]

Implements nsIDateTimeFormat.

Definition at line 127 of file nsDateTimeFormatWin.cpp.

{
  return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, localtime( &timetTime ), stringOut);
}

Here is the call graph for this function:

nsresult nsDateTimeFormatWin::FormatTMTime ( nsILocale locale,
const nsDateFormatSelector  dateFormatSelector,
const nsTimeFormatSelector  timeFormatSelector,
const struct tm tmTime,
nsString stringOut 
) [virtual]

Implements nsIDateTimeFormat.

Definition at line 137 of file nsDateTimeFormatWin.cpp.

{
  SYSTEMTIME system_time;
  DWORD dwFlags_Date = 0, dwFlags_Time = 0;
  int dateLen, timeLen;
  PRUnichar 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, 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 = nsGetTimeFormatW(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");

  // Copy the result
  stringOut.SetLength(0);
  if (dateLen != 0 && timeLen != 0) {
    stringOut.Assign(dateBuffer, dateLen);
    stringOut.Append((PRUnichar *)(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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 57 of file nsDateTimeFormatWin.cpp.

{
  nsAutoString localeStr;
  nsresult res = NS_OK;

  // use cached info if match with stored locale
  if (NULL == 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;
      }
    }
  }

  // get os version
  OSVERSIONINFO os;
  os.dwOSVersionInfoSize = sizeof(os);
  ::GetVersionEx(&os);
  if (VER_PLATFORM_WIN32_NT == os.dwPlatformId && os.dwMajorVersion >= 4) {
    mW_API = PR_TRUE;   // has W API
  }
  else {
    mW_API = PR_FALSE;
  }

  // default LCID (en-US)
  mLCID = 1033;

  // get locale string, use app default if no locale specified
  if (NULL == locale) {
    nsCOMPtr<nsILocaleService> localeService = 
             do_GetService(NS_LOCALESERVICE_CONTRACTID);
    if (localeService) {
      nsCOMPtr<nsILocale> 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

    nsCOMPtr <nsIWin32Locale> win32Locale = do_GetService(NS_WIN32LOCALE_CONTRACTID);
    if (win32Locale) {
      res = win32Locale->GetPlatformLocale(mLocale, (LCID *) &mLCID);
    }
  }

  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int nsDateTimeFormatWin::nsGetDateFormatW ( DWORD  dwFlags,
const SYSTEMTIME *  lpDate,
const char *  format,
PRUnichar dateStr,
int  cchDate 
) [private]

Definition at line 310 of file nsDateTimeFormatWin.cpp.

{
  int len = 0;

#ifndef WINCE // Always use wide APIs on Win CE.
  if (mW_API) {
#endif
    nsString formatString; if (format) formatString.AssignWithConversion(format);
    LPCWSTR wstr = format ? (LPCWSTR) formatString.get() : NULL;
    len = GetDateFormatW(mLCID, dwFlags, lpDate, wstr, (LPWSTR) dateStr, cchDate);
#ifndef WINCE // Always use wide APIs on Win CE.
  }
  else {
    char cstr_date[NSDATETIMEFORMAT_BUFFER_LEN];

    len = GetDateFormatA(mLCID, dwFlags, lpDate, (LPCSTR) format, 
                         (LPSTR) cstr_date, NSDATETIMEFORMAT_BUFFER_LEN);

    // convert result to unicode
    if (len > 0)
      len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR) cstr_date, len, (LPWSTR) dateStr, cchDate);
  }
#endif
  return len;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int nsDateTimeFormatWin::nsGetTimeFormatW ( DWORD  dwFlags,
const SYSTEMTIME *  lpTime,
const char *  format,
PRUnichar timeStr,
int  cchTime 
) [private]

Definition at line 283 of file nsDateTimeFormatWin.cpp.

{
  int len = 0;

#ifndef WINCE // Always use wide APIs on Win CE.
  if (mW_API) {
#endif
    nsString formatString; if (format) formatString.AssignWithConversion(format);
    LPCWSTR wstr = format ? (LPCWSTR) formatString.get() : NULL;
    len = GetTimeFormatW(mLCID, dwFlags, lpTime, wstr, (LPWSTR) timeStr, cchTime);
#ifndef WINCE // Always use wide APIs on Win CE.
  }
  else {
    char cstr_time[NSDATETIMEFORMAT_BUFFER_LEN];

    len = GetTimeFormatA(mLCID, dwFlags, lpTime, (LPCSTR) format, 
                         (LPSTR) cstr_time, NSDATETIMEFORMAT_BUFFER_LEN);

    // convert result to unicode
    if (len > 0)
      len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR) cstr_time, len, (LPWSTR) timeStr, cchTime);
  }
#endif
  return len;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 100 of file nsDateTimeFormatWin.h.

Definition at line 102 of file nsDateTimeFormatWin.h.

Definition at line 99 of file nsDateTimeFormatWin.h.

Definition at line 101 of file nsDateTimeFormatWin.h.


The documentation for this class was generated from the following files: