好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PostgreSQL中常用的时间日期脚本使用教程

获取系统 时间 函数

?

1

2

3

4

select now();        --2013-11-28 16:20:25.259715+08

select current_timestamp ;  --2013-11-28 16:20:38.815466+08

select current_date ;     --2013-11-28

select current_time ;     --16:21:08.981171+08

时间的计算
--使用interval

?

1

2

3

select now()+interval '2 day' ; --2013-11-30 16:21:47.610118+08 2天后

select now()-interval '2 day' ; --2013-11-26 16:22:03.390593+08 2天前

select now()+interval '2 hour' ; --2013-11-28 18:22:14.578733+08 2小时后

-- interval可以不写,其值可以是

?

1

2

3

4

5

6

7

-- Abbreviation   Meaning

-- Y           Years

-- M           Months (in the date part)

-- W          Weeks

-- D           Days

-- H           Hours

-- M        Minutes (in the time part)

时间的截取 
--使用extract extract(interval,timestamp);

?

1

2

select extract( year from now());    --2013

select extract(mon from now());     --5月份

时间的转换

?

1

2

3

4

select timestamp '2012-05-12 18:54:54' ;         --2012-05-12 18:54:54

select date '2012-05-12 18:54:54' ;            --2012-05-12

select time '2012-05-12 18:54:54' ;           --18:54:54

select TIMESTAMP WITH TIME ZONE '2012-05-12 18:54:54'   --2012-05-12 18:54:54+08

与unix时间戳的转换 

?

1

2

SELECT TIMESTAMP 'epoch' + 1341174767 * INTERVAL '1 second' ;

--2012-07-01 20:32:47

实例
1.当前时间/ 日期 /时间戳
获取当前时间的方式有很多种,在这之前我们需要知道以下两种类型的区别:
总是返回当前的值 (clock_timestamp())
总是返回当前值,但在事务中它返回的是事务开始的时间(now())
让我们看下面这个例子

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

postgres=# BEGIN ;

postgres=# SELECT now();

        now

-------------------------------

  2013-08-26 12:17:43.182331+02

 

postgres=# SELECT now();

        now

-------------------------------

  2013-08-26 12:17:43.182331+02

 

postgres=# SELECT clock_timestamp();

     clock_timestamp

-------------------------------

  2013-08-26 12:17:50.698413+02

 

postgres=# SELECT clock_timestamp();

     clock_timestamp

-------------------------------

  2013-08-26 12:17:51.123905+02

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

2.时间区间:比如3天前
使用interval操作符你可以轻松的构建一个时间区间,例如

?

1

2

3

4

interval '1 day'

interval '5 days'

interval '5 days' + interval '3 hours'

interval '5 days 3 hours'

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

?

1

2

3

4

postgres=# SELECT now() - interval '3 days' ;

       ? column ?

-------------------------------

  2013-08-23 12:23:40.069717+02

3.获取星期几
有些时候对于一个给定的时间,你仅仅只想知道的是这天是星期几或者是它属于那个世纪的更或者你只想知道它是一年中的第几天。 PostgreSQL 中的extract()函数提供了这种功能。
如下例子是在8月26日 星期一进行测试的。

?

1

2

3

4

5

6

7

8

9

postgres=# SELECT extract( DAY FROM now());

  date_part

-----------

     26

 

postgres=# SELECT extract(DOW FROM now());

  date_part

-----------

      1  

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

postgres=# BEGIN ;

BEGIN

postgres=# SELECT now();

        now

-------------------------------

  2013-08-26 12:39:39.122218+02

 

postgres=# SELECT now() AT TIME ZONE 'GMT' ;

      timezone

----------------------------

  2013-08-26 10:39:39.122218

 

postgres=# SELECT now() AT TIME ZONE 'GMT+1' ;

      timezone

----------------------------

  2013-08-26 09:39:39.122218

 

postgres=# SELECT now() AT TIME ZONE 'PST' ;

      timezone

----------------------------

  2013-08-26 02:39:39.122218

 

查看更多关于PostgreSQL中常用的时间日期脚本使用教程的详细内容...

  阅读:46次