[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}
This commit is contained in:
parent
e61ff99104
commit
ccfe50b95a
6
BUILD.gn
6
BUILD.gn
@ -2323,6 +2323,7 @@ v8_component("v8_libbase") {
|
||||
"src/base/safe_math_impl.h",
|
||||
"src/base/sys-info.cc",
|
||||
"src/base/sys-info.h",
|
||||
"src/base/timezone-cache.h",
|
||||
"src/base/utils/random-number-generator.cc",
|
||||
"src/base/utils/random-number-generator.h",
|
||||
]
|
||||
@ -2338,7 +2339,10 @@ v8_component("v8_libbase") {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -29,9 +29,9 @@
|
||||
#undef MAP_TYPE
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform-posix.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
|
||||
|
||||
namespace v8 {
|
||||
namespace base {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
class AIXTimezoneCache : public PosixTimezoneCache {
|
||||
const char* LocalTimezone(double time) override;
|
||||
|
||||
const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
|
||||
double LocalTimeOffset() override;
|
||||
|
||||
~AIXTimezoneCache() override {}
|
||||
};
|
||||
|
||||
const char* AIXTimezoneCache::LocalTimezone(double time) {
|
||||
if (std::isnan(time)) return "";
|
||||
time_t tv = static_cast<time_t>(floor(time / msPerSecond));
|
||||
struct tm tm;
|
||||
@ -52,8 +59,7 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
|
||||
return tzname[0]; // The location of the timezone string on AIX.
|
||||
}
|
||||
|
||||
|
||||
double OS::LocalTimeOffset(TimezoneCache* cache) {
|
||||
double AIXTimezoneCache::LocalTimeOffset() {
|
||||
// On AIX, struct tm does not contain a tm_gmtoff field.
|
||||
time_t utc = time(NULL);
|
||||
DCHECK(utc != -1);
|
||||
@ -63,6 +69,7 @@ double OS::LocalTimeOffset(TimezoneCache* cache) {
|
||||
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) {
|
||||
const size_t msize = RoundUp(requested, getpagesize());
|
||||
|
@ -19,14 +19,22 @@
|
||||
#undef MAP_TYPE
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform-posix.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
#include "src/base/win32-headers.h"
|
||||
|
||||
namespace v8 {
|
||||
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 "";
|
||||
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
|
||||
struct tm tm;
|
||||
@ -35,8 +43,7 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
|
||||
return tzname[0]; // The location of the timezone string on Cygwin.
|
||||
}
|
||||
|
||||
|
||||
double OS::LocalTimeOffset(TimezoneCache* cache) {
|
||||
double CygwinTimezoneCache::LocalTimeOffset() {
|
||||
// On Cygwin, struct tm does not contain a tm_gmtoff field.
|
||||
time_t utc = time(NULL);
|
||||
DCHECK(utc != -1);
|
||||
|
@ -29,32 +29,13 @@
|
||||
#undef MAP_TYPE
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform-posix.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
|
||||
|
||||
namespace v8 {
|
||||
namespace base {
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
|
||||
|
||||
void* OS::Allocate(const size_t requested,
|
||||
size_t* allocated,
|
||||
|
@ -44,6 +44,7 @@
|
||||
#undef MAP_TYPE
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform-posix.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
|
||||
namespace v8 {
|
||||
@ -91,23 +92,7 @@ bool OS::ArmUsingHardFloat() {
|
||||
|
||||
#endif // def __arm__
|
||||
|
||||
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 (!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));
|
||||
}
|
||||
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
|
||||
|
||||
void* OS::Allocate(const size_t requested, size_t* allocated,
|
||||
bool is_executable) {
|
||||
|
@ -36,6 +36,7 @@
|
||||
#undef MAP_TYPE
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform-posix.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
|
||||
|
||||
@ -98,26 +99,7 @@ std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
|
||||
void OS::SignalCodeMovingGC() {
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
|
||||
|
||||
VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
|
||||
|
||||
|
@ -27,32 +27,13 @@
|
||||
#undef MAP_TYPE
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform-posix.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
|
||||
|
||||
namespace v8 {
|
||||
namespace base {
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
|
||||
|
||||
void* OS::Allocate(const size_t requested,
|
||||
size_t* allocated,
|
||||
|
@ -37,6 +37,8 @@
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "src/base/platform/platform-posix.h"
|
||||
|
||||
#include "src/base/lazy-instance.h"
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
@ -380,26 +382,25 @@ double OS::TimeCurrentMillis() {
|
||||
return Time::Now().ToJsTime();
|
||||
}
|
||||
|
||||
|
||||
class TimezoneCache {};
|
||||
|
||||
|
||||
TimezoneCache* OS::CreateTimezoneCache() {
|
||||
return NULL;
|
||||
const char* PosixTimezoneCache::LocalTimezone(double time) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
void OS::DisposeTimezoneCache(TimezoneCache* cache) {
|
||||
DCHECK(cache == NULL);
|
||||
double PosixTimezoneCache::LocalTimeOffset() {
|
||||
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::ClearTimezoneCache(TimezoneCache* cache) {
|
||||
DCHECK(cache == NULL);
|
||||
}
|
||||
|
||||
|
||||
double OS::DaylightSavingsOffset(double time, TimezoneCache*) {
|
||||
double PosixTimezoneCache::DaylightSavingsOffset(double time) {
|
||||
if (std::isnan(time)) return std::numeric_limits<double>::quiet_NaN();
|
||||
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
|
||||
struct tm tm;
|
||||
|
28
src/base/platform/platform-posix.h
Normal file
28
src/base/platform/platform-posix.h
Normal file
@ -0,0 +1,28 @@
|
||||
// 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 @@
|
||||
#undef MAP_TYPE
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform-posix.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
|
||||
|
||||
namespace v8 {
|
||||
namespace base {
|
||||
|
||||
@ -84,26 +84,7 @@ bool OS::ArmUsingHardFloat() {
|
||||
|
||||
#endif // __arm__
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
|
||||
|
||||
void* OS::Allocate(const size_t requested,
|
||||
size_t* allocated,
|
||||
|
@ -28,14 +28,21 @@
|
||||
#undef MAP_TYPE
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform-posix.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
|
||||
|
||||
namespace v8 {
|
||||
namespace base {
|
||||
|
||||
class SolarisTimezoneCache : public PosixTimezoneCache {
|
||||
const char* LocalTimezone(double time) override;
|
||||
|
||||
const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
|
||||
double LocalTimeOffset() override;
|
||||
|
||||
~SolarisTimezoneCache() override {}
|
||||
};
|
||||
|
||||
const char* SolarisTimezoneCache::LocalTimezone(double time) {
|
||||
if (std::isnan(time)) return "";
|
||||
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
|
||||
struct tm tm;
|
||||
@ -44,12 +51,12 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
|
||||
return tzname[0]; // The location of the timezone string on Solaris.
|
||||
}
|
||||
|
||||
|
||||
double OS::LocalTimeOffset(TimezoneCache* cache) {
|
||||
double SolarisTimezoneCache::LocalTimeOffset() {
|
||||
tzset();
|
||||
return -static_cast<double>(timezone * msPerSecond);
|
||||
}
|
||||
|
||||
TimezoneCache* OS::CreateTimezoneCache() { return new SolarisTimezoneCache(); }
|
||||
|
||||
void* OS::Allocate(const size_t requested,
|
||||
size_t* allocated,
|
||||
|
@ -24,9 +24,9 @@
|
||||
#include "src/base/macros.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
#include "src/base/platform/time.h"
|
||||
#include "src/base/timezone-cache.h"
|
||||
#include "src/base/utils/random-number-generator.h"
|
||||
|
||||
|
||||
// Extra functions for MinGW. Most of these are the _s functions which are in
|
||||
// the Microsoft Visual Studio C++ CRT.
|
||||
#ifdef __MINGW32__
|
||||
@ -101,13 +101,19 @@ bool g_hard_abort = false;
|
||||
|
||||
} // namespace
|
||||
|
||||
class TimezoneCache {
|
||||
class WindowsTimezoneCache : public TimezoneCache {
|
||||
public:
|
||||
TimezoneCache() : initialized_(false) { }
|
||||
WindowsTimezoneCache() : initialized_(false) {}
|
||||
|
||||
void Clear() {
|
||||
initialized_ = false;
|
||||
}
|
||||
~WindowsTimezoneCache() override {}
|
||||
|
||||
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
|
||||
// windows. If we cannot get the timezone information we fall back to CET.
|
||||
@ -216,14 +222,14 @@ class Win32Time {
|
||||
// LocalOffset(CET) = 3600000 and LocalOffset(PST) = -28800000. This
|
||||
// routine also takes into account whether daylight saving is effect
|
||||
// at the time.
|
||||
int64_t LocalOffset(TimezoneCache* cache);
|
||||
int64_t LocalOffset(WindowsTimezoneCache* cache);
|
||||
|
||||
// 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
|
||||
// timestamp taking into account daylight saving.
|
||||
char* LocalTimezone(TimezoneCache* cache);
|
||||
char* LocalTimezone(WindowsTimezoneCache* cache);
|
||||
|
||||
private:
|
||||
// Constants for time conversion.
|
||||
@ -235,7 +241,7 @@ class Win32Time {
|
||||
static const bool kShortTzNames = false;
|
||||
|
||||
// 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.
|
||||
FILETIME& ft() { return time_.ft_; }
|
||||
@ -350,7 +356,7 @@ void Win32Time::SetToCurrentTime() {
|
||||
// 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.
|
||||
// The function EquivalentTime() in date.js guarantees this.
|
||||
int64_t Win32Time::LocalOffset(TimezoneCache* cache) {
|
||||
int64_t Win32Time::LocalOffset(WindowsTimezoneCache* cache) {
|
||||
cache->InitializeIfNeeded();
|
||||
|
||||
Win32Time rounded_to_second(*this);
|
||||
@ -384,7 +390,7 @@ int64_t Win32Time::LocalOffset(TimezoneCache* cache) {
|
||||
|
||||
|
||||
// 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();
|
||||
|
||||
// Determine if DST is in effect at the specified time.
|
||||
@ -409,14 +415,14 @@ bool Win32Time::InDST(TimezoneCache* cache) {
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
// Returns a string identifying the current timezone for the
|
||||
// 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
|
||||
// saving is in effect at the given time.
|
||||
return InDST(cache) ? cache->dst_tz_name_ : cache->std_tz_name_;
|
||||
@ -448,47 +454,30 @@ double OS::TimeCurrentMillis() {
|
||||
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
|
||||
// account daylight saving.
|
||||
const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
|
||||
return Win32Time(time).LocalTimezone(cache);
|
||||
const char* WindowsTimezoneCache::LocalTimezone(double time) {
|
||||
return Win32Time(time).LocalTimezone(this);
|
||||
}
|
||||
|
||||
|
||||
// Returns the local time offset in milliseconds east of UTC without
|
||||
// taking daylight savings time into account.
|
||||
double OS::LocalTimeOffset(TimezoneCache* cache) {
|
||||
double WindowsTimezoneCache::LocalTimeOffset() {
|
||||
// Use current time, rounded to the millisecond.
|
||||
Win32Time t(TimeCurrentMillis());
|
||||
Win32Time t(OS::TimeCurrentMillis());
|
||||
// Time::LocalOffset inlcudes any daylight savings offset, so subtract it.
|
||||
return static_cast<double>(t.LocalOffset(cache) -
|
||||
t.DaylightSavingsOffset(cache));
|
||||
return static_cast<double>(t.LocalOffset(this) -
|
||||
t.DaylightSavingsOffset(this));
|
||||
}
|
||||
|
||||
|
||||
// Returns the daylight savings offset in milliseconds for the given
|
||||
// time.
|
||||
double OS::DaylightSavingsOffset(double time, TimezoneCache* cache) {
|
||||
int64_t offset = Win32Time(time).DaylightSavingsOffset(cache);
|
||||
double WindowsTimezoneCache::DaylightSavingsOffset(double time) {
|
||||
int64_t offset = Win32Time(time).DaylightSavingsOffset(this);
|
||||
return static_cast<double>(offset);
|
||||
}
|
||||
|
||||
TimezoneCache* OS::CreateTimezoneCache() { return new WindowsTimezoneCache(); }
|
||||
|
||||
int OS::GetLastError() {
|
||||
return ::GetLastError();
|
||||
|
@ -124,19 +124,6 @@ class V8_BASE_EXPORT OS {
|
||||
static double TimeCurrentMillis();
|
||||
|
||||
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.
|
||||
static int GetLastError();
|
||||
|
38
src/base/timezone-cache.h
Normal file
38
src/base/timezone-cache.h
Normal file
@ -0,0 +1,38 @@
|
||||
// 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() {
|
||||
after_ = &dst_[1];
|
||||
local_offset_ms_ = kInvalidLocalOffsetInMs;
|
||||
ymd_valid_ = false;
|
||||
base::OS::ClearTimezoneCache(tz_cache_);
|
||||
tz_cache_->Clear();
|
||||
}
|
||||
|
||||
|
||||
|
11
src/date.h
11
src/date.h
@ -7,9 +7,9 @@
|
||||
|
||||
#include "src/allocation.h"
|
||||
#include "src/base/platform/platform.h"
|
||||
#include "src/base/timezone-cache.h"
|
||||
#include "src/globals.h"
|
||||
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
||||
@ -44,7 +44,7 @@ class DateCache {
|
||||
}
|
||||
|
||||
virtual ~DateCache() {
|
||||
base::OS::DisposeTimezoneCache(tz_cache_);
|
||||
delete tz_cache_;
|
||||
tz_cache_ = NULL;
|
||||
}
|
||||
|
||||
@ -93,7 +93,7 @@ class DateCache {
|
||||
if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) {
|
||||
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
|
||||
@ -204,12 +204,11 @@ class DateCache {
|
||||
// These functions are virtual so that we can override them when testing.
|
||||
virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) {
|
||||
double time_ms = static_cast<double>(time_sec * 1000);
|
||||
return static_cast<int>(
|
||||
base::OS::DaylightSavingsOffset(time_ms, tz_cache_));
|
||||
return static_cast<int>(tz_cache_->DaylightSavingsOffset(time_ms));
|
||||
}
|
||||
|
||||
virtual int GetLocalOffsetFromOS() {
|
||||
double offset = base::OS::LocalTimeOffset(tz_cache_);
|
||||
double offset = tz_cache_->LocalTimeOffset();
|
||||
DCHECK(offset < kInvalidLocalOffsetInMs);
|
||||
return static_cast<int>(offset);
|
||||
}
|
||||
|
11
src/v8.gyp
11
src/v8.gyp
@ -1873,6 +1873,7 @@
|
||||
'base/safe_math_impl.h',
|
||||
'base/sys-info.cc',
|
||||
'base/sys-info.h',
|
||||
'base/timezone-cache.h',
|
||||
'base/utils/random-number-generator.cc',
|
||||
'base/utils/random-number-generator.h',
|
||||
],
|
||||
@ -1913,6 +1914,7 @@
|
||||
'sources': [
|
||||
'base/debug/stack_trace_posix.cc',
|
||||
'base/platform/platform-linux.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc',
|
||||
],
|
||||
}
|
||||
@ -1920,6 +1922,7 @@
|
||||
['OS=="android"', {
|
||||
'sources': [
|
||||
'base/debug/stack_trace_android.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc',
|
||||
],
|
||||
'link_settings': {
|
||||
@ -1975,6 +1978,7 @@
|
||||
},
|
||||
'sources': [
|
||||
'base/debug/stack_trace_posix.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc',
|
||||
'base/qnx-math.h'
|
||||
],
|
||||
@ -2005,6 +2009,7 @@
|
||||
'sources': [
|
||||
'base/debug/stack_trace_posix.cc',
|
||||
'base/platform/platform-freebsd.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc',
|
||||
],
|
||||
}
|
||||
@ -2016,6 +2021,7 @@
|
||||
]},
|
||||
'sources': [
|
||||
'base/platform/platform-openbsd.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc'
|
||||
],
|
||||
}
|
||||
@ -2028,6 +2034,7 @@
|
||||
'sources': [
|
||||
'base/debug/stack_trace_posix.cc',
|
||||
'base/platform/platform-openbsd.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc',
|
||||
],
|
||||
}
|
||||
@ -2036,6 +2043,7 @@
|
||||
'sources': [
|
||||
'base/debug/stack_trace_posix.cc',
|
||||
'base/platform/platform-aix.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc'
|
||||
]},
|
||||
],
|
||||
@ -2047,6 +2055,7 @@
|
||||
'sources': [
|
||||
'base/debug/stack_trace_posix.cc',
|
||||
'base/platform/platform-solaris.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc',
|
||||
],
|
||||
}
|
||||
@ -2055,6 +2064,7 @@
|
||||
'sources': [
|
||||
'base/debug/stack_trace_posix.cc',
|
||||
'base/platform/platform-macos.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc',
|
||||
]},
|
||||
],
|
||||
@ -2075,6 +2085,7 @@
|
||||
'sources': [
|
||||
'base/debug/stack_trace_posix.cc',
|
||||
'base/platform/platform-cygwin.cc',
|
||||
'base/platform/platform-posix.h',
|
||||
'base/platform/platform-posix.cc',
|
||||
],
|
||||
}, {
|
||||
|
Loading…
Reference in New Issue
Block a user