intl/locale/src/nsScriptableDateFormat.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/locale/src/nsScriptableDateFormat.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,142 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsILocaleService.h"
    1.10 +#include "nsDateTimeFormatCID.h"
    1.11 +#include "nsIDateTimeFormat.h"
    1.12 +#include "nsIScriptableDateFormat.h"
    1.13 +#include "nsCOMPtr.h"
    1.14 +#include "nsServiceManagerUtils.h"
    1.15 +
    1.16 +static NS_DEFINE_CID(kLocaleServiceCID, NS_LOCALESERVICE_CID);
    1.17 +static NS_DEFINE_CID(kDateTimeFormatCID, NS_DATETIMEFORMAT_CID);
    1.18 +
    1.19 +class nsScriptableDateFormat : public nsIScriptableDateFormat {
    1.20 + public: 
    1.21 +  NS_DECL_ISUPPORTS 
    1.22 +
    1.23 +  NS_IMETHOD FormatDateTime(const char16_t *locale, 
    1.24 +                            nsDateFormatSelector dateFormatSelector, 
    1.25 +                            nsTimeFormatSelector timeFormatSelector, 
    1.26 +                            int32_t year, 
    1.27 +                            int32_t month, 
    1.28 +                            int32_t day, 
    1.29 +                            int32_t hour, 
    1.30 +                            int32_t minute, 
    1.31 +                            int32_t second, 
    1.32 +                            char16_t **dateTimeString);
    1.33 +
    1.34 +  NS_IMETHOD FormatDate(const char16_t *locale, 
    1.35 +                        nsDateFormatSelector dateFormatSelector, 
    1.36 +                        int32_t year, 
    1.37 +                        int32_t month, 
    1.38 +                        int32_t day, 
    1.39 +                        char16_t **dateString)
    1.40 +                        {return FormatDateTime(locale, dateFormatSelector, kTimeFormatNone, 
    1.41 +                                               year, month, day, 0, 0, 0, dateString);}
    1.42 +
    1.43 +  NS_IMETHOD FormatTime(const char16_t *locale, 
    1.44 +                        nsTimeFormatSelector timeFormatSelector, 
    1.45 +                        int32_t hour, 
    1.46 +                        int32_t minute, 
    1.47 +                        int32_t second, 
    1.48 +                        char16_t **timeString)
    1.49 +                        {return FormatDateTime(locale, kDateFormatNone, timeFormatSelector, 
    1.50 +                                               1999, 1, 1, hour, minute, second, timeString);}
    1.51 +
    1.52 +  nsScriptableDateFormat() {}
    1.53 +  virtual ~nsScriptableDateFormat() {}
    1.54 +private:
    1.55 +  nsString mStringOut;   
    1.56 +};
    1.57 +
    1.58 +NS_IMPL_ISUPPORTS(nsScriptableDateFormat, nsIScriptableDateFormat)
    1.59 +
    1.60 +NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime(
    1.61 +                            const char16_t *aLocale, 
    1.62 +                            nsDateFormatSelector dateFormatSelector, 
    1.63 +                            nsTimeFormatSelector timeFormatSelector, 
    1.64 +                            int32_t year, 
    1.65 +                            int32_t month, 
    1.66 +                            int32_t day, 
    1.67 +                            int32_t hour, 
    1.68 +                            int32_t minute, 
    1.69 +                            int32_t second, 
    1.70 +                            char16_t **dateTimeString)
    1.71 +{
    1.72 +  // We can't have a valid date with the year, month or day
    1.73 +  // being lower than 1.
    1.74 +  if (year < 1 || month < 1 || day < 1)
    1.75 +    return NS_ERROR_INVALID_ARG;
    1.76 +
    1.77 +  nsresult rv;
    1.78 +  nsAutoString localeName(aLocale);
    1.79 +  *dateTimeString = nullptr;
    1.80 +
    1.81 +  nsCOMPtr<nsILocale> locale;
    1.82 +  // re-initialise locale pointer only if the locale was given explicitly
    1.83 +  if (!localeName.IsEmpty()) {
    1.84 +    // get locale service
    1.85 +    nsCOMPtr<nsILocaleService> localeService(do_GetService(kLocaleServiceCID, &rv));
    1.86 +    NS_ENSURE_SUCCESS(rv, rv);
    1.87 +    // get locale
    1.88 +    rv = localeService->NewLocale(localeName, getter_AddRefs(locale));
    1.89 +    NS_ENSURE_SUCCESS(rv, rv);
    1.90 +  }
    1.91 +
    1.92 +  nsCOMPtr<nsIDateTimeFormat> dateTimeFormat(do_CreateInstance(kDateTimeFormatCID, &rv));
    1.93 +  NS_ENSURE_SUCCESS(rv, rv);
    1.94 +
    1.95 +  tm tmTime;
    1.96 +  time_t timetTime;
    1.97 +
    1.98 +  memset(&tmTime, 0, sizeof(tmTime));
    1.99 +  tmTime.tm_year = year - 1900;
   1.100 +  tmTime.tm_mon = month - 1;
   1.101 +  tmTime.tm_mday = day;
   1.102 +  tmTime.tm_hour = hour;
   1.103 +  tmTime.tm_min = minute;
   1.104 +  tmTime.tm_sec = second;
   1.105 +  tmTime.tm_yday = tmTime.tm_wday = 0;
   1.106 +  tmTime.tm_isdst = -1;
   1.107 +  timetTime = mktime(&tmTime);
   1.108 +
   1.109 +  if ((time_t)-1 != timetTime) {
   1.110 +    rv = dateTimeFormat->FormatTime(locale, dateFormatSelector, timeFormatSelector, 
   1.111 +                                     timetTime, mStringOut);
   1.112 +  }
   1.113 +  else {
   1.114 +    // if mktime fails (e.g. year <= 1970), then try NSPR.
   1.115 +    PRTime prtime;
   1.116 +    char string[32];
   1.117 +    sprintf(string, "%.2d/%.2d/%d %.2d:%.2d:%.2d", month, day, year, hour, minute, second);
   1.118 +    if (PR_SUCCESS != PR_ParseTimeString(string, false, &prtime))
   1.119 +      return NS_ERROR_INVALID_ARG;
   1.120 +
   1.121 +    rv = dateTimeFormat->FormatPRTime(locale, dateFormatSelector, timeFormatSelector, 
   1.122 +                                      prtime, mStringOut);
   1.123 +  }
   1.124 +  if (NS_SUCCEEDED(rv))
   1.125 +    *dateTimeString = ToNewUnicode(mStringOut);
   1.126 +
   1.127 +  return rv;
   1.128 +}
   1.129 +
   1.130 +nsresult
   1.131 +NS_NewScriptableDateFormat(nsISupports* aOuter, REFNSIID aIID, void** aResult)
   1.132 +{
   1.133 +  if (aOuter)
   1.134 +    return NS_ERROR_NO_AGGREGATION;
   1.135 +
   1.136 +  nsScriptableDateFormat* result = new nsScriptableDateFormat();
   1.137 +  if (! result)
   1.138 +    return NS_ERROR_OUT_OF_MEMORY;
   1.139 +
   1.140 +  NS_ADDREF(result);
   1.141 +  nsresult rv = result->QueryInterface(aIID, aResult);
   1.142 +  NS_RELEASE(result);
   1.143 +
   1.144 +  return rv;
   1.145 +}

mercurial