|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2011 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 #include "datetime-format.h" | |
29 | |
30 #include "i18n-utils.h" | |
31 #include "unicode/dtfmtsym.h" | |
32 #include "unicode/dtptngen.h" | |
33 #include "unicode/locid.h" | |
34 #include "unicode/smpdtfmt.h" | |
35 | |
36 namespace v8 { | |
37 namespace internal { | |
38 | |
39 v8::Persistent<v8::FunctionTemplate> DateTimeFormat::datetime_format_template_; | |
40 | |
41 static icu::DateFormat* CreateDateTimeFormat(v8::Handle<v8::String>, | |
42 v8::Handle<v8::Object>); | |
43 static v8::Handle<v8::Value> GetSymbols( | |
44 const v8::Arguments&, const icu::UnicodeString*, int32_t, | |
45 const icu::UnicodeString*, int32_t); | |
46 static v8::Handle<v8::Value> ThrowUnexpectedObjectError(); | |
47 | |
48 icu::SimpleDateFormat* DateTimeFormat::UnpackDateTimeFormat( | |
49 v8::Handle<v8::Object> obj) { | |
50 if (datetime_format_template_->HasInstance(obj)) { | |
51 return static_cast<icu::SimpleDateFormat*>( | |
52 obj->GetPointerFromInternalField(0)); | |
53 } | |
54 | |
55 return NULL; | |
56 } | |
57 | |
58 void DateTimeFormat::DeleteDateTimeFormat(v8::Persistent<v8::Value> object, | |
59 void* param) { | |
60 v8::Persistent<v8::Object> persistent_object = | |
61 v8::Persistent<v8::Object>::Cast(object); | |
62 | |
63 // First delete the hidden C++ object. | |
64 // Unpacking should never return NULL here. That would only happen if | |
65 // this method is used as the weak callback for persistent handles not | |
66 // pointing to a date time formatter. | |
67 delete UnpackDateTimeFormat(persistent_object); | |
68 | |
69 // Then dispose of the persistent handle to JS object. | |
70 persistent_object.Dispose(); | |
71 } | |
72 | |
73 v8::Handle<v8::Value> DateTimeFormat::Format(const v8::Arguments& args) { | |
74 v8::HandleScope handle_scope; | |
75 | |
76 double millis = 0.0; | |
77 if (args.Length() != 1 || !args[0]->IsDate()) { | |
78 // Create a new date. | |
79 v8::TryCatch try_catch; | |
80 v8::Local<v8::Script> date_script = | |
81 v8::Script::Compile(v8::String::New("eval('new Date()')")); | |
82 millis = date_script->Run()->NumberValue(); | |
83 if (try_catch.HasCaught()) { | |
84 return try_catch.ReThrow(); | |
85 } | |
86 } else { | |
87 millis = v8::Date::Cast(*args[0])->NumberValue(); | |
88 } | |
89 | |
90 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); | |
91 if (!date_format) { | |
92 return ThrowUnexpectedObjectError(); | |
93 } | |
94 | |
95 icu::UnicodeString result; | |
96 date_format->format(millis, result); | |
97 | |
98 return v8::String::New( | |
99 reinterpret_cast<const uint16_t*>(result.getBuffer()), result.length()); | |
100 } | |
101 | |
102 v8::Handle<v8::Value> DateTimeFormat::GetMonths(const v8::Arguments& args) { | |
103 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); | |
104 if (!date_format) { | |
105 return ThrowUnexpectedObjectError(); | |
106 } | |
107 | |
108 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); | |
109 | |
110 int32_t abbrev_count; | |
111 const icu::UnicodeString* abbrev = symbols->getShortMonths(abbrev_count); | |
112 int32_t wide_count; | |
113 const icu::UnicodeString* wide = symbols->getMonths(wide_count); | |
jungshik at Google
2011/05/19 18:24:05
Again, how about 'narrow'?
More importantly, usi
Nebojša Ćirić
2011/05/19 20:44:41
Narrow was not part of the proposed standard, but
| |
114 | |
115 return GetSymbols(args, abbrev, abbrev_count, wide, wide_count); | |
116 } | |
117 | |
118 v8::Handle<v8::Value> DateTimeFormat::GetWeekdays(const v8::Arguments& args) { | |
119 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); | |
120 if (!date_format) { | |
121 ThrowUnexpectedObjectError(); | |
122 } | |
123 | |
124 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); | |
125 | |
126 int32_t abbrev_count; | |
127 const icu::UnicodeString* abbrev = symbols->getShortWeekdays(abbrev_count); | |
128 int32_t wide_count; | |
129 const icu::UnicodeString* wide = symbols->getWeekdays(wide_count); | |
130 | |
131 // getXXXWeekdays always returns 8 elements. | |
132 CHECK_EQ(8, abbrev_count); | |
133 CHECK_EQ(8, wide_count); | |
134 | |
135 // ICU documentation says we should ignore element 0 of the returned array. | |
136 return GetSymbols( | |
137 args, abbrev + 1, abbrev_count -1 , wide + 1, wide_count - 1); | |
138 } | |
139 | |
140 v8::Handle<v8::Value> DateTimeFormat::GetEras(const v8::Arguments& args) { | |
141 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); | |
142 if (!date_format) { | |
143 return ThrowUnexpectedObjectError(); | |
144 } | |
145 | |
146 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); | |
147 | |
148 int32_t abbrev_count; | |
149 const icu::UnicodeString* abbrev = symbols->getEras(abbrev_count); | |
150 int32_t wide_count; | |
151 const icu::UnicodeString* wide = symbols->getEraNames(wide_count); | |
152 | |
153 return GetSymbols(args, abbrev, abbrev_count, wide, wide_count); | |
154 } | |
155 | |
156 v8::Handle<v8::Value> DateTimeFormat::GetAmPm(const v8::Arguments& args) { | |
157 icu::SimpleDateFormat* date_format = UnpackDateTimeFormat(args.Holder()); | |
158 if (!date_format) { | |
159 return ThrowUnexpectedObjectError(); | |
160 } | |
161 | |
162 const icu::DateFormatSymbols* symbols = date_format->getDateFormatSymbols(); | |
163 | |
164 int32_t abbrev_count; | |
165 const icu::UnicodeString* abbrev = symbols->getAmPmStrings(abbrev_count); | |
166 int32_t wide_count = abbrev_count; | |
167 const icu::UnicodeString* wide = abbrev; | |
168 | |
169 return GetSymbols(args, abbrev, abbrev_count, wide, wide_count); | |
170 } | |
171 | |
172 v8::Handle<v8::Value> DateTimeFormat::JSDateTimeFormat( | |
173 const v8::Arguments& args) { | |
174 v8::HandleScope handle_scope; | |
175 | |
176 ASSERT_EQ(2, args.Length()); | |
177 // LanguageID string. | |
178 ASSERT(args[0]->IsString()); | |
179 // Settings object. | |
180 ASSERT(args[1]->IsObject()); | |
181 | |
182 icu::SimpleDateFormat* date_format = static_cast<icu::SimpleDateFormat*>( | |
183 CreateDateTimeFormat(args[0]->ToString(), args[1]->ToObject())); | |
184 | |
185 if (datetime_format_template_.IsEmpty()) { | |
186 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); | |
187 | |
188 raw_template->SetClassName(v8::String::New("v8Locale.DateTimeFormat")); | |
189 | |
190 // Define internal field count on instance template. | |
191 v8::Local<v8::ObjectTemplate> object_template = | |
192 raw_template->InstanceTemplate(); | |
193 | |
194 // Set aside internal field for icu date time formatter. | |
195 object_template->SetInternalFieldCount(1); | |
196 | |
197 // Define all of the prototype methods on prototype template. | |
198 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); | |
199 proto->Set(v8::String::New("format"), | |
200 v8::FunctionTemplate::New(Format)); | |
201 proto->Set(v8::String::New("getMonths"), | |
202 v8::FunctionTemplate::New(GetMonths)); | |
203 proto->Set(v8::String::New("getWeekdays"), | |
204 v8::FunctionTemplate::New(GetWeekdays)); | |
205 proto->Set(v8::String::New("getEras"), | |
206 v8::FunctionTemplate::New(GetEras)); | |
207 proto->Set(v8::String::New("getAmPm"), | |
208 v8::FunctionTemplate::New(GetAmPm)); | |
209 | |
210 datetime_format_template_ = | |
211 v8::Persistent<v8::FunctionTemplate>::New(raw_template); | |
212 } | |
213 | |
214 // Create an empty object wrapper. | |
215 v8::Local<v8::Object> local_object = | |
216 datetime_format_template_->GetFunction()->NewInstance(); | |
217 v8::Persistent<v8::Object> wrapper = | |
218 v8::Persistent<v8::Object>::New(local_object); | |
219 | |
220 // Set date time formatter as internal field of the resulting JS object. | |
221 wrapper->SetPointerInInternalField(0, date_format); | |
222 | |
223 // Set resolved pattern in options.pattern. | |
224 icu::UnicodeString pattern; | |
225 date_format->toPattern(pattern); | |
226 v8::Local<v8::Object> options = v8::Object::New(); | |
227 options->Set(v8::String::New("pattern"), | |
228 v8::String::New(reinterpret_cast<const uint16_t*>( | |
229 pattern.getBuffer()), pattern.length())); | |
230 wrapper->Set(v8::String::New("options"), options); | |
231 | |
232 // Make object handle weak so we can delete iterator once GC kicks in. | |
233 wrapper.MakeWeak(NULL, DeleteDateTimeFormat); | |
234 | |
235 return wrapper; | |
236 } | |
237 | |
238 // Returns SimpleDateFormat. | |
239 static icu::DateFormat* CreateDateTimeFormat( | |
240 v8::Handle<v8::String> locale, v8::Handle<v8::Object> settings) { | |
241 v8::HandleScope handle_scope; | |
242 | |
243 v8::String::AsciiValue ascii_locale(locale); | |
244 icu::Locale icu_locale(*ascii_locale); | |
245 | |
246 // Make formatter from skeleton. | |
247 icu::SimpleDateFormat* date_format = NULL; | |
248 UErrorCode status = U_ZERO_ERROR; | |
249 icu::UnicodeString skeleton; | |
250 if (I18NUtils::ExtractStringSetting(settings, "skeleton", &skeleton)) { | |
251 v8::Local<icu::DateTimePatternGenerator> generator( | |
252 icu::DateTimePatternGenerator::createInstance(icu_locale, status)); | |
253 icu::UnicodeString pattern = | |
254 generator->getBestPattern(skeleton, status); | |
255 | |
256 date_format = new icu::SimpleDateFormat(pattern, icu_locale, status); | |
257 if (U_SUCCESS(status)) { | |
258 return date_format; | |
259 } else { | |
260 delete date_format; | |
261 } | |
262 } | |
263 | |
264 // Extract date type and time type from settings. | |
265 icu::UnicodeString short_dt = icu::UnicodeString::fromUTF8("short"); | |
jungshik at Google
2011/05/19 18:24:05
short_dt("short", -1, US_INV);
is more succinct.
Nebojša Ćirić
2011/05/19 20:44:41
I support all types now. We should state in propos
| |
266 icu::UnicodeString date_type; | |
267 icu::DateFormat::EStyle date_style = icu::DateFormat::kNone; | |
268 if (I18NUtils::ExtractStringSetting(settings, "dateType", &date_type)) { | |
269 if (date_type == short_dt) { | |
270 date_style = icu::DateFormat::kShort; | |
271 } else { | |
272 date_style = icu::DateFormat::kLong; | |
273 } | |
274 } | |
275 | |
276 icu::UnicodeString time_type; | |
277 icu::DateFormat::EStyle time_style = icu::DateFormat::kNone; | |
278 if (I18NUtils::ExtractStringSetting(settings, "timeType", &time_type)) { | |
279 if (time_type == short_dt) { | |
280 time_style = icu::DateFormat::kShort; | |
281 } else { | |
282 time_style = icu::DateFormat::kMedium; | |
283 } | |
284 } | |
285 | |
286 // Try all combinations of date/time types. | |
287 if (date_style == icu::DateFormat::kNone && | |
288 time_style == icu::DateFormat::kNone) { | |
289 // Return default short date, short | |
290 return icu::DateFormat::createDateTimeInstance( | |
291 icu::DateFormat::kShort, icu::DateFormat::kShort, icu_locale); | |
292 } else if (date_style != icu::DateFormat::kNone && | |
293 time_style != icu::DateFormat::kNone) { | |
294 return icu::DateFormat::createDateTimeInstance( | |
295 date_style, time_style, icu_locale); | |
296 } else if (date_style != icu::DateFormat::kNone) { | |
297 return icu::DateFormat::createDateInstance(date_style, icu_locale); | |
298 } else { | |
299 // time_style != icu::DateFormat::kNone | |
300 return icu::DateFormat::createTimeInstance(time_style, icu_locale); | |
301 } | |
302 } | |
303 | |
304 // Creates a v8::Array of abbrev or wide symbols. | |
305 static v8::Handle<v8::Value> GetSymbols(const v8::Arguments& args, | |
306 const icu::UnicodeString* abbrev, | |
307 int32_t abbrev_count, | |
308 const icu::UnicodeString* wide, | |
309 int32_t wide_count) { | |
310 v8::HandleScope handle_scope; | |
311 | |
312 const icu::UnicodeString* result = wide; | |
313 int32_t count = wide_count; | |
314 | |
315 if (args.Length() == 1 && args[0]->IsString()) { | |
316 v8::String::AsciiValue ascii_value(args[0]); | |
317 if (strcmp(*ascii_value, "abbreviated") == 0) { | |
318 result = abbrev; | |
319 count = abbrev_count; | |
320 } | |
321 } | |
322 | |
323 v8::Handle<v8::Array> symbols = v8::Array::New(); | |
324 for (int32_t i = 0; i < count; ++i) { | |
325 symbols->Set(i, v8::String::New( | |
326 reinterpret_cast<const uint16_t*>(result[i].getBuffer()), | |
327 result[i].length())); | |
328 } | |
329 | |
330 return handle_scope.Close(symbols); | |
331 } | |
332 | |
333 // Throws a JavaScript exception. | |
334 static v8::Handle<v8::Value> ThrowUnexpectedObjectError() { | |
335 // Returns undefined, and schedules an exception to be thrown. | |
336 return v8::ThrowException(v8::Exception::Error( | |
337 v8::String::New("DateTimeFormat method called on an object " | |
338 "that is not a DateTimeFormat."))); | |
339 } | |
340 | |
341 } } // namespace v8::internal | |
OLD | NEW |