Date/Time Values

PostgreSQL supports four basic temporal data types plus a couple of extensions that deal with time zone issues.

The DATE type is used to store dates. A DATE value stores a century, year, month, and day.

The TIME data type is used to store a time-of-day value. A TIME value stores hours, minutes, seconds, and microseconds. It is important to note that a TIME value does not contain a time zone—if you want to include a time zone, you should use the type TIME WITH TIME ZONE. TIMETZ is a synonym for TIME WITH TIME ZONE.

The TIMESTAMP data type combines a DATE and a TIME, storing a century, year, month, day, hour, minutes, seconds, and microseconds. Unlike the TIME data type, a TIMESTAMP does include a time zone. If, for some reason, you want a date/time value that does not include a time zone, you can use the type TIMESTAMP WITHOUT TIME ZONE.

The last temporal data type is the INTERVAL. An INTERVAL represents a span of time. I find that the easiest way to think about INTERVAL values is to remember that an INTERVAL stores some (possibly large) number of seconds, but you can group the seconds into larger units for convenience. For example, the CAST( '1 week' AS INTERVAL ) is equal to CAST( '604800 seconds' AS INTERVAL ), which is equal to CAST( '7 days' AS INTERVAL ) —you can use whichever format you find easiest to work with.

Table 2.9 lists the size and range for each of the temporal data types.

Table 2.9. Temporal Data Type Sizes and Ranges
Data TypeSize (in bytes)Range
DATE4-01-MAR-4801 BC 31-DEC-32767
TIME [ WITHOUT TIME ZONE ]4-00:00:00.00 23:59:59.99
TIME WITH TIME ZONE12-00:00:00.00+12 23:59:59.00-12
TIMESTAMP [ WITH TIME ZONE ]8-24-NOV-4714 BC 31-DEC- 5874897
TIMESTAMP WITHOUT TIME ZONE8-24-NOV-4714 BC 31-DEC- 5874897
INTERVAL12--178000000 YEARS +178000000 YEARS

The data types that contain a time value (TIME, TIME WITH TIME ZONE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, and INTERVAL) have microsecond precision. The DATE data type has a precision of one day.

Syntax for Literal Values

I covered date literal syntax pretty thoroughly in Chapter 1; see the section titled “Working with Date Values.”

You may recall from Chapter 1 that date values can be entered in many formats, and you have to tell PostgreSQL how to interpret ambiguous values. Fortunately, the syntax for TIME, TIMESTAMP, and INTERVAL values is much more straightforward.

A TIME value stores hours, minutes, seconds, and microseconds. The syntax for a TIME literal is

hh:mm[:ss[.µ ]][AM|PM]µ

where hh specifies the hour, mm specifies the number of minutes past the hour, ss specifies the number of seconds, and µ specifies the number of microseconds. If you include an AM or PM indicator, the hh component must be less than or equal to 12; otherwise, the hour can range from 0 to 24.

Entering a TIME WITH TIME ZONE value is a bit more complex. A TIME WITH TIME ZONE value is a TIME value, plus a time zone. The time zone component can be specified in two ways. First, you can include an offset (in minutes and hours) from UTC:

hh:mm[:ss[.µ ]][AM|PM][{+|-}HH[:MM]]

where HH is the number of hours and MM is the number of minutes distant from UTC. Negative values are considered to be west of the prime meridian, and positive values are east of the prime meridian.

You can also use a standard time zone abbreviation (such as UTC, PDT, or EST) to specify the time zone:

hh:mm[:ss[.µ ]][AM|PM][ZZZ]

Table 2.10 shows all the time zone abbreviations accepted by PostgreSQL version 8.0.

Table 2.10. PostgreSQL Time Zone Names
NamesOffsetDescription
FJST-13:00Fiji Summer Time
FJT-12:00Fiji Time
IDLW-12:00International Date Line, West
BST-11:00Bering Summer Time
NT-11:00Nome Time
NUT-11:00Niue Time
AHST-10:00Alaska-Hawaii Std Time
CAT-10:00Central Alaska Time
HST-10:00Hawaii Std Time
THAT-10:00Tahiti Time
TKT-10:00Tokelau Time
MART-09:30Marquesas Time
AKST-09:00Alaska Standard Time
GAMT-09:00Gambier Time
HDT-09:00Hawaii/Alaska Daylight Time
YST-09:00Yukon Standard Time
AKDT-08:00Alaska Daylight Time
PST-08:00Pacific Standard Time
YDT-08:00Yukon Daylight Time
MST-07:00Mountain Standard Time
PDT-07:00Pacific Daylight Time
CST-06:00Central Standard Time
EAST-06:00Easter Island Time
GALT-06:00Galapagos Time
MDT-06:00Mountain Daylight Time
ZP6-06:00UTC +6 hours
ACT-05:00Atlantic/Porto Acre Time
CDT-05:00Central Daylight Time
COT-05:00Columbia Time
EASST-05:00Easter Island Summer Time
ECT-05:00Ecuador Time
EST-05:00Eastern Standard Time
PET-05:00Peru Time
ZP5-05:00UTC +5 hours
ACST-04:00Atlantic/Porto Acre Summer Time
AMT-04:00Amazon Time (Porto Velho)
AST-04:00Atlantic Std Time (Canada)
BOT-04:00Bolivia Time
CLT-04:00Chile Time
ECT-04:00Eastern Caribbean Time
EDT-04:00Eastern Daylight Time
GYT-04:00Guyana Time
PYT-04:00Paraguay Time
VET-04:00Venezuela Time
ZP4-04:00UTC +4 hours
NFT-03:30Newfoundland Standard Time
NST-03:30Newfoundland Standard Time
ADT-03:00Atlantic Daylight Time
AMST-03:00Amazon Summer Time (Porto Velho)
ART-03:00Argentina Time
AWT-03:00Brazil Time
BRT-03:00Brasilia Time
BST-03:00Brazil Standard Time
CLST-03:00Chile Summer Time
FKST-03:00Falkland Islands Summer Time
GFT-03:00French Guiana Time
PYST-03:00Paraguay Summer Time
UYT-03:00Uruguay Time
WGT-03:00West Greenland Time
NDT-02:30Newfoundland Daylight Time
BRST-02:00Brasilia Summer Time
FKT-02:00Falkland Islands Time
FNT-02:00Fernando de Noronha Time
PMDT-02:00Pierre & Miquelon Daylight Time
UYST-02:00Uruguay Summer Time
WGST-02:00West Greenland Summer Time
AZOT-01:00Azores Time
EGT-01:00East Greenland Time
FNST-01:00Fernando de Noronha Summer Time
SET-01:00Seychelles Time
WAT-01:00West Africa Time
AZOST+00:00Azores Summer Time
EGST+00:00East Greenland Summer Time
GMT+00:00Greenwich Mean Time
UTC+00:00Universal Coordinated Time
UT+00:00Universal Time
WET+00:00Western Europe
ZULU+00:00Universal Time
Z+00:00ISO-8601 Universal Time
BST+01:00British Summer Time
CET+01:00Central European Time
DNT+01:00Dansk Normal Time
FST+01:00French Summer Time
MET+01:00Middle Europe Time
MEWT+01:00Middle Europe Winter Time
MEZ+01:00Middle Europe Zone
NOR+01:00Norway Standard Time
SWT+01:00Swedish Winter Time
WEST+01:00Western Europe Summer Time
WETDST+01:00Western Europe Daylight Savings Time
BDST+02:00British Double Summer Time
CEST+02:00Central European Dayl.Time
CETDST+02:00Central European Dayl.Time
EET+02:00Eastern Europe, USSR Zone 1
FWT+02:00French Winter Time
IST+02:00Israel Time
MEST+02:00Middle Europe Summer Time
METDST+02:00Middle Europe Daylight Time
SST+02:00Swedish Summer Time
BT+03:00Baghdad Time
EAT+03:00East Africa Time
EAT+03:00Indian Antananarivo Time
EEST+03:00Eastern Europe Summer Time
EETDST+03:00Eastern Europe Daylight Time
HMT+03:00Hellas Mediterranean Time
MSK+03:00Moscow Time
IRT+03:30Iran Time
IT+03:30Iran Time
AMT+04:00Armenia Time (Yerevan)
AZT+04:00Azerbaijan Time
EAST+04:00Indian Antananarivo Savings Time
GET+04:00Georgia Time
MSD+04:00Moscow Summer Time
MUT+04:00Mauritius Island Time
RET+04:00Reunion Island Time
SCT+04:00Mahe Island Time
AFT+04:30Kabul Time
AMST+05:00Armenia Summer Time (Yerevan)
AZST+05:00Azerbaijan Summer Time
GEST+05:00Georgia Summer Time
IOT+05:00Indian Chagos Time
KGT+05:00Kyrgyzstan Time
MVT+05:00Maldives Island Time
PKT+05:00Pakistan Time
TFT+05:00Kerguelen Time
TJT+05:00Tajikistan Time
TMT+05:00Turkmenistan Time
UZT+05:00Uzbekistan Time
YEKT+05:00Yekaterinburg Time
NPT+05:45Nepal Standard Time
ALMT+06:00Almaty Time
BDT+06:00Dacca Time
BTT+06:00Bhutan Time
DUSST+06:00Dushanbe Summer Time
KGST+06:00Kyrgyzstan Summer Time
LKT+06:00Lanka Time
MAWT+06:00Mawson, Antarctica
NOVT+06:00Novosibirsk Standard Time
OMST+06:00Omsk Time
UZST+06:00Uzbekistan Summer Time
YEKST+06:00Yekaterinburg Summer Time
CCT+06:30Indian Cocos (Island) Time
MMT+06:30Myanmar Time
ALMST+07:00Almaty Savings Time
CVT+07:00Christmas Island Time (Indian Ocean)
CXT+07:00Christmas Island Time (Indian Ocean)
DAVT+07:00Davis Time (Antarctica)
ICT+07:00Indochina Time
JAVT+07:00Java Time
KRAST+07:00Krasnoyarsk Summer Time
NOVST+07:00Novosibirsk Summer Time
OMSST+07:00Omsk Summer Time
WAST+07:00West Australian Std Time
JT+07:30Java Time
AWST+08:00Western Australia
BNT+08:00Brunei Darussalam Time
BORT+08:00Borneo Time (Indonesia)
CCT+08:00China Coast Time
HKT+08:00Hong Kong Time
IRKT+08:00Irkutsk Time
KRAT+08:00Krasnoyarsk Standard Time
MYT+08:00Malaysia Time
PHT+08:00Phillipine Time
ULAT+08:00Ulan Bator Time
WADT+08:00West Australian DST
WST+08:00West Australian Standard Time
MT+08:30Moluccas Time
AWSST+09:00Western Australia Time
IRKST+09:00Irkutsk Summer Time
JAYT+09:00Jayapura Time (Indonesia)
JST+09:00Japan Std Time, USSR Zone 8
KST+09:00Korea Standard Time
PWT+09:00Palau Time
ULAST+09:00Ulan Bator Summer Time
WDT+09:00West Australian DST
YAKT+09:00Yakutsk Time
ACST+09:30Central Australia
CAST+09:30Central Australian ST
SAST+09:30South Australian Std Time
SAT+09:30South Australian Std Time
AEST+10:00Australia Eastern Std Time
DDUT+10:00Dumont-d'Urville Time (Antarctica)
EAST+10:00East Australian Std Time
EST+10:00Australia Eastern Std Time
GST+10:00Guam Std Time, USSR Zone 9
KDT+10:00Korea Daylight Time
LIGT+10:00From Melbourne, Australia
MPT+10:00North Mariana Islands Time
PGT+10:00Papua New Guinea Time
TRUK+10:00Truk Time
VLAT+10:00Vladivostok Time
YAKST+10:00Yakutsk Summer Time
YAPT+10:00Yap Time (Micronesia)
ACSST+10:30Central Australia Time
CADT+10:30Central Australian DST
CST+10:30Australia Central Std Time
LHST+10:30Lord Howe Standard Time, Australia
SADT+10:30South Australian Daylight Time
AESST+11:00Eastern Australia
LHDT+11:00Lord Howe Daylight Time, Australia
MAGT+11:00Magadan Time
NCT+11:00New Caledonia Time
PONT+11:00Ponape Time (Micronesia)
VLAST+11:00Vladivostok Summer Time
VUT+11:00Vanuata Time
ANAT+12:00Anadyr Time (Russia)
CKT+12:00Cook Islands Time
GILT+12:00Gilbert Islands Time
IDLE+12:00International Date Line, East
KOST+12:00Kosrae Time
MAGST+12:00Magadan Summer Time
MHT+12:00Kwajalein Time
NZST+12:00New Zealand Standard Time
NZT+12:00New Zealand Time
PETT+12:00Petropavlovsk-Kamchatski Time
TVT+12:00Tuvalu Time
WAKT+12:00Wake Time
WFT+12:00Wallis and Futuna Time
CHAST+12:45Chatham Island Time
ANAST+13:00Anadyr Summer Time (Russia)
NZDT+13:00New Zealand Daylight Time
PETST+13:00Petropavlovsk-Kamchatski Summer Time
PHOT+13:00Phoenix Islands (Kiribati) Time
TOT+13:00Tonga Time
CHADT+13:45Chatham Island Daylight Time
LINT+14:00Line Islands Time (Kiribati)

I mentioned earlier in this section that an INTERVAL value represents a time span. I also mentioned than an INTERVAL stores some number of seconds. The syntax for an INTERVAL literal allows you to specify the number of seconds in a variety of units.

The format of an INTERVAL value is

quantity unit [quantity unit ...][AGO]

The unit component specifies a number of seconds, as shown in Table 2.11. The quantity component acts as a multiplier (and may be fractional). If you have multiple quantity unit groups, they are all added together. The optional phrase AGO will cause the INTERVAL to be negative.

Table 2.11. INTERVAL Units
DescriptionSecondsUnit Names
Microsecond[3] .000001us, usec, usecs, useconds, microsecon
Millisecond[3].001-ms, msecs, mseconds, millisecon
Second1s, sec, secs, second, seconds
Minute60m, min, mins, minute, minutes
Hour3600h, hr, hrs, hours
Day86400d, day, days
Week604800w, week, weeks
Month (30 days)2592000mon, mons, month, months
Year31557600y, yr, yrs, year, years
Decade315576000dec, decs, decade, decades
Century3155760000c, cent, century, centuries
Millennium31557600000mil, mils, millennia, millennium

[3] Millisecond and microsecond can be used only in combination with another date/time component. For example, CAST( '1 SECOND 5000 MSEC' AS INTERVAL ) results in an interval of six seconds.

You can use the EXTRACT( EPOCH FROM interval ) function to convert an INTERVAL into a number of seconds. A few sample INTERVAL values are shown in Table 2.12. The Display column shows how PostgreSQL would format the Input Value for display. The EPOCH column shows the value that would be returned by extracting the EPOCH from the Input Value.

Table 2.12. Sample INTERVAL Values
Input ValueDisplayEPOCH
.5 minutes00:00:3030
22 seconds 1 msec00:00:22.0022.001
22.001 seconds00:00:22.0022.001
10 centuries 2 decades1020 years32188752000
1 week 2 days 3.5 msec9 days 00:00:00.00777600.0035

Supported Operators

There are two types of operators that you can use with temporal values: arithmetic operators (addition and subtraction) and comparison operators.

You can add an INT4, a TIME, or a TIMETZ to a DATE. When you add an INT4, you are adding a number of days. Adding a TIME or TIMETZ to a DATE results in a TIMESTAMP. Table 2.13 lists the valid data type and operator combinations for temporal data types. The last column in Table 2.14 shows the data type of the resulting value.

Table 2.13. Arithmetic Date/Time Operators
Data TypesValid Operators (θ)Result Type
DATE θ DATE-INTEGER
DATE θ TIME+TIMESTAMP
DATE θ TIMETZ+TIMESTAMP WITH TIMEZONE
DATE θ INT4+ -DATE
TIME θ DATE+TIMESTAMP
TIME θ INTERVAL+ -TIME
TIMETZ θ DATE+TIMESTAMP WITH TIMEZONE
TIMETZ θ INTERVAL+ -TIMETZ
TIMESTAMP θ TIMESTAMP-INTERVAL
TIMESTAMP θ INTERVAL+ -TIMESTAMP WITH TIMEZONE
INTERVAL θ TIME+TIME WITHOUT TIMEZONE

Table 2.14. Arithmetic Date/Time Operator Examples
ExampleResult
'23-JAN-2003'::DATE - '23-JAN-2002'::DATE365
'23-JAN-2003'::DATE + '2:35 PM'::TIME2003-01-23 14:35:00
'23-JAN-2003'::DATE + '2:35 PM GMT'::TIMETZ2003-01-23 09:35:00-05
'23-JAN-2003'::DATE + 2::INT42003-01-25
'2:35 PM'::TIME + '23-JAN-2003'::DATE2003-01-23 14:35:00
'2:35 PM'::TIME + '2 hours 5 minutes'::INTERVAL16:40:00
'2:35 PM EST'::TIMETZ + '23-JAN-2003'::DATE2003-01-23 14:35:00-05
'2:35 PM EST'::TIMETZ + '2 hours 5 minutes'::INTERVAL16:40:00-05
'23-JAN-2003 2:35 PM EST'::TIMESTAMP - '23-JAN-2002 1:00 PM EST'::TIMESTAMP365 days 01:35
'23-JAN-2003 2:35 PM EST'::TIMESTAMP + '3 days 2 hours 5 minutes'::INTERVAL2003-01-26 16:40:00-05
'2 hours 5 minutes'::INTERVAL + '2:34 PM'::TIME16:39:00

Table 2.14 shows how each of the arithmetic operators behave when applied to date/time values.

Using the temporal comparison operators, you can determine the relationship between two date/time values. For purposes of comparison, an earlier date/time value is considered to be less than a later date/time value.

Table 2.15 shows how you can combine the various temporal types with comparison operators.

Table 2.15. Date/Time Comparison Operators
Data TypesValid Operators (θ)
date θ date< <= <> = >= >
time θ time< <= <> = >= >
timetz θ timetz< <= <> = >= >
timestamp θ timestamp< <= <> = >= >

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.175.101