postgresql时间相关函数

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: 时间与字符串转换to_charto_date, to_timestamp时间的计算日期时间戳timeinterval时间相关函数agecurrent_xxx,loc...

时间与字符串转换

to_char

postgres=# select to_char(timestamp'now','yyyy-mm-dd hh24:mi:ss.ssssss'),to_char(timestamp'now','hh24:mi:ss');
 2015-02-28 23:59:15.8635515 | 23:59:15

按照给定的格式输出

to_date, to_timestamp

postgres=# select to_date('2013-11-11','yyyy-mm-dd'),to_timestamp('2013-11-11 11:12:13','yyyy-mm-dd hh24:mi:ss');
 2013-11-11 | 2013-11-11 11:12:13+08

格式支持参考:时间格式-官方

时间的计算

日期

日期加减整数,interval,time

postgres=# select date'2015-2-16' - 1,date'2015-2-16' - interval '1 second',date'2015-2-16' + time '12:00:00';
 2015-02-15 | 2015-02-15 23:59:59 | 2015-02-16 12:00:00

日期减日期

postgres=# select date'2015-2-28' - date'2015-2-26';
        2

时间戳

时间戳加减跟日期是一样的

时间戳加减 interval,time

postgres=# select timestamp'2015-2-28 12:12:50' - 1;
ERROR:  operator does not exist: timestamp without time zone - integer
LINE 1: select timestamp'2015-2-28 12:12:50' - 1;
                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
postgres=# select timestamp'2015-2-28 12:12:50' - interval '1 day',timestamp'2015-2-28 12:12:50' + time '00:00:10';
2015-02-27 12:12:50 | 2015-02-28 12:13:00

时间戳不支持- 整数

时间戳-时间戳

postgres=# select timestamp'2015-2-28 12:12:50' - timestamp'2015-2-26 1:16:30';
 P2DT10H56M20S

结果是interval

time

postgres=# select time '1:00:00' - time '00:00:30',time '1:00:00' + interval '1 second';
 PT59M30S | 01:00:01

time 不支持 time + time

interval

postgres=# select interval '1 hour' - interval '1 minute',interval '1 hour' + interval '1 minute';
 PT59M    | PT1H1M

postgres=# select interval '1 hour' * 3,interval '1 hour' / 3;
 PT3H     | PT20M

interval 支持乘除

时间相关函数

age

postgres=# select age(timestamp'2015-2-28 12:30:50',timestamp'2015-2-24 10:21:23');
 P4DT2H9M27S

postgres=# select age(date'now');
 PT0S

age函数相当于两个时间戳相减,第二个参数不填默认是当天0点

current_xxx,localxxx

postgres=# select current_date,current_timestamp,current_time,localtime,localtimestamp;
 2015-03-01 | 2015-03-01 00:49:31.359069+08 | 00:49:31.359069+08 | 00:49:31.359069 | 2015-03-01 00:49:31.359069

current_timestamp, current_time是带时区的

isfinite

postgres=# select isfinite(date'infinity'),isfinite(date'2013-1-1');
 f        | t

postgres=# select isfinite(timestamp'-infinity'),isfinite(timestamp'now'),isfinite(interval'1 hour');
 f        | t        | t

判断是否无穷大无穷小

justify_xxx

postgres=# select justify_days(interval '90 day'),justify_hours(interval '98 hour'),justify_interval(interval '31 day - 6 hour');
 P3M          | P4DT2H        | P1MT18H

调整计算,justify_days计算30天为一个月, justify_hours计算24小时一天, justify_interval为前两者结合

extract,date_part

postgres=# select extract(day from date'2015-2-28'),extract(minute from timestamp'2015-2-28 23:59:58');
        28 |        59

postgres=# select extract(month from interval '2 year 3 month 50 day');
         3

postgres=# select date_part('day',date'2015-2-28'),date_part('minute',timestamp'2015-2-28 23:59:58');
        28 |        59

postgres=# select date_part('month',interval '2 year 3 month 50 day');
         3

提取interval不自动计算,只是单纯提取,date_part 只是写法不一样

date_trunc

postgres=# select date_trunc('month',date'2015-2-28'),date_trunc('minute',timestamp'2015-2-28 23:59:58');
 2015-02-01 00:00:00+08 | 2015-02-28 23:59:00

postgres=# select date_trunc('month',interval '2 year 3 month 50 day');
 P2Y3M

截断到指定位置

跟事务相关的时间戳

postgres=# begin;
BEGIN
postgres=# select now(),transaction_timestamp(),current_timestamp,statement_timestamp();
 2015-03-01 01:18:37.661025+08 | 2015-03-01 01:18:37.661025+08 | 2015-03-01 01:18:37.661025+08 | 2015-03-01 01:18:40.400022+08

postgres=# select now(),transaction_timestamp(),current_timestamp,statement_timestamp();
 2015-03-01 01:18:37.661025+08 | 2015-03-01 01:18:37.661025+08 | 2015-03-01 01:18:37.661025+08 | 2015-03-01 01:19:02.023012+08

postgres=# end;
COMMIT

now(), transaction_timestamp(), current_timestamp 是事务开始的时间,在同一个事务中查询的结果是一样的. 
statement_timestamp() 是语句执行的时间,每次执行都不一样

clock_timestamp, timeofday

postgres=# select clock_timestamp(),clock_timestamp(),clock_timestamp();
 2015-03-01 01:27:17.056253+08 | 2015-03-01 01:27:17.056257+08 | 2015-03-01 01:27:17.056259+08

跟cpu时钟有关,所以即使在同一句语句中,也有差异 
timeofday跟clock_timestamp性质一样,但是返回的值类型是text

make_xxx

postgres=# select make_date(2013,12,11),make_time(12,12,23.33),make_timestamp(2015,2,28,17,27,30.666),make_timestamptz(2015,2,28,17,27,30.666);
 2013-12-11 | 12:12:23.33 | 2015-02-28 17:27:30.666 | 2015-02-28 17:27:30.666+08

postgres=# select make_interval(1,2,3,4,5,6,7),make_interval(hours:=5,secs:=7);
 P1Y2M25DT5H6M7S | PT5H7S

overlaps

postgres=# select (date'2010-1-1',date'2011-1-1') overlaps (date'2011-1-1',date'2012-1-1');
 f

postgres=# select (date'2010-1-1',timestamp'2011-1-1 00:00:01') overlaps (date'2011-1-1',date'2012-1-1');
 t

postgres=# select (date'2011-1-1',interval '1 month') overlaps (date'2011-2-1',interval '1 hour');
 f

判断时间区间是否重叠,时间区间是闭开区间

延迟执行sleep

pg_sleep

postgres=# select clock_timestamp(),pg_sleep(2),clock_timestamp();
 2015-03-01 14:13:34.100984+08 |          | 2015-03-01 14:13:36.103236+08

pg_sleep_for

postgres=# select clock_timestamp(),pg_sleep_for('2 second'),clock_timestamp();
 2015-03-01 14:14:27.4425+08 |              | 2015-03-01 14:14:29.445029+08

pg_sleep_until

postgres=# select clock_timestamp(),pg_sleep_until(timestamp 'now' + '2 second'),clock_timestamp();
 2015-03-01 14:15:58.029409+08 |                | 2015-03-01 14:16:00.029504+08

postgres=# select pg_sleep_until(timestamp 'tomorrow 12:00:00');
...... --时间太长取消了

注意不要在事务中持有锁时间太长,影响其他事务

//END

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
移动开发 关系型数据库 PostgreSQL
PostgreSQL 条件判断函数
PostgreSQL 条件判断函数
874 1
|
1月前
|
关系型数据库 Serverless 定位技术
PostgreSQL GIS函数判断两条线有交点的函数是什么?
PostgreSQL GIS函数判断两条线有交点的函数是什么?
143 60
|
2月前
|
SQL 自然语言处理 关系型数据库
在 PostgreSQL 中使用 `REPLACE` 函数
【8月更文挑战第8天】
680 9
在 PostgreSQL 中使用 `REPLACE` 函数
|
1月前
|
SQL 关系型数据库 C语言
PostgreSQL SQL扩展 ---- C语言函数(三)
可以用C(或者与C兼容,比如C++)语言编写用户自定义函数(User-defined functions)。这些函数被编译到动态可加载目标文件(也称为共享库)中并被守护进程加载到服务中。“C语言函数”与“内部函数”的区别就在于动态加载这个特性,二者的实际编码约定本质上是相同的(因此,标准的内部函数库为用户自定义C语言函数提供了丰富的示例代码)
|
2月前
|
关系型数据库 PostgreSQL
PostgreSQL的null值函数
【8月更文挑战第20天】PostgreSQL的null值函数
61 3
|
2月前
|
SQL 关系型数据库 MySQL
SQL Server、MySQL、PostgreSQL:主流数据库SQL语法异同比较——深入探讨数据类型、分页查询、表创建与数据插入、函数和索引等关键语法差异,为跨数据库开发提供实用指导
【8月更文挑战第31天】SQL Server、MySQL和PostgreSQL是当今最流行的关系型数据库管理系统,均使用SQL作为查询语言,但在语法和功能实现上存在差异。本文将比较它们在数据类型、分页查询、创建和插入数据以及函数和索引等方面的异同,帮助开发者更好地理解和使用这些数据库。尽管它们共用SQL语言,但每个系统都有独特的语法规则,了解这些差异有助于提升开发效率和项目成功率。
228 0
|
3月前
|
关系型数据库 BI 数据处理
|
3月前
|
SQL Oracle 关系型数据库
|
4月前
|
JSON 关系型数据库 数据库
PostgreSQL中json_to_record函数的神秘面纱
`json_to_record`是PostgreSQL中的函数,用于将JSON数据转换为RECORD类型,便于查询和分析。基本用法是传入JSON数据,如`SELECT json_to_record('{"name": "张三", "age": 30}'::json);`。还可结合FUNCTION创建自定义函数,实现复杂功能。在实际应用中,它简化了对JSON格式数据的处理,例如筛选年龄大于30的用户。了解并善用此函数能提升数据库操作效率。本文由木头左分享,期待你的点赞和收藏,下次见!
PostgreSQL中json_to_record函数的神秘面纱
|
4月前
|
SQL 关系型数据库 数据库
PostgreSQL 常用函数分享
PostgreSQL 常用函数分享
37 0