Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: src/js/i18n.js

Issue 1973183002: Override Date.prototype.to*String with Intl.DateTimeFormat.format Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: rebased Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/runtime/runtime-i18n.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // ECMAScript 402 API implementation. 5 // ECMAScript 402 API implementation.
6 6
7 /** 7 /**
8 * Intl object is a single object that has some named properties, 8 * Intl object is a single object that has some named properties,
9 * all of which are constructors. 9 * all of which are constructors.
10 */ 10 */
(...skipping 1941 matching lines...) Expand 10 before | Expand all | Expand 10 after
1952 'breakiterator'); 1952 'breakiterator');
1953 AddBoundMethod(GlobalIntlv8BreakIterator, 'breakType', breakType, 0, 1953 AddBoundMethod(GlobalIntlv8BreakIterator, 'breakType', breakType, 0,
1954 'breakiterator'); 1954 'breakiterator');
1955 1955
1956 // Save references to Intl objects and methods we use, for added security. 1956 // Save references to Intl objects and methods we use, for added security.
1957 var savedObjects = { 1957 var savedObjects = {
1958 'collator': GlobalIntlCollator, 1958 'collator': GlobalIntlCollator,
1959 'numberformat': GlobalIntlNumberFormat, 1959 'numberformat': GlobalIntlNumberFormat,
1960 'dateformatall': GlobalIntlDateTimeFormat, 1960 'dateformatall': GlobalIntlDateTimeFormat,
1961 'dateformatdate': GlobalIntlDateTimeFormat, 1961 'dateformatdate': GlobalIntlDateTimeFormat,
1962 'dateformattime': GlobalIntlDateTimeFormat 1962 'dateformattime': GlobalIntlDateTimeFormat,
1963 'datetostring': GlobalIntlDateTimeFormat,
1964 'datetodatestring': GlobalIntlDateTimeFormat,
1965 'datetotimestring': GlobalIntlDateTimeFormat,
1963 }; 1966 };
1964 1967
1965 1968
1966 // Default (created with undefined locales and options parameters) collator, 1969 // Default (created with undefined locales and options parameters) collator,
1967 // number and date format instances. They'll be created as needed. 1970 // number and date format instances. They'll be created as needed.
1968 var defaultObjects = { 1971 var defaultObjects = {
1969 'collator': UNDEFINED, 1972 'collator': UNDEFINED,
1970 'numberformat': UNDEFINED, 1973 'numberformat': UNDEFINED,
1971 'dateformatall': UNDEFINED, 1974 'dateformatall': UNDEFINED,
1972 'dateformatdate': UNDEFINED, 1975 'dateformatdate': UNDEFINED,
1973 'dateformattime': UNDEFINED, 1976 'dateformattime': UNDEFINED,
1977 'datetostring': UNDEFINED,
1978 'datetodatestring': UNDEFINED,
1979 'datetotimestring': UNDEFINED,
1974 }; 1980 };
1975 1981
1976 function clearDefaultObjects() { 1982 function clearDefaultObjects() {
1977 defaultObjects['dateformatall'] = UNDEFINED; 1983 defaultObjects['dateformatall'] = UNDEFINED;
1978 defaultObjects['dateformatdate'] = UNDEFINED; 1984 defaultObjects['dateformatdate'] = UNDEFINED;
1979 defaultObjects['dateformattime'] = UNDEFINED; 1985 defaultObjects['dateformattime'] = UNDEFINED;
1986 defaultObjects['datetostring'] = UNDEFINED;
1987 defaultObjects['datetodatestring'] = UNDEFINED;
1988 defaultObjects['datetotimestring'] = UNDEFINED;
1980 } 1989 }
1981 1990
1982 var date_cache_version = 0; 1991 var date_cache_version = 0;
1983 1992
1984 function checkDateCacheCurrent() { 1993 function checkDateCacheCurrent() {
1985 var new_date_cache_version = %DateCacheVersion(); 1994 var new_date_cache_version = %DateCacheVersion();
1986 if (new_date_cache_version == date_cache_version) { 1995 if (new_date_cache_version == date_cache_version) {
1987 return; 1996 return;
1988 } 1997 }
1989 date_cache_version = new_date_cache_version; 1998 date_cache_version = new_date_cache_version;
1990 1999
1991 clearDefaultObjects(); 2000 clearDefaultObjects();
1992 } 2001 }
1993 2002
1994 /** 2003 /**
1995 * Returns cached or newly created instance of a given service. 2004 * Returns cached or newly created instance of a given service.
1996 * We cache only default instances (where no locales or options are provided). 2005 * We cache only default instances (where no locales or options are provided)
2006 * or overrides of Date.prototype.to{,Date,Time}String for which useCached
2007 * is defined.
1997 */ 2008 */
1998 function cachedOrNewService(service, locales, options, defaults) { 2009 function cachedOrNewService(service, locales, options, defaults, useCached) {
1999 var useOptions = (IS_UNDEFINED(defaults)) ? options : defaults; 2010 var useOptions = (IS_UNDEFINED(defaults)) ? options : defaults;
2000 if (IS_UNDEFINED(locales) && IS_UNDEFINED(options)) { 2011 if (!IS_UNDEFINED(useCached) ||
2012 (IS_UNDEFINED(locales) && IS_UNDEFINED(options))) {
2001 checkDateCacheCurrent(); 2013 checkDateCacheCurrent();
2002 if (IS_UNDEFINED(defaultObjects[service])) { 2014 if (IS_UNDEFINED(defaultObjects[service])) {
2003 defaultObjects[service] = new savedObjects[service](locales, useOptions); 2015 defaultObjects[service] = new savedObjects[service](locales, useOptions);
2004 } 2016 }
2005 return defaultObjects[service]; 2017 return defaultObjects[service];
2006 } 2018 }
2007 return new savedObjects[service](locales, useOptions); 2019 return new savedObjects[service](locales, useOptions);
2008 } 2020 }
2009 2021
2010 function LocaleConvertCase(s, locales, isToUpper) { 2022 function LocaleConvertCase(s, locales, isToUpper) {
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
2132 var options = arguments[1]; 2144 var options = arguments[1];
2133 var numberFormat = cachedOrNewService('numberformat', locales, options); 2145 var numberFormat = cachedOrNewService('numberformat', locales, options);
2134 return formatNumber(numberFormat, this); 2146 return formatNumber(numberFormat, this);
2135 } 2147 }
2136 ); 2148 );
2137 2149
2138 2150
2139 /** 2151 /**
2140 * Returns actual formatted date or fails if date parameter is invalid. 2152 * Returns actual formatted date or fails if date parameter is invalid.
2141 */ 2153 */
2142 function toLocaleDateTime(date, locales, options, required, defaults, service) { 2154 function toLocaleDateTime(date, locales, options, required, defaults, service,
2155 useCached) {
2143 if (!(date instanceof GlobalDate)) { 2156 if (!(date instanceof GlobalDate)) {
2144 throw %make_type_error(kMethodInvokedOnWrongType, "Date"); 2157 throw %make_type_error(kMethodInvokedOnWrongType, "Date");
2145 } 2158 }
2146 2159
2147 var dateValue = TO_NUMBER(date); 2160 var dateValue = TO_NUMBER(date);
2148 if (NUMBER_IS_NAN(dateValue)) return 'Invalid Date'; 2161 if (NUMBER_IS_NAN(dateValue)) return 'Invalid Date';
2149 2162
2150 var internalOptions = toDateTimeOptions(options, required, defaults); 2163 var internalOptions = toDateTimeOptions(options, required, defaults);
2151 2164
2152 var dateFormat = 2165 var dateFormat =
2153 cachedOrNewService(service, locales, options, internalOptions); 2166 cachedOrNewService(service, locales, options, internalOptions, useCached);
2154 2167
2155 return formatDate(dateFormat, date); 2168 return formatDate(dateFormat, date);
2156 } 2169 }
2157 2170
2158 2171
2159 /** 2172 /**
2160 * Formats a Date object (this) using locale and options values. 2173 * Formats a Date object (this) using locale and options values.
2161 * If locale or options are omitted, defaults are used - both date and time are 2174 * If locale or options are omitted, defaults are used - both date and time are
2162 * present in the output. 2175 * present in the output.
2163 */ 2176 */
2164 OverrideFunction(GlobalDate.prototype, 'toLocaleString', function() { 2177 OverrideFunction(GlobalDate.prototype, 'toLocaleString', function() {
2165 var locales = arguments[0]; 2178 var locales = arguments[0];
2166 var options = arguments[1]; 2179 var options = arguments[1];
2167 return toLocaleDateTime( 2180 return toLocaleDateTime(
2168 this, locales, options, 'any', 'all', 'dateformatall'); 2181 this, locales, options, 'any', 'all', 'dateformatall');
2169 } 2182 }
2170 ); 2183 );
2171 2184
2185 OverrideFunction(GlobalDate.prototype, 'toString', function() {
2186 var options = {
2187 weekday: "short", month: "short", year: "numeric", day: "2-digit",
2188 hour: "2-digit", minute: "2-digit", second: "2-digit",
2189 timeZoneName: "short", hour12: false,
2190 };
2191
2192 return toLocaleDateTime(
2193 this, "en-US", options, 'any', 'all', 'datetostring', true);
2194 }
2195 );
2196
2172 2197
2173 /** 2198 /**
2174 * Formats a Date object (this) using locale and options values. 2199 * Formats a Date object (this) using locale and options values.
2175 * If locale or options are omitted, defaults are used - only date is present 2200 * If locale or options are omitted, defaults are used - only date is present
2176 * in the output. 2201 * in the output.
2177 */ 2202 */
2178 OverrideFunction(GlobalDate.prototype, 'toLocaleDateString', function() { 2203 OverrideFunction(GlobalDate.prototype, 'toLocaleDateString', function() {
2179 var locales = arguments[0]; 2204 var locales = arguments[0];
2180 var options = arguments[1]; 2205 var options = arguments[1];
2181 return toLocaleDateTime( 2206 return toLocaleDateTime(
2182 this, locales, options, 'date', 'date', 'dateformatdate'); 2207 this, locales, options, 'date', 'date', 'dateformatdate');
2183 } 2208 }
2184 ); 2209 );
2185 2210
2211 OverrideFunction(GlobalDate.prototype, 'toDateString', function() {
2212 var options = { weekday: "short", month: "short", year: "numeric",
2213 day: "2-digit",};
2214
2215 return toLocaleDateTime(
2216 this, "en-US", options, 'date', 'date', 'datetodatestring', true);
2217 }
2218 );
2219
2186 2220
2187 /** 2221 /**
2188 * Formats a Date object (this) using locale and options values. 2222 * Formats a Date object (this) using locale and options values.
2189 * If locale or options are omitted, defaults are used - only time is present 2223 * If locale or options are omitted, defaults are used - only time is present
2190 * in the output. 2224 * in the output.
2191 */ 2225 */
2192 OverrideFunction(GlobalDate.prototype, 'toLocaleTimeString', function() { 2226 OverrideFunction(GlobalDate.prototype, 'toLocaleTimeString', function() {
2193 var locales = arguments[0]; 2227 var locales = arguments[0];
2194 var options = arguments[1]; 2228 var options = arguments[1];
2195 return toLocaleDateTime( 2229 return toLocaleDateTime(
2196 this, locales, options, 'time', 'time', 'dateformattime'); 2230 this, locales, options, 'time', 'time', 'dateformattime');
2197 } 2231 }
2198 ); 2232 );
2199 2233
2200 %FunctionRemovePrototype(FormatDateToParts); 2234 %FunctionRemovePrototype(FormatDateToParts);
2201
2202 utils.Export(function(to) { 2235 utils.Export(function(to) {
2203 to.FormatDateToParts = FormatDateToParts; 2236 to.FormatDateToParts = FormatDateToParts;
2204 }); 2237 });
2205 2238
2239 OverrideFunction(GlobalDate.prototype, 'toTimeString', function() {
2240 var options = { hour: "2-digit", minute: "2-digit", second: "2-digit",
2241 timeZoneName: "short", hour12: false};
2242
2243 return toLocaleDateTime(
2244 this, "en-US", options, 'time', 'time', 'datetotimestring', true);
2245 }
2246 );
2247
2206 }) 2248 })
OLDNEW
« no previous file with comments | « no previous file | src/runtime/runtime-i18n.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698