timestamptztimestamp
SELECT ts AT TIME ZONE 'UTC'
FROM (
VALUES
(timestamptz '2012-03-05 17:00:00+0')
, (timestamptz '2012-03-05 18:00:00+1')
, (timestamp '2012-03-05 18:00:00+1') -- ① loaded footgun!
, (timestamp '2012-03-05 11:00:00' AT TIME ZONE '+6')
, (timestamp '2012-03-05 17:00:00' AT TIME ZONE 'UTC')
, (timestamp '2012-03-05 07:00:00' AT TIME ZONE 'US/Hawaii') -- ②
, (timestamp '2012-03-05 07:00:00' AT TIME ZONE 'HST') -- ②
) t(ts);
 SELECT timestamptz '2012-03-05 20:00+03',
timestamp '2012-03-05 17:00:00' AT TIME ZONE 'UTC',
timestamp '2012-03-05 17:00:00' AT TIME ZONE 'UTC' AT TIME ZONE 'UTC'

  

  

查看现在距1970-01-01 00:00:00 UTC 的秒数

select extract(epoch from now());
select timestamp without time zone 'epoch',timestamp without time zone 'epoch' + 3600*interval '1 sec'
select timestamp without time zone 'epoch',timestamp with time zone 'epoch'

  

  

 

把epoch 值转换回时间戳

select timestamp without time zone 'epoch',timestamp with time zone 'epoch' ,timestamp 'epoch',timestamptz 'epoch'

  

PostgreSQL的时间/日期函数使用

postgreSQL格式化时间的函数详解

mysql转为postgesql

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(UTC_TIMESTAMP()) + tz.gmt_offset, '%a, %d %b %Y, %H:%i:%s') AS local_time
FROM "Dict"."TimeZoneDetail" tz JOIN "Dict"."TimeZoneToCountry" z
ON tz.zone_id=z.zone_id
WHERE tz.time_start <= UNIX_TIMESTAMP(UTC_TIMESTAMP()) AND z.zone_name='America/Los_Angeles'
ORDER BY tz.time_start DESC LIMIT 1; SELECT (now() AT TIME ZONE 'UTC' )+interval '1'* tz."GMTOffset" AS local_time,now() as now,now() AT TIME ZONE 'UTC' as utcnow,tz."GMTOffset" as offsett
FROM "Dict"."TimeZoneDetail" tz inner JOIN "Dict"."TimeZoneToCountry" z
ON tz."ZoneId"=z."ZoneId"
WHERE to_timestamp(tz."TimeStart") <= timestamp 'epoch' AND z."ZoneName"='America/Los_Angeles'
ORDER BY tz."TimeStart" DESC LIMIT 1;

  

函数 返回类型 描述

示例

结果
age(timestamptimestamp) interval 计算两个时间戳的时间间隔

select age(timestamp '2001-04-10',

timestamp '1957-06-13');

43 years 9 mons 27 days
age(timestamp) interval 计算current_date与入参时间戳的时间间隔

select age(timestamp

'2016-07-07 12:00:00');

12:00:00
clock_timestamp() timestamp with time zone 当前时间戳(语句执行时变化) select clock_timestamp(); 2016-07-08 15:14:04.197732-07
current_date date 当前日期 select current_date; 2016-07-08
current_time time with time zone 当前时间 select current_time; 15:15:56.394651-07
current_timestamp timestamp with time zone 当前时间戳 select current_timestamp; 2016-07-08 15:16:50.485864-07
date_part(texttimestamp) double precision 获取时间戳中的某个子域,其中text可以为year,month,day,hour,minute,second等

select

date_part('year',timestamp'2016-07-08 12:05:06'),

date_part('month',timestamp'2016-07-08 12:05:06'),

date_part('day',timestamp'2016-07-08 12:05:06'),
date_part('hour',timestamp'2016-07-08 12:05:06'),

date_part('minute',timestamp'2016-07-08 12:05:06'),

date_part('second',timestamp'2016-07-08 12:05:06');

2016 | 7 | 8 | 12 | 5 | 6

date_part(textinterval) double precision 功能同上,只是第二个入参为时间间隔 select date_part('hour',interval'1 day 13:00:12'); 13
date_trunc(texttimestamp) timestamp

将时间戳截断成指定的精度,

指定精度后面的子域用0补充

select date_trunc('hour',

timestamp'2016-07-08 22:30:33');

2016-07-08 22:00:00
date_trunc(textinterval) interval 功能同上,只是第二个入参为时间间隔 select date_trunc('hour',interval'1 year 2 mon 3 day 22:30:33'); 1 year 2 mons 3 days 22:00:00
extract(field from timestamp) double precision 功能同date_part(texttimestamp) select extract(hour from timestamp'2016-07-08 22:30:29'); 22
extract(field from interval) double precision 功能同date_part(textinterval) select extract(hour from interval'1 day 13:00:12'); 13
isfinite(date) boolean 测试是否为有穷日期 select isfinite(date'2016-07-08'),isfinite(date'infinity'); t,f
isfinite(timestamp) boolean 测试是否为有穷时间戳 select isfinite(timestamp'2016-07-08'); t
isfinite(interval) boolean 测试是否为有穷时间间隔 select isfinite(interval'1day 23:02:12'); t
justify_days(interval) interval 按照每月30天调整时间间隔 select justify_days(interval'1year 45days 23:00:00'); 1 year 1 mon 15 days 23:00:00
justify_hours(interval) interval 按照每天24小时调整时间间隔 select justify_hours(interval'1year 45days 343hour'); 1 year 59 days 07:00:00
justify_interval(interval) interval 同时使用justify_days(interval)和justify_hours(interval) select justify_interval(interval'1year 45days 343hour'); 1 year 1 mon 29 days 07:00:00
localtime time 当日时间 select localtime; 15:45:18.892224
localtimestamp timestamp 当日日期和时间 select localtimestamp; 2016-07-08 15:46:55.181583
make_date(year intmonth intday int) date 创建一个日期 select make_date(2016,7,8); 2016-07-08

make_interval(

years int DEFAULT 0,

months int DEFAULT 0,

weeks int DEFAULT 0,

days int DEFAULT 0,

hours int DEFAULT 0,

mins int DEFAULT 0,

secs double precision

DEFAULT 0.0)

interval 创建一个时间间隔 select make_interval(1,hours=>3); 1 year 03:00:00

make_time(

hour int,

min int,

sec double precision)

time 创建一个时间 select make_time(9,21,23); 09:21:23

make_timestamp(

year intmonth int,

day inthour int,

min int,

sec double precision)

timestamp 创建一个时间戳 select make_timestamp(2016,7,8,22,55,23.5); 2016-07-08 22:55:23.5

make_timestamptz(year int,

month int,

day inthour int,

min intsec double precision, [ timezone text ])

timestamp with time zone 创建一个带有时区的时间戳 select make_timestamptz(2016,7,8,22,55,23.5); 2016-07-08 22:55:23.5-07
now() timestamp with time zone 当前日期和时间 select now(); 2016-07-08 15:55:30.873537-07
statement_timestamp() timestamp with time zone 同now()  select statement_timestamp(); 2016-07-08 15:56:07.259956-07
timeofday() text

当前日期和时间,包含周几,

功能与clock_timestamp()类似

select timeofday(); Fri Jul 08 15:57:51.277239 2016 PDT
transaction_timestamp() timestamp with time zone 事务开始时的时间戳 select transaction_timestamp(); 2016-07-08 16:01:25.007153-07
to_timestamp(double precision) timestamp with time zone

Convert Unix epoch

(seconds since 1970-01-01

00:00:00+00) to timestamp

select to_timestamp(1284352323);

2010-09-12 21:32:03-07

pg_sleep(seconds double precision);  

当前会话休眠seconds秒

select pg_sleep(5);  
pg_sleep_for(interval)   当前会话休眠多长时间的间隔 select pg_sleep_for('5 seconds');  
pg_sleep_until(timestamp with time zone)   当前会话休眠至什么时间点 select pg_sleep_until('2016-07-08 23:59:59');  

 

postgreSql——时区问题的更多相关文章

  1. java&postgresql时区总结

    介绍这篇文章之前,首先回答一个问题,以前都没有时区的概念,程序也写的好好的,为什么要计算时区哪?举个例子,比如有一个订单的时间是:2015-07-04 11:28:19,那么咋一看没什么问题,可是如果 ...

  2. JDBC 连接 postgresql 时区变 UTC

    加上 时区 语句 ..-Duser.timezone=PRC

  3. postgresql 和 mysql 数据库备份恢复以及时区问题

    概要 postgesql 12 备份/恢复脚本 时区设置 mysql 5.6 备份/恢复脚本 时区设置 概要 postgresql 和 mysql 是最常用的 2 种开源关系数据库, 很多项目也会优先 ...

  4. 【PostgreSQL】PostGreSQL数据库,时间数据类型

    ---"17:10:13.236"time without time zone:时:分:秒.毫秒 ---"17:10:13.236+08"time with t ...

  5. PostgreSQL 同步复制(1master+2standby)

    OS: Red Hat Enterprise Linux Server release 6.5 (Santiago) PostgreSQL: postgresql-9.4.5.tar.bz2 mast ...

  6. postgresql编译安装与调试(二)

    接前文postgresql编译安装与调试(一),继续说说postgresql的编译安装与调试. 上一篇已经详细说明了如何在Linux系统上编译安装postgresql,这次我们在此基础上简单讲讲如何在 ...

  7. 在CentOS上编译安装PostgreSQL

    http://my.oschina.net/tashi/blog 第一步:准备阶段 获取必需软件包: CentOS中查看是否安装了某个软件的命令:rpm -qa | grep 软件名.which命令可 ...

  8. PostgreSQL学习手册(常用数据类型)

    一.数值类型:     下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer ...

  9. 免费数据库(SQLite、Berkeley DB、PostgreSQL、MySQL、Firebird、mSQL、MSDE、DB2 Express-C、Oracle XE)

    SQLite数据库是中小站点CMS的最佳选择 SQLite 是一个类似Access的轻量级数据库系统,但是更小.更快.容量更大,并发更高.为什么说 SQLite 最适合做 CMS (内容管理系统)呢? ...

随机推荐

  1. wifi pj WiFiPhisher 安装使用

    1.安装kali linux: https://blog.csdn.net/qq_42545206/article/details/82788119 https://www.kali.org/down ...

  2. 分布式系统ID生成方案

    自增ID 不错,可以限度抑制ID的大小.但需要有一个中心化的节点作为解决原子性问题.可以选用Redis,MySQL,Zookeeper.成本有点高. UUID 分布式,而且唯一!缺点是生产的ID太长. ...

  3. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  4. python模板字符串和格式化字符串

    模板字符串:用string模块里的Template Template()里面把字符串中某个值用设置变量${key}的方式先写好,然后在substitute()的方式把变量用其他值代替,就完成了字符串的 ...

  5. 1113: No mapping for the Unicode character exists in the target multi-byte code page

    windows版本nginx启动 报错. 启动方式:到nginx所在目录执行:nginx.exe -c conf\nginx.conf 原因:所在路径中含有中文字符. 解决:换个没有中文的路径.

  6. Linux:编译安装boost 1.69库

    Boost库是为C++语言标准库提供扩展的一些C++程序库的总称,由Boost社区组织开发.维护.在C++的地位感觉可以和Spring在Java中相比. boost向来有准标准库之称,很多新特性例如智 ...

  7. Django后端项目----restful framework 认证源码流程

    一.请求到来之后,都要先执行dispatch方法,dispatch方法方法根据请求方式的不同触发get/post/put/delete等方法 注意,APIView中的dispatch方法有很多的功能 ...

  8. The Little Prince-12/05

    The Little Prince-12/05 "When a mystery is too overpowering, one dare not disobey. Absurd as it ...

  9. 开启redis-server提示 # Creating Server TCP listening socket *:6379: bind: Address already in use--解决方法

    在bin目录中开启Redis服务器,完整提示如下: 3496:C 25 Apr 00:56:48.717 # Warning: no config file specified, using the  ...

  10. Docker学习笔记之编写 Docker Compose 项目

    0x00 概述 通过阅读之前的小节,相信大家对 Docker 在开发中的应用已经有了一定的了解.作为一款实用的软件,我们必须回归到实践中来,这样才能更好地理解 Docker 的实用逻辑和背后的原理.在 ...