Back to index

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

#include <nsDateTimeFormatMac.h>

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

Private Member Functions

NS_IMETHOD Initialize (nsILocale *locale)

Private Attributes

nsString mLocale
nsString mAppLocale
nsCString mCharset
short mScriptcode
short mLangcode
short mRegioncode
bool mUseDefaultLocale
nsCOMPtr< nsIUnicodeDecodermDecoder

Detailed Description

Definition at line 48 of file nsDateTimeFormatMac.h.


Constructor & Destructor Documentation

Definition at line 80 of file nsDateTimeFormatMac.h.

{}
virtual nsDateTimeFormatMac::~nsDateTimeFormatMac ( ) [inline, virtual]

Definition at line 82 of file nsDateTimeFormatMac.h.

{}

Member Function Documentation

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

Implements nsIDateTimeFormat.

Definition at line 470 of file nsDateTimeFormatMac.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 nsDateTimeFormatMac::FormatPRTime ( nsILocale locale,
const nsDateFormatSelector  dateFormatSelector,
const nsTimeFormatSelector  timeFormatSelector,
const PRTime  prTime,
nsString stringOut 
) [virtual]

Implements nsIDateTimeFormat.

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

Implements nsIDateTimeFormat.

Definition at line 320 of file nsDateTimeFormatMac.cpp.

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

Here is the call graph for this function:

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

Implements nsIDateTimeFormat.

Definition at line 339 of file nsDateTimeFormatMac.cpp.

{
  DateTimeRec macDateTime;
  Str255 timeString, dateString;
  int32 dateTime;    
  nsresult res;

  // set up locale data
  (void) Initialize(locale);
  
  // return, nothing to format
  if (dateFormatSelector == kDateFormatNone && timeFormatSelector == kTimeFormatNone) {
    stringOut.SetLength(0);
    return NS_OK;
  }

  stringOut.AssignWithConversion(asctime(tmTime));      // set the default string, in case for API/conversion errors
  
  // convert struct tm to input format of mac toolbox call
  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");

  macDateTime.year = tmTime->tm_year + 1900;     

  // Mac use 1 for Jan and 12 for Dec
  // tm use 0 for Jan and 11 for Dec 
  macDateTime.month = tmTime->tm_mon + 1; 
  macDateTime.day = tmTime->tm_mday;
  macDateTime.hour = tmTime->tm_hour;
  macDateTime.minute = tmTime->tm_min;
  macDateTime.second = tmTime->tm_sec;

  // Mac use 1 for sunday 7 for saturday
  // tm use 0 for sunday 6 for saturday 
  macDateTime.dayOfWeek = tmTime->tm_wday +1 ; 

  ::DateToSeconds( &macDateTime, (unsigned long *) &dateTime);
  
  // specify itl if not using a default locale
  Handle itl1Handle = mUseDefaultLocale ? nil : (Handle) GetItl1Resource(mScriptcode, mRegioncode);
  Handle itl0Handle = mUseDefaultLocale ? nil : (Handle) GetItl0Resource(mScriptcode, mRegioncode);

  // get time string
  if (timeFormatSelector != kTimeFormatNone) {
    // modify itl0 to force 24 hour time cycle !
    if ( itl0Handle &&
       (timeFormatSelector == kTimeFormatSecondsForce24Hour || 
        timeFormatSelector == kTimeFormatNoSecondsForce24Hour)) {
      Intl0Hndl itl0HandleToModify = (Intl0Hndl) itl0Handle;
      UInt8 timeCycle = (**itl0HandleToModify).timeCycle;
      (**itl0HandleToModify).timeCycle = timeCycle24;
      ::TimeString(dateTime, (timeFormatSelector == kTimeFormatSeconds), timeString, itl0Handle);
      (**itl0HandleToModify).timeCycle = timeCycle;
    }
    else {
      ::TimeString(dateTime, (timeFormatSelector == kTimeFormatSeconds), timeString, itl0Handle);
    }
  }
  
  // get date string
  switch (dateFormatSelector) {
    case kDateFormatLong:
      ::DateString(dateTime, abbrevDate, dateString, itl1Handle);
      break;
    case kDateFormatShort:
      ::DateString(dateTime, shortDate, dateString, itl0Handle);
      break;
    case kDateFormatYearMonth:
      dateString[0] =  strftime((char*)&dateString[1],254,"%y/%m",tmTime);
      break;
    case kDateFormatWeekday:
      AbbrevWeekdayString(macDateTime, dateString, (Intl1Hndl)itl1Handle);
      // try fallback if it return with null string.
      if(dateString[0] == 0) {     //     cannot get weekdayString from itl1 , try fallback
        dateString[0] =  strftime((char*)&dateString[1],254,"%a",tmTime);
      }
      break;
  }
  
  // construct a C string
  char localBuffer[2 * 255 + 2 + 1]; // 2 255-byte-max pascal strings, plus
                                     // a space and a null.
  if (dateFormatSelector != kDateFormatNone && timeFormatSelector != kTimeFormatNone) {
    CopyPascalStringToC(dateString, localBuffer);
    localBuffer[dateString[0]] = ' ';
    CopyPascalStringToC(timeString, &(localBuffer[dateString[0] + 1]));
  }
  else if (dateFormatSelector != kDateFormatNone) {
    CopyPascalStringToC(dateString, localBuffer);
  }
  else if (timeFormatSelector != kTimeFormatNone) {
    CopyPascalStringToC(timeString, localBuffer);
  }

  // convert result to unicode
  if (mDecoder) {
    PRInt32 srcLength = (PRInt32) PL_strlen(localBuffer);
    PRInt32 unicharLength = sizeof(Str255)*2;
    PRUnichar unichars[sizeof(Str255)*2];   // buffer for combined date and time

    res = mDecoder->Convert(localBuffer, &srcLength, unichars, &unicharLength);
    if (NS_SUCCEEDED(res)) {
      stringOut.Assign(unichars, unicharLength);
    }
  }
  
  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 235 of file nsDateTimeFormatMac.cpp.

{
  nsAutoString localeStr;
  nsAutoString category(NS_LITERAL_STRING("NSILOCALE_TIME"));
  nsresult res;

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

  mScriptcode = smSystemScript;
  mLangcode = langEnglish;
  mRegioncode = verUS;
  mCharset.AssignLiteral("x-mac-roman");
  

  // get application 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(category, localeStr);
      if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) {
        mAppLocale = localeStr; // cache app locale name
      }
    }
  }
  
  // use app default if no locale specified
  if (nsnull == locale) {
    mUseDefaultLocale = true;
  }
  else {
    mUseDefaultLocale = false;
    res = locale->GetCategory(category, localeStr);
  }
    
  // Get a script code and charset name from locale, if available
  if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) {
    mLocale.Assign(localeStr); // cache locale name

    nsCOMPtr <nsIMacLocale> macLocale = do_GetService(NS_MACLOCALE_CONTRACTID, &res);
    if (NS_SUCCEEDED(res)) {
      res = macLocale->GetPlatformLocale(mLocale, &mScriptcode, &mLangcode, &mRegioncode);
    }

    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 <nsIAtom>                      charsetAtom;
  nsCOMPtr <nsICharsetConverterManager>  charsetConverterManager;
  charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
  if (NS_SUCCEEDED(res)) {
    res = charsetConverterManager->GetUnicodeDecoder(mCharset.get(),
                                                     getter_AddRefs(mDecoder));
  }
  
  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 89 of file nsDateTimeFormatMac.h.

Definition at line 90 of file nsDateTimeFormatMac.h.

Definition at line 95 of file nsDateTimeFormatMac.h.

Definition at line 92 of file nsDateTimeFormatMac.h.

Definition at line 88 of file nsDateTimeFormatMac.h.

Definition at line 93 of file nsDateTimeFormatMac.h.

Definition at line 91 of file nsDateTimeFormatMac.h.

Definition at line 94 of file nsDateTimeFormatMac.h.


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