5 8.5.2. Date/Time Output
10 PostgreSQL supports the full set of SQL date and time types, shown in
11 Table 8.9. The operations available on these data types are described
12 in Section 9.9. Dates are counted according to the Gregorian calendar,
13 even in years before that calendar was introduced (see Section B.6 for
16 Table 8.9. Date/Time Types
17 Name Storage Size Description Low Value High Value Resolution
18 timestamp [ (p) ] [ without time zone ] 8 bytes both date and time (no
19 time zone) 4713 BC 294276 AD 1 microsecond
20 timestamp [ (p) ] with time zone 8 bytes both date and time, with time
21 zone 4713 BC 294276 AD 1 microsecond
22 date 4 bytes date (no time of day) 4713 BC 5874897 AD 1 day
23 time [ (p) ] [ without time zone ] 8 bytes time of day (no date)
24 00:00:00 24:00:00 1 microsecond
25 time [ (p) ] with time zone 12 bytes time of day (no date), with time
26 zone 00:00:00+1559 24:00:00-1559 1 microsecond
27 interval [ fields ] [ (p) ] 16 bytes time interval -178000000 years
28 178000000 years 1 microsecond
32 The SQL standard requires that writing just timestamp be equivalent to
33 timestamp without time zone, and PostgreSQL honors that behavior.
34 timestamptz is accepted as an abbreviation for timestamp with time
35 zone; this is a PostgreSQL extension.
37 time, timestamp, and interval accept an optional precision value p
38 which specifies the number of fractional digits retained in the seconds
39 field. By default, there is no explicit bound on precision. The allowed
40 range of p is from 0 to 6.
42 The interval type has an additional option, which is to restrict the
43 set of stored fields by writing one of these phrases:
58 Note that if both fields and p are specified, the fields must include
59 SECOND, since the precision applies only to the seconds.
61 The type time with time zone is defined by the SQL standard, but the
62 definition exhibits properties which lead to questionable usefulness.
63 In most cases, a combination of date, time, timestamp without time
64 zone, and timestamp with time zone should provide a complete range of
65 date/time functionality required by any application.
67 8.5.1. Date/Time Input #
69 Date and time input is accepted in almost any reasonable format,
70 including ISO 8601, SQL-compatible, traditional POSTGRES, and others.
71 For some formats, ordering of day, month, and year in date input is
72 ambiguous and there is support for specifying the expected ordering of
73 these fields. Set the DateStyle parameter to MDY to select
74 month-day-year interpretation, DMY to select day-month-year
75 interpretation, or YMD to select year-month-day interpretation.
77 PostgreSQL is more flexible in handling date/time input than the SQL
78 standard requires. See Appendix B for the exact parsing rules of
79 date/time input and for the recognized text fields including months,
80 days of the week, and time zones.
82 Remember that any date or time literal input needs to be enclosed in
83 single quotes, like text strings. Refer to Section 4.1.2.7 for more
84 information. SQL requires the following syntax
87 where p is an optional precision specification giving the number of
88 fractional digits in the seconds field. Precision can be specified for
89 time, timestamp, and interval types, and can range from 0 to 6. If no
90 precision is specified in a constant specification, it defaults to the
91 precision of the literal value (but not more than 6 digits).
95 Table 8.10 shows some possible inputs for the date type.
97 Table 8.10. Date Input
99 1999-01-08 ISO 8601; January 8 in any mode (recommended format)
100 January 8, 1999 unambiguous in any datestyle input mode
101 1/8/1999 January 8 in MDY mode; August 1 in DMY mode
102 1/18/1999 January 18 in MDY mode; rejected in other modes
103 01/02/03 January 2, 2003 in MDY mode; February 1, 2003 in DMY mode;
104 February 3, 2001 in YMD mode
105 1999-Jan-08 January 8 in any mode
106 Jan-08-1999 January 8 in any mode
107 08-Jan-1999 January 8 in any mode
108 99-Jan-08 January 8 in YMD mode, else error
109 08-Jan-99 January 8, except error in YMD mode
110 Jan-08-99 January 8, except error in YMD mode
111 19990108 ISO 8601; January 8, 1999 in any mode
112 990108 ISO 8601; January 8, 1999 in any mode
113 1999.008 year and day of year
115 January 8, 99 BC year 99 BC
119 The time-of-day types are time [ (p) ] without time zone and time [ (p)
120 ] with time zone. time alone is equivalent to time without time zone.
122 Valid input for these types consists of a time of day followed by an
123 optional time zone. (See Table 8.11 and Table 8.12.) If a time zone is
124 specified in the input for time without time zone, it is silently
125 ignored. You can also specify a date but it will be ignored, except
126 when you use a time zone name that involves a daylight-savings rule,
127 such as America/New_York. In this case specifying the date is required
128 in order to determine whether standard or daylight-savings time
129 applies. The appropriate time zone offset is recorded in the time with
130 time zone value and is output as stored; it is not adjusted to the
133 Table 8.11. Time Input
135 04:05:06.789 ISO 8601
139 04:05 AM same as 04:05; AM does not affect value
140 04:05 PM same as 16:05; input hour must be <= 12
141 04:05:06.789-8 ISO 8601, with time zone as UTC offset
142 04:05:06-08:00 ISO 8601, with time zone as UTC offset
143 04:05-08:00 ISO 8601, with time zone as UTC offset
144 040506-08 ISO 8601, with time zone as UTC offset
145 040506+0730 ISO 8601, with fractional-hour time zone as UTC offset
146 040506+07:30:00 UTC offset specified to seconds (not allowed in ISO
148 04:05:06 PST time zone specified by abbreviation
149 2003-04-12 04:05:06 America/New_York time zone specified by full name
151 Table 8.12. Time Zone Input
153 PST Abbreviation (for Pacific Standard Time)
154 America/New_York Full time zone name
155 PST8PDT POSIX-style time zone specification
156 -8:00:00 UTC offset for PST
157 -8:00 UTC offset for PST (ISO 8601 extended format)
158 -800 UTC offset for PST (ISO 8601 basic format)
159 -8 UTC offset for PST (ISO 8601 basic format)
160 zulu Military abbreviation for UTC
161 z Short form of zulu (also in ISO 8601)
163 Refer to Section 8.5.3 for more information on how to specify time
166 8.5.1.3. Time Stamps #
168 Valid input for the time stamp types consists of the concatenation of a
169 date and a time, followed by an optional time zone, followed by an
170 optional AD or BC. (Alternatively, AD/BC can appear before the time
171 zone, but this is not the preferred ordering.) Thus:
175 1999-01-08 04:05:06 -8:00
177 are valid values, which follow the ISO 8601 standard. In addition, the
179 January 8 04:05:06 1999 PST
183 The SQL standard differentiates timestamp without time zone and
184 timestamp with time zone literals by the presence of a “+” or “-”
185 symbol and time zone offset after the time. Hence, according to the
187 TIMESTAMP '2004-10-19 10:23:54'
189 is a timestamp without time zone, while
190 TIMESTAMP '2004-10-19 10:23:54+02'
192 is a timestamp with time zone. PostgreSQL never examines the content of
193 a literal string before determining its type, and therefore will treat
194 both of the above as timestamp without time zone. To ensure that a
195 literal is treated as timestamp with time zone, give it the correct
197 TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'
199 In a value that has been determined to be timestamp without time zone,
200 PostgreSQL will silently ignore any time zone indication. That is, the
201 resulting value is derived from the date/time fields in the input
202 string, and is not adjusted for time zone.
204 For timestamp with time zone values, an input string that includes an
205 explicit time zone will be converted to UTC (Universal Coordinated
206 Time) using the appropriate offset for that time zone. If no time zone
207 is stated in the input string, then it is assumed to be in the time
208 zone indicated by the system's TimeZone parameter, and is converted to
209 UTC using the offset for the timezone zone. In either case, the value
210 is stored internally as UTC, and the originally stated or assumed time
211 zone is not retained.
213 When a timestamp with time zone value is output, it is always converted
214 from UTC to the current timezone zone, and displayed as local time in
215 that zone. To see the time in another time zone, either change timezone
216 or use the AT TIME ZONE construct (see Section 9.9.4).
218 Conversions between timestamp without time zone and timestamp with time
219 zone normally assume that the timestamp without time zone value should
220 be taken or given as timezone local time. A different time zone can be
221 specified for the conversion using AT TIME ZONE.
223 8.5.1.4. Special Values #
225 PostgreSQL supports several special date/time input values for
226 convenience, as shown in Table 8.13. The values infinity and -infinity
227 are specially represented inside the system and will be displayed
228 unchanged; but the others are simply notational shorthands that will be
229 converted to ordinary date/time values when read. (In particular, now
230 and related strings are converted to a specific time value as soon as
231 they are read.) All of these values need to be enclosed in single
232 quotes when used as constants in SQL commands.
234 Table 8.13. Special Date/Time Inputs
235 Input String Valid Types Description
236 epoch date, timestamp 1970-01-01 00:00:00+00 (Unix system time zero)
237 infinity date, timestamp, interval later than all other time stamps
238 -infinity date, timestamp, interval earlier than all other time stamps
239 now date, time, timestamp current transaction's start time
240 today date, timestamp midnight (00:00) today
241 tomorrow date, timestamp midnight (00:00) tomorrow
242 yesterday date, timestamp midnight (00:00) yesterday
243 allballs time 00:00:00.00 UTC
245 The following SQL-compatible functions can also be used to obtain the
246 current time value for the corresponding data type: CURRENT_DATE,
247 CURRENT_TIME, CURRENT_TIMESTAMP, LOCALTIME, LOCALTIMESTAMP. (See
248 Section 9.9.5.) Note that these are SQL functions and are not
249 recognized in data input strings.
253 While the input strings now, today, tomorrow, and yesterday are fine to
254 use in interactive SQL commands, they can have surprising behavior when
255 the command is saved to be executed later, for example in prepared
256 statements, views, and function definitions. The string can be
257 converted to a specific time value that continues to be used long after
258 it becomes stale. Use one of the SQL functions instead in such
259 contexts. For example, CURRENT_DATE + 1 is safer than 'tomorrow'::date.
261 8.5.2. Date/Time Output #
263 The output format of the date/time types can be set to one of the four
264 styles ISO 8601, SQL (Ingres), traditional POSTGRES (Unix date format),
265 or German. The default is the ISO format. (The SQL standard requires
266 the use of the ISO 8601 format. The name of the “SQL” output format is
267 a historical accident.) Table 8.14 shows examples of each output style.
268 The output of the date and time types is generally only the date or
269 time part in accordance with the given examples. However, the POSTGRES
270 style outputs date-only values in ISO format.
272 Table 8.14. Date/Time Output Styles
273 Style Specification Description Example
274 ISO ISO 8601, SQL standard 1997-12-17 07:37:16-08
275 SQL traditional style 12/17/1997 07:37:16.00 PST
276 Postgres original style Wed Dec 17 07:37:16 1997 PST
277 German regional style 17.12.1997 07:37:16.00 PST
281 ISO 8601 specifies the use of uppercase letter T to separate the date
282 and time. PostgreSQL accepts that format on input, but on output it
283 uses a space rather than T, as shown above. This is for readability and
284 for consistency with RFC 3339 as well as some other database systems.
286 In the SQL and POSTGRES styles, day appears before month if DMY field
287 ordering has been specified, otherwise month appears before day. (See
288 Section 8.5.1 for how this setting also affects interpretation of input
289 values.) Table 8.15 shows examples.
291 Table 8.15. Date Order Conventions
292 datestyle Setting Input Ordering Example Output
293 SQL, DMY day/month/year 17/12/1997 15:37:16.00 CET
294 SQL, MDY month/day/year 12/17/1997 07:37:16.00 PST
295 Postgres, DMY day/month/year Wed 17 Dec 07:37:16 1997 PST
297 In the ISO style, the time zone is always shown as a signed numeric
298 offset from UTC, with positive sign used for zones east of Greenwich.
299 The offset will be shown as hh (hours only) if it is an integral number
300 of hours, else as hh:mm if it is an integral number of minutes, else as
301 hh:mm:ss. (The third case is not possible with any modern time zone
302 standard, but it can appear when working with timestamps that predate
303 the adoption of standardized time zones.) In the other date styles, the
304 time zone is shown as an alphabetic abbreviation if one is in common
305 use in the current zone. Otherwise it appears as a signed numeric
306 offset in ISO 8601 basic format (hh or hhmm). The alphabetic
307 abbreviations shown in these styles are taken from the IANA time zone
308 database entry currently selected by the TimeZone run-time parameter;
309 they are not affected by the timezone_abbreviations setting.
311 The date/time style can be selected by the user using the SET datestyle
312 command, the DateStyle parameter in the postgresql.conf configuration
313 file, or the PGDATESTYLE environment variable on the server or client.
315 The formatting function to_char (see Section 9.8) is also available as
316 a more flexible way to format date/time output.
320 Time zones, and time-zone conventions, are influenced by political
321 decisions, not just earth geometry. Time zones around the world became
322 somewhat standardized during the 1900s, but continue to be prone to
323 arbitrary changes, particularly with respect to daylight-savings rules.
324 PostgreSQL uses the widely-used IANA (Olson) time zone database for
325 information about historical time zone rules. For times in the future,
326 the assumption is that the latest known rules for a given time zone
327 will continue to be observed indefinitely far into the future.
329 PostgreSQL endeavors to be compatible with the SQL standard definitions
330 for typical usage. However, the SQL standard has an odd mix of date and
331 time types and capabilities. Two obvious problems are:
332 * Although the date type cannot have an associated time zone, the
333 time type can. Time zones in the real world have little meaning
334 unless associated with a date as well as a time, since the offset
335 can vary through the year with daylight-saving time boundaries.
336 * The default time zone is specified as a constant numeric offset
337 from UTC. It is therefore impossible to adapt to daylight-saving
338 time when doing date/time arithmetic across DST boundaries.
340 To address these difficulties, we recommend using date/time types that
341 contain both date and time when using time zones. We do not recommend
342 using the type time with time zone (though it is supported by
343 PostgreSQL for legacy applications and for compliance with the SQL
344 standard). PostgreSQL assumes your local time zone for any type
345 containing only date or time.
347 All timezone-aware dates and times are stored internally in UTC. They
348 are converted to local time in the zone specified by the TimeZone
349 configuration parameter before being displayed to the client.
351 PostgreSQL allows you to specify time zones in three different forms:
352 * A full time zone name, for example America/New_York. The recognized
353 time zone names are listed in the pg_timezone_names view (see
354 Section 53.34). PostgreSQL uses the widely-used IANA time zone data
355 for this purpose, so the same time zone names are also recognized
357 * A time zone abbreviation, for example PST. Such a specification
358 merely defines a particular offset from UTC, in contrast to full
359 time zone names which can imply a set of daylight savings
360 transition rules as well. The recognized abbreviations are listed
361 in the pg_timezone_abbrevs view (see Section 53.33). You cannot set
362 the configuration parameters TimeZone or log_timezone to a time
363 zone abbreviation, but you can use abbreviations in date/time input
364 values and with the AT TIME ZONE operator.
365 * In addition to the timezone names and abbreviations, PostgreSQL
366 will accept POSIX-style time zone specifications, as described in
367 Section B.5. This option is not normally preferable to using a
368 named time zone, but it may be necessary if no suitable IANA time
369 zone entry is available.
371 In short, this is the difference between abbreviations and full names:
372 abbreviations represent a specific offset from UTC, whereas many of the
373 full names imply a local daylight-savings time rule, and so have two
374 possible UTC offsets. As an example, 2014-06-04 12:00 America/New_York
375 represents noon local time in New York, which for this particular date
376 was Eastern Daylight Time (UTC-4). So 2014-06-04 12:00 EDT specifies
377 that same time instant. But 2014-06-04 12:00 EST specifies noon Eastern
378 Standard Time (UTC-5), regardless of whether daylight savings was
379 nominally in effect on that date.
383 The sign in POSIX-style time zone specifications has the opposite
384 meaning of the sign in ISO-8601 datetime values. For example, the POSIX
385 time zone for 2014-06-04 12:00+04 would be UTC-4.
387 To complicate matters, some jurisdictions have used the same timezone
388 abbreviation to mean different UTC offsets at different times; for
389 example, in Moscow MSK has meant UTC+3 in some years and UTC+4 in
390 others. PostgreSQL interprets such abbreviations according to whatever
391 they meant (or had most recently meant) on the specified date; but, as
392 with the EST example above, this is not necessarily the same as local
393 civil time on that date.
395 In all cases, timezone names and abbreviations are recognized
396 case-insensitively. (This is a change from PostgreSQL versions prior to
397 8.2, which were case-sensitive in some contexts but not others.)
399 Neither timezone names nor abbreviations are hard-wired into the
400 server; they are obtained from configuration files stored under
401 .../share/timezone/ and .../share/timezonesets/ of the installation
402 directory (see Section B.4).
404 The TimeZone configuration parameter can be set in the file
405 postgresql.conf, or in any of the other standard ways described in
406 Chapter 19. There are also some special ways to set it:
407 * The SQL command SET TIME ZONE sets the time zone for the session.
408 This is an alternative spelling of SET TIMEZONE TO with a more
409 SQL-spec-compatible syntax.
410 * The PGTZ environment variable is used by libpq clients to send a
411 SET TIME ZONE command to the server upon connection.
413 8.5.4. Interval Input #
415 interval values can be written using the following verbose syntax:
416 [@] quantity unit [quantity unit...] [direction]
418 where quantity is a number (possibly signed); unit is microsecond,
419 millisecond, second, minute, hour, day, week, month, year, decade,
420 century, millennium, or abbreviations or plurals of these units;
421 direction can be ago or empty. The at sign (@) is optional noise. The
422 amounts of the different units are implicitly added with appropriate
423 sign accounting. ago negates all the fields. This syntax is also used
424 for interval output, if IntervalStyle is set to postgres_verbose.
426 Quantities of days, hours, minutes, and seconds can be specified
427 without explicit unit markings. For example, '1 12:59:10' is read the
428 same as '1 day 12 hours 59 min 10 sec'. Also, a combination of years
429 and months can be specified with a dash; for example '200-10' is read
430 the same as '200 years 10 months'. (These shorter forms are in fact the
431 only ones allowed by the SQL standard, and are used for output when
432 IntervalStyle is set to sql_standard.)
434 Interval values can also be written as ISO 8601 time intervals, using
435 either the “format with designators” of the standard's section 4.4.3.2
436 or the “alternative format” of section 4.4.3.3. The format with
437 designators looks like this:
438 P quantity unit [ quantity unit ...] [ T [ quantity unit ...]]
440 The string must start with a P, and may include a T that introduces the
441 time-of-day units. The available unit abbreviations are given in
442 Table 8.16. Units may be omitted, and may be specified in any order,
443 but units smaller than a day must appear after T. In particular, the
444 meaning of M depends on whether it is before or after T.
446 Table 8.16. ISO 8601 Interval Unit Abbreviations
449 M Months (in the date part)
453 M Minutes (in the time part)
456 In the alternative format:
457 P [ years-months-days ] [ T hours:minutes:seconds ]
459 the string must begin with P, and a T separates the date and time parts
460 of the interval. The values are given as numbers similar to ISO 8601
463 When writing an interval constant with a fields specification, or when
464 assigning a string to an interval column that was defined with a fields
465 specification, the interpretation of unmarked quantities depends on the
466 fields. For example INTERVAL '1' YEAR is read as 1 year, whereas
467 INTERVAL '1' means 1 second. Also, field values “to the right” of the
468 least significant field allowed by the fields specification are
469 silently discarded. For example, writing INTERVAL '1 day 2:03:04' HOUR
470 TO MINUTE results in dropping the seconds field, but not the day field.
472 According to the SQL standard all fields of an interval value must have
473 the same sign, so a leading negative sign applies to all fields; for
474 example the negative sign in the interval literal '-1 2:03:04' applies
475 to both the days and hour/minute/second parts. PostgreSQL allows the
476 fields to have different signs, and traditionally treats each field in
477 the textual representation as independently signed, so that the
478 hour/minute/second part is considered positive in this example. If
479 IntervalStyle is set to sql_standard then a leading sign is considered
480 to apply to all fields (but only if no additional signs appear).
481 Otherwise the traditional PostgreSQL interpretation is used. To avoid
482 ambiguity, it's recommended to attach an explicit sign to each field if
483 any field is negative.
485 Internally, interval values are stored as three integral fields:
486 months, days, and microseconds. These fields are kept separate because
487 the number of days in a month varies, while a day can have 23 or 25
488 hours if a daylight savings time transition is involved. An interval
489 input string that uses other units is normalized into this format, and
490 then reconstructed in a standardized way for output, for example:
491 SELECT '2 years 15 months 100 weeks 99 hours 123456789 milliseconds'::interval;
493 ---------------------------------------
494 3 years 3 mons 700 days 133:17:36.789
496 Here weeks, which are understood as “7 days”, have been kept separate,
497 while the smaller and larger time units were combined and normalized.
499 Input field values can have fractional parts, for example '1.5 weeks'
500 or '01:02:03.45'. However, because interval internally stores only
501 integral fields, fractional values must be converted into smaller
502 units. Fractional parts of units greater than months are rounded to be
503 an integer number of months, e.g. '1.5 years' becomes '1 year 6 mons'.
504 Fractional parts of weeks and days are computed to be an integer number
505 of days and microseconds, assuming 30 days per month and 24 hours per
506 day, e.g., '1.75 months' becomes 1 mon 22 days 12:00:00. Only seconds
507 will ever be shown as fractional on output.
509 Table 8.17 shows some examples of valid interval input.
511 Table 8.17. Interval Input
513 1-2 SQL standard format: 1 year 2 months
514 3 4:05:06 SQL standard format: 3 days 4 hours 5 minutes 6 seconds
515 1 year 2 months 3 days 4 hours 5 minutes 6 seconds Traditional Postgres
516 format: 1 year 2 months 3 days 4 hours 5 minutes 6 seconds
517 P1Y2M3DT4H5M6S ISO 8601 “format with designators”: same meaning as
519 P0001-02-03T04:05:06 ISO 8601 “alternative format”: same meaning as
522 8.5.5. Interval Output #
524 As previously explained, PostgreSQL stores interval values as months,
525 days, and microseconds. For output, the months field is converted to
526 years and months by dividing by 12. The days field is shown as-is. The
527 microseconds field is converted to hours, minutes, seconds, and
528 fractional seconds. Thus months, minutes, and seconds will never be
529 shown as exceeding the ranges 0–11, 0–59, and 0–59 respectively, while
530 the displayed years, days, and hours fields can be quite large. (The
531 justify_days and justify_hours functions can be used if it is desirable
532 to transpose large days or hours values into the next higher field.)
534 The output format of the interval type can be set to one of the four
535 styles sql_standard, postgres, postgres_verbose, or iso_8601, using the
536 command SET intervalstyle. The default is the postgres format.
537 Table 8.18 shows examples of each output style.
539 The sql_standard style produces output that conforms to the SQL
540 standard's specification for interval literal strings, if the interval
541 value meets the standard's restrictions (either year-month only or
542 day-time only, with no mixing of positive and negative components).
543 Otherwise the output looks like a standard year-month literal string
544 followed by a day-time literal string, with explicit signs added to
545 disambiguate mixed-sign intervals.
547 The output of the postgres style matches the output of PostgreSQL
548 releases prior to 8.4 when the DateStyle parameter was set to ISO.
550 The output of the postgres_verbose style matches the output of
551 PostgreSQL releases prior to 8.4 when the DateStyle parameter was set
554 The output of the iso_8601 style matches the “format with designators”
555 described in section 4.4.3.2 of the ISO 8601 standard.
557 Table 8.18. Interval Output Style Examples
558 Style Specification Year-Month Interval Day-Time Interval Mixed
560 sql_standard 1-2 3 4:05:06 -1-2 +3 -4:05:06
561 postgres 1 year 2 mons 3 days 04:05:06 -1 year -2 mons +3 days
563 postgres_verbose @ 1 year 2 mons @ 3 days 4 hours 5 mins 6 secs @ 1
564 year 2 mons -3 days 4 hours 5 mins 6 secs ago
565 iso_8601 P1Y2M P3DT4H5M6S P-1Y-2M3DT-4H-5M-6S