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

Unified Diff: src/i18n.cc

Issue 2724373002: [date] Add ICU backend for timezone info behind a flag (Closed)
Patch Set: Convert to UTF8 rather than check-failing 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/i18n.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/i18n.cc
diff --git a/src/i18n.cc b/src/i18n.cc
index 7c22871ff5e440f771659d44a0db013b34ec2105..f2e8e49370830038eec8da26983f3e20b2659fc7 100644
--- a/src/i18n.cc
+++ b/src/i18n.cc
@@ -12,6 +12,7 @@
#include "src/isolate.h"
#include "src/objects-inl.h"
#include "unicode/brkiter.h"
+#include "unicode/bytestream.h"
#include "unicode/calendar.h"
#include "unicode/coll.h"
#include "unicode/curramt.h"
@@ -909,5 +910,56 @@ void V8BreakIterator::DeleteBreakIterator(
GlobalHandles::Destroy(reinterpret_cast<Object**>(data.GetParameter()));
}
+ICUTimezoneCache::ICUTimezoneCache() : timezone_(nullptr) { Clear(); }
+
+ICUTimezoneCache::~ICUTimezoneCache() { Clear(); }
+
+const char* ICUTimezoneCache::LocalTimezone(double time_ms) {
+ bool is_dst = DaylightSavingsOffset(time_ms) != 0;
+ char* name = is_dst ? dst_timezone_name_ : timezone_name_;
+ if (name[0] == '\0') {
+ icu::UnicodeString result;
+ GetTimeZone()->getDisplayName(is_dst, icu::TimeZone::LONG, result);
+ result += '\0';
+
+ icu::CheckedArrayByteSink byte_sink(name, kMaxTimezoneChars);
ulan 2017/03/17 11:00:05 CHECK(!byte_sink.Overflowed());
Dan Ehrenberg 2017/04/06 15:35:21 Check has been added.
+ result.toUTF8(byte_sink);
+ }
+ return const_cast<const char*>(name);
+}
+
+icu::TimeZone* ICUTimezoneCache::GetTimeZone() {
+ if (timezone_ == nullptr) {
+ timezone_ = icu::TimeZone::createDefault();
+ }
+ return timezone_;
+}
+
+bool ICUTimezoneCache::GetOffsets(double time_ms, int32_t* raw_offset,
+ int32_t* dst_offset) {
+ UErrorCode status = U_ZERO_ERROR;
+ GetTimeZone()->getOffset(time_ms, false, *raw_offset, *dst_offset, status);
+ return U_SUCCESS(status);
+}
+
+double ICUTimezoneCache::DaylightSavingsOffset(double time_ms) {
+ int32_t raw_offset, dst_offset;
+ if (!GetOffsets(time_ms, &raw_offset, &dst_offset)) return 0;
+ return dst_offset;
+}
+
+double ICUTimezoneCache::LocalTimeOffset() {
+ int32_t raw_offset, dst_offset;
+ if (!GetOffsets(icu::Calendar::getNow(), &raw_offset, &dst_offset)) return 0;
+ return raw_offset;
+}
+
+void ICUTimezoneCache::Clear() {
+ delete timezone_;
+ timezone_ = nullptr;
+ timezone_name_[0] = '\0';
+ dst_timezone_name_[0] = '\0';
+}
+
} // namespace internal
} // namespace v8
« 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