獲取系統(tǒng)時(shí)間函數(shù)
select now(); --2013-11-28 16:20:25.259715+08
select current_timest --2013-11-28 16:20:38.815466+08
select current_date; --2013-11-28
select current_time; --16:21:08.981171+08
時(shí)間的計(jì)算
--使用interval
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小時(shí)后
-- interval可以不寫,其值可以是
-- Abbreviation Meaning
-- Y Years
-- M Months (in the date part)
-- W Weeks
-- D Days
-- H Hours
-- M Minutes (in the time part)
時(shí)間的截取
--使用extract extract(interval,timestamp);
select extract(year from now()); --2013
select extract(mon from now()); --5月份
時(shí)間的轉(zhuǎn)換
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時(shí)間戳的轉(zhuǎn)換
SELECT TIMESTAMP 'epoch' + 1341174767 * INTERVAL '1 second';
--2012-07-01 20:32:47
實(shí)例
1.當(dāng)前時(shí)間/日期/時(shí)間戳
獲取當(dāng)前時(shí)間的方式有很多種,在這之前我們需要知道以下兩種類型的區(qū)別:
總是返回當(dāng)前的值 (clock_timestamp())
總是返回當(dāng)前值,但在事務(wù)中它返回的是事務(wù)開始的時(shí)間(now())
讓我們看下面這個(gè)例子
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
你會(huì)發(fā)現(xiàn),語句執(zhí)行時(shí)候clock_timestamp()的返回值每次都發(fā)生了改變,但是now()總是返回相同的值。當(dāng)你需要考慮時(shí)區(qū)時(shí),你應(yīng)該特別注意這兩個(gè)函數(shù)差異。
2.時(shí)間區(qū)間:比如3天前
使用interval操作符你可以輕松的構(gòu)建一個(gè)時(shí)間區(qū)間,例如
interval '1 day'
interval '5 days'
interval '5 days' + interval '3 hours'
interval '5 days 3 hours'
你可以看到,我們可以用interval操作符來簡(jiǎn)單的進(jìn)行數(shù)學(xué)運(yùn)算,這特別適合于構(gòu)建例如3天前這樣的時(shí)間區(qū)間,比如:
postgres=# SELECT now() - interval '3 days';
?column?
-------------------------------
2013-08-23 12:23:40.069717+02
3.獲取星期幾
有些時(shí)候?qū)τ谝粋€(gè)給定的時(shí)間,你僅僅只想知道的是這天是星期幾或者是它屬于那個(gè)世紀(jì)的更或者你只想知道它是一年中的第幾天。PostgreSQL中的extract()函數(shù)提供了這種功能。
如下例子是在8月26日 星期一進(jìn)行測(cè)試的。
postgres=# SELECT extract(DAY FROM now());
date_part
-----------
26
postgres=# SELECT extract(DOW FROM now());
date_part
-----------
1
4.時(shí)區(qū)轉(zhuǎn)換
有些時(shí)候,時(shí)區(qū)轉(zhuǎn)換對(duì)于特定時(shí)間在不同時(shí)區(qū)顯示特別有用。AT TIME ZONE提供了這種功能,它是如何做到的?我們將在一個(gè)事務(wù)中進(jìn)行演示,因?yàn)橥皇聞?wù)中now()函數(shù)總是返回相同的值,從而我們可以很容易看到同一時(shí)間在不同時(shí)區(qū)顯示的差別。
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 實(shí)現(xiàn)查詢某時(shí)間區(qū)間的所有日期案例
- PostgreSQL TIMESTAMP類型 時(shí)間戳操作
- PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng)
- PostgreSQL更新表時(shí)時(shí)間戳不會(huì)自動(dòng)更新的解決方法
- 用一整天的時(shí)間安裝postgreSQL NTFS權(quán)限
- postgresql 中的時(shí)間處理小技巧(推薦)