7.4. Date/Time Types

EnterpriseDB supports the date and time types shown in Table 7-7.

Table 7-7. Date/Time Types

NameStorage SizeDescriptionLow ValueHigh ValueResolution
DATE8 bytesdate and time4713 BC5874897 AD1 second
TIMESTAMP [ (p) ]8 bytesdate and time4713 BC5874897 AD1 microsecond

TIMESTAMP accepts an optional precision value p which specifies the number of fractional digits retained in the seconds field. The allowed range of p is from 0 to 6 with the default being 6.

Note: When TIMESTAMP values are stored as double precision floating-point numbers (currently the default), the effective limit of precision may be less than 6. TIMESTAMP values are stored as seconds before or after midnight 2000-01-01. Microsecond precision is achieved for dates within a few years of 2000-01-01, but the precision degrades for dates further away. When TIMESTAMP values are stored as eight-byte integers (a compile-time option), microsecond precision is available over the full range of values. However eight-byte integer timestamps have a more limited range of dates than shown above: from 4713 BC up to 294276 AD.

DATE stores the date and time with second precision. DATE is equivalent to TIMESTAMP(0).

DATETIME and SMALLDATETIME may also be used as synonyms for TIMESTAMP.

7.4.1. Date/Time Input

Date and time input is accepted in ISO 8601, SQL-compatible format. Remember that any date or time literal input needs to be enclosed in single quotes, like text strings. SQL requires the following syntax

type [ (p) ] 'value'

where p in the optional precision specification is an integer corresponding to the number of fractional digits in the seconds field. Precision can be specified for the TIMESTAMP type. The allowed values are mentioned above. If no precision is specified in a constant specification, it defaults to the precision of the literal value.

For some formats, ordering of month, day, and year in date input is ambiguous and there is support for specifying the expected ordering of these fields. Set the DateStyle parameter to MDY to select month-day-year interpretation, DMY to select day-month-year interpretation, or YMD to select year-month-day interpretation.

7.4.1.1. Dates

Table 7-8 shows some possible input formats for dates.

Table 7-8. Date Input

ExampleDescription
January 8, 1999unambiguous in any datestyle input mode
1999-01-08ISO 8601; January 8 in any mode (recommended format)
1/8/1999January 8 in MDY mode; August 1 in DMY mode
1/18/1999January 18 in MDY mode; rejected in other modes
01/02/03January 2, 2003 in MDY mode; February 1, 2003 in DMY mode; February 3, 2001 in YMD mode
1999-Jan-08January 8 in any mode
Jan-08-1999January 8 in any mode
08-Jan-1999January 8 in any mode
99-Jan-08January 8 in YMD mode, else error
08-Jan-99January 8, except error in YMD mode
Jan-08-99January 8, except error in YMD mode
19990108ISO 8601; January 8, 1999 in any mode
990108ISO 8601; January 8, 1999 in any mode
1999.008year and day of year
J2451187Julian day
January 8, 99 BCyear 99 before the Common Era

The date values can be assigned to a DATE or TIMESTAMP column or variable. The hour, minute, and seconds fields will be set to zero if the date value is not appended with a time value, examples of which are shown in Table 7-9.

7.4.1.2. Times

The following table gives examples of time input values that can be used in conjunction with the date values shown in Table 7-8.

Table 7-9. Time Input

ExampleDescription
04:05:06.789ISO 8601
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

7.4.1.3. Time Stamps

Valid input for the time stamp types consists of a concatenation of a date and a time. The date portion of the time stamp can be formatted according to any of the examples shown in Table 7-8. The time portion of the time stamp can be formatted according to any of examples shown in Table 7-9.

The following is an example of a time stamp which follows the ISO 8601 standard.

1999-01-08 04:05:06

In addition, the wide-spread format

January 8 04:05:06 1999

is supported.

7.4.1.4. Special Values

EnterpriseDB supports several special date/time input values for convenience, as shown in Table 7-10. The values infinity and -infinity are specially represented inside the system and will be displayed the same way; but the others are simply notational shorthands that will be converted to ordinary date/time values when read. All of these values are treated as normal constants and need to be written in single quotes.

Table 7-10. Special Date/Time Inputs

Input StringValid TypesDescription
epochDATE, TIMESTAMP1970-01-01 00:00:00 (Unix system time zero)
infinityTIMESTAMPlater than all other time stamps
-infinityTIMESTAMPearlier than all other time stamps
nowDATE, TIMESTAMPcurrent transaction's start time
todayDATE, TIMESTAMPmidnight today
tomorrowDATE, TIMESTAMPmidnight tomorrow
yesterdayDATE, TIMESTAMPmidnight yesterday

The following functions can be used to obtain the current date/time value for the corresponding data type: CURRENT_DATE, SYSDATE, and CURRENT_TIMESTAMP. The latter accepts an optional precision specification. (See also Section 8.8.4.)

7.4.2. Date/Time Output

The output format of the date/time types can be set to one of the four styles ISO 8601, SQL (Ingres), traditional POSTGRES, and German, using the command SET datestyle. The default is the ISO format. (The SQL standard requires the use of the ISO 8601 format. The name of the "SQL" output format is a historical accident.) Table 7-11 shows examples of each output style.

Table 7-11. Date/Time Output Styles

Style SpecificationDescriptionExample
ISOISO 8601/SQL standard1997-12-17 07:37:16
SQLtraditional style12/17/1997 07:37:16.00
POSTGRESoriginal styleWed Dec 17 07:37:16 1997
Germanregional style17.12.1997 07:37:16.00

In the SQL and POSTGRES styles, day appears before month if DMY field ordering has been specified, otherwise month appears before day. (See Section 7.4.1 for how this setting also affects interpretation of input values.) Table 7-12 shows an example.

Table 7-12. Date Order Conventions

datestyle SettingInput OrderingExample Output
SQL, DMYday/month/year17/12/1997 15:37:16.00
SQL, MDYmonth/day/year12/17/1997 07:37:16.00
Postgres, DMYday/month/yearWed 17 Dec 07:37:16 1997

The date/time styles can be selected by the user using the SET datestyle command, the DateStyle parameter in the postgresql.conf configuration file, or the PGDATESTYLE environment variable on the server or client. The formatting function to_char (see Section 8.7) is also available as a more flexible way to format the date/time output.

7.4.3. Internals

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