From 367d12402ae6f3822b769da63fa5adeeaf8643b2 Mon Sep 17 00:00:00 2001 From: Robo Date: Wed, 17 Feb 2016 13:06:18 +0530 Subject: [PATCH] ipc: allow passing date instances --- .../v8_value_converter.cc | 35 +++++++++++++------ .../v8_value_converter.h | 4 --- spec/api-ipc-spec.js | 9 +++++ 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/atom/common/native_mate_converters/v8_value_converter.cc b/atom/common/native_mate_converters/v8_value_converter.cc index 7d3a1277cb8b..f5d7217ae52a 100644 --- a/atom/common/native_mate_converters/v8_value_converter.cc +++ b/atom/common/native_mate_converters/v8_value_converter.cc @@ -4,6 +4,7 @@ #include "atom/common/native_mate_converters/v8_value_converter.h" +#include #include #include #include @@ -20,6 +21,14 @@ namespace { const int kMaxRecursionDepth = 100; +const double kMsPerDay = 24 * 60 * 60 * 1000; + +int TimeInDay(double time_ms) { + if (time_ms < 0) time_ms -= (kMsPerDay -1); + int days = static_cast(time_ms / kMsPerDay); + return static_cast(time_ms - days * kMsPerDay); +} + } // namespace // The state of a call to FromV8Value. @@ -76,15 +85,10 @@ class V8ValueConverter::FromV8ValueState { }; V8ValueConverter::V8ValueConverter() - : date_allowed_(false), - reg_exp_allowed_(false), + : reg_exp_allowed_(false), function_allowed_(false), strip_null_from_objects_(false) {} -void V8ValueConverter::SetDateAllowed(bool val) { - date_allowed_ = val; -} - void V8ValueConverter::SetRegExpAllowed(bool val) { reg_exp_allowed_ = val; } @@ -243,12 +247,21 @@ base::Value* V8ValueConverter::FromV8ValueImpl( return NULL; if (val->IsDate()) { - if (!date_allowed_) - // JSON.stringify would convert this to a string, but an object is more - // consistent within this class. - return FromV8Object(val->ToObject(), state, isolate); v8::Date* date = v8::Date::Cast(*val); - return new base::FundamentalValue(date->NumberValue() / 1000.0); + double const time_ms = date->NumberValue(); + int const time_in_day_ms = TimeInDay(time_ms); + std::time_t time_s(time_ms / 1000.0); + char buf[128]; + std::tm* gm_time = std::gmtime(&time_s); + int year = gm_time->tm_year + 1900; + int month = gm_time->tm_mon + 1; + int ms = time_in_day_ms % 1000; + size_t length = snprintf(buf, sizeof buf, + "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", + year, month, gm_time->tm_mday, + gm_time->tm_hour, gm_time->tm_min, + gm_time->tm_sec, ms); + return new base::StringValue(std::string(buf, length)); } if (val->IsRegExp()) { diff --git a/atom/common/native_mate_converters/v8_value_converter.h b/atom/common/native_mate_converters/v8_value_converter.h index 2b695b43747b..95840c01ba7a 100644 --- a/atom/common/native_mate_converters/v8_value_converter.h +++ b/atom/common/native_mate_converters/v8_value_converter.h @@ -22,7 +22,6 @@ class V8ValueConverter { public: V8ValueConverter(); - void SetDateAllowed(bool val); void SetRegExpAllowed(bool val); void SetFunctionAllowed(bool val); void SetStripNullFromObjects(bool val); @@ -58,9 +57,6 @@ class V8ValueConverter { FromV8ValueState* state, v8::Isolate* isolate) const; - // If true, we will convert Date JavaScript objects to doubles. - bool date_allowed_; - // If true, we will convert RegExp JavaScript objects to string. bool reg_exp_allowed_; diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index 2c8bc28f1560..92a8c001bf17 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -102,6 +102,15 @@ describe('ipc module', function() { }); ipcRenderer.send('message', obj); }); + + it('can send instance of Date', function(done) { + const currentDate = new Date(); + ipcRenderer.once('message', function(event, value) { + assert.equal(value, currentDate.toISOString()); + done(); + }); + ipcRenderer.send('message', currentDate); + }); }); describe('ipc.sendSync', function() {