使用PostgreSQL数据库日期类型的4个提示

数据库 其他数据库 数据库运维 PostgreSQL
PostgreSQL你可以通过简单调用PostgreSQL内置函数来实现数据库日期类型功能。

当我们这些使用Rails的人看到例如5.weeks.from_nowor3.days.ago + 2.hours时并不会感到惊讶。同样,PostgreSQL也可以做到,你可以通过简单调用PostgreSQL内置函数来实现相同的功能。

当前时间/日期/时间戳

获取当前时间的方式有很多种,在这之前我们需要知道以下两种类型的区别:

  1. 总是返回当前的值 (clock_timestamp())
  2. 总是返回当前值,但在事务中它返回的是事务开始的时间(now())

让我们看下面这个例子

  1. postgres=# BEGIN;  
  2. postgres=# SELECT now();  
  3.               now  
  4. -------------------------------  
  5.  2013-08-26 12:17:43.182331+02  
  6.  
  7. postgres=# SELECT now();  
  8.               now  
  9. -------------------------------  
  10.  2013-08-26 12:17:43.182331+02  
  11.  
  12. postgres=# SELECT clock_timestamp();  
  13.         clock_timestamp  
  14. -------------------------------  
  15.  2013-08-26 12:17:50.698413+02  
  16.  
  17. postgres=# SELECT clock_timestamp();  
  18.         clock_timestamp  
  19. -------------------------------  
  20.  2013-08-26 12:17:51.123905+02  

你会发现,语句执行时候clock_timestamp()的返回值每次都发生了改变,但是now()总是返回相同的值。当你需要考虑时区时,你应该特别注意这两个函数差异。

时间区间:比如3天前

使用interval操作符你可以轻松的构建一个时间区间,例如

  • interval '1 day'
  • interval '5 days'
  • interval '5 days' + interval '3 hours'
  • interval '5 days 3 hours'

你可以看到,我们可以用interval操作符来简单的进行数学运算,这特别适合于构建例如3天前这样的时间区间,比如:

  1. postgres=# SELECT now() - interval '3 days';  
  2.            ?column?  
  3. -------------------------------  
  4.  2013-08-23 12:23:40.069717+02  

获取星期几

有些时候对于一个给定的时间,你仅仅只想知道的是这天是星期几或者是它属于那个世纪的更或者你只想知道它是一年中的第几天。PostgreSQL中的extract()函数提供了这种功能。

如下例子是在8月26日 星期一进行测试的。

  1. postgres=# SELECT extract(DAY FROM now());  
  2.  date_part  
  3. -----------  
  4.         26  
  5.  
  6. postgres=# SELECT extract(DOW FROM now());  
  7.  date_part  
  8. -----------  
  9.          1  

 

extract()还有其他更强大的功能,详情请参阅官方文档,在这里只列举了一小部分:

  • day
  • century
  • dow(day of week)
  • doy(day of year)
  • minute
  • month
  • year

时区转换

有些时候,时区转换对于特定时间在不同时区显示特别有用。AT TIME ZONE提供了这种功能,它是如何做到的?我们将在一个事务中进行演示,因为同一事务中now()函数总是返回相同的值,从而我们可以很容易看到同一时间在不同时区显示的差别。

  1. postgres=# BEGIN;  
  2. BEGIN 
  3. postgres=# SELECT now();  
  4.               now  
  5. -------------------------------  
  6.  2013-08-26 12:39:39.122218+02  
  7.  
  8. postgres=# SELECT now() AT TIME ZONE 'GMT';  
  9.           timezone  
  10. ----------------------------  
  11.  2013-08-26 10:39:39.122218  
  12.  
  13. postgres=# SELECT now() AT TIME ZONE 'GMT+1';  
  14.           timezone  
  15. ----------------------------  
  16.  2013-08-26 09:39:39.122218  
  17.  
  18. postgres=# SELECT now() AT TIME ZONE 'PST';  
  19.           timezone  
  20. ----------------------------  
  21.  2013-08-26 02:39:39.122218  

英文原文:4 Tips for Working with Dates in PostgreSQL

译文链接:http://www.oschina.net/translate/4-tips-for-working-with-dates-in-postgresql

责任编辑:林师授 来源: OSCHINA 编译
相关推荐

2022-10-12 13:33:25

PostgreSQL数据库

2019-11-20 09:08:46

PostgreSQL数据库

2010-05-26 10:15:11

MySQL数据库

2011-08-25 09:56:05

PostgreSQLpg_ident.co

2024-03-04 10:48:15

PostgreSQL数据库

2022-06-26 07:18:17

数据库NodePostgreSQL

2011-03-25 13:08:19

PostgreSQL数

2011-08-24 13:37:33

PostgreSQLpg_hba.conf

2014-03-03 10:10:37

PostgreSQL数组

2021-09-28 09:25:05

NoSQL数据库列式数据库

2010-05-13 15:30:47

2014-01-05 17:08:09

PostgreSQL数据类型

2017-10-13 15:06:18

数据库PostgreSQL特性

2010-04-29 15:22:25

Oracle数据库

2024-04-03 09:25:53

数据库OraclePostgreSQL

2023-06-28 11:14:18

2019-02-11 09:48:02

2023-03-29 08:00:00

PostgreSQL数据库

2020-02-17 09:06:52

安全数据库SQL

2010-05-13 11:45:56

MySQL数据库
点赞
收藏

51CTO技术栈公众号