【发布时间】:2015-11-09 01:41:29
【问题描述】:
我不太了解时间戳的用法,
例如
用户创建文章,可以选择PublishDate,系统也会自动存储CreateDate。
一个。我应该使用时区制作PublishDate 和CreateDate 时间戳并设置UTC?
b.用户发布字符串,然后我像下面使用momentjs 转换为utc 时间戳并存储,当有人选择此行时,将它们显示为用户客户端时间反向使用momentjs
c。我使用CURRENT_TIMESTAMP 到CreateDate,CURRENT_TIMESTAMP 这是否意味着服务器时间?我做对了吗?
我的想法是我总是将 utc 时区时间戳插入数据库,并且无论用户/客户端读取的位置,将数据转换为用户/客户端时区?我做对了吗?
一个。我的数据库(postgres)由
创建CREATE TABLE IF NOT EXISTS "Article"(
"ArticleId" SERIAL NOT NULL,
"PublishDate" timestamp with time zone,
"Active" bit NOT NULL,
"CreateByUserId" integer,
"CreateDate" timestamp with time zone,
PRIMARY KEY ("ArticleId")
);
SET timezone = 'UTC';
b.用户提交帖子到存储(nodejs)
// publishDate: '{"y":2015,"m":8,"d":16,"h":15,"mi":46,"s":24}
var publishDate = JSON.parse(req.body.publishDate);
var leadingZeroAndDateFormat = function(publishDate) {
return new Promise(function (fulfill, reject){
if (publishDate.m < 10) { publishDate.m = '0'+publishDate.m; }
if (publishDate.d < 10) { publishDate.d = '0'+publishDate.d; }
if (publishDate.h < 10) { publishDate.h = '0'+publishDate.h; }
if (publishDate.mi < 10) { publishDate.mi = '0'+publishDate.mi; }
if (publishDate.s < 10) { publishDate.s = '0'+publishDate.s; }
var str = publishDate.y+'-'+publishDate.m+'-'+publishDate.d+' '+publishDate.h+':'+publishDate.mi+':'+publishDate.s;
var utc = moment(str).unix();
fulfill(utc);
});
};
c。将CreateDate 插入数据库使用CURRENT_TIMESTAMP
var insertArticle = function(publishDate, active, createByUserId) {
return new Promise(function (fulfill, reject){
var query = 'INSERT INTO "Article" ("PublishDate","Active","CreateByUserId","CreateDate") VALUES ($1,$2,$3,CURRENT_TIMESTAMP) RETURNING "ArticleId"';
dbClient.query(query,[publishDate,active,createByUserId], function(error, result) {
if (error) {
reject(error);
} else {
fulfill(result);
}
});
});
};
更新
当我更改没有时区的所有列时,我执行insertArticle 显示错误
{ [error: date/time field value out of range: "1439717298"]
name: 'error',
length: 158,
severity: 'ERROR',
code: '22008',
detail: undefined,
hint: 'Perhaps you need a different "datestyle" setting.',
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'datetime.c',
line: '3775',
routine: 'DateTimeParseError' }
var insertArticle = function(publishDate, active, createByUserId) {
return new Promise(function (fulfill, reject){
var query = 'INSERT INTO "Article" ("PublishDate","Active","CreateByUserId","CreateDate") VALUES ($1,$2,$3,$4) RETURNING "ArticleId"';
dbClient.query(query,[publishDate,active,createByUserId,moment.utc().unix()], function(error, result) {
if (error) {
reject(error);
} else {
fulfill(result);
}
});
});
};
【问题讨论】:
-
始终使用 UTC 时间和没有时区的时间戳,因为不需要存储不同的时区。如果需要,然后在表示层处理区域。
-
感谢您的回复,是否意味着将列设置为不带时区?然后在插入时使用 momentjs utc 时间戳
-
是的,使数据库中的列没有时区,并始终存储从时刻开始的 UTC 时间。通过这种方式,您始终知道数据是什么,并且可以轻松处理它。演示也可以在您轻松选择的任何时区进行。
-
A) 始终使用类型 timestamptz。 B) 始终插入为 UTC,这是 PostgreSQL 始终自动理解的格式。如果您使用pg-promise,则默认情况下始终获得 UTC。
标签: javascript node.js postgresql datetime timestamp