postgresql支持的时间类型如下图所示:
在这里插入图片描述

日期 date:

建议日期的输入格式为1997-01-01,虽然也支持19970101,1/1/1997,Jan-1-1997等多种格式。

时间戳 timestamp[(p)] with(without) time zone:

其实配置文件是可以设置时区的,且做上层业务时也不会在多个时区间切换,所以一般使用无时区的时间戳就可以满足需要了。

建议时间戳的输入格式为1997-01-01 00:00:00

时间 time[(p)] with(without) time zone:

同样无时区的时间也是可以满足需要的,只表示一天的时间点,不包含日期,可以有如下格式:

12:00:00,120000,12:00

8:00 AM,8:00 PM

时间间隔 interval [fields][(p)]

我还是比较喜欢明确的时间间隔表示方法,如:

1 year 2 months 3 days 4 hours 5 minutes 6 seconds

可以用单词复数,也可以不用

test=# select interval '1 year 2 months 3 days 4 hours 5 minutes 6 seconds';
           interval            
-------------------------------
 1 year 2 mons 3 days 04:05:06
(1 row)

test=# select interval '2 year 2 months 3 days 4 hours 5 minutes 6 seconds';
            interval            
--------------------------------
 2 years 2 mons 3 days 04:05:06
(1 row)

test=# select interval '2 year 2 month 3 day 4 hour 5 minute 6 second';
            interval            
--------------------------------
 2 years 2 mons 3 days 04:05:06
(1 row)

单词太复杂?记不住?

test=# select interval '1 y 2 mon 2d 12:09:12';
           interval            
-------------------------------
 1 year 2 mons 2 days 12:09:12
(1 row)

test=# select interval '1 y 2 mon 2d 12h 09m 12s';
           interval            
-------------------------------
 1 year 2 mons 2 days 12:09:12
(1 row)

几个特殊日期和时间
在这里插入图片描述
以下是一些时间运算的示例:

test=# select timestamp(1) without time zone '2016-07-08 12:00:00.234';
timestamp
-----------------------
2016-07-08 12:00:00.2
(1 row)

test=# select time(1) without time zone '2016-07-08 12:00:00.234';
time
------------
12:00:00.2
(1 row)

test=# select date'2016-07-08' - 7;
  ?column?  
------------
 2016-07-01
(1 row)

test=# select date'2016-07-08' + 7;
  ?column?  
------------
 2016-07-15
(1 row)

test=# 
test=# select date'2016-07-08' + interval'1 day 2h';
      ?column?       
---------------------
 2016-07-09 02:00:00
(1 row)

test=# select date'2016-07-08' + time'22:00';
      ?column?       
---------------------
 2016-07-08 22:00:00
(1 row)

test=# select interval'1day' + interval'1h';
    ?column?    
----------------
 1 day 01:00:00
(1 row)

test=# select timestamp'2016-07-08 22:00:00' + interval'2hour';
      ?column?       
---------------------
 2016-07-09 00:00:00
(1 row)

test=# select timestamp'2016-07-08 22:00:00' - date'2016-07-08';
 ?column? 
----------
 22:00:00
(1 row)

test=# select 10*interval'1h';
 ?column? 
----------
 10:00:00
(1 row)

与格林威治时间相互转换

test=# select timestamp without time zone 'epoch';
      timestamp      
---------------------
 1970-01-01 00:00:00
(1 row)

test=# select timestamp without time zone 'epoch' + 3600*interval '1 sec';
      ?column?       
---------------------
 1970-01-01 01:00:00
(1 row)

时间函数:

函数返回类型描述示例结果
函数interval计算两个时间戳的时间间隔select age(timestamp ‘2001-04-10’,43 years 9 mons 27 days
timestamp ‘1957-06-13’);
age(timestamp)interval计算current_date与入参时间戳的时间间隔select age(timestamp12:00:00
‘2016-07-07 12:00:00’);
clock_timestamp()timestamp with time zone当前时间戳(语句执行时变化)select clock_timestamp();2016-07-08 15:14:04.197732-07
current_datedate当前日期select current_date;2016/7/8
current_timetime with time zone当前时间select current_time;15:15:56.394651-07
current_timestamptimestamp with time zone当前时间戳select current_timestamp;2016-07-08 15:16:50.485864-07
date_part(text, timestamp)double precision获取时间戳中的某个子域,其中text可以为year,month,day,hour,minute,second等select
date_part(‘year’,timestamp’2016-07-08 12:05:06’),2016
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’);
date_part(text, interval)double precision功能同上,只是第二个入参为时间间隔select date_part(‘hour’,interval’1 day 13:00:12’);13
date_trunc(text, timestamp)timestamp将时间戳截断成指定的精度,select date_trunc(‘hour’,2016/7/8 22:00
指定精度后面的子域用0补充timestamp’2016-07-08 22:30:33’);
date_trunc(text, interval)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(text, timestamp)select extract(hour from timestamp’2016-07-08 22:30:29’);22
extract(field from interval)double precision功能同date_part(text, interval)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
localtimetime当日时间select localtime;45:18.9
localtimestamptimestamp当日日期和时间select localtimestamp;46:55.2
make_date(year int, month int, day int)date创建一个日期select make_date(2016,7,8);2016/7/8
make_interval(interval创建一个时间间隔select make_interval(1,hours=>3);1 year 03:00:00
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)
make_time(time创建一个时间select make_time(9,21,23);9:21:23
hour int,
min int,
sec double precision)
make_timestamp(timestamp创建一个时间戳select make_timestamp(2016,7,8,22,55,23.5);55:23.5
year int, month int,
day int, hour int,
min int,
sec double precision)
make_timestamptz(year int,timestamp with time zone创建一个带有时区的时间戳select make_timestamptz(2016,7,8,22,55,23.5);2016-07-08 22:55:23.5-07
month int,
day int, hour int,
min int, sec double precision, [ timezone text ])
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当前日期和时间,包含周几,select timeofday();fri jul 08 15:57:51.277239 2016 pdt
功能与clock_timestamp()类似
transaction_timestamp()timestamp with time zone事务开始时的时间戳select transaction_timestamp();2016-07-08 16:01:25.007153-07
to_timestamp(double precision)timestamp with time zoneconvert unix epochselect to_timestamp(1284352323);2010-09-12 21:32:03-07
(seconds since 1970-01-01
00:00:00+00) to timestamp
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’);
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐