Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将带有时区的时间戳转换为localtime postgres_Sql_Database_Postgresql - Fatal编程技术网

Sql 将带有时区的时间戳转换为localtime postgres

Sql 将带有时区的时间戳转换为localtime postgres,sql,database,postgresql,Sql,Database,Postgresql,我不知道如何将存储的时间戳转换为本地时区,例如,我有一个查询 SELECT INTO result string_agg(concat_ws(' - ' ,sampleid ,starttime ,stoptime) ,'|') FROM ( Select "ID" as sampleid, to_char("StartTimestamp",'YYYY/MM/DD HH24:MI:SS') as start

我不知道如何将存储的时间戳转换为本地时区,例如,我有一个查询

SELECT INTO result string_agg(concat_ws(' - '
            ,sampleid
            ,starttime
            ,stoptime)
        ,'|')
FROM (
Select "ID" as sampleid,
 to_char("StartTimestamp",'YYYY/MM/DD HH24:MI:SS') as starttime,
to_char("EndTimestamp",'YYYY/MM/DD HH24:MI:SS') as stoptime from "Samples" ORDER BY starttime DESC) res;

即使“StartTimestamp”的类型为带时区的时间戳,我也不知道如何将其转换为系统所在的本地时区。

使用
AT time zone
操作符-或者只设置
TimeZone
系统变量

thetimestamptz AT TIME ZONE 'Australia/Perth'
这也适用于UTC偏移量,但请注意,PostgreSQL对UTC偏移量使用ISO-8601标准

请参阅PostgreSQL文档中的。特别注意有关时区格式的段落,以及以下段落:

另一个需要记住的问题是,在POSIX时区名称中,正偏移量用于格林威治以西的位置。在其他地方,PostgreSQL遵循ISO-8601约定,即正时区偏移位于格林威治以东


也许已经是在当地时间了,但是你把错误的时间存储到数据库中了?不,假设我在亚利桑那时间记录了很多记录,然后我在调用这个查询时去纽约,我希望它返回纽约时区的时间,我可以建议您确保数据库中记录的所有时间都是基于UTC的。您可以决定将本地时间记录为附加信息。对于任何以前的日期和时间,都可以将UTC时间转换为当地时间。要在web浏览器中显示UTC时间,此链接可能很有用:@RyanVincent
时区时间戳
存储在UTC中,在加载和存储时转换为
时区
系统变量中的时区。遗憾的是,在显示本地日期和转换输入日期时,存在比我喜欢的更多的“故障模式”;-/我住在UTC区域,DST在显示日期和时间时会把我弄糊涂-/我将如何在我的查询中使用at时区?@UbaldoQuintero我在上面展示了这一点。另见