LCOV - code coverage report
Current view: directory - intl/locale/src - nsScriptableDateFormat.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 51 40 78.4 %
Date: 2012-06-02 Functions: 10 10 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Pierre Phaneuf <pp@ludusdesign.com>
      24                 :  *   Constantine A. Murenin <cnst+moz#bugmail.mojo.ru>
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      28                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #include "nsIServiceManager.h"
      41                 : #include "nsIComponentManager.h"
      42                 : #include "nsLocaleCID.h"
      43                 : #include "nsILocaleService.h"
      44                 : #include "nsDateTimeFormatCID.h"
      45                 : #include "nsIDateTimeFormat.h"
      46                 : #include "nsIScriptableDateFormat.h"
      47                 : #include "nsCRT.h"
      48                 : #include "nsReadableUtils.h"
      49                 : 
      50                 : static NS_DEFINE_CID(kLocaleServiceCID, NS_LOCALESERVICE_CID);
      51                 : static NS_DEFINE_CID(kDateTimeFormatCID, NS_DATETIMEFORMAT_CID);
      52                 : 
      53                 : class nsScriptableDateFormat : public nsIScriptableDateFormat {
      54                 :  public: 
      55                 :   NS_DECL_ISUPPORTS 
      56                 : 
      57                 :   NS_IMETHOD FormatDateTime(const PRUnichar *locale, 
      58                 :                             nsDateFormatSelector dateFormatSelector, 
      59                 :                             nsTimeFormatSelector timeFormatSelector, 
      60                 :                             PRInt32 year, 
      61                 :                             PRInt32 month, 
      62                 :                             PRInt32 day, 
      63                 :                             PRInt32 hour, 
      64                 :                             PRInt32 minute, 
      65                 :                             PRInt32 second, 
      66                 :                             PRUnichar **dateTimeString);
      67                 : 
      68               1 :   NS_IMETHOD FormatDate(const PRUnichar *locale, 
      69                 :                         nsDateFormatSelector dateFormatSelector, 
      70                 :                         PRInt32 year, 
      71                 :                         PRInt32 month, 
      72                 :                         PRInt32 day, 
      73                 :                         PRUnichar **dateString)
      74                 :                         {return FormatDateTime(locale, dateFormatSelector, kTimeFormatNone, 
      75               1 :                                                year, month, day, 0, 0, 0, dateString);}
      76                 : 
      77               4 :   NS_IMETHOD FormatTime(const PRUnichar *locale, 
      78                 :                         nsTimeFormatSelector timeFormatSelector, 
      79                 :                         PRInt32 hour, 
      80                 :                         PRInt32 minute, 
      81                 :                         PRInt32 second, 
      82                 :                         PRUnichar **timeString)
      83                 :                         {return FormatDateTime(locale, kDateFormatNone, timeFormatSelector, 
      84               4 :                                                1999, 1, 1, hour, minute, second, timeString);}
      85                 : 
      86               2 :   nsScriptableDateFormat() {}
      87               8 :   virtual ~nsScriptableDateFormat() {}
      88                 : private:
      89                 :   nsString mStringOut;   
      90                 : };
      91                 : 
      92             122 : NS_IMPL_ISUPPORTS1(nsScriptableDateFormat, nsIScriptableDateFormat)
      93                 : 
      94              14 : NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime(
      95                 :                             const PRUnichar *aLocale, 
      96                 :                             nsDateFormatSelector dateFormatSelector, 
      97                 :                             nsTimeFormatSelector timeFormatSelector, 
      98                 :                             PRInt32 year, 
      99                 :                             PRInt32 month, 
     100                 :                             PRInt32 day, 
     101                 :                             PRInt32 hour, 
     102                 :                             PRInt32 minute, 
     103                 :                             PRInt32 second, 
     104                 :                             PRUnichar **dateTimeString)
     105                 : {
     106                 :   // We can't have a valid date with the year, month or day
     107                 :   // being lower than 1.
     108              14 :   if (year < 1 || month < 1 || day < 1)
     109               1 :     return NS_ERROR_INVALID_ARG;
     110                 : 
     111                 :   nsresult rv;
     112              26 :   nsAutoString localeName(aLocale);
     113              13 :   *dateTimeString = nsnull;
     114                 : 
     115              26 :   nsCOMPtr<nsILocale> locale;
     116                 :   // re-initialise locale pointer only if the locale was given explicitly
     117              13 :   if (!localeName.IsEmpty()) {
     118                 :     // get locale service
     119               0 :     nsCOMPtr<nsILocaleService> localeService(do_GetService(kLocaleServiceCID, &rv));
     120               0 :     NS_ENSURE_SUCCESS(rv, rv);
     121                 :     // get locale
     122               0 :     rv = localeService->NewLocale(localeName, getter_AddRefs(locale));
     123               0 :     NS_ENSURE_SUCCESS(rv, rv);
     124                 :   }
     125                 : 
     126              26 :   nsCOMPtr<nsIDateTimeFormat> dateTimeFormat(do_CreateInstance(kDateTimeFormatCID, &rv));
     127              13 :   NS_ENSURE_SUCCESS(rv, rv);
     128                 : 
     129                 :   tm tmTime;
     130                 :   time_t timetTime;
     131                 : 
     132              13 :   memset(&tmTime, 0, sizeof(tmTime));
     133              13 :   tmTime.tm_year = year - 1900;
     134              13 :   tmTime.tm_mon = month - 1;
     135              13 :   tmTime.tm_mday = day;
     136              13 :   tmTime.tm_hour = hour;
     137              13 :   tmTime.tm_min = minute;
     138              13 :   tmTime.tm_sec = second;
     139              13 :   tmTime.tm_yday = tmTime.tm_wday = 0;
     140              13 :   tmTime.tm_isdst = -1;
     141              13 :   timetTime = mktime(&tmTime);
     142                 : 
     143              13 :   if ((time_t)-1 != timetTime) {
     144              13 :     rv = dateTimeFormat->FormatTime(locale, dateFormatSelector, timeFormatSelector, 
     145              13 :                                      timetTime, mStringOut);
     146                 :   }
     147                 :   else {
     148                 :     // if mktime fails (e.g. year <= 1970), then try NSPR.
     149                 :     PRTime prtime;
     150                 :     char string[32];
     151               0 :     sprintf(string, "%.2d/%.2d/%d %.2d:%.2d:%.2d", month, day, year, hour, minute, second);
     152               0 :     if (PR_SUCCESS != PR_ParseTimeString(string, false, &prtime))
     153               0 :       return NS_ERROR_INVALID_ARG;
     154                 : 
     155               0 :     rv = dateTimeFormat->FormatPRTime(locale, dateFormatSelector, timeFormatSelector, 
     156               0 :                                       prtime, mStringOut);
     157                 :   }
     158              13 :   if (NS_SUCCEEDED(rv))
     159              13 :     *dateTimeString = ToNewUnicode(mStringOut);
     160                 : 
     161              13 :   return rv;
     162                 : }
     163                 : 
     164                 : nsresult
     165               2 : NS_NewScriptableDateFormat(nsISupports* aOuter, REFNSIID aIID, void** aResult)
     166                 : {
     167               2 :   if (aOuter)
     168               0 :     return NS_ERROR_NO_AGGREGATION;
     169                 : 
     170               2 :   nsScriptableDateFormat* result = new nsScriptableDateFormat();
     171               2 :   if (! result)
     172               0 :     return NS_ERROR_OUT_OF_MEMORY;
     173                 : 
     174               2 :   NS_ADDREF(result);
     175               2 :   nsresult rv = result->QueryInterface(aIID, aResult);
     176               2 :   NS_RELEASE(result);
     177                 : 
     178               2 :   return rv;
     179                 : }

Generated by: LCOV version 1.7