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