Реклама:

info.krc.karelia.ru

win -:|:- koi -:|:- iso -:|:- dos -:|:- mac

Start -:|:- Проекты -:|:- О нас

Date/Time Types

There are two fundamental kinds of date and time measurements provided by Postgres: absolute clock times and relative time intervals. Both kinds of time measurements should demonstrate both continuity and smoothness.

Postgres supplies two primary user-oriented date and time types, datetime and timespan, as well as the related SQL92 types timestamp, interval, date and time.

In a future release, datetime and timespan are likely to merge with the SQL92 types timestamp, interval. Other date and time types are also available, mostly for historical reasons.

Table 2-7. Postgres Date/Time Types

Date/Time TypeStorageRecommendationDescription
abstime4 bytesoriginal date and timelimited range
date4 bytesSQL92 typewide range
datetime8 bytesbest general date and timewide range, high precision
interval12 bytesSQL92 typeequivalent to timespan
reltime4 bytesoriginal time intervallimited range, low precision
time4 bytesSQL92 typewide range
timespan12 bytesbest general time intervalwide range, high precision
timestamp4 bytesSQL92 typelimited range
timestamp is currently implemented separately from datetime, although they share input and output routines.

Table 2-8. Postgres Date/Time Ranges

Date/Time TypeEarliestLatestResolution
abstime1901-12-142038-01-191 sec
date4713 BC32767 AD1 day
datetime4713 BC1465001 AD1 microsec to 14 digits
interval-178000000 years178000000 years1 microsec
reltime-68 years+68 years1 sec
time00:00:00.0023:59:59.991 microsec
timespan-178000000 years178000000 years1 microsec (14 digits)
timestamp1901-12-142038-01-191 sec

SQL92 Conventions

Postgres endeavors to be compatible with SQL92 definitions for typical usage. However, the SQL92 standard has an odd mix of date and time types and capabilities. Two obvious problems are:

Time zones in the real world can have no meaning unless associated with a date as well as a time since the offset may vary through the year with daylight savings time boundaries.

To address these difficulties, Postgres associates time zones only with date and time types which contain both date and time, and assumes local time for any type containing only date or time. Further, time zone support is derived from the underlying operating system time zone capabilities, and hence can handle daylight savings time and other expected behavior.

In future releases, the number of date/time types will decrease, with the current implementation of datetime becoming timestamp, timespan becoming interval, and (possibly) abstime and reltime being deprecated in favor of timestamp and interval. The more arcane features of the date/time definitions from the SQL92 standard are not likely to be pursued.

Date/Time Styles

Output formats can be set to one of four styles: ISO-8601, SQL (Ingres), traditional Postgres, and German.

Table 2-9. Postgres Date Styles

Style SpecificationDescriptionExample
ISOISO-8601 standard1997-12-17 07:37:16-08
SQLTraditional style12/17/1997 07:37:16.00 PST
PostgresOriginal styleWed Dec 17 07:37:16 1997 PST
GermanRegional style17.12.1997 07:37:16.00 PST

The SQL style has European and non-European (US) variants, which determines whether month follows day or vica versa.

Table 2-10. Postgres Date Order Conventions

Style SpecificationDescriptionExample
EuropeanRegional convention17/12/1997 15:37:16.00 MET
NonEuropeanRegional convention12/17/1997 07:37:16.00 PST
USRegional convention12/17/1997 07:37:16.00 PST

There are several ways to affect the appearance of date/time types:

For Postgres v6.4 (and earlier) the default date/time style is "non-European traditional Postgres". In future releases, the default may become "ISO" (compatible with ISO-8601), which alleviates date specification ambiguities and Y2K collation problems.

Calendar

Postgres uses Julian dates for all date/time calculations. They have the nice property of correctly predicting/calculating any date more recent than 4713BC to far into the future, using the assumption that the length of the year is 365.2425 days.

Date conventions before the 19th century make for interesting reading, but are not consistant enough to warrant coding into a date/time handler.

Time Zones

Postgres obtains time zone support from the underlying operating system for dates between 1902 and 2038 (near the typical date limits for Unix-style systems). Outside of this range, all dates are assumed to be specified and used in Universal Coordinated Time (UTC).

All dates and times are stored internally in Universal UTC, alternately known as Greenwich Mean Time (GMT). Times are converted to local time on the database server before being sent to the client frontend, hence by default are in the server time zone.

There are several ways to affect the time zone behavior:

If an invalid time zone is specified, the time zone becomes GMT (on most systems anyway).

Date/Time Input

General-use date and time is input using a wide range of styles, including ISO-compatible, SQL-compatible, traditional Postgres and other permutations of date and time. In cases where interpretation can be ambiguous (quite possible with many traditional styles of date specification) Postgres uses a style setting to resolve the ambiguity.

Most date and time types share code for data input. For those types the input can have any of a wide variety of styles. For numeric date representations, European and US conventions can differ, and the proper interpretation is obtained by using the SET DATESTYLE command before entering data. Note that the style setting does not preclude use of various styles for input; it is used primarily to determine the output style and to resolve ambiguities.

The special values current, infinity and -infinity are provided. infinity specifies a time later than any other valid time, and -infinity specifies a time earlier than any other valid time. current indicates that the current time should be substituted whenever this value appears in a computation.

The strings now, today, yesterday, tomorrow, and epoch can be used to specify time values. now means the current transaction time, and differs from current in that the current time is immediately substituted for it. epoch means Jan 1 00:00:00 1970 GMT.

Table 2-11. Postgres Date/Time Special Constants

ConstantDescription
currentCurrent transaction time, deferred
epoch1970-01-01 00:00:00+00 (Unix system time zero)
infinityLater than other valid times
-infinityEarlier than other valid times
invalidIllegal entry
nowCurrent transaction time
todayMidnight today
tomorrowMidnight tomorrow
yesterdayMidnight yesterday

Table 2-12. Postgres Date Input

ExampleDescription
January 8, 1999Unambiguous text month
1999-01-08ISO-8601
1/8/1999US; read as August 1 in European mode
8/1/1999European; read as August 1 in US mode
1/18/1999US; read as January 18 in any mode
1999.008Year and day of year
19990108ISO-8601 year, month, day
990108ISO-8601 year, month, day
1999.008Year and day of year
99008Year and day of year
January 8, 99 BCYear 99 before the Christian Era

Table 2-13. Postgres Month Abbreviations

MonthAbbreviations
AprilApr
AugustAug
DecemberDec
FebruaryFeb
JanuaryJan
JulyJul
JuneJun
MarchMar
NovemberNov
OctoberOct
SeptemberSep, Sept

Note: The month May has no explicit abbreviation, for obvious reasons.

Table 2-14. Postgres Day of Week Abbreviations

DayAbbreviation
SundaySun
MondayMon
TuesdayTue, Tues
WednesdayWed, Weds
ThursdayThu, Thur, Thurs
FridayFri
SaturdaySat

Table 2-15. Postgres Time Input

ExampleDescription
04:05:06.789ISO-8601, with all time fields
04:05:06ISO-8601
04:05ISO-8601
040506ISO-8601
04:05 AMSame as 04:05; AM does not affect value
04:05 PMSame as 16:05; input hour must be <= 12
zSame as 00:00:00
zuluSame as 00:00:00
allballsSame as 00:00:00

Table 2-16. Postgres Time Zone Input

Time ZoneDescription
PSTPacific Standard Time
-8:00ISO-8601 offset for PST
-800ISO-8601 offset for PST
-8ISO-8601 offset for PST

See Date/Time Support for details on time zones recognized by Postgres.

Note: If the compiler option USE_AUSTRALIAN_RULES is set then EST refers to Australia Eastern Std Time, which has an offset of +10:00 hours from UTC.

Australian time zones and their naming variants account for fully one quarter of all time zones in the Postgres time zone lookup table.

datetime

General-use date and time is input using a wide range of styles, including ISO-compatible, SQL-compatible, traditional Postgres (see section on "absolute time") and other permutations of date and time. Output styles can be ISO-compatible, SQL-compatible, or traditional Postgres, with the default set to be compatible with Postgres v6.0.

datetime is specified using the following syntax:

Year-Month-Day [ Hour : Minute : Second ]      [AD,BC] [ Timezone ]
  YearMonthDay [ Hour : Minute : Second ]      [AD,BC] [ Timezone ]
     Month Day [ Hour : Minute : Second ] Year [AD,BC] [ Timezone ]
where	
    Year is 4013 BC, ..., very large
    Month is Jan, Feb, ..., Dec or 1, 2, ..., 12
    Day is 1, 2, ..., 31
    Hour is 00, 02, ..., 23
    Minute is 00, 01, ..., 59
    Second is 00, 01, ..., 59 (60 for leap second)
    Timezone is 3 characters or ISO offset to GMT

Valid dates are from Nov 13 00:00:00 4013 BC GMT to far into the future. Timezones are either three characters (e.g. "GMT" or "PST") or ISO-compatible offsets to GMT (e.g. "-08" or "-08:00" when in Pacific Standard Time). Dates are stored internally in Greenwich Mean Time. Input and output routines translate time to the local time zone of the server.

timespan

General-use time span is input using a wide range of syntaxes, including ISO-compatible, SQL-compatible, traditional Postgres (see section on "relative time") and other permutations of time span. Output formats can be ISO-compatible, SQL-compatible, or traditional Postgres, with the default set to be Postgres-compatible. Months and years are a "qualitative" time interval, and are stored separately from the other "quantitative" time intervals such as day or hour. For date arithmetic, the qualitative time units are instantiated in the context of the relevant date or time.

Time span is specified with the following syntax:

  Quantity Unit [Quantity Unit...] [Direction]
@ Quantity Unit [Direction]
where 	
    Quantity is ..., -1, 0, 1, 2, ...
    Unit is second, minute, hour, day, week, month, year,
      decade, century, millenium, or abbreviations or plurals of these units.
    Direction is ago.

abstime

Absolute time (abstime) is a limited-range (+/- 68 years) and limited-precision (1 sec) date data type. datetime may be preferred, since it covers a larger range with greater precision.

Absolute time is specified using the following syntax:

Month  Day [ Hour : Minute : Second ]  Year [ Timezone ]
where	
    Month is Jan, Feb, ..., Dec
    Day is 1, 2, ..., 31
    Hour is 01, 02, ..., 24
    Minute is 00, 01, ..., 59
    Second is 00, 01, ..., 59
    Year is 1901, 1902, ..., 2038

Valid dates are from Dec 13 20:45:53 1901 GMT to Jan 19 03:14:04 2038 GMT.

Historical Note: As of Version 3.0, times are no longer read and written using Greenwich Mean Time; the input and output routines default to the local time zone.

All special values allowed for datetime are also allowed for "absolute time".

reltime

Relative time reltime is a limited-range (+/- 68 years) and limited-precision (1 sec) time span data type. timespan should be preferred, since it covers a larger range with greater precision and, more importantly, can distinguish between relative units (months and years) and quantitative units (days, hours, etc). Instead, reltime must force months to be exactly 30 days, so time arithmetic does not always work as expected. For example, adding one reltime year to abstime today does not produce today's date one year from now, but rather a date 360 days from today.

reltime shares input and output routines with the other time span types. The section on timespan covers this in more detail.

timestamp

This is currently a limited-range absolute time which closely resembles the abstime data type. It shares the general input parser with the other date/time types. In future releases this type will absorb the capabilities of the datetime type and will move toward SQL92 compliance.

timestamp is specified using the same syntax as for datetime.

interval

interval is an SQL92 data type which is currently mapped to the timespan Postgres data type.

tinterval

Time ranges are specified as:

[ 'abstime' 'abstime']
where
    abstime is a time in the absolute time format.
Special abstime values such as current', infinity' and -infinity' can be used.