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. neuFlow&CNP-卷积计算加速器&神经网络加速芯片生态系统

    上周看到韩松毕业论文,扯出神经网络加速器EIE,刚好这周调研了一下neuFlow,扯出09年的一篇做卷积加速的文章,大牛Lecun Yan的学生做的,一晃眼,快十年了.也记录之. 这一套还没研究透,又 ...

  2. 财务自由VS精神自由

    财务自由 财务自由,在物质层面改善人的生活.它使人不愁生计.住更宽敞明亮的房间,穿锦衣绸缎,自由自在地游玩,做自己想做的事儿.可是,这就是它的能力所及了.钱无法改变人的品味.审美和人格.它也无法告诉人 ...

  3. C#获取驱动器盘符

    一.使用ManagementObjectSearcher类 static void Main(string[] args) { SelectQuery selectQuery = new Select ...

  4. javamail邮件Multipart支持同时发text和html混合消息,alternative纯文本与超文本共存

    javamail邮件Multipart支持同时发text和html混合消息alternative纯文本与超文本共存 multipart/mixed:附件. multipart/related:内嵌资源 ...

  5. maven 项目打可执行jar包

    昨晚,突然就来了紧急任务. 验签较慢,着手优化,发来一个demo.     首先需要把该demo部署在Linux上.     该项目是maven 项目,所以用maven打个jar包,打完jar包之后, ...

  6. Hbase 过滤器的使用

    Filter filter= new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("."+d ...

  7. Python基础(一)_数据类型、条件判断、循环、列表

    编译型语言(中文版)运行代码之前,要先编译.然后再运行编译时间比较长c.c++.c# 解释型语言(翻译版)运行的时候才去编译,运行一次编译.运行效率没有编译型语言快python.ruby.shell. ...

  8. STM32开发 -- 4G模块开发详解(转)

    STM32开发 -- 4G模块开发详解(1) STM32开发 -- 4G模块开发详解(2) STM32开发 -- 4G模块开发详解(3) STM32开发 -- 4G模块开发详解(4)

  9. ==和equasl、hashmap原理(***)

    public class String01 { public static void main(String[] args) { String a="test"; String b ...

  10. kivy中size和pos的使用

    kivy中位置和大小属性的使用: -------------------位置---------------------------- 1.pos_hint(‘x-axis-key’:value,’y- ...