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 |