Back to index

lightning-sunbird  0.9+nobinonly
nsScriptableDateFormat.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *   Constantine A. Murenin <cnst+moz#bugmail.mojo.ru>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsIServiceManager.h"
00041 #include "nsIComponentManager.h"
00042 #include "nsLocaleCID.h"
00043 #include "nsILocaleService.h"
00044 #include "nsDateTimeFormatCID.h"
00045 #include "nsIDateTimeFormat.h"
00046 #include "nsIScriptableDateFormat.h"
00047 #include "nsCRT.h"
00048 #include "nsReadableUtils.h"
00049 
00050 static NS_DEFINE_CID(kLocaleServiceCID, NS_LOCALESERVICE_CID);
00051 static NS_DEFINE_CID(kDateTimeFormatCID, NS_DATETIMEFORMAT_CID);
00052 
00053 class nsScriptableDateFormat : public nsIScriptableDateFormat {
00054  public: 
00055   NS_DECL_ISUPPORTS 
00056 
00057   NS_IMETHOD FormatDateTime(const PRUnichar *locale, 
00058                             nsDateFormatSelector dateFormatSelector, 
00059                             nsTimeFormatSelector timeFormatSelector, 
00060                             PRInt32 year, 
00061                             PRInt32 month, 
00062                             PRInt32 day, 
00063                             PRInt32 hour, 
00064                             PRInt32 minute, 
00065                             PRInt32 second, 
00066                             PRUnichar **dateTimeString);
00067 
00068   NS_IMETHOD FormatDate(const PRUnichar *locale, 
00069                         nsDateFormatSelector dateFormatSelector, 
00070                         PRInt32 year, 
00071                         PRInt32 month, 
00072                         PRInt32 day, 
00073                         PRUnichar **dateString)
00074                         {return FormatDateTime(locale, dateFormatSelector, kTimeFormatNone, 
00075                                                year, month, day, 0, 0, 0, dateString);}
00076 
00077   NS_IMETHOD FormatTime(const PRUnichar *locale, 
00078                         nsTimeFormatSelector timeFormatSelector, 
00079                         PRInt32 hour, 
00080                         PRInt32 minute, 
00081                         PRInt32 second, 
00082                         PRUnichar **timeString)
00083                         {return FormatDateTime(locale, kDateFormatNone, timeFormatSelector, 
00084                                                1999, 1, 1, hour, minute, second, timeString);}
00085 
00086   nsScriptableDateFormat() {}
00087   virtual ~nsScriptableDateFormat() {}
00088 private:
00089   nsString mStringOut;   
00090 };
00091 
00092 NS_IMPL_ISUPPORTS1(nsScriptableDateFormat, nsIScriptableDateFormat)
00093 
00094 NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime(
00095                             const PRUnichar *aLocale, 
00096                             nsDateFormatSelector dateFormatSelector, 
00097                             nsTimeFormatSelector timeFormatSelector, 
00098                             PRInt32 year, 
00099                             PRInt32 month, 
00100                             PRInt32 day, 
00101                             PRInt32 hour, 
00102                             PRInt32 minute, 
00103                             PRInt32 second, 
00104                             PRUnichar **dateTimeString)
00105 {
00106   nsresult rv;
00107   nsAutoString localeName(aLocale);
00108   *dateTimeString = nsnull;
00109 
00110   nsCOMPtr<nsILocale> locale;
00111   // re-initialise locale pointer only if the locale was given explicitly
00112   if (!localeName.IsEmpty()) {
00113     // get locale service
00114     nsCOMPtr<nsILocaleService> localeService(do_GetService(kLocaleServiceCID, &rv));
00115     NS_ENSURE_SUCCESS(rv, rv);
00116     // get locale
00117     rv = localeService->NewLocale(localeName, getter_AddRefs(locale));
00118     NS_ENSURE_SUCCESS(rv, rv);
00119   }
00120 
00121   nsCOMPtr<nsIDateTimeFormat> dateTimeFormat(do_CreateInstance(kDateTimeFormatCID, &rv));
00122   NS_ENSURE_SUCCESS(rv, rv);
00123 
00124   tm tmTime;
00125   time_t timetTime;
00126 
00127   memset(&tmTime, 0, sizeof(tmTime));
00128   tmTime.tm_year = year - 1900;
00129   tmTime.tm_mon = month - 1;
00130   tmTime.tm_mday = day;
00131   tmTime.tm_hour = hour;
00132   tmTime.tm_min = minute;
00133   tmTime.tm_sec = second;
00134   tmTime.tm_yday = tmTime.tm_wday = 0;
00135   tmTime.tm_isdst = -1;
00136   timetTime = mktime(&tmTime);
00137 
00138   if ((time_t)-1 != timetTime) {
00139     rv = dateTimeFormat->FormatTime(locale, dateFormatSelector, timeFormatSelector, 
00140                                      timetTime, mStringOut);
00141   }
00142   else {
00143     // if mktime fails (e.g. year <= 1970), then try NSPR.
00144     PRTime prtime;
00145     char string[32];
00146     sprintf(string, "%.2d/%.2d/%d %.2d:%.2d:%.2d", month, day, year, hour, minute, second);
00147     if (PR_SUCCESS != PR_ParseTimeString(string, PR_FALSE, &prtime))
00148       return NS_ERROR_ILLEGAL_VALUE; // invalid arg value
00149 
00150     rv = dateTimeFormat->FormatPRTime(locale, dateFormatSelector, timeFormatSelector, 
00151                                       prtime, mStringOut);
00152   }
00153   if (NS_SUCCEEDED(rv))
00154     *dateTimeString = ToNewUnicode(mStringOut);
00155 
00156   return rv;
00157 }
00158 
00159 NS_IMETHODIMP
00160 NS_NewScriptableDateFormat(nsISupports* aOuter, REFNSIID aIID, void** aResult)
00161 {
00162   if (aOuter)
00163     return NS_ERROR_NO_AGGREGATION;
00164 
00165   nsScriptableDateFormat* result = new nsScriptableDateFormat();
00166   if (! result)
00167     return NS_ERROR_OUT_OF_MEMORY;
00168 
00169   NS_ADDREF(result);
00170   nsresult rv = result->QueryInterface(aIID, aResult);
00171   NS_RELEASE(result);
00172 
00173   return rv;
00174 }