Cassandra - 插入时使用了错误的时区

Cassandra - Wrong timezone being used when inserting

这是我要插入数据库的日期:

'1970-01-18T00:00:00+00:00'

但是当我查看数据库中的条目时,它晚了 8 小时:

1970-01-17 16:00:00

我的 Ubuntu 系统使用的时区是 UTC。我在输出的终端中输入 date 进行了检查:

Tue Oct  4 00:00:53 UTC 2016

我正在使用 node.js,这实际上是用于插入的代码:

const Cassandra = require( 'cassandra-driver' );
const Promise = require( 'bluebird' );
const db = Promise.promisifyAll( new Cassandra.Client({ contactPoints: ['127.0.0.1'], keyspace: 'project' }) );

let date_created = '1970-01-18T00:00:00+00:00';
db.executeAsync( "INSERT INTO posts (id, date_created) VALUES (?, ?);", [someID, date_created], {prepare: true} );

我直接从 apache website 安装了 cassandra,在安装或执行二进制文件时没有指定任何时区或任何内容。

有谁知道为什么时间是 8 小时,我该如何解决?

编辑: 我尝试了不同的硬编码日期,显然并非所有日期都晚了 8 小时。与 2 月、3 月和 4 月相关联的日期在数据库中出现 7 小时。 12月还是晚了8小时。

编辑2: 哇!所以我没有提到我用来查看数据的 IDE 因为我认为它不重要,但我正在使用 dbeaver 。我决定在终端中使用 cqlsh 查看我的数据(而不是在 dbeaver 中查看),并且 值是正确的! 我也尝试通过 node.js 检索数据并打印出值,并且值也是正确的!这让我得出结论,dbeaver 或它使用的 cassandra 驱动程序有问题。

这是一个错误吗?或者有没有办法让日期在 dbeaver 中正确显示?

Cassandra timestamp represents a single moment in time and does not store timezone information, the correct representation in JavaScript is Date.

当转换为字符串时,Date实例根据系统时区显示日期和时间(系统运行Node.js)。

你的情况:

const d1 = new Date('1970-01-18T00:00:00+00:00');
// Numeric value as the number of milliseconds since UNIX epoch.
const time = d1.getTime();
db.executeAsync(insertQuery, [ id, d1 ], { prepare: true } );
// ...
db.executeAsync(selectQuery, [ id ], { prepare: true })
  .then((result) => {
    console.log('Are equal?', time, result.first()['date_created'].getTime())
  })

2 个值应该相等。

问题是 dbeaver 在后台使用 jvm,它依赖于 jvm 用来显示日期的任何时区,对我来说这是太平洋时区,因为我住在加利福尼亚。这解释了 7/8 小时的时差(取决于夏令时)。数据本身是正确的(dbeaver 只是为了显示目的而改变它)。

要更改用于在 dbeaver 中显示日期的时区,您只需添加几个命令行参数。如果您从终端 运行 dbeaver,则命令为:

path/to/dbeaver.exe -vmargs -Duser.timezone=UTC

为了简化操作,让您不必每次都输入,我建议创建一个桌面快捷方式。我在 Windows 机器上,所以我只是右键单击可执行文件,单击新建 > 快捷方式,右键单击快捷方式,单击 properties,然后将 -vmargs -Duser.timezone=UTC 附加到目标命令,使其看起来像 path/to/dbeaver.exe -vmargs -Duser.timezone=UTC。然后我就直接把快捷方式拖到桌面上了