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