渲染器和主进程的时间差
Time difference between renderer and main process
我认为这可能与夏令时有关,但不完全确定。
基本上我有一个在渲染器和主进程之间共享的文件。
'use strict';
const time = new Date();
module.exports = () => console.log(time);
当我在主进程中要求并执行时,我得到
2017-07-10T12:34:17.613Z
然而,当我在渲染器进程中要求并执行时,我得到
Mon Jul 10 2017 13:44:08 GMT+0100 (GMT Daylight Time)
当我在节点控制台中执行 node -e "console.log(new Date())"
时,我得到与主进程相同的输出,这是有道理的,所以我假设 Chromium 管理日期的方式与节点不同。
我猜时差和我无法解释它指向两个问题:
- 时差 - 为什么会有时差?这是夏令时的一些影响吗?如果可以,渲染器和主进程是否可以同步?
- 二阶差 - 为什么还有二阶差?当主进程和渲染进程需要相同的文件时,它们共享相同的缓存吗?那么应该获取完全相同的日期并记录下来吗?
如能就这两点提供帮助,我们将不胜感激。
new Date()返回的对象在两种情况下都是一样的,只是通过两种不同的方式自动转换成字符串。
在主进程中,console.log()大概利用了toISOString ().
在渲染器进程中,对 toString () 的隐式调用发生:
The toString() method always returns a string representation of the
date in American English. JavaScript calls the toString() method
automatically when a date is to be represented as a text value or when
a date is referred to in a string concatenation.
关于第二个区别,我做了一些调查,似乎缓存是在进程级别处理的。实际上,我在此页面上找到了一些有用的信息:
Deep dive into Electron’s main and renderer processes
So for example, let’s say I have a module that holds some state that I
require in both my main and renderer process.
If I increment in my renderer, the count in the renderer will be 1,
but it’ll still be 0 in the main process. The two processes don’t
share memory or state. There are literally two instances of that
module running.
为了在两个进程中显示相同的时间,一个可能更好的方法是使用一个共享的全局变量来存储通过将.toISOString()或toString()应用于new Date()返回的字符串,然后从每个进程显式调用 console.log()。
在主进程中:
global.time = new Date ().toString ();
console.log (global.time);
在渲染器进程中:
console.log (remote.getGlobal ('time'));
我认为这可能与夏令时有关,但不完全确定。
基本上我有一个在渲染器和主进程之间共享的文件。
'use strict';
const time = new Date();
module.exports = () => console.log(time);
当我在主进程中要求并执行时,我得到
2017-07-10T12:34:17.613Z
然而,当我在渲染器进程中要求并执行时,我得到
Mon Jul 10 2017 13:44:08 GMT+0100 (GMT Daylight Time)
当我在节点控制台中执行 node -e "console.log(new Date())"
时,我得到与主进程相同的输出,这是有道理的,所以我假设 Chromium 管理日期的方式与节点不同。
我猜时差和我无法解释它指向两个问题:
- 时差 - 为什么会有时差?这是夏令时的一些影响吗?如果可以,渲染器和主进程是否可以同步?
- 二阶差 - 为什么还有二阶差?当主进程和渲染进程需要相同的文件时,它们共享相同的缓存吗?那么应该获取完全相同的日期并记录下来吗?
如能就这两点提供帮助,我们将不胜感激。
new Date()返回的对象在两种情况下都是一样的,只是通过两种不同的方式自动转换成字符串。
在主进程中,console.log()大概利用了toISOString ().
在渲染器进程中,对 toString () 的隐式调用发生:
The toString() method always returns a string representation of the date in American English. JavaScript calls the toString() method automatically when a date is to be represented as a text value or when a date is referred to in a string concatenation.
关于第二个区别,我做了一些调查,似乎缓存是在进程级别处理的。实际上,我在此页面上找到了一些有用的信息:
Deep dive into Electron’s main and renderer processes
So for example, let’s say I have a module that holds some state that I require in both my main and renderer process.
If I increment in my renderer, the count in the renderer will be 1, but it’ll still be 0 in the main process. The two processes don’t share memory or state. There are literally two instances of that module running.
为了在两个进程中显示相同的时间,一个可能更好的方法是使用一个共享的全局变量来存储通过将.toISOString()或toString()应用于new Date()返回的字符串,然后从每个进程显式调用 console.log()。
在主进程中:
global.time = new Date ().toString ();
console.log (global.time);
在渲染器进程中:
console.log (remote.getGlobal ('time'));