make_timestamptz() 是一个系统函数,用于从提供的年、月、日、小时、分钟、秒以及可选的时区组装一个 timestamptz 值。
make_timestamptz() 在 PostgreSQL 9.4 中被添加。
用法
make_timestamptz (yearint,monthint,dayint,hourint,minint,secdouble precision[,timezonetext] )
→ timestamp with time zone
所有参数都必须提供。
如果提供的任何参数无效,则会引发错误。
变更历史
- PostgreSQL 9.4
- 添加(提交 84df54b2)
示例
make_timestamptz() 的基本用法示例
postgres=# SELECT make_timestamptz(2021,8,29,23,10,56.32);
make_timestamptz
---------------------------
2021-08-29 23:10:56.32+01
(1 row)
提供一个明确的时区
postgres=# SELECT make_timestamptz(2020,12,1,4,55,56.32, 'JST');
make_timestamptz
---------------------------
2020-11-30 20:55:56.32+01
(1 row)
提供一个或多个无效的日期参数
postgres=# SELECT make_timestamptz(2021,13,29,23,10,11.22); ERROR: date field value out of range: 2021-13-29
提供一个或多个无效的时间参数
postgres=# SELECT make_timestamptz(2021,8,29,25,10,99.32); ERROR: time field value out of range: 25:10:99.32
提供一个无效的时区参数
postgres=# SELECT make_timestamptz(2001,2,12,14,13,22.36, 'Moon/Clavius'); ERROR: time zone "Moon/Clavius" not recognized
参考资料
- PostgreSQL 文档: 日期/时间函数
