2 $Header: /cvsroot/pgsql/doc/src/sgml/datetime.sgml,v 2.33 2003/08/25 23:30:25 tgl Exp $
5 <appendix id="datetime-appendix">
6 <title>Date/Time Support</title>
9 <productname>PostgreSQL</productname> uses an internal heuristic
10 parser for all date/time input support. Dates and times are input as
11 strings, and are broken up into distinct fields with a preliminary
12 determination of what kind of information may be in the
13 field. Each field is interpreted and either assigned a numeric
14 value, ignored, or rejected.
15 The parser contains internal lookup tables for all textual fields,
16 including months, days of the week, and time
21 This appendix includes information on the content of these
22 lookup tables and describes the steps used by the parser to decode
27 <title>Date/Time Input Interpretation</title>
30 The date/time type inputs are all decoded using the following procedure.
36 Break the input string into tokens and categorize each token as
37 a string, time, time zone, or number.
43 If the numeric token contains a colon (<literal>:</>), this is
44 a time string. Include all subsequent digits and colons.
50 If the numeric token contains a dash (<literal>-</>), slash
51 (<literal>/</>), or two or more dots (<literal>.</>), this is
52 a date string which may have a text month.
58 If the token is numeric only, then it is either a single field
59 or an ISO 8601 concatenated date (e.g.,
60 <literal>19990113</literal> for January 13, 1999) or time
61 (e.g., <literal>141516</literal> for 14:15:16).
67 If the token starts with a plus (<literal>+</>) or minus
68 (<literal>-</>), then it is either a time zone or a special
77 If the token is a text string, match up with possible strings.
83 Do a binary-search table lookup for the token
84 as either a special string (e.g., <literal>today</literal>),
85 day (e.g., <literal>Thursday</literal>),
86 month (e.g., <literal>January</literal>),
87 or noise word (e.g., <literal>at</literal>, <literal>on</literal>).
91 Set field values and bit mask for fields.
92 For example, set year, month, day for <literal>today</literal>,
93 and additionally hour, minute, second for <literal>now</literal>.
99 If not found, do a similar binary-search table lookup to match
100 the token with a time zone.
106 If still not found, throw an error.
114 When the token is a number or number field:
120 If there are eight or six digits,
121 and if no other date fields have been previously read, then interpret
122 as a <quote>concatenated date</quote> (e.g.,
123 <literal>19990118</literal> or <literal>990118</literal>).
124 The interpretation is <literal>YYYYMMDD</> or <literal>YYMMDD</>.
130 If the token is three digits
131 and a year has already been read, then interpret as day of year.
137 If four or six digits and a year has already been read, then
138 interpret as a time (<literal>HHMM</> or <literal>HHMMSS</>).
144 If three or more digits and no date fields have yet been found,
145 interpret as a year (this forces yy-mm-dd ordering of the remaining
152 Otherwise the date field ordering is assumed to follow the
153 <varname>DateStyle</> setting: mm-dd-yy, dd-mm-yy, or yy-mm-dd.
154 Throw an error if a month or day field is found to be out of range.
162 If BC has been specified, negate the year and add one for
163 internal storage. (There is no year zero in the Gregorian
164 calendar, so numerically 1 BC becomes year
171 If BC was not specified, and if the year field was two digits in length, then
172 adjust the year to four digits. If the field is less than 70, then add 2000,
177 Gregorian years AD 1-99 may be entered by using 4 digits with leading
178 zeros (e.g., <literal>0099</> is AD 99). Previous versions of
179 <productname>PostgreSQL</productname> accepted years with three
180 digits and with single digits, but as of version 7.0 the rules have
181 been tightened up to reduce the possibility of ambiguity.
190 <sect1 id="datetime-keywords">
191 <title>Date/Time Key Words</title>
194 <xref linkend="datetime-month-table"> shows the tokens that are
195 permissible as abbreviations for the names of the month.
198 <table id="datetime-month-table">
199 <title>Month Abbreviations</title>
204 <entry>Abbreviations</entry>
213 <entry>August</entry>
217 <entry>December</entry>
221 <entry>February</entry>
225 <entry>January</entry>
241 <entry>November</entry>
245 <entry>October</entry>
249 <entry>September</entry>
250 <entry>Sep, Sept</entry>
258 The month May has no explicit abbreviation, for obvious reasons.
263 <xref linkend="datetime-dow-table"> shows the tokens that are
264 permissible as abbreviations for the names of the days of the
268 <table id="datetime-dow-table">
269 <title>Day of the Week Abbreviations</title>
274 <entry>Abbreviation</entry>
279 <entry>Sunday</entry>
283 <entry>Monday</entry>
287 <entry>Tuesday</entry>
288 <entry>Tue, Tues</entry>
291 <entry>Wednesday</entry>
292 <entry>Wed, Weds</entry>
295 <entry>Thursday</entry>
296 <entry>Thu, Thur, Thurs</entry>
299 <entry>Friday</entry>
303 <entry>Saturday</entry>
311 <xref linkend="datetime-mod-table"> shows the tokens that serve
312 various modifier purposes.
315 <table id="datetime-mod-table">
316 <title>Date/Time Field Modifiers</title>
320 <entry>Identifier</entry>
321 <entry>Description</entry>
326 <entry><literal>ABSTIME</literal></entry>
327 <entry>Key word ignored</entry>
330 <entry><literal>AM</literal></entry>
331 <entry>Time is before 12:00</entry>
334 <entry><literal>AT</literal></entry>
335 <entry>Key word ignored</entry>
338 <entry><literal>JULIAN</>, <literal>JD</>, <literal>J</></entry>
339 <entry>Next field is Julian Day</entry>
342 <entry><literal>ON</literal></entry>
343 <entry>Key word ignored</entry>
346 <entry><literal>PM</literal></entry>
347 <entry>Time is on or after after 12:00</entry>
350 <entry><literal>T</literal></entry>
351 <entry>Next field is time</entry>
358 The key word <literal>ABSTIME</literal> is ignored for historical
359 reasons: In very old releases of
360 <productname>PostgreSQL</productname>, invalid values of type <type>abstime</type>
361 were emitted as <literal>Invalid Abstime</literal>. This is no
362 longer the case however and this key word will likely be dropped in
367 <primary>time zones</primary>
371 <xref linkend="datetime-timezone-table"> shows the time zone
372 abbreviations recognized by <productname>PostgreSQL</productname>.
373 <productname>PostgreSQL</productname> contains internal tabular
374 information for time zone decoding, since there is no standard
375 operating system interface to provide access to general,
376 cross-time zone information. The underlying operating system
377 <emphasis>is</emphasis> used to provide time zone information for
378 <emphasis>output</emphasis>, however.
382 The table is organized by time zone offset from <acronym>UTC</>,
383 rather than alphabetically. This is intended to facilitate
384 matching local usage with recognized abbreviations for cases where
388 <table id="datetime-timezone-table">
389 <title>Time Zone Abbreviations</title>
393 <entry>Time Zone</entry>
394 <entry>Offset from UTC</entry>
395 <entry>Description</entry>
401 <entry>+13:00</entry>
402 <entry>New Zealand Daylight-Saving Time</entry>
406 <entry>+12:00</entry>
407 <entry>International Date Line, East</entry>
411 <entry>+12:00</entry>
412 <entry>New Zealand Standard Time</entry>
416 <entry>+12:00</entry>
417 <entry>New Zealand Time</entry>
421 <entry>+11:00</entry>
422 <entry>Australia Eastern Summer Standard Time</entry>
426 <entry>+10:30</entry>
427 <entry>Central Australia Summer Standard Time</entry>
431 <entry>+10:30</entry>
432 <entry>Central Australia Daylight-Saving Time</entry>
436 <entry>+10:30</entry>
437 <entry>South Australian Daylight-Saving Time</entry>
441 <entry>+10:00</entry>
442 <entry>Australia Eastern Standard Time</entry>
446 <entry>+10:00</entry>
447 <entry>East Australian Standard Time</entry>
451 <entry>+10:00</entry>
452 <entry>Guam Standard Time, Russia zone 9</entry>
456 <entry>+10:00</entry>
457 <entry>Melbourne, Australia</entry>
461 <entry>+09:30</entry>
462 <entry>South Australia Standard Time</entry>
466 <entry>+09:30</entry>
467 <entry>Central Australia Standard Time</entry>
471 <entry>+09:00</entry>
472 <entry>Australia Western Summer Standard Time</entry>
476 <entry>+09:00</entry>
477 <entry>Japan Standard Time, Russia zone 8</entry>
481 <entry>+09:00</entry>
482 <entry>Korea Standard Time</entry>
486 <entry>+09:00</entry>
487 <entry>Kwajalein Time</entry>
491 <entry>+09:00</entry>
492 <entry>West Australian Daylight-Saving Time</entry>
496 <entry>+08:30</entry>
497 <entry>Moluccas Time</entry>
501 <entry>+08:00</entry>
502 <entry>Australia Western Standard Time</entry>
506 <entry>+08:00</entry>
507 <entry>China Coastal Time</entry>
511 <entry>+08:00</entry>
512 <entry>West Australian Daylight-Saving Time</entry>
516 <entry>+08:00</entry>
517 <entry>West Australian Standard Time</entry>
521 <entry>+07:30</entry>
522 <entry>Java Time</entry>
526 <entry>+07:00</entry>
527 <entry>Almaty Summer Time</entry>
531 <entry>+07:00</entry>
532 <entry>West Australian Standard Time</entry>
536 <entry>+07:00</entry>
537 <entry>Christmas (Island) Time</entry>
541 <entry>+06:30</entry>
542 <entry>Myanmar Time</entry>
546 <entry>+06:00</entry>
547 <entry>Almaty Time</entry>
551 <entry>+06:00</entry>
552 <entry>Mawson (Antarctica) Time</entry>
556 <entry>+05:00</entry>
557 <entry>Indian Chagos Time</entry>
561 <entry>+05:00</entry>
562 <entry>Maldives Island Time</entry>
566 <entry>+05:00</entry>
567 <entry>Kerguelen Time</entry>
571 <entry>+04:30</entry>
572 <entry>Afganistan Time</entry>
576 <entry>+04:00</entry>
577 <entry>Antananarivo Summer Time</entry>
581 <entry>+04:00</entry>
582 <entry>Mauritius Island Time</entry>
586 <entry>+04:00</entry>
587 <entry>Reunion Island Time</entry>
591 <entry>+04:00</entry>
592 <entry>Mahe Island Time</entry>
595 <entry>IRT, IT</entry>
596 <entry>+03:30</entry>
597 <entry>Iran Time</entry>
601 <entry>+03:00</entry>
602 <entry>Antananarivo, Comoro Time</entry>
606 <entry>+03:00</entry>
607 <entry>Baghdad Time</entry>
610 <entry>EETDST</entry>
611 <entry>+03:00</entry>
612 <entry>Eastern Europe Daylight-Saving Time</entry>
616 <entry>+03:00</entry>
617 <entry>Hellas Mediterranean Time (?)</entry>
621 <entry>+02:00</entry>
622 <entry>British Double Standard Time</entry>
626 <entry>+02:00</entry>
627 <entry>Central European Summer Time</entry>
630 <entry>CETDST</entry>
631 <entry>+02:00</entry>
632 <entry>Central European Daylight-Saving Time</entry>
636 <entry>+02:00</entry>
637 <entry>Eastern European Time, Russia zone 1</entry>
641 <entry>+02:00</entry>
642 <entry>French Winter Time</entry>
646 <entry>+02:00</entry>
647 <entry>Israel Standard Time</entry>
651 <entry>+02:00</entry>
652 <entry>Middle European Summer Time</entry>
655 <entry>METDST</entry>
656 <entry>+02:00</entry>
657 <entry>Middle Europe Daylight-Saving Time</entry>
661 <entry>+02:00</entry>
662 <entry>Swedish Summer Time</entry>
666 <entry>+01:00</entry>
667 <entry>British Summer Time</entry>
671 <entry>+01:00</entry>
672 <entry>Central European Time</entry>
676 <entry>+01:00</entry>
677 <entry><foreignphrase>Dansk Normal Tid</foreignphrase></entry>
681 <entry>+01:00</entry>
682 <entry>French Summer Time</entry>
686 <entry>+01:00</entry>
687 <entry>Middle European Time</entry>
691 <entry>+01:00</entry>
692 <entry>Middle European Winter Time</entry>
696 <entry>+01:00</entry>
697 <entry><foreignphrase>Mitteleuropäische Zeit</></entry>
701 <entry>+01:00</entry>
702 <entry>Norway Standard Time</entry>
706 <entry>+01:00</entry>
707 <entry>Seychelles Time</entry>
711 <entry>+01:00</entry>
712 <entry>Swedish Winter Time</entry>
715 <entry>WETDST</entry>
716 <entry>+01:00</entry>
717 <entry>Western European Daylight-Saving Time</entry>
722 <entry>Greenwich Mean Time</entry>
727 <entry>Universal Time</entry>
732 <entry>Universal Coordinated Time</entry>
737 <entry>Same as UTC</entry>
742 <entry>Same as UTC</entry>
747 <entry>Western European Time</entry>
751 <entry>-01:00</entry>
752 <entry>West Africa Time</entry>
756 <entry>-01:00</entry>
757 <entry>Fernando de Noronha Summer Time</entry>
761 <entry>-02:00</entry>
762 <entry>Fernando de Noronha Time</entry>
766 <entry>-02:00</entry>
767 <entry>Brasilia Summer Time</entry>
771 <entry>-02:30</entry>
772 <entry>Newfoundland Daylight-Saving Time</entry>
776 <entry>-03:00</entry>
777 <entry>Atlantic Daylight-Saving Time</entry>
781 <entry>-03:00</entry>
782 <entry>(unknown)</entry>
786 <entry>-03:00</entry>
787 <entry>Brasilia Time</entry>
791 <entry>-03:30</entry>
792 <entry>Newfoundland Standard Time</entry>
796 <entry>-03:30</entry>
797 <entry>Newfoundland Standard Time</entry>
801 <entry>-04:00</entry>
802 <entry>Atlantic Standard Time (Canada)</entry>
806 <entry>-04:00</entry>
807 <entry>Atlantic/Porto Acre Summer Time</entry>
811 <entry>-04:00</entry>
812 <entry>Eastern Daylight-Saving Time</entry>
817 <entry>-04:00</entry>
818 <entry>GMT +4 hours</entry>
823 <entry>-05:00</entry>
824 <entry>Atlantic/Porto Acre Standard Time</entry>
828 <entry>-05:00</entry>
829 <entry>Central Daylight-Saving Time</entry>
833 <entry>-05:00</entry>
834 <entry>Eastern Standard Time</entry>
839 <entry>-05:00</entry>
840 <entry>GMT +5 hours</entry>
845 <entry>-06:00</entry>
846 <entry>Central Standard Time</entry>
850 <entry>-06:00</entry>
851 <entry>Mountain Daylight-Saving Time</entry>
856 <entry>-06:00</entry>
857 <entry>GMT +6 hours</entry>
862 <entry>-07:00</entry>
863 <entry>Mountain Standard Time</entry>
867 <entry>-07:00</entry>
868 <entry>Pacific Daylight-Saving Time</entry>
872 <entry>-08:00</entry>
873 <entry>Alaska Daylight-Saving Time</entry>
877 <entry>-08:00</entry>
878 <entry>Pacific Standard Time</entry>
882 <entry>-08:00</entry>
883 <entry>Yukon Daylight-Saving Time</entry>
887 <entry>-09:00</entry>
888 <entry>Alaska Standard Time</entry>
892 <entry>-09:00</entry>
893 <entry>Hawaii/Alaska Daylight-Saving Time</entry>
897 <entry>-09:00</entry>
898 <entry>Yukon Standard Time</entry>
902 <entry>-09:30</entry>
903 <entry>Marquesas Time</entry>
907 <entry>-10:00</entry>
908 <entry>Alaska/Hawaii Standard Time</entry>
912 <entry>-10:00</entry>
913 <entry>Hawaii Standard Time</entry>
917 <entry>-10:00</entry>
918 <entry>Central Alaska Time</entry>
922 <entry>-11:00</entry>
923 <entry>Nome Time</entry>
927 <entry>-12:00</entry>
928 <entry>International Date Line, West</entry>
935 <title>Australian Time Zones</title>
938 There are three naming conflicts between Australian time zone
939 names and time zone names commonly used in North and South America:
940 <literal>ACST</literal>, <literal>CST</literal>, and
941 <literal>EST</literal>. If the run-time option
942 <varname>australian_timezones</varname> is set to true then
943 <literal>ACST</literal>, <literal>CST</literal>,
944 <literal>EST</literal>, and <literal>SAT</literal> are interpreted
945 as Australian time zone names, as shown in <xref
946 linkend="datetime-oztz-table">. If it is false (which is the
947 default), then <literal>ACST</literal>, <literal>CST</literal>,
948 and <literal>EST</literal> are taken as American time zone names,
949 and <literal>SAT</literal> is interpreted as a noise word
954 <table id="datetime-oztz-table">
955 <title>Australian Time Zone Abbreviations</title>
959 <entry>Time Zone</entry>
960 <entry>Offset from UTC</entry>
961 <entry>Description</entry>
967 <entry>+09:30</entry>
968 <entry>Central Australia Standard Time</entry>
972 <entry>+10:30</entry>
973 <entry>Australian Central Standard Time</entry>
977 <entry>+10:00</entry>
978 <entry>Australian Eastern Standard Time</entry>
982 <entry>+09:30</entry>
983 <entry>South Australian Standard Time</entry>
991 <sect1 id="datetime-units-history">
992 <title>History of Units</title>
995 The Julian Date was invented by the French scholar
996 Joseph Justus Scaliger (1540-1609)
997 and probably takes its name from the Scaliger's father,
998 the Italian scholar Julius Caesar Scaliger (1484-1558).
999 Astronomers have used the Julian period to assign a unique number to
1000 every day since 1 January 4713 BC. This is the so-called Julian Date
1001 (JD). JD 0 designates the 24 hours from noon UTC on 1 January 4713 BC
1002 to noon UTC on 2 January 4713 BC.
1006 The <quote>Julian Date</quote> is different from the <quote>Julian
1007 Calendar</quote>. The Julian calendar
1008 was introduced by Julius Caesar in 45 BC. It was in common use
1009 until the 1582, when countries started changing to the Gregorian
1010 calendar. In the Julian calendar, the tropical year is
1011 approximated as 365 1/4 days = 365.25 days. This gives an error of
1012 about 1 day in 128 years.
1016 The accumulating calendar error prompted
1017 Pope Gregory XIII to reform the calendar in accordance with
1018 instructions from the Council of Trent.
1019 In the Gregorian calendar, the tropical year is approximated as
1020 365 + 97 / 400 days = 365.2425 days. Thus it takes approximately 3300
1021 years for the tropical year to shift one day with respect to the
1026 The approximation 365+97/400 is achieved by having 97 leap years
1027 every 400 years, using the following rules:
1031 Every year divisible by 4 is a leap year.
1034 However, every year divisible by 100 is not a leap year.
1037 However, every year divisible by 400 is a leap year after all.
1041 So, 1700, 1800, 1900, 2100, and 2200 are not leap years. But 1600,
1042 2000, and 2400 are leap years.
1044 By contrast, in the older Julian calendar only years divisible by 4 are leap years.
1048 The papal bull of February 1582 decreed that 10 days should be dropped
1049 from October 1582 so that 15 October should follow immediately after
1051 This was observed in Italy, Poland, Portugal, and Spain. Other Catholic
1052 countries followed shortly after, but Protestant countries were
1053 reluctant to change, and the Greek orthodox countries didn't change
1054 until the start of the 20th century.
1056 The reform was observed by Great Britain and Dominions (including what is
1057 now the USA) in 1752.
1058 Thus 2 September 1752 was followed by 14 September 1752.
1060 This is why Unix systems have the <command>cal</command> program
1061 produce the following:
1064 $ <userinput>cal 9 1752</userinput>
1068 17 18 19 20 21 22 23
1069 24 25 26 27 28 29 30
1075 The SQL standard states that <quote>Within the definition of a
1076 <quote>datetime literal</quote>, the <quote>datetime
1077 value</quote>s are constrained by the natural rules for dates and
1078 times according to the Gregorian calendar</quote>. Dates between
1079 1752-09-03 and 1752-09-13, although eliminated in some countries
1080 by Papal fiat, conform to <quote>natural rules</quote> and are
1086 Different calendars have been developed in various parts of the
1087 world, many predating the Gregorian system.
1090 the beginnings of the Chinese calendar can be traced back to the 14th
1091 century BC. Legend has it that the Emperor Huangdi invented the
1092 calendar in 2637 BC.
1094 The People's Republic of China uses the Gregorian calendar
1095 for civil purposes. The Chinese calendar is used for determining
1101 <!-- Keep this comment at the end of the file
1106 sgml-minimize-attributes:nil
1107 sgml-always-quote-attributes:t
1110 sgml-parent-document:nil
1111 sgml-default-dtd-file:"./reference.ced"
1112 sgml-exposed-tags:nil
1113 sgml-local-catalogs:("/usr/lib/sgml/catalog")
1114 sgml-local-ecat-files:nil