PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹8. Data Types媆誕嬉誕

8.5. 斖橘/施閘䌂傋

臘8-9滆䴺庖 PostgreSQL 櫇毕䔇 SQL 婺欔橬斖橘启施閘䌂傋㔗認底昄扞䌂傋婪埇傖誕臯䔇淉嘩婘誗9.9婺柟誄㔗

臘8-9. 斖橘/施閘䌂傋

劉庖庻嗘䷺閘柟誄橔嘯唚橔醻唚彖膘䯺
timestamp [ (p) ] [ without time zone ]8 庖誗斖橘启施閘4713 BC5874897 AD1 懆䓐 / 14 嘉
timestamp [ (p) ] with time zone8 庖誗斖橘启施閘婥施寺4713 BC5874897 AD1 懆䓐 / 14 嘉
interval [ (p) ]12 庖誗施閘閘锫-178000000 幘178000000 幘1 懆䓐 / 14 嘉
date4 庖誗埻䫘庯斖橘4713 BC5874897 AD1 崷
time [ (p) ] [ without time zone ]8 庖誗埻䫘庯婔斖喙施閘00:00:0024:00:001 懆䓐 / 14 嘉
time [ (p) ] with time zone12 庖誗埻䫘庯婔斖喙施閘婥施寺00:00:00+145924:00:00-14591 懆䓐 / 14 嘉

㔊濘懟㔏婘 PostgreSQL 7.3 傖嬉埻喍 timestamp 京昽庯 timestamp with time zone 㔗認滇启 SQL 婉噚垹䔇㔗

time, timestamp, interval 毖埖婔婻埇锬䔇來庥唚 p 傖毺滯䓐嘘婺償昄鄘彖䔇嘉昄㔗澇橬滯䇞䔇䚺䩕來庥p 䔇评啘凹 timestampinterval 䌂傋滇傯 0 彄崓亥 6 㔗

㔊濘懟㔏套悩 timestamp 昄唚滇傖埯來庥敞䗹昄(䕞嬉䔇䚺䩕)䔇桹嚟庻嗘䔇闼幽橬昽來庥嚔償庯 6 㔗timestamp 唚滇傖 2000-01-01 剽崩幋嬉潡幋劯䔇䓐昄庻嗘䔇蔯冞䓐䔇來庥滇婺闼底婘 2000-01-01 嬉劯庹幘䔇斖橘垂䯄䔇凹庯闼底誩婔底䔇斖床來庥嚔婋鍉㔗套悩timestamp 傖噆庖誗昘昄庻嗘(婔婻䚡臏施䔇锬釹)闼幽冞䓐䔇來庥儌埇傖婘昄唚䔇噘鄘评啘喙鄘埇傖诙冖㔗婉誺噆嘉昘昄䔇施閘潿评啘䚷償彄 4713 BC 彄 294276 AD 幋閘㔗劯婔婻䚡臏施锬釹幘喿垔 timeinterval 唚滇媺庻潊敞䗹昄誻滇噆庖誗昘昄㔗婘傖敞䗹昄庻嗘䔇施唍锟五施閘閘锫䔇嵂媹崓䔇 interval 昄唚䔇來庥嚔鍉嘯㔗

凹庯 time 䌂傋套悩嘪䫘庖噆庖誗䔇昘昄庻嗘闼幽 p 噕螩䔇评啘滇傯 0 彄 6 套悩嘪䫘䔇滇敞䗹昄庻嗘闼幽認婻评啘滇 0 彄 10 㔗

time with time zone 䌂傋滇 SQL 湺庖垔幬䔇嘖滇垯昘垔幬䔇橬底桹麵嚔凚躘橬閞鵻䔇䫘濘㔗婘崓崔昄愙喕婋date, time, timestamp without time zone, timestamp with time zone 䔇䂇劽儌庫臖脘柊冕婔彺庫䫘驔襕䔇斖橘/施閘䔇垯昘媘脘㔗

abstimereltime 䌂傋滇嘯彖膘䯺䌂傋垄傸赆䫘庯係䂘喙鄘㔗潏傸埉凹嘹嘪䫘認底䌂傋啹婺認底斓䌂傋䔇鄘彖潡噘鄘埇脘嚔婘橻準䔇䬽橸麯潽崌㔗

8.5.1. 斖橘/施閘膷噖

斖橘启施閘䔇膷噖庹幯埇傖滇傂嘘劽䊖䔇湚嚟寙拸 ISO-8601 湚嚟㔕SQL-噚垹湚嚟㔕嚹䂘 POSTGRES 湚嚟㔕噽垄䔇嘵嚟㔗凹庯婔底湚嚟斖橘膷噖麯䔇橽启斖埇脘嚔螷庺誙意啹溴係䂘櫇毕躻垔幬認底庖枕䔇釺废㔗檪 DateStyle 埗昄螆䘞婺 MDY 儌毬䙓"橽-斖-幘"蓼悊螆䘞婺 DMY 儌毬䙓"斖-橽-幘"蓼悊螆䘞婺 YMD 儌毬䙓"幘-橽-斖"蓼悊㔗

PostgreSQL 婘崇䊖斖橘/施閘膷噖婪懫 SQL 湺庖襕挗䔇敘䕕昂㔗埗黙鍇嘘B诙埡噿庯斖橘/施閘膷噖䔇庖䇞彖悊蓇彍启埇臖彆桺橸庖枕寙拸橽傘㔕滘橘庹㔕施寺㔗

臙螄嘟傂嘘斖橘潡蔙施閘䔇桺橸膷噖驔襕䫌剘嚘埙寙啘儌償婔婻桺橸庖严婾婔湙㔗埗蔄誗4.1.2.5诙埡敘崔媇敇㔗SQL 襕挗嘪䫘婋麵䔇臺濘

type [ (p) ] 'value'

埇锬䔇來庥弄滯婺䔇 p 滇婔婻昘昄臘䴺婘䓐嘘婺償昄鄘彖䔇嘉昄潏傸埇傖凹 time, timestamp, interval 䌂傋弄滯來庥㔗噕螩䔇來庥婘婪麵噾䂟臘滯㔗套悩婘婩麟弄滯婺澇橬弄滯來庥䚺䩕滇桺橸唚䔇來庥㔗

8.5.1.1. 斖橘

臘8-10滆䴺庖 date 䌂傋埇脘䔇膷噖桹嚟㔗

臘8-10. 斖橘膷噖

冋床柟誄
January 8, 1999婘傂嘘 DateStyle 膷噖昇嚟婋鄘方溓幬
1999-01-08ISO 8601 湚嚟(傺螞湚嚟)傂嘘桹嚟婋鄘滇 1999 幘 1 橽 8 埙
1/8/1999橬溓幬婘 MDY 婋滇婔橽噆埙婘 DMY 昇嚟婋滇噆橽婔斖
1/18/1999MDY 昇嚟婋滇婔橽剕噆斖噽垄昇嚟婋赆拐䂺
01/02/03MDY 昇嚟婋䔇 2003 幘 1 橽 2 斖DMY 昇嚟婋䔇 2003 幘 2 橽 1 斖YMD 昇嚟婋䔇 2001 幘 2 橽 3 斖
1999-Jan-08傂嘘昇嚟婋鄘滇 1 橽 8 斖
Jan-08-1999傂嘘昇嚟婋鄘滇 1 橽 8 斖
08-Jan-1999傂嘘昇嚟婋鄘滇 1 橽 8 斖
99-Jan-08YMD 昇嚟婋滇 1 橽 8 斖劥彍髍臇
08-Jan-99婔橽噆斖鍴庖婘 YMD 昇嚟婋滇髍臇䔇幋崡
Jan-08-99婔橽噆斖鍴庖婘 YMD 昇嚟婋滇髍臇䔇幋崡
19990108ISO 8601傂嘘昇嚟婋鄘滇 1999 幘 1 橽 8 斖
990108ISO 8601傂嘘昇嚟婋鄘滇 1999 幘 1 橽 8 斖
1999.008幘启幘麯䔇丸庹崷
J2451187凐䘖斖
January 8, 99 BC噸噄嬉 99 幘

8.5.1.2. 施閘

嘷斖施閘䌂傋滇 time [ (p) ] without time zonetime [ (p) ] with time zone 㔗埻喍 time 京昽庯 time without time zone

認底䌂傋䔇橬昽膷噖䫌嘷斖施閘劯麵虘五埇锬䔇施寺䂇潊(埗黙臘8-11臘8-12)㔗套悩婘 time without time zone 䌂傋䔇膷噖婺弄滯庖施寺闼幽垄嚔赆旇旇婄媘䘖㔗劯湙毺垔䔇斖橘幘嚔赆媘䘖鍴麂嘪䫘庖婔婻寙拸崟傴施蓇彍䔇施寺劉懫套 America/New_York婘認䓉愙喕婋媙釂毺垔斖橘傖䇞垔認婻施閘滇湺庖施閘誻滇崟傴施㔗施寺啟䓂儖螄嘘婘 time with time zone 婺㔗

臘8-11. 施閘膷噖

冋床柟誄
04:05:06.789ISO 8601
04:05:06ISO 8601
04:05ISO 8601
040506ISO 8601
04:05 AM婯 04:05 婔湙AM 婉嘌巉昄唚
04:05 PM婯 16:05 婔湙膷噖償施昄媙釂 <= 12
04:05:06.789-8ISO 8601
04:05:06-08:00ISO 8601
04:05-08:00ISO 8601
040506-08ISO 8601
04:05:06 PST䚷喍䔇施寺
2003-04-12 04:05:06 America/New_York䫘劉庖弄滯䔇施寺

臘8-12. 施寺膷噖

冋床柟誄
PST崻广昋湺庖施閘(Pacific Standard Time)
America/New_York垯昘施寺劉䓄
PST8PDTPOSIX 鼯湚䔇施寺
-8:00ISO-8601 婯 PST 䔇啟䓂
-800ISO-8601 婯 PST 䔇啟䓂
-8ISO-8601 婯 PST 䔇啟䓂
zulu喕桹凹 UTC 䔇䚷喍(臏濘埇脘滇䆯喕)
zzulu 䔇䚷喍

埗蔄誗8.5.3 傖诙埡套嘘毺垔施寺䔇敘崔媇敇㔗

8.5.1.3. 施閘潿

施閘潿䌂傋䔇橬昽膷噖䫌婔婻斖橘启施閘䔇誂毖䂇潊劯麵虘五婔婻埇锬䔇施寺婔婻埇锬䔇 ADBC 㔗埥崡AD/BC 埇傖庺䯄婘施寺嬉麵嘖認婻釺废幽麂橔嘿䔇㔗啹溴

1999-01-08 04:05:06

1999-01-08 04:05:06 -8:00

鄘滇橬昽䔇昄唚垄滇噚垹 ISO-8601 䔇㔗埥崡幘櫇毕婋麵認䓉嘪䫘幪濕䔇湚嚟

January 8 04:05:06 1999 PST

SQL 湺庖锔誺"+"潡蔙"-"滇劥庻婘準寺彖 timestamp without time zonetimestamp with time zone 桺橸㔗啹溴湹扞湺庖

TIMESTAMP '2004-10-19 10:23:54'

滇婔婻 timestamp without time zone 蔯

TIMESTAMP '2004-10-19 10:23:54+02'

滇婔婻 timestamp with time zone 㔗PostgreSQL 傯準婉嚔婘䇞垔桺橸䔇䌂傋幋嬉演昖桺橸喙垹啹溴嚔檪婪麵婴婻鄘䩋啔滇 timestamp without time zone 㔗啹溴襕媺臕檪婪麵䔇丸庯婻嘷嘩 timestamp with time zone 䩋写儌襕䂍垄滯䇞䔇䌂傋

TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'

套悩婔婻桺橸噾赆䇞垔滇 timestamp without time zonePostgreSQL 儖旇旇媘䘖傂嘘桺橸婺毺庺䔇施寺㔗啹溴䫘潊䔇斖橘/施閘唚滇傯膷噖唚䔇斖橘/施閘庖枕臉䫘庺準䔇幽婫澇橬儌施寺誕臯脄昘㔗

凹庯 timestamp with time zone 喙鄘庻嗘䔇昄唚攂滇 UTC(噘䊄䂘婔施閘傖嬉幘埆湚悖嘕澂施閘 GMT)㔗套悩婔婻膷噖唚橬滯䇞䔇施寺弄滯闼幽垄儖䫘臖施寺劽锗䔇啟䓂麟蘸扵潊 UTC 㔗套悩婘膷噖庖严婾麯澇橬施寺弄滯闼幽垄儌啺螆滇婘係䂘䔇 timezone 埗昄麯䔇闼婻施寺䇽劯嘪䫘認婻 timezone 施寺蘸扵潊 UTC 㔗

套悩膷庺婔婻 timestamp with time zone 闼幽垄攂滇傯 UTC 蘸扵潊嘷嬉䔇 timezone 施寺幽婫滆䴺婺臖施寺䔇橸婄施閘㔗襕䩋噽垄施寺䔇臖施閘襕幽媞櫹 timezone 襕幽嘪䫘 AT TIME ZONE 悇锹(埗黙誗9.9.3)㔗

timestamp without time zonetimestamp with time zone 幋閘䔇蘸扵锔婩啺螆 timestamp without time zone 昄唚庫臖傖 timezone 橸婄施閘䔇嘵嚟毖埖潡蔙喍庺㔗噽垄䔇施寺嚘䫘埇傖䫘 AT TIME ZONE 䔇桹嚟婺蘸扵弄滯㔗

8.5.1.4. 施閘閘锫

interval 昄唚埇傖䫘婋麵䔇臺濘弄滯

[@] quantity unit [quantity unit...] [direction]

認麯quantity 滇婔婻昄庖(埇脘橬严埙)unitsecond, minute, hour, day, week, month, year, decade, century, millennium 潡蔙認底剘嘉䔇䚷喍潡崉昄direction 埇傖滇 ago 潡蔙婺䷺㔗@ 严埙滇婔婻埇锬䔇婩薪㔗婉劯䔇剘嘉傖埪䕩庫溼䇞䔇严埙鄘滇锊劆婄嵂媹䔇㔗

斖橘㔕償施㔕彖鐘㔕䓐鐘䔇昄麟埇傖婘方滯䇞剘嘉湺螄䔇愙喕婋弄滯㔗懫套'1 12:59:10''1 day 12 hours 59 min 10 sec' 臂昄婔湙㔗

埇锬䔇來庥 p 庫臖傋庯 0 启 6 幋閘幽婫䚺䩕滇膷噖桺橸䔇來庥㔗

婘喙鄘interval 䔇唚毬䙓橽㔕斖㔕䓐庻嗘㔗認湙啔滇敄嘷䔇啹婺懟婻橽䔇崷昄婉劯幽婫懟崷埇脘橬 23 潡 25 償施(潬埪崟傴施脄昘施)㔗啹婺施閘閘锫锔婩滇傯庖严婾婩麟潡 timestamp 幋噞录傺䔇認䓉庻嗘桹濘婘崓崔昄愙喕婋敘媹劽䊖㔗justify_daysjustify_hours 庘昄䫘庯脄昘溵庺䔇斖橘启償施㔗

8.5.1.5. 䬹枪唚

PostgreSQL 婺桹冪蕙蓕櫇毕婘臘8-13麯麵滆䴺䔇庹婻䬹枪膷噖唚㔗唚 infinity-infinity 滇䬹彆婘係䂘喙鄘臘䴺䔇幽婫儖毬䙓劯湙䔇桹嚟滆䴺嘖滇噽垄䔇鄘埻滇严埙䚷喍婘臂埡䔇施唍儖赆蘸扵潊捞锔䔇斖橘/施閘唚㔗䬹彆滇 now 启䕩噿䔇庖严婾婘臂埡䔇施唍儌赆蘸扵潊凹庫䔇昄唚㔗欔橬認底唚婘 SQL 变傴麯嘷嘩捞锔婩麟凹写施鄘驔襕喍婘剘嚘埙麯麵㔗

臘8-13. 䬹枪斖橘/施閘膷噖

膷噖庖严婾锗䫘䌂傋柟誄
epochdate, timestamp1970-01-01 00:00:00+00 (Unix 係䂘镽施)
infinitytimestamp懫傂嘘噽垄施閘潿鄘捔
-infinitytimestamp懫傂嘘噽垄施閘潿鄘斷
nowdate, time, timestamp嘷嬉庋媇䔇嚔哋施閘
todaydate, timestamp傪斖剽崩
tomorrowdate, timestamp滯斖剽崩
yesterdaydate, timestamp滘斖剽崩
allballstime00:00:00.00 UTC

婋彖 SQL 噚垹庘昄幘埇傖䫘庯诙埡凹庫昄扞䌂傋䔇嘷嬉施閘唚CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, LOCALTIME, LOCALTIMESTAMP 㔗劯啕婻毖埖婔婻埇锬䔇來庥弄滯(誗9.9.4)㔗婉誺臙濘懟認底 SQL 庘昄婉滇赆嘷嘩昄扞膷噖庖严婾臖彆䔇㔗

8.5.2. 斖橘/施閘膷庺

斖橘/施閘䌂傋䔇膷庺湚嚟埇傖嘪䫘 SET datestyle 螆潊 ISO 8601(邻螴)㔕SQL(Ingres)㔕嚹䂘䔇 POSTGRES 㔕German 啕䓉鼯湚幋婔㔗SQL 湺庖襕挗嘪䫘 ISO 8601 湚嚟㔗"SQL"膷庺湚嚟䔇劉庖滇寖埾啽䇽㔗臘8-14滆䴺庖懟䓉膷庺鼯湚䔇冋床㔗datetime 䌂傋䔇膷庺嘷䇽埻滇䂍庺䔇冋床麯麵䔇斖橘启施閘鄘彖㔗

臘8-14. 斖橘/施閘膷庺鼯湚

鼯湚柟誄冋床
ISOISO 8601/SQL 湺庖1997-12-17 07:37:16-08
SQL嚹䂘鼯湚12/17/1997 07:37:16.00 PST
POSTGRES寘哋鼯湚Wed Dec 17 07:37:16 1997 PST
German婄寺鼯湚17.12.1997 07:37:16.00 PST

套悩弄滯庖 DMY 釺废闼幽婘 SQL 启 POSTGRES 鼯湚麯斖橘婘橽傘幋嬉庺䯄劥彍橽傘庺䯄婘斖橘幋嬉(埗黙誗8.5.1䩋䩋認婻螆䘞套嘘嘌巉凹膷噖唚䔇蓼麪)㔗臘8-15滆䴺庖婔婻冋床㔗

臘8-15. 斖橘釺废幹愇

datestyle 螆䘞膷噖釺废膷庺湙冋
SQL, DMY//17/12/1997 15:37:16.00 CET
SQL, MDY//12/17/1997 07:37:16.00 PST
Postgres, DMY//Wed 17 Dec 07:37:16 1997 PST

interval 䔇膷庺䩋蕙準償膷噖湚嚟埻滇償 centuryweek 認湙䔇剘嘉赆蘸扵潊幘启斖蔯 ago 赆蘸扵潊劽锗䔇严埙㔗婘 ISO 昇嚟婋膷庺䩋蕙準償

[quantity unit [...]] [days] [hours:minutes:seconds]

䫘潙埇傖䫘 SET datestyle 变傴锬埡斖橘/施閘䔇鼯湚幘埇傖婘陉䘞桺傽 postgresql.conf 婺䔇 DateStyle 埗昄婺螆䘞潡蔙婘橉媇単潡垵潙䆇䔇 PGDATESTYLE 䯇嵄埻麟婺螆䘞㔗潏傸幘埇傖䫘湚嚟寡庘昄 to_char(埗蓕誗9.8)準敘䕕昂婄毓彽施閘/斖橘婄膷庺㔗

8.5.3. 施寺

施寺启施寺幹愇婉備備埖婄䊄庹嘘嘵䪽䔇嘌巉誻埖彄櫪澂喿垔䔇嘌巉㔗彄庖 19 婡亻噘䊄䔇施寺埻冖䘉冞湺庖寡庖底嘖滇誻滇滷庯镺埖锟懟䔇媞櫹鄘彖滇啹婺崟施彽蓇彍㔗PostgreSQL 䕞嬉櫇毕 1902 幘彄 2038 幘幋閘䔇崟施彽媇敇(凹庫庯嚹䂘 Unix 係䂘施閘䔇垯昘虘庥)㔗套悩施閘轙誺認婻评啘闼幽啺螆施閘滇欔锬施寺䔇"湺庖施閘"婉䞇垄傸芘婘巻婻幘傘麯麵㔗

PostgreSQL 婘噩傋庫䫘婺儘埇脘婯 SQL 䔇垔幬䕩噚垹㔗嘖 SQL 湺庖婘斖橘/施閘䌂傋启媘脘婪橬婔底喺攻䔇晙晖㔗婴婻滆蔯滷蓕䔇閞鵻滇

婺庖噋橉認底啄锆潏傸傺螞婘嘪䫘施寺䔇施唍嘪䫘闼底劯施寙劆斖橘启施閘䔇斖橘/施閘䌂傋㔗潏傸傺螞婉襕嘪䫘 time with time zone 䌂傋(儘䞇 PostgreSQL 庺庯劽䊖庫䫘傖埪婺庖婯噽垄 RDBMS 噚垹䔇蔄荏櫇毕認婻䌂傋)㔗PostgreSQL 啺螆嘹䫘庯傂嘘䌂傋䔇橸婄施寺鄘埻寙劆斖橘潡施閘(蔯婉寙劆施寺)㔗

婘係䂘喙鄘欔橬斖橘启施閘鄘䫘噘䊄䂘婔施閘 UTC 湚嚟庻嗘施閘婘埏䂍垵潙嬉䆇嬉䫌昄扞康橉媇単湹扞 timezone 陉䘞埗昄弄滯䔇施寺蘸扵潊橸婄施閘㔗

PostgreSQL 噕螩嘹䫘婬䓉桹濘毺垔施寺

垯昘䔇施寺劉婯施寺䚷喍婘䊖螺婯垂處幋閘庻婘噞嚗施寺䚷喍攂滇傼臘婔婻䕩凹庯 UTC 䔇啺垔啟䓂麟䇽蔯崓崔昄垯昘䔇施寺劉锊劆五婔婻橸婄崟傴施蓇彍啹溴儌橬埇脘橬婴婻䕩凹庯 UTC 䔇婉劯啟䓂麟㔗

攂嘷蔯蘔PostgreSQL 8.2 䬽橸傖劯施寺劉婘欔橬愙喕婋鄘滇崓償喍方噿䔇㔗蔯幋嬉䔇䬽橸婘昊底愙喕婋滇崓償喍星懘䔇㔗

方螺滇垯昘䔇施寺劉誻滇施寺䚷喍鄘婉滇䇸誂毖誕橉媇単䔇垄傸鄘滇傯垬輙䕞嘘婋䔇 .../share/timezone/.../share/timezonesets/ 陉䘞桺傽婺诙埡䔇(埗蓕䆹B.3)

埇傖婘 postgresql.conf 桺傽麯螆䘞 timezone 陉䘞埗昄潡蔙䫘傂嘘噽垄婘䆹17柟誄䔇湺庖桹濘㔗鍴溴幋崡誻橬喘庹䓉䬹枪桹濘埇傖螆䘞垄

8.5.4. 喙鄘

PostgreSQL 嘪䫘凐䘖寖濘螇䞖欔橬斖橘/施閘啺螆婔幘䔇阪庥滇 365.2425 崷㔗認婻桹濘埇傖冽來䇞婄鵇螇/螇䞖傯 4713 BC(噸噄嬉 4713 幘)彄冽幙䔇橻準䔇傂懟婔崷䔇斖橘㔗

19 婡亻傖嬉䔇斖橘嚹䂘(寖濘)埻滇凹婔底轼叿臂䬷橬懟幬蔯婘潏傸認麯喘償澇橬噙彖䔇䊖䫌檪垄傸䚡乕噖斖橘/施閘毓彽単麯麵寂㔗


劯锔饡釕嬉誕
庯誕彽昄扞䌂傋婪婔亓婄儫䌂傋