EnterpriseDB supports the date and time types shown in Table 7-7.
Table 7-7. Date/Time Types
Name | Storage Size | Description | Low Value | High Value | Resolution |
---|---|---|---|---|---|
DATE | 8 bytes | date and time | 4713 BC | 5874897 AD | 1 second |
TIMESTAMP [ (p) ] | 8 bytes | date and time | 4713 BC | 5874897 AD | 1 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.
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.
Table 7-8 shows some possible input formats for dates.
Table 7-8. Date Input
Example | Description |
---|---|
January 8, 1999 | unambiguous in any datestyle input mode |
1999-01-08 | ISO 8601; January 8 in any mode (recommended format) |
1/8/1999 | January 8 in MDY mode; August 1 in DMY mode |
1/18/1999 | January 18 in MDY mode; rejected in other modes |
01/02/03 | January 2, 2003 in MDY mode; February 1, 2003 in DMY mode; February 3, 2001 in YMD mode |
1999-Jan-08 | January 8 in any mode |
Jan-08-1999 | January 8 in any mode |
08-Jan-1999 | January 8 in any mode |
99-Jan-08 | January 8 in YMD mode, else error |
08-Jan-99 | January 8, except error in YMD mode |
Jan-08-99 | January 8, except error in YMD mode |
19990108 | ISO 8601; January 8, 1999 in any mode |
990108 | ISO 8601; January 8, 1999 in any mode |
1999.008 | year and day of year |
J2451187 | Julian day |
January 8, 99 BC | year 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.
The following table gives examples of time input values that can be used in conjunction with the date values shown in Table 7-8.
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.
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 String | Valid Types | Description |
---|---|---|
epoch | DATE, TIMESTAMP | 1970-01-01 00:00:00 (Unix system time zero) |
infinity | TIMESTAMP | later than all other time stamps |
-infinity | TIMESTAMP | earlier than all other time stamps |
now | DATE, TIMESTAMP | current transaction's start time |
today | DATE, TIMESTAMP | midnight today |
tomorrow | DATE, TIMESTAMP | midnight tomorrow |
yesterday | DATE, TIMESTAMP | midnight 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.)
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 Specification | Description | Example |
---|---|---|
ISO | ISO 8601/SQL standard | 1997-12-17 07:37:16 |
SQL | traditional style | 12/17/1997 07:37:16.00 |
POSTGRES | original style | Wed Dec 17 07:37:16 1997 |
German | regional style | 17.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 Setting | Input Ordering | Example Output |
---|---|---|
SQL, DMY | day/month/year | 17/12/1997 15:37:16.00 |
SQL, MDY | month/day/year | 12/17/1997 07:37:16.00 |
Postgres, DMY | day/month/year | Wed 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.
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.