timestamptz、timestamp
SELECT ts AT TIME ZONE 'UTC'
FROM (
VALUES
(timestamptz '2012-03-05 17:00:00+0')
, (timestamptz '2012-03-05 18:00:00+1')
, (timestamp '2012-03-05 18:00:00+1') -- ① loaded footgun!
, (timestamp '2012-03-05 11:00:00' AT TIME ZONE '+6')
, (timestamp '2012-03-05 17:00:00' AT TIME ZONE 'UTC')
, (timestamp '2012-03-05 07:00:00' AT TIME ZONE 'US/Hawaii') -- ②
, (timestamp '2012-03-05 07:00:00' AT TIME ZONE 'HST') -- ②
) t(ts);
SELECT timestamptz '2012-03-05 20:00+03', timestamp '2012-03-05 17:00:00' AT TIME ZONE 'UTC', timestamp '2012-03-05 17:00:00' AT TIME ZONE 'UTC' AT TIME ZONE 'UTC'
查看現在距1970-01-01 00:00:00 UTC 的秒數
select extract(epoch from now());
select timestamp without time zone 'epoch',timestamp without time zone 'epoch' + 3600*interval '1 sec'
select timestamp without time zone 'epoch',timestamp with time zone 'epoch'
把epoch 值轉換回時間戳
select timestamp without time zone 'epoch',timestamp with time zone 'epoch' ,timestamp 'epoch',timestamptz 'epoch'
mysql轉為postgesql
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(UTC_TIMESTAMP()) + tz.gmt_offset, '%a, %d %b %Y, %H:%i:%s') AS local_time FROM "Dict"."TimeZoneDetail" tz JOIN "Dict"."TimeZoneToCountry" z ON tz.zone_id=z.zone_id WHERE tz.time_start <= UNIX_TIMESTAMP(UTC_TIMESTAMP()) AND z.zone_name='America/Los_Angeles' ORDER BY tz.time_start DESC LIMIT 1; SELECT (now() AT TIME ZONE 'UTC' )+interval '1'* tz."GMTOffset" AS local_time,now() as now,now() AT TIME ZONE 'UTC' as utcnow,tz."GMTOffset" as offsett FROM "Dict"."TimeZoneDetail" tz inner JOIN "Dict"."TimeZoneToCountry" z ON tz."ZoneId"=z."ZoneId" WHERE to_timestamp(tz."TimeStart") <= timestamp 'epoch' AND z."ZoneName"='America/Los_Angeles' ORDER BY tz."TimeStart" DESC LIMIT 1;


| 函數 | 返回類型 | 描述 | 示例 |
結果 |
| age(timestamp, timestamp) | interval | 計算兩個時間戳的時間間隔 | select age(timestamp '2001-04-10', timestamp '1957-06-13'); |
43 years 9 mons 27 days |
| age(timestamp) | interval | 計算current_date與入參時間戳的時間間隔 | select age(timestamp '2016-07-07 12:00:00'); |
12:00:00 |
| clock_timestamp() | timestamp with time zone | 當前時間戳(語句執行時變化) | select clock_timestamp(); | 2016-07-08 15:14:04.197732-07 |
| current_date | date | 當前日期 | select current_date; | 2016-07-08 |
| current_time | time with time zone | 當前時間 | select current_time; | 15:15:56.394651-07 |
| current_timestamp | timestamp with time zone | 當前時間戳 | select current_timestamp; | 2016-07-08 15:16:50.485864-07 |
| date_part(text, timestamp) | double precision | 獲取時間戳中的某個子域,其中text可以為year,month,day,hour,minute,second等 | select date_part('year',timestamp'2016-07-08 12:05:06'), date_part('month',timestamp'2016-07-08 12:05:06'), date_part('day',timestamp'2016-07-08 12:05:06'), date_part('minute',timestamp'2016-07-08 12:05:06'), date_part('second',timestamp'2016-07-08 12:05:06'); |
|
| date_part(text, interval) | double precision | 功能同上,只是第二個入參為時間間隔 | select date_part('hour',interval'1 day 13:00:12'); | 13 |
| date_trunc(text, timestamp) | timestamp | 將時間戳截斷成指定的精度, 指定精度后面的子域用0補充 |
select date_trunc('hour', timestamp'2016-07-08 22:30:33'); |
2016-07-08 22:00:00 |
| date_trunc(text, interval) | interval | 功能同上,只是第二個入參為時間間隔 | select date_trunc('hour',interval'1 year 2 mon 3 day 22:30:33'); | 1 year 2 mons 3 days 22:00:00 |
extract(field from timestamp) |
double precision | 功能同date_part(text, timestamp) | select extract(hour from timestamp'2016-07-08 22:30:29'); | 22 |
extract(field from interval) |
double precision | 功能同date_part(text, interval) | select extract(hour from interval'1 day 13:00:12'); | 13 |
| isfinite(date) | boolean | 測試是否為有窮日期 | select isfinite(date'2016-07-08'),isfinite(date'infinity'); | t,f |
| isfinite(timestamp) | boolean | 測試是否為有窮時間戳 | select isfinite(timestamp'2016-07-08'); | t |
| isfinite(interval) | boolean | 測試是否為有窮時間間隔 | select isfinite(interval'1day 23:02:12'); | t |
| justify_days(interval) | interval | 按照每月30天調整時間間隔 | select justify_days(interval'1year 45days 23:00:00'); | 1 year 1 mon 15 days 23:00:00 |
| justify_hours(interval) | interval | 按照每天24小時調整時間間隔 | select justify_hours(interval'1year 45days 343hour'); | 1 year 59 days 07:00:00 |
| justify_interval(interval) | interval | 同時使用justify_days(interval)和justify_hours(interval) | select justify_interval(interval'1year 45days 343hour'); | 1 year 1 mon 29 days 07:00:00 |
| localtime | time | 當日時間 | select localtime; | 15:45:18.892224 |
| localtimestamp | timestamp | 當日日期和時間 | select localtimestamp; | 2016-07-08 15:46:55.181583 |
| make_date(year int, month int, day int) | date | 創建一個日期 | select make_date(2016,7,8); | 2016-07-08 |
| make_interval( years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0) |
interval | 創建一個時間間隔 | select make_interval(1,hours=>3); | 1 year 03:00:00 |
| make_time( hour int, min int, sec double precision) |
time | 創建一個時間 | select make_time(9,21,23); | 09:21:23 |
| make_timestamp( year int, month int, day int, hour int, min int, sec double precision) |
timestamp | 創建一個時間戳 | select make_timestamp(2016,7,8,22,55,23.5); | 2016-07-08 22:55:23.5 |
| make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ]) |
timestamp with time zone | 創建一個帶有時區的時間戳 | select make_timestamptz(2016,7,8,22,55,23.5); | 2016-07-08 22:55:23.5-07 |
| now() | timestamp with time zone | 當前日期和時間 | select now(); | 2016-07-08 15:55:30.873537-07 |
| statement_timestamp() | timestamp with time zone | 同now() | select statement_timestamp(); | 2016-07-08 15:56:07.259956-07 |
| timeofday() | text | 當前日期和時間,包含周幾, 功能與clock_timestamp()類似 |
select timeofday(); | Fri Jul 08 15:57:51.277239 2016 PDT |
| transaction_timestamp() | timestamp with time zone | 事務開始時的時間戳 | select transaction_timestamp(); | 2016-07-08 16:01:25.007153-07 |
| to_timestamp(double precision) | timestamp with time zone | Convert Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp |
select to_timestamp(1284352323); | 2010-09-12 21:32:03-07 |
| pg_sleep(seconds double precision); | 當前會話休眠seconds秒 |
select pg_sleep(5); | ||
| pg_sleep_for(interval) | 當前會話休眠多長時間的間隔 | select pg_sleep_for('5 seconds'); | ||
| pg_sleep_until(timestamp with time zone) | 當前會話休眠至什么時間點 | select pg_sleep_until('2016-07-08 23:59:59'); |
