Back to index

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

#include <nsDateTimeFormatUnix.h>

Inheritance diagram for nsDateTimeFormatUnix:
Inheritance graph
[legend]
Collaboration diagram for nsDateTimeFormatUnix:
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)
 nsDateTimeFormatUnix ()
virtual ~nsDateTimeFormatUnix ()

Private Member Functions

NS_IMETHOD Initialize (nsILocale *locale)
void LocalePreferred24hour ()

Private Attributes

nsString mLocale
nsString mAppLocale
nsCString mCharset
nsCString mPlatformLocale
PRBool mLocalePreferred24hour
PRBool mLocaleAMPMfirst
nsCOMPtr< nsIUnicodeDecodermDecoder

Detailed Description

Definition at line 49 of file nsDateTimeFormatUnix.h.


Constructor & Destructor Documentation

Definition at line 83 of file nsDateTimeFormatUnix.h.

{mLocale.Truncate();mAppLocale.Truncate();}
virtual nsDateTimeFormatUnix::~nsDateTimeFormatUnix ( ) [inline, virtual]

Definition at line 85 of file nsDateTimeFormatUnix.h.

{}

Member Function Documentation

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

Implements nsIDateTimeFormat.

Definition at line 287 of file nsDateTimeFormatUnix.cpp.

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Implements nsIDateTimeFormat.

Definition at line 274 of file nsDateTimeFormatUnix.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 nsDateTimeFormatUnix::FormatTime ( nsILocale locale,
const nsDateFormatSelector  dateFormatSelector,
const nsTimeFormatSelector  timeFormatSelector,
const time_t  timetTime,
nsString stringOut 
) [virtual]

Implements nsIDateTimeFormat.

Definition at line 169 of file nsDateTimeFormatUnix.cpp.

{
  struct tm tmTime;
  memcpy(&tmTime, localtime(&timetTime), sizeof(struct tm));
  return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, &tmTime, stringOut);
}

Here is the call graph for this function:

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

Implements nsIDateTimeFormat.

Definition at line 181 of file nsDateTimeFormatUnix.cpp.

{
#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
  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, 
                 mLocalePreferred24hour ? "%H:%M:%S" : mLocaleAMPMfirst ? "%p %I:%M:%S" : "%I:%M:%S %p", 
                 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, NULL);
  (void) setlocale(LC_TIME, mPlatformLocale.get());
  if (PL_strlen(fmtD) && PL_strlen(fmtT)) {
    PL_strncat(fmtD, " ", NSDATETIME_FORMAT_BUFFER_LEN);
    PL_strncat(fmtD, fmtT, NSDATETIME_FORMAT_BUFFER_LEN);
    strftime(strOut, NSDATETIME_FORMAT_BUFFER_LEN*2, fmtD, tmTime);
  }
  else if (PL_strlen(fmtD) && !PL_strlen(fmtT)) {
    strftime(strOut, NSDATETIME_FORMAT_BUFFER_LEN*2, fmtD, tmTime);
  }
  else if (!PL_strlen(fmtD) && PL_strlen(fmtT)) {
    strftime(strOut, NSDATETIME_FORMAT_BUFFER_LEN*2, fmtT, tmTime);
  }
  else {
    PL_strncpy(strOut, "", NSDATETIME_FORMAT_BUFFER_LEN*2);
  }
  (void) setlocale(LC_TIME, old_locale);

  // convert result to unicode
  PRInt32 srcLength = (PRInt32) PL_strlen(strOut);
  PRInt32 unicharLength = NSDATETIME_FORMAT_BUFFER_LEN*2;
  PRUnichar 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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 55 of file nsDateTimeFormatUnix.cpp.

{
  nsAutoString localeStr;
  NS_NAMED_LITERAL_STRING(aCategory, "NSILOCALE_TIME##PLATFORM");
  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(aCategory, localeStr);
    if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) {
      if (!mLocale.IsEmpty() &&
          mLocale.Equals(localeStr,
                         nsCaseInsensitiveStringComparator())) {
        return NS_OK;
      }
    }
  }

  mCharset.AssignLiteral("ISO-8859-1");
  mPlatformLocale.Assign("en_US");

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

    nsCOMPtr <nsIPosixLocale> posixLocale = do_GetService(NS_POSIXLOCALE_CONTRACTID, &res);
    if (NS_SUCCEEDED(res)) {
      res = posixLocale->GetPlatformLocale(mLocale, mPlatformLocale);
    }

    nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res);
    if (NS_SUCCEEDED(res)) {
      nsCAutoString mappedCharset;
      res = platformCharset->GetDefaultCharsetForLocale(mLocale, mappedCharset);
      if (NS_SUCCEEDED(res)) {
        mCharset = mappedCharset;
      }
    }
  }

  // Initialize unicode decoder
  nsCOMPtr <nsICharsetConverterManager>  charsetConverterManager;
  charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
  if (NS_SUCCEEDED(res)) {
    res = charsetConverterManager->GetUnicodeDecoder(mCharset.get(), getter_AddRefs(mDecoder));
  }

  LocalePreferred24hour();

  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 133 of file nsDateTimeFormatUnix.cpp.

{
  char str[100];
  time_t tt;
  struct tm *tmc;
  int i;

  tt = time((time_t)NULL);
  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 = PR_FALSE;
  for (i=0; str[i]; i++) {
    if (str[i] == '2') {    // if there is any '2', that locale use 0-23 time format
        mLocalePreferred24hour = PR_TRUE;
        break;
    }
  }

  mLocaleAMPMfirst = PR_TRUE;
  if (mLocalePreferred24hour == PR_FALSE) {
    if (str[0] && str[0] == '1') { // if the first character is '1' of 10:00,
                                // AMPM string is located after 10:00
      mLocaleAMPMfirst = PR_FALSE;
    }
  }
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 94 of file nsDateTimeFormatUnix.h.

Definition at line 95 of file nsDateTimeFormatUnix.h.

Definition at line 99 of file nsDateTimeFormatUnix.h.

Definition at line 93 of file nsDateTimeFormatUnix.h.

Definition at line 98 of file nsDateTimeFormatUnix.h.

Definition at line 97 of file nsDateTimeFormatUnix.h.

Definition at line 96 of file nsDateTimeFormatUnix.h.


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