2

我正在尝试构建我的 Web 应用程序,它将数据存储在地球上某个位置上运行的 PostgreSQL 数据库服务器上,并让用户从其他位置连接,因此可能与我的服务器不同的时区和偏移量。

我需要根据每个连接用户显示操作的日期和时间,例如创建的帖子、编辑的帖子、提交的评论等。这就像 StackExchange。但是,我遇到了时区和偏移量的问题,如下所述:

在我的pgAdmin3 SQL Editor中,一切似乎都正常。例如,当我在pgAdmin3 SQL 编辑器中编写下面的查询时set local time zone 'Europe/Oslo',我得到了正确的poststagscreated_at字段+2,输出中的偏移量。在输出行中,表的字段created_at是 ,posts2016-08-29 19:15:53.758+02的同一行created_at是。tags2016-08-29T19:15:53.758+02:00

但是,当我将它放在我的Nodejs Express.js服务器中的路由函数中,并使用pg-promise作为连接库时,我只得到正确的表字段,其中的时间按预期附加了时区偏移量,我得到了表的字段在没有像预期的那样。 tagscreated_atOslocreated_atpostsUTC

所有时间戳的定义timestamp(3) with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP如下所示。此外,如果没有设置set local time zone,我会得到相同的行为,对于第一个表,我会得到UTC时间,对于后者,我会得到带有服务器偏移量的时间戳。

set local time zone指令不绑定所有查询吗?我的方法中缺少什么?

我使用的示例查询:

select
    q.*, -- created_at timestamp (with time zone) is one of those columns
    u.name as author,
    u.reputation,
    case when count(t.*)=0 then '[]' e json_agg(t.*) end as tags
from posts q

-- authors
join users u
on q.author_id = u.id

-- tags
left join post_has_tag p_h_t
on q.id = p_h_t.post_id
left join tags t
on p_h_t.tag_id = t.id

where q.post_type = 'question'
group by q.id, u.id;

express.js 路由函数示例:

trialRoutes.get('/x', function (req, res) {
    db.query(
        `
        --begin;
        SET LOCAL TIME ZONE 'Europe/Oslo';
        SELECT
            q.*, -- created_at timestamp (with time zone) is already in here
            u.name AS author,
            u.reputation,
            CASE WHEN count(t.*)=0 THEN '[]' ELSE json_agg(t.*) END as tags
        FROM posts q

        -- authors
        JOIN users u
        ON q.author_id = u.id

        -- tags
        left join post_has_tag p_h_t
        on q.id = p_h_t.post_id
        left join tags t
        on p_h_t.tag_id = t.id

        WHERE q.post_type = 'question'
        group by q.id, u.id;
        --commit;
        `
    )
        .then(function (data) {
            res.json(data)
        })
        .catch(function (error) {
            console.log("/login, database quesry error.", error);
        });
})

我使用pg-promiseExpress.js http 服务器获得的结果。请注意实际上应该指向 UTC 中相同点的不同时间戳,这是正确完成的,以及未正确完成的表示:

[
  {
    "id": "7",
    "created_at": "2016-08-29T21:02:04.153Z", // same point in time, different representation
    "title": "AAAAAAAAAAA",
    "text": "aaaaa aaaaaaa aaaaaa",
    "post_url": "AAAAAAAAAAA",
    "score": 0,
    "author_id": 1,
    "parent_post_id": null,
    "post_type": "question",
    "is_accepted": false,
    "acceptor_id": null,
    "timezone": "2016-08-29T20:02:04.153Z",
    "author": "Faruk",
    "reputation": 0,
    "tags": [
      {
        "id": 4,
        "created_at": "2016-08-29T23:02:04.153+02:00", // same point in time, different representation
        "label": "physics",
        "description": null,
        "category": null
      }
    ]
  },
  {
    "id": "6",
    "created_at": "2016-08-29T17:24:10.151Z",
    "title": "Ignoring timezones altogether in Rails and PostgreSQL",
    "text": "Ignoring timezones altogether in Rails and PostgreSQL",
    "post_url": "Ignoring-timezones-altogether-in-Rails-and-PostgreSQL",
    "score": 0,
    "author_id": 2,
    "parent_post_id": null,
    "post_type": "question",
    "is_accepted": false,
    "acceptor_id": null,
    "timezone": "2016-08-29T16:24:10.151Z",
    "author": "Selçuk",
    "reputation": 0,
    "tags": [
      {
        "id": 3,
        "created_at": "2016-08-29T19:24:10.151+02:00",
        "label": "sql",
        "description": null,
        "category": null
      }
    ]
  }
]

posts这里使用的和tags表的定义:

-- questions and answers
CREATE TABLE posts
(
  id bigserial PRIMARY KEY,
  created_at timestamp(3) with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,

  title character varying(100),
  text text,
  post_url character varying(100),

  score integer DEFAULT 0,
  author_id integer NOT NULL REFERENCES users (id),
  parent_post_id integer REFERENCES posts (id),
  post_type varchar(30),

  is_accepted boolean DEFAULT FALSE,
  acceptor_id integer REFERENCES users (id) DEFAULT NULL
  --seen_by_parent_post_author boolean DEFAULT false

  --view_count
  --accepted_answer_id
  --answer_count
);

CREATE TABLE tags
(
    id bigserial PRIMARY KEY,
    created_at timestamp(3) with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    label character varying(30) NOT NULL,
    description character varying(200),
    category character varying(50)
);
4

0 回答 0