将postgres日期表示转换为ISO 8601字符串

问题描述 投票:24回答:5

我正在尝试将Postgres日期表示格式化为ISO 8601字符串。我假设有一个Postgres函数可以做到,但我发现文档简短的例子。

我的疑问是

SELECT
  now()::timestamp

返回

[{{2016, 8, 9}, {3, 56, 55, 754181}}]

我正在尝试将日期变成一种看起来更像2016-8-9T03:56:55+00:00的格式。

我需要对查询进行哪些更改才能实现此目的?谢谢你的帮助。

postgresql date elixir iso8601 ecto
5个回答
22
投票

我想我找到了一种格式化的方法,但它并不理想,因为我自己编写格式。

这是一个潜在的解决方案:

SELECT to_char (now()::timestamp at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')

17
投票

也许对于某人而言,知道由于Postgres 9.4 to_json函数(以及row_to_json)也将时间戳转换为适当的ISO 8601格式会有所帮助,但此外它还会在引号中包含一些可能不合适的值:

SELECT now();
  2017-05-10 15:57:23.736054+03

SELECT to_json(now());
  "2017-05-10T15:57:23.769561+03:00"

-- in case you want to trim the quotes
SELECT trim(both '"' from to_json(now())::text);
  2017-05-10T15:57:23.806563+03:00

5
投票

这是“将PostgreSQL日期表示转换为ISO 8601字符串”的简洁方法:

SELECT to_json(now())#>>'{}'

它使用#>>运算符结合to_json()函数,可以在这个页面上找到:https://www.postgresql.org/docs/current/functions-json.html

运算符“将指定路径中的[s] JSON对象作为文本”。但是,当您指定空数组文字'{}'作为路径时,它指定根对象。

将此方法与类似方法进行比较:

SELECT
to_json(now())::text AS has_unwanted_quotes,
trim(both '"' from to_json(now())::text) AS a_bit_lengthy,
to_json(now())#>>'{}' AS just_right

它更短但产生相同的结果。


4
投票

timezone会话变量设置为您希望输出所在的任何时区,然后使用to_char(now(), 'YYYY-MM-DD"T"HH24:MI:SSOF')

如果你使用at time zone '...',请注意这将剥离任何时区信息,并假设用户已经知道时区。

如果您使用at time zone 'UTC',则输出应始终为UTC时间,并具有正确的时区信息(无偏移)。

set timezone='UTC';


select to_char(now(), 'YYYY-MM-DD"T"HH24:MI:SSOF');

2017-11-17T02:02:26+00  /* UTC time */


select to_char(now() at time zone 'Australia/Sydney', 'YYYY-MM-DD"T"HH24:MI:SSOF');

2017-11-17T13:02:26+00  /* Local Sydney time, but note timezone is incorrect. */


set timezone='Australia/Sydney';


select to_char(now(), 'YYYY-MM-DD"T"HH24:MI:SSOF');

2017-11-17T13:02:26+11  /* Local Sydney time with correct time zone! */


select to_char(now() at time zone 'Australia/Sydney', 'YYYY-MM-DD"T"HH24:MI:SSOF');

2017-11-17T13:02:26+00  /* Still local Sydney time, but time zone info has been removed. */


select to_char(now() at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SSOF');

2017-11-17T02:02:26+00  /* Correct UTC time with correct offset. */

This blog post给出了非常详细的解释。


3
投票

只有功能对我有用,因为你需要设置时区。

要使用区域具有默认值时区:

create table somedata (
  release_date timestamptz DEFAULT NOW()
)

创建function

CREATE OR REPLACE FUNCTION date_display_tz(param_dt timestamp with time zone)
 RETURNS text AS
$$
DECLARE var_result varchar;
BEGIN
PERFORM set_config('timezone', 'UTC', true);
var_result := to_char(param_dt , 'YYYY-MM-DD"T"HH24:MI:SS:MS"Z"');
RETURN var_result;
END;
$$ language plpgsql VOLATILE;

并输出:

# SELECT
#   localtimestamp, current_timestamp,
#   to_char(localtimestamp, 'YYYY-MM-DD"T"HH24:MI:SS:MS"Z"'),
#   to_char(current_timestamp, 'YYYY-MM-DD"T"HH24:MI:SS:MS"Z"'),
#   date_display_tz(localtimestamp), date_display_tz(current_timestamp);
         timestamp          |              now              |         to_char          |         to_char          |     date_display_tz      |     date_display_tz
----------------------------+-------------------------------+--------------------------+--------------------------+--------------------------+--------------------------
 2017-04-27 23:48:03.802764 | 2017-04-27 21:48:03.802764+00 | 2017-04-27T23:48:03:802Z | 2017-04-27T23:48:03:802Z | 2017-04-27T21:48:03:802Z | 2017-04-27T21:48:03:802Z
(1 row)

再看看this

如果您希望服务器返回另一个时区的时区信息,我相信您需要使用SET TIME ZONE。否则,服务器自动(转换时间戳)并返回服务器的时区。

test=# select (current_timestamp at time zone 'UTC') at time zone 'UTC';
            timezone
-------------------------------
  2005-04-22 16:26:57.209082+09
(1 row)

test=# set time zone 'UTC';
SET
test=# select (current_timestamp at time zone 'UTC') at time zone 'UTC';
            timezone
-------------------------------
  2005-04-22 07:27:55.841596+00
(1 row)

test=# select (current_timestamp at time zone 'UTC');
           timezone
----------------------------
  2005-04-22 07:28:48.888154
(1 row)

test=# select (current_timestamp at time zone 'UTC')::timestamptz;
            timezone
-------------------------------
  2005-04-22 07:38:19.979511+00
(1 row)
© www.soinside.com 2019 - 2024. All rights reserved.