Commit ccfe50b9 authored by littledan's avatar littledan Committed by Commit bot

[date] Refactor TimezoneCache to be separate from the OS

This refactoring is preparatory work to enable ICU to be the backend
for timezone information rather than system calls. In the process, a
bit of code duplication that was inserted in the Solaris port patch is
eliminated here among modern POSIX backends.

One possible performance downside of this patch is that it introduces
a virtual method call for operations which were previously not virtual
methods. However, a couple factors mitigate this effect:
- The DateCache minimizes the need for calls into the TimezoneCache
- These calls were already not very high performance, as they included
  a system call which requires an RPC to get out of the sandbox, and
  they are surrounded by C++ builtins, which require a JS to C++
  transition.
- A future transition to ICU, enabled by this refactoring, may improve
  performance by eliminating the system call.

BUG=v8:6031

Review-Url: https://codereview.chromium.org/2731463003
Cr-Commit-Position: refs/heads/master@{#43588}
parent e61ff991
...@@ -2323,6 +2323,7 @@ v8_component("v8_libbase") { ...@@ -2323,6 +2323,7 @@ v8_component("v8_libbase") {
"src/base/safe_math_impl.h", "src/base/safe_math_impl.h",
"src/base/sys-info.cc", "src/base/sys-info.cc",
"src/base/sys-info.h", "src/base/sys-info.h",
"src/base/timezone-cache.h",
"src/base/utils/random-number-generator.cc", "src/base/utils/random-number-generator.cc",
"src/base/utils/random-number-generator.h", "src/base/utils/random-number-generator.h",
] ]
...@@ -2338,7 +2339,10 @@ v8_component("v8_libbase") { ...@@ -2338,7 +2339,10 @@ v8_component("v8_libbase") {
} }
if (is_posix) { if (is_posix) {
sources += [ "src/base/platform/platform-posix.cc" ] sources += [
"src/base/platform/platform-posix.cc",
"src/base/platform/platform-posix.h",
]
} }
if (is_linux) { if (is_linux) {
......
...@@ -29,9 +29,9 @@ ...@@ -29,9 +29,9 @@
#undef MAP_TYPE #undef MAP_TYPE
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform-posix.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
namespace v8 { namespace v8 {
namespace base { namespace base {
...@@ -42,8 +42,15 @@ static inline void* mmapHelper(size_t len, int prot, int flags, int fildes, ...@@ -42,8 +42,15 @@ static inline void* mmapHelper(size_t len, int prot, int flags, int fildes,
return mmap(addr, len, prot, flags, fildes, off); return mmap(addr, len, prot, flags, fildes, off);
} }
class AIXTimezoneCache : public PosixTimezoneCache {
const char* LocalTimezone(double time) override;
double LocalTimeOffset() override;
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ~AIXTimezoneCache() override {}
};
const char* AIXTimezoneCache::LocalTimezone(double time) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(floor(time / msPerSecond)); time_t tv = static_cast<time_t>(floor(time / msPerSecond));
struct tm tm; struct tm tm;
...@@ -52,8 +59,7 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ...@@ -52,8 +59,7 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
return tzname[0]; // The location of the timezone string on AIX. return tzname[0]; // The location of the timezone string on AIX.
} }
double AIXTimezoneCache::LocalTimeOffset() {
double OS::LocalTimeOffset(TimezoneCache* cache) {
// On AIX, struct tm does not contain a tm_gmtoff field. // On AIX, struct tm does not contain a tm_gmtoff field.
time_t utc = time(NULL); time_t utc = time(NULL);
DCHECK(utc != -1); DCHECK(utc != -1);
...@@ -63,6 +69,7 @@ double OS::LocalTimeOffset(TimezoneCache* cache) { ...@@ -63,6 +69,7 @@ double OS::LocalTimeOffset(TimezoneCache* cache) {
return static_cast<double>((mktime(loc) - utc) * msPerSecond); return static_cast<double>((mktime(loc) - utc) * msPerSecond);
} }
TimezoneCache* OS::CreateTimezoneCache() { return new AIXTimezoneCache(); }
void* OS::Allocate(const size_t requested, size_t* allocated, bool executable) { void* OS::Allocate(const size_t requested, size_t* allocated, bool executable) {
const size_t msize = RoundUp(requested, getpagesize()); const size_t msize = RoundUp(requested, getpagesize());
......
...@@ -19,14 +19,22 @@ ...@@ -19,14 +19,22 @@
#undef MAP_TYPE #undef MAP_TYPE
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform-posix.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#include "src/base/win32-headers.h" #include "src/base/win32-headers.h"
namespace v8 { namespace v8 {
namespace base { namespace base {
class CygwinTimezoneCache : public PosixTimezoneCache {
const char* LocalTimezone(double time) override;
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { double LocalTimeOffset() override;
~CygwinTimezoneCache() override {}
};
const char* CygwinTimezoneCache::LocalTimezone(double time) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm tm; struct tm tm;
...@@ -35,8 +43,7 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ...@@ -35,8 +43,7 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
return tzname[0]; // The location of the timezone string on Cygwin. return tzname[0]; // The location of the timezone string on Cygwin.
} }
double CygwinTimezoneCache::LocalTimeOffset() {
double OS::LocalTimeOffset(TimezoneCache* cache) {
// On Cygwin, struct tm does not contain a tm_gmtoff field. // On Cygwin, struct tm does not contain a tm_gmtoff field.
time_t utc = time(NULL); time_t utc = time(NULL);
DCHECK(utc != -1); DCHECK(utc != -1);
......
...@@ -29,32 +29,13 @@ ...@@ -29,32 +29,13 @@
#undef MAP_TYPE #undef MAP_TYPE
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform-posix.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
namespace v8 { namespace v8 {
namespace base { namespace base {
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return "";
return t->tm_zone;
}
double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL);
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
}
void* OS::Allocate(const size_t requested, void* OS::Allocate(const size_t requested,
size_t* allocated, size_t* allocated,
......
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
#undef MAP_TYPE #undef MAP_TYPE
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform-posix.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
namespace v8 { namespace v8 {
...@@ -91,23 +92,7 @@ bool OS::ArmUsingHardFloat() { ...@@ -91,23 +92,7 @@ bool OS::ArmUsingHardFloat() {
#endif // def __arm__ #endif // def __arm__
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time / msPerSecond));
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (!t || !t->tm_zone) return "";
return t->tm_zone;
}
double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL);
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
}
void* OS::Allocate(const size_t requested, size_t* allocated, void* OS::Allocate(const size_t requested, size_t* allocated,
bool is_executable) { bool is_executable) {
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#undef MAP_TYPE #undef MAP_TYPE
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform-posix.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
...@@ -98,26 +99,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { ...@@ -98,26 +99,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
void OS::SignalCodeMovingGC() { void OS::SignalCodeMovingGC() {
} }
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return "";
return t->tm_zone;
}
double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL);
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
}
VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
......
...@@ -27,32 +27,13 @@ ...@@ -27,32 +27,13 @@
#undef MAP_TYPE #undef MAP_TYPE
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform-posix.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
namespace v8 { namespace v8 {
namespace base { namespace base {
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return "";
return t->tm_zone;
}
double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL);
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
}
void* OS::Allocate(const size_t requested, void* OS::Allocate(const size_t requested,
size_t* allocated, size_t* allocated,
......
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
#include "src/base/platform/platform-posix.h"
#include "src/base/lazy-instance.h" #include "src/base/lazy-instance.h"
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
...@@ -380,26 +382,25 @@ double OS::TimeCurrentMillis() { ...@@ -380,26 +382,25 @@ double OS::TimeCurrentMillis() {
return Time::Now().ToJsTime(); return Time::Now().ToJsTime();
} }
const char* PosixTimezoneCache::LocalTimezone(double time) {
class TimezoneCache {}; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time / msPerSecond));
struct tm tm;
TimezoneCache* OS::CreateTimezoneCache() { struct tm* t = localtime_r(&tv, &tm);
return NULL; if (!t || !t->tm_zone) return "";
} return t->tm_zone;
void OS::DisposeTimezoneCache(TimezoneCache* cache) {
DCHECK(cache == NULL);
} }
double PosixTimezoneCache::LocalTimeOffset() {
void OS::ClearTimezoneCache(TimezoneCache* cache) { time_t tv = time(NULL);
DCHECK(cache == NULL); struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
} }
double PosixTimezoneCache::DaylightSavingsOffset(double time) {
double OS::DaylightSavingsOffset(double time, TimezoneCache*) {
if (std::isnan(time)) return std::numeric_limits<double>::quiet_NaN(); if (std::isnan(time)) return std::numeric_limits<double>::quiet_NaN();
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm tm; struct tm tm;
......
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_BASE_PLATFORM_PLATFORM_POSIX_H_
#define V8_BASE_PLATFORM_PLATFORM_POSIX_H_
#include "src/base/timezone-cache.h"
namespace v8 {
namespace base {
class PosixTimezoneCache : public TimezoneCache {
public:
const char* LocalTimezone(double time_ms) override;
double DaylightSavingsOffset(double time_ms) override;
double LocalTimeOffset() override;
void Clear() override {}
~PosixTimezoneCache() override {}
protected:
static const int msPerSecond = 1000;
};
} // namespace base
} // namespace v8
#endif // V8_BASE_PLATFORM_PLATFORM_POSIX_H_
...@@ -31,9 +31,9 @@ ...@@ -31,9 +31,9 @@
#undef MAP_TYPE #undef MAP_TYPE
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform-posix.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
namespace v8 { namespace v8 {
namespace base { namespace base {
...@@ -84,26 +84,7 @@ bool OS::ArmUsingHardFloat() { ...@@ -84,26 +84,7 @@ bool OS::ArmUsingHardFloat() {
#endif // __arm__ #endif // __arm__
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return "";
return t->tm_zone;
}
double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL);
struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
}
void* OS::Allocate(const size_t requested, void* OS::Allocate(const size_t requested,
size_t* allocated, size_t* allocated,
......
...@@ -28,14 +28,21 @@ ...@@ -28,14 +28,21 @@
#undef MAP_TYPE #undef MAP_TYPE
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform-posix.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
namespace v8 { namespace v8 {
namespace base { namespace base {
class SolarisTimezoneCache : public PosixTimezoneCache {
const char* LocalTimezone(double time) override;
double LocalTimeOffset() override;
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ~SolarisTimezoneCache() override {}
};
const char* SolarisTimezoneCache::LocalTimezone(double time) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm tm; struct tm tm;
...@@ -44,12 +51,12 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ...@@ -44,12 +51,12 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
return tzname[0]; // The location of the timezone string on Solaris. return tzname[0]; // The location of the timezone string on Solaris.
} }
double SolarisTimezoneCache::LocalTimeOffset() {
double OS::LocalTimeOffset(TimezoneCache* cache) {
tzset(); tzset();
return -static_cast<double>(timezone * msPerSecond); return -static_cast<double>(timezone * msPerSecond);
} }
TimezoneCache* OS::CreateTimezoneCache() { return new SolarisTimezoneCache(); }
void* OS::Allocate(const size_t requested, void* OS::Allocate(const size_t requested,
size_t* allocated, size_t* allocated,
......
...@@ -24,9 +24,9 @@ ...@@ -24,9 +24,9 @@
#include "src/base/macros.h" #include "src/base/macros.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#include "src/base/platform/time.h" #include "src/base/platform/time.h"
#include "src/base/timezone-cache.h"
#include "src/base/utils/random-number-generator.h" #include "src/base/utils/random-number-generator.h"
// Extra functions for MinGW. Most of these are the _s functions which are in // Extra functions for MinGW. Most of these are the _s functions which are in
// the Microsoft Visual Studio C++ CRT. // the Microsoft Visual Studio C++ CRT.
#ifdef __MINGW32__ #ifdef __MINGW32__
...@@ -101,13 +101,19 @@ bool g_hard_abort = false; ...@@ -101,13 +101,19 @@ bool g_hard_abort = false;
} // namespace } // namespace
class TimezoneCache { class WindowsTimezoneCache : public TimezoneCache {
public: public:
TimezoneCache() : initialized_(false) { } WindowsTimezoneCache() : initialized_(false) {}
void Clear() { ~WindowsTimezoneCache() override {}
initialized_ = false;
} void Clear() override { initialized_ = false; }
const char* LocalTimezone(double time) override;
double LocalTimeOffset() override;
double DaylightSavingsOffset(double time) override;
// Initialize timezone information. The timezone information is obtained from // Initialize timezone information. The timezone information is obtained from
// windows. If we cannot get the timezone information we fall back to CET. // windows. If we cannot get the timezone information we fall back to CET.
...@@ -216,14 +222,14 @@ class Win32Time { ...@@ -216,14 +222,14 @@ class Win32Time {
// LocalOffset(CET) = 3600000 and LocalOffset(PST) = -28800000. This // LocalOffset(CET) = 3600000 and LocalOffset(PST) = -28800000. This
// routine also takes into account whether daylight saving is effect // routine also takes into account whether daylight saving is effect
// at the time. // at the time.
int64_t LocalOffset(TimezoneCache* cache); int64_t LocalOffset(WindowsTimezoneCache* cache);
// Returns the daylight savings time offset for the time in milliseconds. // Returns the daylight savings time offset for the time in milliseconds.
int64_t DaylightSavingsOffset(TimezoneCache* cache); int64_t DaylightSavingsOffset(WindowsTimezoneCache* cache);
// Returns a string identifying the current timezone for the // Returns a string identifying the current timezone for the
// timestamp taking into account daylight saving. // timestamp taking into account daylight saving.
char* LocalTimezone(TimezoneCache* cache); char* LocalTimezone(WindowsTimezoneCache* cache);
private: private:
// Constants for time conversion. // Constants for time conversion.
...@@ -235,7 +241,7 @@ class Win32Time { ...@@ -235,7 +241,7 @@ class Win32Time {
static const bool kShortTzNames = false; static const bool kShortTzNames = false;
// Return whether or not daylight savings time is in effect at this time. // Return whether or not daylight savings time is in effect at this time.
bool InDST(TimezoneCache* cache); bool InDST(WindowsTimezoneCache* cache);
// Accessor for FILETIME representation. // Accessor for FILETIME representation.
FILETIME& ft() { return time_.ft_; } FILETIME& ft() { return time_.ft_; }
...@@ -350,7 +356,7 @@ void Win32Time::SetToCurrentTime() { ...@@ -350,7 +356,7 @@ void Win32Time::SetToCurrentTime() {
// Only times in the 32-bit Unix range may be passed to this function. // Only times in the 32-bit Unix range may be passed to this function.
// Also, adding the time-zone offset to the input must not overflow. // Also, adding the time-zone offset to the input must not overflow.
// The function EquivalentTime() in date.js guarantees this. // The function EquivalentTime() in date.js guarantees this.
int64_t Win32Time::LocalOffset(TimezoneCache* cache) { int64_t Win32Time::LocalOffset(WindowsTimezoneCache* cache) {
cache->InitializeIfNeeded(); cache->InitializeIfNeeded();
Win32Time rounded_to_second(*this); Win32Time rounded_to_second(*this);
...@@ -384,7 +390,7 @@ int64_t Win32Time::LocalOffset(TimezoneCache* cache) { ...@@ -384,7 +390,7 @@ int64_t Win32Time::LocalOffset(TimezoneCache* cache) {
// Return whether or not daylight savings time is in effect at this time. // Return whether or not daylight savings time is in effect at this time.
bool Win32Time::InDST(TimezoneCache* cache) { bool Win32Time::InDST(WindowsTimezoneCache* cache) {
cache->InitializeIfNeeded(); cache->InitializeIfNeeded();
// Determine if DST is in effect at the specified time. // Determine if DST is in effect at the specified time.
...@@ -409,14 +415,14 @@ bool Win32Time::InDST(TimezoneCache* cache) { ...@@ -409,14 +415,14 @@ bool Win32Time::InDST(TimezoneCache* cache) {
// Return the daylight savings time offset for this time. // Return the daylight savings time offset for this time.
int64_t Win32Time::DaylightSavingsOffset(TimezoneCache* cache) { int64_t Win32Time::DaylightSavingsOffset(WindowsTimezoneCache* cache) {
return InDST(cache) ? 60 * kMsPerMinute : 0; return InDST(cache) ? 60 * kMsPerMinute : 0;
} }
// Returns a string identifying the current timezone for the // Returns a string identifying the current timezone for the
// timestamp taking into account daylight saving. // timestamp taking into account daylight saving.
char* Win32Time::LocalTimezone(TimezoneCache* cache) { char* Win32Time::LocalTimezone(WindowsTimezoneCache* cache) {
// Return the standard or DST time zone name based on whether daylight // Return the standard or DST time zone name based on whether daylight
// saving is in effect at the given time. // saving is in effect at the given time.
return InDST(cache) ? cache->dst_tz_name_ : cache->std_tz_name_; return InDST(cache) ? cache->dst_tz_name_ : cache->std_tz_name_;
...@@ -448,47 +454,30 @@ double OS::TimeCurrentMillis() { ...@@ -448,47 +454,30 @@ double OS::TimeCurrentMillis() {
return Time::Now().ToJsTime(); return Time::Now().ToJsTime();
} }
TimezoneCache* OS::CreateTimezoneCache() {
return new TimezoneCache();
}
void OS::DisposeTimezoneCache(TimezoneCache* cache) {
delete cache;
}
void OS::ClearTimezoneCache(TimezoneCache* cache) {
cache->Clear();
}
// Returns a string identifying the current timezone taking into // Returns a string identifying the current timezone taking into
// account daylight saving. // account daylight saving.
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { const char* WindowsTimezoneCache::LocalTimezone(double time) {
return Win32Time(time).LocalTimezone(cache); return Win32Time(time).LocalTimezone(this);
} }
// Returns the local time offset in milliseconds east of UTC without // Returns the local time offset in milliseconds east of UTC without
// taking daylight savings time into account. // taking daylight savings time into account.
double OS::LocalTimeOffset(TimezoneCache* cache) { double WindowsTimezoneCache::LocalTimeOffset() {
// Use current time, rounded to the millisecond. // Use current time, rounded to the millisecond.
Win32Time t(TimeCurrentMillis()); Win32Time t(OS::TimeCurrentMillis());
// Time::LocalOffset inlcudes any daylight savings offset, so subtract it. // Time::LocalOffset inlcudes any daylight savings offset, so subtract it.
return static_cast<double>(t.LocalOffset(cache) - return static_cast<double>(t.LocalOffset(this) -
t.DaylightSavingsOffset(cache)); t.DaylightSavingsOffset(this));
} }
// Returns the daylight savings offset in milliseconds for the given // Returns the daylight savings offset in milliseconds for the given
// time. // time.
double OS::DaylightSavingsOffset(double time, TimezoneCache* cache) { double WindowsTimezoneCache::DaylightSavingsOffset(double time) {
int64_t offset = Win32Time(time).DaylightSavingsOffset(cache); int64_t offset = Win32Time(time).DaylightSavingsOffset(this);
return static_cast<double>(offset); return static_cast<double>(offset);
} }
TimezoneCache* OS::CreateTimezoneCache() { return new WindowsTimezoneCache(); }
int OS::GetLastError() { int OS::GetLastError() {
return ::GetLastError(); return ::GetLastError();
......
...@@ -124,19 +124,6 @@ class V8_BASE_EXPORT OS { ...@@ -124,19 +124,6 @@ class V8_BASE_EXPORT OS {
static double TimeCurrentMillis(); static double TimeCurrentMillis();
static TimezoneCache* CreateTimezoneCache(); static TimezoneCache* CreateTimezoneCache();
static void DisposeTimezoneCache(TimezoneCache* cache);
static void ClearTimezoneCache(TimezoneCache* cache);
// Returns a string identifying the current time zone. The
// timestamp is used for determining if DST is in effect.
static const char* LocalTimezone(double time, TimezoneCache* cache);
// Returns the local time offset in milliseconds east of UTC without
// taking daylight savings time into account.
static double LocalTimeOffset(TimezoneCache* cache);
// Returns the daylight savings offset for the given time.
static double DaylightSavingsOffset(double time, TimezoneCache* cache);
// Returns last OS error. // Returns last OS error.
static int GetLastError(); static int GetLastError();
......
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_BASE_TIMEZONE_CACHE_H_
#define V8_BASE_TIMEZONE_CACHE_H_
namespace v8 {
namespace base {
class TimezoneCache {
public:
// Short name of the local timezone (e.g., EST)
virtual const char* LocalTimezone(double time_ms) = 0;
// ES #sec-daylight-saving-time-adjustment
// Daylight Saving Time Adjustment
virtual double DaylightSavingsOffset(double time_ms) = 0;
// ES #sec-local-time-zone-adjustment
// Local Time Zone Adjustment
//
// TODO(littledan): Make more accurate with another parameter along the
// lines of this spec change:
// https://github.com/tc39/ecma262/pull/778
virtual double LocalTimeOffset() = 0;
// Called when the local timezone changes
virtual void Clear() = 0;
// Called when tearing down the isolate
virtual ~TimezoneCache() {}
};
} // namespace base
} // namespace v8
#endif // V8_BASE_TIMEZONE_CACHE_H_
...@@ -38,7 +38,7 @@ void DateCache::ResetDateCache() { ...@@ -38,7 +38,7 @@ void DateCache::ResetDateCache() {
after_ = &dst_[1]; after_ = &dst_[1];
local_offset_ms_ = kInvalidLocalOffsetInMs; local_offset_ms_ = kInvalidLocalOffsetInMs;
ymd_valid_ = false; ymd_valid_ = false;
base::OS::ClearTimezoneCache(tz_cache_); tz_cache_->Clear();
} }
......
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
#include "src/allocation.h" #include "src/allocation.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#include "src/base/timezone-cache.h"
#include "src/globals.h" #include "src/globals.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
...@@ -44,7 +44,7 @@ class DateCache { ...@@ -44,7 +44,7 @@ class DateCache {
} }
virtual ~DateCache() { virtual ~DateCache() {
base::OS::DisposeTimezoneCache(tz_cache_); delete tz_cache_;
tz_cache_ = NULL; tz_cache_ = NULL;
} }
...@@ -93,7 +93,7 @@ class DateCache { ...@@ -93,7 +93,7 @@ class DateCache {
if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) { if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) {
time_ms = EquivalentTime(time_ms); time_ms = EquivalentTime(time_ms);
} }
return base::OS::LocalTimezone(static_cast<double>(time_ms), tz_cache_); return tz_cache_->LocalTimezone(static_cast<double>(time_ms));
} }
// ECMA 262 - 15.9.5.26 // ECMA 262 - 15.9.5.26
...@@ -204,12 +204,11 @@ class DateCache { ...@@ -204,12 +204,11 @@ class DateCache {
// These functions are virtual so that we can override them when testing. // These functions are virtual so that we can override them when testing.
virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) { virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) {
double time_ms = static_cast<double>(time_sec * 1000); double time_ms = static_cast<double>(time_sec * 1000);
return static_cast<int>( return static_cast<int>(tz_cache_->DaylightSavingsOffset(time_ms));
base::OS::DaylightSavingsOffset(time_ms, tz_cache_));
} }
virtual int GetLocalOffsetFromOS() { virtual int GetLocalOffsetFromOS() {
double offset = base::OS::LocalTimeOffset(tz_cache_); double offset = tz_cache_->LocalTimeOffset();
DCHECK(offset < kInvalidLocalOffsetInMs); DCHECK(offset < kInvalidLocalOffsetInMs);
return static_cast<int>(offset); return static_cast<int>(offset);
} }
......
...@@ -1873,6 +1873,7 @@ ...@@ -1873,6 +1873,7 @@
'base/safe_math_impl.h', 'base/safe_math_impl.h',
'base/sys-info.cc', 'base/sys-info.cc',
'base/sys-info.h', 'base/sys-info.h',
'base/timezone-cache.h',
'base/utils/random-number-generator.cc', 'base/utils/random-number-generator.cc',
'base/utils/random-number-generator.h', 'base/utils/random-number-generator.h',
], ],
...@@ -1913,6 +1914,7 @@ ...@@ -1913,6 +1914,7 @@
'sources': [ 'sources': [
'base/debug/stack_trace_posix.cc', 'base/debug/stack_trace_posix.cc',
'base/platform/platform-linux.cc', 'base/platform/platform-linux.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc', 'base/platform/platform-posix.cc',
], ],
} }
...@@ -1920,6 +1922,7 @@ ...@@ -1920,6 +1922,7 @@
['OS=="android"', { ['OS=="android"', {
'sources': [ 'sources': [
'base/debug/stack_trace_android.cc', 'base/debug/stack_trace_android.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc', 'base/platform/platform-posix.cc',
], ],
'link_settings': { 'link_settings': {
...@@ -1975,6 +1978,7 @@ ...@@ -1975,6 +1978,7 @@
}, },
'sources': [ 'sources': [
'base/debug/stack_trace_posix.cc', 'base/debug/stack_trace_posix.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc', 'base/platform/platform-posix.cc',
'base/qnx-math.h' 'base/qnx-math.h'
], ],
...@@ -2005,6 +2009,7 @@ ...@@ -2005,6 +2009,7 @@
'sources': [ 'sources': [
'base/debug/stack_trace_posix.cc', 'base/debug/stack_trace_posix.cc',
'base/platform/platform-freebsd.cc', 'base/platform/platform-freebsd.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc', 'base/platform/platform-posix.cc',
], ],
} }
...@@ -2016,6 +2021,7 @@ ...@@ -2016,6 +2021,7 @@
]}, ]},
'sources': [ 'sources': [
'base/platform/platform-openbsd.cc', 'base/platform/platform-openbsd.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc' 'base/platform/platform-posix.cc'
], ],
} }
...@@ -2028,6 +2034,7 @@ ...@@ -2028,6 +2034,7 @@
'sources': [ 'sources': [
'base/debug/stack_trace_posix.cc', 'base/debug/stack_trace_posix.cc',
'base/platform/platform-openbsd.cc', 'base/platform/platform-openbsd.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc', 'base/platform/platform-posix.cc',
], ],
} }
...@@ -2036,6 +2043,7 @@ ...@@ -2036,6 +2043,7 @@
'sources': [ 'sources': [
'base/debug/stack_trace_posix.cc', 'base/debug/stack_trace_posix.cc',
'base/platform/platform-aix.cc', 'base/platform/platform-aix.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc' 'base/platform/platform-posix.cc'
]}, ]},
], ],
...@@ -2047,6 +2055,7 @@ ...@@ -2047,6 +2055,7 @@
'sources': [ 'sources': [
'base/debug/stack_trace_posix.cc', 'base/debug/stack_trace_posix.cc',
'base/platform/platform-solaris.cc', 'base/platform/platform-solaris.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc', 'base/platform/platform-posix.cc',
], ],
} }
...@@ -2055,6 +2064,7 @@ ...@@ -2055,6 +2064,7 @@
'sources': [ 'sources': [
'base/debug/stack_trace_posix.cc', 'base/debug/stack_trace_posix.cc',
'base/platform/platform-macos.cc', 'base/platform/platform-macos.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc', 'base/platform/platform-posix.cc',
]}, ]},
], ],
...@@ -2075,6 +2085,7 @@ ...@@ -2075,6 +2085,7 @@
'sources': [ 'sources': [
'base/debug/stack_trace_posix.cc', 'base/debug/stack_trace_posix.cc',
'base/platform/platform-cygwin.cc', 'base/platform/platform-cygwin.cc',
'base/platform/platform-posix.h',
'base/platform/platform-posix.cc', 'base/platform/platform-posix.cc',
], ],
}, { }, {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment