Back to index

lightning-sunbird  0.9+nobinonly
nsDateTimeFormatOS2.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Mozilla OS/2 libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * John Fairhurst, <john_fairhurst@iname.com>.
00018  * Portions created by the Initial Developer are Copyright (C) 1999
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Henry Sobotka <sobotka@axess.com>
00023  *   IBM Corp.
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <unidef.h>
00040 #include "nsDateTimeFormatOS2.h"
00041 
00042 NS_IMPL_THREADSAFE_ISUPPORTS1(nsDateTimeFormatOS2,nsIDateTimeFormat)
00043 
00044 #define NSDATETIME_FORMAT_BUFFER_LEN  80
00045 
00046 #ifndef LOCI_iTime
00047 #define LOCI_iTime   ((LocaleItem)73)
00048 #endif
00049 
00050 nsresult nsDateTimeFormatOS2::FormatTime(nsILocale* locale, 
00051                                const nsDateFormatSelector  dateFormatSelector, 
00052                                const nsTimeFormatSelector  timeFormatSelector, 
00053                                const time_t                timetTime,
00054                                nsString                   &stringOut)
00055 {
00056   return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, localtime( &timetTime ), stringOut);
00057 }
00058 
00059 // performs a locale sensitive date formatting operation on the struct tm parameter
00060 nsresult nsDateTimeFormatOS2::FormatTMTime(nsILocale* locale, 
00061                                const nsDateFormatSelector  dateFormatSelector, 
00062                                const nsTimeFormatSelector  timeFormatSelector, 
00063                                const struct tm*            tmTime, 
00064                                nsString                   &stringOut)
00065 {
00066 
00067   nsresult rc = NS_ERROR_FAILURE;
00068   UniChar uFmtD[NSDATETIME_FORMAT_BUFFER_LEN] = { 0 };
00069   UniChar uFmtT[NSDATETIME_FORMAT_BUFFER_LEN] = { 0 };
00070   UniChar *pString = NULL;
00071   LocaleObject locObj = NULL;
00072   int ret = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &locObj);
00073   if (ret != ULS_SUCCESS)
00074     UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"C", &locObj);
00075 
00076   PRBool f24Hour = PR_FALSE;
00077 
00078   UniQueryLocaleItem(locObj, LOCI_iTime, &pString);
00079 
00080   if (pString[0] == '1') {
00081     f24Hour = PR_TRUE;
00082   }
00083 
00084   // set date format
00085   switch (dateFormatSelector) {
00086     case kDateFormatNone:
00087       UniStrcat( uFmtD, (UniChar*)L"");
00088       break; 
00089     case kDateFormatLong:
00090     case kDateFormatShort:
00091       UniStrcat( uFmtD, (UniChar*)L"%x");
00092       break; 
00093     case kDateFormatYearMonth:
00094       UniQueryLocaleItem( locObj, DATESEP, &pString);
00095       UniStrcat( uFmtD, (UniChar*)L"%y");
00096       UniStrcat( uFmtD, pString);
00097       UniStrcat( uFmtD, (UniChar*)L"%m");
00098       UniFreeMem(pString);
00099       break; 
00100     case kDateFormatWeekday:
00101       UniStrcat( uFmtD, (UniChar*)L"%a");
00102       break;
00103     default: 
00104       UniStrcat( uFmtD, (UniChar*)L"");
00105   }
00106 
00107   // set time format
00108   switch (timeFormatSelector) {
00109     case kTimeFormatNone: 
00110       UniStrcat( uFmtT, (UniChar*)L"");
00111       break;
00112    case kTimeFormatSeconds:
00113       UniQueryLocaleItem( locObj, TIMESEP, &pString);
00114       if (f24Hour)
00115         UniStrcat( uFmtT, (UniChar*)L"%H");
00116       else
00117         UniStrcat( uFmtT, (UniChar*)L"%I");
00118       UniStrcat( uFmtT, pString);
00119       UniStrcat( uFmtT, (UniChar*)L"%M");
00120       UniStrcat( uFmtT, pString);
00121       UniStrcat( uFmtT, (UniChar*)L"%S");
00122       if (!f24Hour)
00123         UniStrcat( uFmtT, (UniChar*)L" %p");
00124       UniFreeMem(pString);
00125       break;
00126     case kTimeFormatNoSeconds:
00127       UniQueryLocaleItem( locObj, TIMESEP, &pString);
00128       if (f24Hour)
00129         UniStrcat( uFmtT, (UniChar*)L"%H");
00130       else
00131         UniStrcat( uFmtT, (UniChar*)L"%I");
00132       UniStrcat( uFmtT, pString);
00133       UniStrcat( uFmtT, (UniChar*)L"%M");
00134       if (!f24Hour)
00135         UniStrcat( uFmtT, (UniChar*)L" %p");
00136       UniFreeMem(pString);
00137       break;
00138     case kTimeFormatSecondsForce24Hour:
00139       UniQueryLocaleItem( locObj, TIMESEP, &pString);
00140       UniStrcat( uFmtT, (UniChar*)L"%H");
00141       UniStrcat( uFmtT, pString);
00142       UniStrcat( uFmtT, (UniChar*)L"%M");
00143       UniStrcat( uFmtT, pString);
00144       UniStrcat( uFmtT, (UniChar*)L"%S");
00145       UniFreeMem(pString);
00146       break;
00147     case kTimeFormatNoSecondsForce24Hour:
00148       UniQueryLocaleItem( locObj, TIMESEP, &pString);
00149       UniStrcat( uFmtT, (UniChar*)L"%H");
00150       UniStrcat( uFmtT, pString);
00151       UniStrcat( uFmtT, (UniChar*)L"%M");
00152       UniFreeMem(pString);
00153       break;  
00154     default: 
00155       UniStrcat( uFmtT, (UniChar*)L"");
00156   }
00157 
00158   PRUnichar buffer[NSDATETIME_FORMAT_BUFFER_LEN] = {0};
00159   if ((dateFormatSelector != kDateFormatNone) && (timeFormatSelector != kTimeFormatNone)) {
00160     UniStrcat( uFmtD, (UniChar*)L" ");
00161   }
00162   UniStrcat( uFmtD, uFmtT);
00163   int length = UniStrftime(locObj, NS_REINTERPRET_CAST(UniChar *, buffer),
00164                            NSDATETIME_FORMAT_BUFFER_LEN, uFmtD, tmTime);
00165   UniFreeLocaleObject(locObj);
00166 
00167   if ( length != 0) {
00168     stringOut.Assign(buffer, length);
00169     rc = NS_OK;
00170   }
00171   
00172   return rc;
00173 }
00174 
00175 // performs a locale sensitive date formatting operation on the PRTime parameter
00176 nsresult nsDateTimeFormatOS2::FormatPRTime(nsILocale* locale, 
00177                                            const nsDateFormatSelector  dateFormatSelector, 
00178                                            const nsTimeFormatSelector timeFormatSelector, 
00179                                            const PRTime  prTime, 
00180                                            nsString& stringOut)
00181 {
00182   PRExplodedTime explodedTime;
00183   PR_ExplodeTime(prTime, PR_LocalTimeParameters, &explodedTime);
00184 
00185   return FormatPRExplodedTime(locale, dateFormatSelector, timeFormatSelector, &explodedTime, stringOut);
00186 }
00187 
00188 // performs a locale sensitive date formatting operation on the PRExplodedTime parameter
00189 nsresult nsDateTimeFormatOS2::FormatPRExplodedTime(nsILocale* locale, 
00190                                                    const nsDateFormatSelector  dateFormatSelector, 
00191                                                    const nsTimeFormatSelector timeFormatSelector, 
00192                                                    const PRExplodedTime*  explodedTime, 
00193                                                    nsString& stringOut)
00194 {
00195   struct tm  tmTime;
00196   /* be safe and set all members of struct tm to zero
00197    *
00198    * there are other fields in the tm struct that we aren't setting
00199    * (tm_isdst, tm_gmtoff, tm_zone, should we set these?) and since
00200    * tmTime is on the stack, it may be filled with garbage, but
00201    * the garbage may vary.  (this may explain why some saw bug #10412, and
00202    * others did not.
00203    *
00204    * when tmTime is passed to strftime() with garbage bad things may happen. 
00205    * see bug #10412
00206    */
00207   memset( &tmTime, 0, sizeof(tmTime) );
00208 
00209   tmTime.tm_yday = explodedTime->tm_yday;
00210   tmTime.tm_wday = explodedTime->tm_wday;
00211   tmTime.tm_year = explodedTime->tm_year;
00212   tmTime.tm_year -= 1900;
00213   tmTime.tm_mon = explodedTime->tm_month;
00214   tmTime.tm_mday = explodedTime->tm_mday;
00215   tmTime.tm_hour = explodedTime->tm_hour;
00216   tmTime.tm_min = explodedTime->tm_min;
00217   tmTime.tm_sec = explodedTime->tm_sec;
00218 
00219   return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, &tmTime, stringOut);
00220 }
00221