LCOV - code coverage report
Current view: top level - intl/locale - nsScriptableDateFormat.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 42 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 10 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /* This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       5             : 
       6             : #include "DateTimeFormat.h"
       7             : #include "mozilla/Sprintf.h"
       8             : #include "nsIScriptableDateFormat.h"
       9             : #include "nsCOMPtr.h"
      10             : #include "nsServiceManagerUtils.h"
      11             : #include "nsILocaleService.h"
      12             : 
      13             : static NS_DEFINE_CID(kLocaleServiceCID, NS_LOCALESERVICE_CID);
      14             : 
      15             : class nsScriptableDateFormat final : public nsIScriptableDateFormat {
      16             :  public:
      17             :   NS_DECL_ISUPPORTS
      18             : 
      19             :   NS_IMETHOD FormatDateTime(const char16_t *locale,
      20             :                             nsDateFormatSelector dateFormatSelector,
      21             :                             nsTimeFormatSelector timeFormatSelector,
      22             :                             int32_t year,
      23             :                             int32_t month,
      24             :                             int32_t day,
      25             :                             int32_t hour,
      26             :                             int32_t minute,
      27             :                             int32_t second,
      28             :                             char16_t **dateTimeString) override;
      29             : 
      30           0 :   NS_IMETHOD FormatDate(const char16_t *locale,
      31             :                         nsDateFormatSelector dateFormatSelector,
      32             :                         int32_t year,
      33             :                         int32_t month,
      34             :                         int32_t day,
      35             :                         char16_t **dateString) override
      36             :                         {return FormatDateTime(locale, dateFormatSelector, kTimeFormatNone,
      37           0 :                                                year, month, day, 0, 0, 0, dateString);}
      38             : 
      39           0 :   NS_IMETHOD FormatTime(const char16_t *locale,
      40             :                         nsTimeFormatSelector timeFormatSelector,
      41             :                         int32_t hour,
      42             :                         int32_t minute,
      43             :                         int32_t second,
      44             :                         char16_t **timeString) override
      45             :                         {return FormatDateTime(locale, kDateFormatNone, timeFormatSelector,
      46           0 :                                                1999, 1, 1, hour, minute, second, timeString);}
      47             : 
      48           0 :   nsScriptableDateFormat() {}
      49             : 
      50             : private:
      51             :   nsString mStringOut;
      52             : 
      53           0 :   virtual ~nsScriptableDateFormat() {}
      54             : };
      55             : 
      56           0 : NS_IMPL_ISUPPORTS(nsScriptableDateFormat, nsIScriptableDateFormat)
      57             : 
      58           0 : NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime(
      59             :                             const char16_t *aLocale,
      60             :                             nsDateFormatSelector dateFormatSelector,
      61             :                             nsTimeFormatSelector timeFormatSelector,
      62             :                             int32_t year,
      63             :                             int32_t month,
      64             :                             int32_t day,
      65             :                             int32_t hour,
      66             :                             int32_t minute,
      67             :                             int32_t second,
      68             :                             char16_t **dateTimeString)
      69             : {
      70             :   // We can't have a valid date with the year, month or day
      71             :   // being lower than 1.
      72           0 :   if (year < 1 || month < 1 || day < 1)
      73           0 :     return NS_ERROR_INVALID_ARG;
      74             : 
      75             :   nsresult rv;
      76           0 :   *dateTimeString = nullptr;
      77             : 
      78             :   tm tmTime;
      79             :   time_t timetTime;
      80             : 
      81           0 :   memset(&tmTime, 0, sizeof(tmTime));
      82           0 :   tmTime.tm_year = year - 1900;
      83           0 :   tmTime.tm_mon = month - 1;
      84           0 :   tmTime.tm_mday = day;
      85           0 :   tmTime.tm_hour = hour;
      86           0 :   tmTime.tm_min = minute;
      87           0 :   tmTime.tm_sec = second;
      88           0 :   tmTime.tm_yday = tmTime.tm_wday = 0;
      89           0 :   tmTime.tm_isdst = -1;
      90           0 :   timetTime = mktime(&tmTime);
      91             : 
      92           0 :   if ((time_t)-1 != timetTime) {
      93           0 :     rv = mozilla::DateTimeFormat::FormatTime(dateFormatSelector, timeFormatSelector,
      94           0 :                                              timetTime, mStringOut);
      95             :   }
      96             :   else {
      97             :     // if mktime fails (e.g. year <= 1970), then try NSPR.
      98             :     PRTime prtime;
      99             :     char string[32];
     100           0 :     SprintfLiteral(string, "%.2d/%.2d/%d %.2d:%.2d:%.2d", month, day, year, hour, minute, second);
     101           0 :     if (PR_SUCCESS != PR_ParseTimeString(string, false, &prtime))
     102           0 :       return NS_ERROR_INVALID_ARG;
     103             : 
     104           0 :     rv = mozilla::DateTimeFormat::FormatPRTime(dateFormatSelector, timeFormatSelector,
     105           0 :                                                prtime, mStringOut);
     106             :   }
     107           0 :   if (NS_SUCCEEDED(rv))
     108           0 :     *dateTimeString = ToNewUnicode(mStringOut);
     109             : 
     110           0 :   return rv;
     111             : }
     112             : 
     113             : nsresult
     114           0 : NS_NewScriptableDateFormat(nsISupports* aOuter, REFNSIID aIID, void** aResult)
     115             : {
     116           0 :   if (aOuter)
     117           0 :     return NS_ERROR_NO_AGGREGATION;
     118             : 
     119           0 :   nsScriptableDateFormat* result = new nsScriptableDateFormat();
     120           0 :   if (! result)
     121           0 :     return NS_ERROR_OUT_OF_MEMORY;
     122             : 
     123           0 :   NS_ADDREF(result);
     124           0 :   nsresult rv = result->QueryInterface(aIID, aResult);
     125           0 :   NS_RELEASE(result);
     126             : 
     127           0 :   return rv;
     128             : }

Generated by: LCOV version 1.13