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

Side by Side Diff: src/i18n.cc

Issue 2724373002: [date] Add ICU backend for timezone info behind a flag (Closed)
Patch Set: ulan's formatting suggestion Created 3 years, 9 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 | « src/i18n.h ('k') | no next file » | 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 // limitations under the License. 4 // limitations under the License.
5 5
6 #include "src/i18n.h" 6 #include "src/i18n.h"
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "src/api.h" 10 #include "src/api.h"
(...skipping 881 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 return reinterpret_cast<icu::BreakIterator*>(obj->GetInternalField(0)); 892 return reinterpret_cast<icu::BreakIterator*>(obj->GetInternalField(0));
893 } 893 }
894 894
895 void V8BreakIterator::DeleteBreakIterator( 895 void V8BreakIterator::DeleteBreakIterator(
896 const v8::WeakCallbackInfo<void>& data) { 896 const v8::WeakCallbackInfo<void>& data) {
897 delete reinterpret_cast<icu::BreakIterator*>(data.GetInternalField(0)); 897 delete reinterpret_cast<icu::BreakIterator*>(data.GetInternalField(0));
898 delete reinterpret_cast<icu::UnicodeString*>(data.GetInternalField(1)); 898 delete reinterpret_cast<icu::UnicodeString*>(data.GetInternalField(1));
899 GlobalHandles::Destroy(reinterpret_cast<Object**>(data.GetParameter())); 899 GlobalHandles::Destroy(reinterpret_cast<Object**>(data.GetParameter()));
900 } 900 }
901 901
902 ICUTimezoneCache::ICUTimezoneCache() : timezone_(nullptr) { Clear(); }
903
904 ICUTimezoneCache::~ICUTimezoneCache() { Clear(); }
905
906 const char* ICUTimezoneCache::LocalTimezone(double time_ms) {
907 bool is_dst = DaylightSavingsOffset(time_ms) != 0;
908 char* name = is_dst ? dst_timezone_name_ : timezone_name_;
909 if (name[0] == '\0') {
910 icu::UnicodeString result;
911 GetTimeZone()->getDisplayName(is_dst, icu::TimeZone::SHORT, result);
jungshik at Google 2017/03/08 07:15:20 By using SHORT, you're matching v8 on Linux, but v
jungshik at Google 2017/03/08 07:27:43 Dan, you talked about root locale in your comment,
Dan Ehrenberg 2017/03/11 09:10:50 OK, I switched to LONG, but I am not sure whether
912
913 // With the SHORT format and the default locale, the result should be of
914 // the form PST or CET, short and within ASCII range.
915 int32_t length = result.length();
916 CHECK(length < kMaxTimezoneChars);
917 for (int32_t i = 0; i < length; i++) {
918 UChar ch = result[i];
919 CHECK(ch == static_cast<UChar>(static_cast<char>(ch)));
920 name[i] = static_cast<char>(ch);
921 }
922 name[length] = '\0';
923 }
924 return const_cast<const char*>(name);
925 }
926
927 icu::TimeZone* ICUTimezoneCache::GetTimeZone() {
928 if (timezone_ == nullptr) {
929 timezone_ = icu::TimeZone::createDefault();
930 }
931 return timezone_;
932 }
933
934 bool ICUTimezoneCache::GetOffsets(double time_ms, int32_t* raw_offset,
935 int32_t* dst_offset) {
936 UErrorCode status = U_ZERO_ERROR;
937 GetTimeZone()->getOffset(time_ms, false, *raw_offset, *dst_offset, status);
938 return U_SUCCESS(status);
939 }
940
941 double ICUTimezoneCache::DaylightSavingsOffset(double time_ms) {
942 int32_t raw_offset, dst_offset;
943 if (!GetOffsets(time_ms, &raw_offset, &dst_offset)) return 0;
944 return dst_offset;
945 }
946
947 double ICUTimezoneCache::LocalTimeOffset() {
948 int32_t raw_offset, dst_offset;
949 if (!GetOffsets(icu::Calendar::getNow(), &raw_offset, &dst_offset)) return 0;
950 return raw_offset;
951 }
952
953 void ICUTimezoneCache::Clear() {
954 delete timezone_;
955 timezone_ = nullptr;
956 timezone_name_[0] = '\0';
957 dst_timezone_name_[0] = '\0';
958 }
959
960 ICUOSTimezoneCache::ICUOSTimezoneCache() : os_tz_cache_(nullptr) {}
961
962 ICUOSTimezoneCache::~ICUOSTimezoneCache() { delete os_tz_cache_; }
963
964 const char* ICUOSTimezoneCache::LocalTimezone(double time_ms) {
965 const char* result = ICUTimezoneCache::LocalTimezone(time_ms);
966 // ICU often returns timezones which are not as descriptive as they
967 // could be, e.g., GMT+1 for Europe/Madrid when in en_US. When a timezone
968 // starts with GMT, call out to the OS to get the 'real' name.
jungshik at Google 2017/03/08 06:44:41 IMHO, if-clause had better be dropped for the rea
jungshik at Google 2017/03/08 07:15:20 "GMT+1030 (LHST)" : Who'd know what LHST stands
969 if (strncmp(result, "GMT", 3) == 0) {
970 if (os_tz_cache_ == nullptr) {
971 os_tz_cache_ = base::OS::CreateTimezoneCache();
972 }
973 return os_tz_cache_->LocalTimezone(time_ms);
974 }
975 return result;
976 }
977
978 void ICUOSTimezoneCache::Clear() {
979 ICUTimezoneCache::Clear();
980 if (os_tz_cache_) os_tz_cache_->Clear();
981 }
982
902 } // namespace internal 983 } // namespace internal
903 } // namespace v8 984 } // namespace v8
OLDNEW
« no previous file with comments | « src/i18n.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698