バージョンごとのドキュメント一覧

8.5. 日付/時刻データ型 #

<title>Date/Time Types</title>

<productname>PostgreSQL</productname> supports the full set of <acronym>SQL</acronym> date and time types, shown in <xref linkend="datatype-datetime-table"/>. The operations available on these data types are described in <xref linkend="functions-datetime"/>. Dates are counted according to the Gregorian calendar, even in years before that calendar was introduced (see <xref linkend="datetime-units-history"/> for more information). PostgreSQLでは、表 8.9に示されているSQLの日付と時刻データ型のすべてがサポートされています。 これらのデータ型で利用できる演算については9.9で説明します。 グレゴリオ暦が導入されるより前の年であっても(B.6参照)、日付はグレゴリオ暦にしたがって計算されます。

表8.9 日付/時刻データ型

<title>Date/Time Types</title>
型名格納サイズ説明最遠の過去最遠の未来精度
timestamp [ (p) ] [ without time zone ]8 バイト日付と時刻両方(時間帯なし)4713 BC294276 AD1マイクロ秒
timestamp [ (p) ] with time zone8バイト日付と時刻両方、時間帯付き4713 BC294276 AD1マイクロ秒
date4バイト日付(時刻なし)4713 BC5874897 AD1日
time [ (p) ] [ without time zone ]8バイト時刻(日付なし)00:00:0024:00:001マイクロ秒
time [ (p) ] with time zone12 バイト時刻(日付なし)、時間帯付き00:00:00+155924:00:00-15591 マイクロ秒
interval [ fields ] [ (p) ]16バイト時間間隔-178000000年178000000年1マイクロ秒

注記

The SQL standard requires that writing just <type>timestamp</type> be equivalent to <type>timestamp without time zone</type>, and <productname>PostgreSQL</productname> honors that behavior. <type>timestamptz</type> is accepted as an abbreviation for <type>timestamp with time zone</type>; this is a <productname>PostgreSQL</productname> extension. 標準SQLでは、単なるtimestampという記述はtimestamp without time zoneと同じであることを要求します。 PostgreSQLはこれに準じます。 timestamp with time zonetimestamptzと省略することができますが、これはPostgreSQLの拡張です。

<type>time</type>, <type>timestamp</type>, and <type>interval</type> accept an optional precision value <replaceable>p</replaceable> which specifies the number of fractional digits retained in the seconds field. By default, there is no explicit bound on precision. The allowed range of <replaceable>p</replaceable> is from 0 to 6. timetimestampおよびintervalは秒フィールドに保有されている小数点以下の桁数を指定する精度値pをオプションで受け付けます。 デフォルトでは、精度についての明示的な限界はありません。 pの許容範囲は0から6です。

The <type>interval</type> type has an additional option, which is to restrict the set of stored fields by writing one of these phrases: intervalデータ型には追加のオプションがあり、以下の1つの語句を使用して格納されるフィールドの集合を制約します。

YEAR
MONTH
DAY
HOUR
MINUTE
SECOND
YEAR TO MONTH
DAY TO HOUR
DAY TO MINUTE
DAY TO SECOND
HOUR TO MINUTE
HOUR TO SECOND
MINUTE TO SECOND

Note that if both <replaceable>fields</replaceable> and <replaceable>p</replaceable> are specified, the <replaceable>fields</replaceable> must include <literal>SECOND</literal>, since the precision applies only to the seconds. fieldsおよびpが共に指定されると、精度は秒のみに適用されるので、fieldsSECONDを含まなければならないことに注意してください。

The type <type>time with time zone</type> is defined by the SQL standard, but the definition exhibits properties which lead to questionable usefulness. In most cases, a combination of <type>date</type>, <type>time</type>, <type>timestamp without time zone</type>, and <type>timestamp with time zone</type> should provide a complete range of date/time functionality required by any application. time with time zoneは標準SQLで定義されていますが、その定義は、その有用性を疑問視することになりかねない特性を示しています。 ほとんどの場合、datetimetimestamp without time zonetimestamp with time zoneの組み合わせで、すべてのアプリケーションで要求される日付/時刻機能すべてを提供しているはずです。

8.5.1. 日付/時刻の入力 #

<title>Date/Time Input</title>

Date and time input is accepted in almost any reasonable format, including ISO 8601, <acronym>SQL</acronym>-compatible, traditional <productname>POSTGRES</productname>, and others. For some formats, ordering of day, month, and year in date input is ambiguous and there is support for specifying the expected ordering of these fields. Set the <xref linkend="guc-datestyle"/> parameter to <literal>MDY</literal> to select month-day-year interpretation, <literal>DMY</literal> to select day-month-year interpretation, or <literal>YMD</literal> to select year-month-day interpretation. 日付と時刻の入力は、ISO 8601、SQL互換、伝統的なPOSTGRES、その他を含むほとんどの適正とみなされる書式を受け付けます。 一部の書式では日付の入力における日-月-年の順序が曖昧ですが、これらのフィールドの期待される順序を指定する方式が提供されています。 DateStyleパラメータをMDYに設定すれば、月日年という順で解釈され、DMYに設定すれば日月年という順で、YMDに設定すれば年月日という順で解釈されます。

<productname>PostgreSQL</productname> is more flexible in handling date/time input than the <acronym>SQL</acronym> standard requires. See <xref linkend="datetime-appendix"/> for the exact parsing rules of date/time input and for the recognized text fields including months, days of the week, and time zones. PostgreSQLは日付/時刻入力の取扱いにおいて標準SQLの要求よりも柔軟です。 日付/時刻の入力における厳密な構文解析規則と、月および週、そして時間帯を含む使用可能なテキストフィールドに関しては付録Bを参照してください。

Remember that any date or time literal input needs to be enclosed in single quotes, like text strings. Refer to <xref linkend="sql-syntax-constants-generic"/> for more information. <acronym>SQL</acronym> requires the following syntax 日付や時刻リテラルの入力では、テキスト文字列のように、単一引用符で囲む必要があることを思い出してください。 詳細は4.1.2.7を参照してください。 SQLでは下記の構文が要求されます。

type [ (p) ] 'value'

where <replaceable>p</replaceable> is an optional precision specification giving the number of fractional digits in the seconds field. Precision can be specified for <type>time</type>, <type>timestamp</type>, and <type>interval</type> types, and can range from 0 to 6. If no precision is specified in a constant specification, it defaults to the precision of the literal value (but not more than 6 digits). ここで、pは秒フィールドの小数点以下の桁数を与えるオプションの精度の指定です。 精度はtimetimestampおよびinterval型に対して0から6の範囲で設定できます。 値の許容範囲は既に説明しています。 定数指定において精度指定がない場合は、リテラル値の精度がデフォルトとして使われます(ただし、6桁を超えることはありません)。

8.5.1.1. 日付 #

<title>Dates</title>

<xref linkend="datatype-datetime-date-table"/> shows some possible inputs for the <type>date</type> type. 表 8.10date型で入力可能なものの一部を示します。

表8.10 日付入力

<title>Date Input</title>
説明
1999-01-08ISO 8601。すべてのモードで1月8日になります(推奨書式)。
January 8, 1999すべてのdatestyle入力モードにおいて曖昧さがありません。
1/8/1999MDYモードでは1月8日、DMYモードでは8月1日。
1/18/1999MDYモードでは1月18日、他のモードでは拒絶されます。
01/02/03 MDYモードでは2003年1月2日、DMYモードでは2003年2月1日、YMDモードでは2001年2月3日。
1999-Jan-08すべてのモードで1月8日になります。
Jan-08-1999すべてのモードで1月8日になります。
08-Jan-1999すべてのモードで1月8日になります。
99-Jan-08YMDモードで1月8日、他のモードではエラー。
08-Jan-991月8日。ただしYMDモードではエラー。
Jan-08-991月8日。ただしYMDモードではエラー。
19990108ISO 8601。すべてのモードで1999年1月8日になります。
990108ISO 8601。すべてのモードで1999年1月8日になります。
1999.008年と年間通算日
J2451187ユリウス日
January 8, 99 BC西暦紀元前99年

8.5.1.2. 時刻 #

<title>Times</title>

The time-of-day types are <type>time [ (<replaceable>p</replaceable>) ] without time zone</type> and <type>time [ (<replaceable>p</replaceable>) ] with time zone</type>. <type>time</type> alone is equivalent to <type>time without time zone</type>. ある一日の時刻を表す型はtime [(p) ] without time zonetime [ (p) ] with time zoneです。 time単独ではtime without time zoneと同じです。

Valid input for these types consists of a time of day followed by an optional time zone. (See <xref linkend="datatype-datetime-time-table"/> and <xref linkend="datatype-timezone-table"/>.) If a time zone is specified in the input for <type>time without time zone</type>, it is silently ignored. You can also specify a date but it will be ignored, except when you use a time zone name that involves a daylight-savings rule, such as <literal>America/New_York</literal>. In this case specifying the date is required in order to determine whether standard or daylight-savings time applies. The appropriate time zone offset is recorded in the <type>time with time zone</type> value. これらの型への有効な入力は、時刻、その後にオプションで時間帯からなります。 (表 8.11表 8.12を参照してください。) time without time zoneへの入力に時間帯が指定された場合、時間帯は警告なく無視されます。 また、日付を指定することもできますが、America/New_Yorkのような夏時間規則を含む時間帯名を使用しているのでなければ、それは無視されます。 夏時間規則のある時間帯名の場合は、標準と夏時間のどちらを適用するかを決定できるように、日付の指定が必要です。 適切な時間帯オフセットはtime with time zone型の値に記録されています。

表8.11 時刻入力

<title>Time Input</title>
説明
04:05:06.789ISO 8601
04:05:06ISO 8601
04:05ISO 8601
040506ISO 8601
04:05 AM04:05と同じ。AMは値に影響を与えない。
04:05 PM16:05と同じ。時の入力は12以下でなければなりません。
04:05:06.789-8ISO 8601, UTC オフセットとしてのタイムゾーン
04:05:06-08:00ISO 8601, UTC オフセットとしてのタイムゾーン
04:05-08:00ISO 8601, UTC オフセットとしてのタイムゾーン
040506-08ISO 8601, UTC オフセットとしてのタイムゾーン
040506+0730ISO 8601, UTC オフセットとしての小数付き時間のタイムゾーン
040506+07:30:00秒まで指定されたUTCオフセット(ISO 8601では未サポート)
04:05:06 PST省略形による時間帯の指定。
2003-04-12 04:05:06 America/New_York名前による時間帯の指定。

表8.12 時間帯入力

<title>Time Zone Input</title>
説明
PST省略形(米国太平洋標準時間)
America/New_York完全な時間帯名
PST8PDTPOSIX書式の時間帯指定
-8:00:00PSTのUTCオフセット
-8:00PSTのUTCオフセット(ISO 8601の拡張フォーマット)
-800PSTのUTCオフセット(ISO 8601の基本フォーマット)
-8PSTのUTCオフセット(ISO 8601の基本フォーマット)
zuluUTC用の軍事用略記
zzuluの略記(また、ISO 8601)

Refer to <xref linkend="datatype-timezones"/> for more information on how to specify time zones. 時間帯の指定方法に関する詳細は8.5.3を参照してください。

8.5.1.3. タイムスタンプ #

<title>Time Stamps</title>

Valid input for the time stamp types consists of the concatenation of a date and a time, followed by an optional time zone, followed by an optional <literal>AD</literal> or <literal>BC</literal>. (Alternatively, <literal>AD</literal>/<literal>BC</literal> can appear before the time zone, but this is not the preferred ordering.) Thus: タイムスタンプ型への有効な入力は、日付と時刻を連結し、さらにその後にオプションで時間帯、その後にオプションでADもしくはBCからなります。 (他にAD/BCを時間帯の前に付ける方法もありますが、これは推奨される順序ではありません。) したがって、

1999-01-08 04:05:06

and:

1999-01-08 04:05:06 -8:00

are valid values, which follow the <acronym>ISO</acronym> 8601 standard. In addition, the common format: は有効な値で、ISO 8601に準拠しています。 また、広く使用されている

January 8 04:05:06 1999 PST

is supported. という書式もサポートされます。

The <acronym>SQL</acronym> standard differentiates <type>timestamp without time zone</type> and <type>timestamp with time zone</type> literals by the presence of a <quote>+</quote> or <quote>-</quote> symbol and time zone offset after the time. Hence, according to the standard, 標準SQLでは、timestamp without time zoneのリテラルとtimestamp with time zoneのリテラルを、時刻の後の+もしくは-記号と時間帯補正の有無により区別します。 そのため、標準に従うと、

TIMESTAMP '2004-10-19 10:23:54'

is a <type>timestamp without time zone</type>, while timestamp without time zoneに、

TIMESTAMP '2004-10-19 10:23:54+02'

is a <type>timestamp with time zone</type>. <productname>PostgreSQL</productname> never examines the content of a literal string before determining its type, and therefore will treat both of the above as <type>timestamp without time zone</type>. To ensure that a literal is treated as <type>timestamp with time zone</type>, give it the correct explicit type: timestamp with time zoneになります。 PostgreSQLでは、その型を決める前に文字列リテラルの内容を検証しません。 そのため上の例はいずれもtimestamp without time zoneとして扱います。 リテラルが確実にtimestamp with time zoneとして扱われるようにするには、例えば、

TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'

In a literal that has been determined to be <type>timestamp without time zone</type>, <productname>PostgreSQL</productname> will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input value, and is not adjusted for time zone. のように正しい明示的な型を指定してください。 timestamp without time zoneと決定済みのリテラルでは、PostgreSQLは警告なく時間帯情報をすべて無視します。 つまり、結果の値は明示された入力値の日付/時刻フィールドから持ち込まれますが、時間帯の調整はなされません。

For <type>timestamp with time zone</type>, the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, <acronym>GMT</acronym>). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone. If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's <xref linkend="guc-timezone"/> parameter, and is converted to UTC using the offset for the <varname>timezone</varname> zone. timestamp with time zoneについて内部に格納されている値は常にUTCです(協定世界時、歴史的にグリニッジ標準時GMTとして知られています)。 時間帯が明示的に指定された入力値は、その時間帯に適したオフセットを使用してUTCに変換されます。 入力文字列に時間帯が指定されていない場合は、システムのTimeZoneパラメータに示されている値が時間帯とみなされ、timezone時間帯用のオフセットを使用してUTCに変換されます。

When a <type>timestamp with time zone</type> value is output, it is always converted from UTC to the current <varname>timezone</varname> zone, and displayed as local time in that zone. To see the time in another time zone, either change <varname>timezone</varname> or use the <literal>AT TIME ZONE</literal> construct (see <xref linkend="functions-datetime-zoneconvert"/>). timestamp with time zoneの値が出力されると、この値はUTCから現行のtimezoneに変換され、その時間帯のローカル時間として表示されます。 他の時間帯での時間を表示するには、timezoneを変更するか、あるいはAT TIME ZONE構文(9.9.4 を参照)を使用します。

Conversions between <type>timestamp without time zone</type> and <type>timestamp with time zone</type> normally assume that the <type>timestamp without time zone</type> value should be taken or given as <varname>timezone</varname> local time. A different time zone can be specified for the conversion using <literal>AT TIME ZONE</literal>. timestamp without time zonetimestamp with time zoneの間の変換では、通常timestamp without time zoneの値はtimezoneのローカル時間としてみなされる、または、指定されるものと想定されます。 AT TIME ZONEを使用する変換では、異なる時間帯を指定できます。

8.5.1.4. 特別な値 #

<title>Special Values</title>

<productname>PostgreSQL</productname> supports several special date/time input values for convenience, as shown in <xref linkend="datatype-datetime-special-table"/>. The values <literal>infinity</literal> and <literal>-infinity</literal> are specially represented inside the system and will be displayed unchanged; but the others are simply notational shorthands that will be converted to ordinary date/time values when read. (In particular, <literal>now</literal> and related strings are converted to a specific time value as soon as they are read.) All of these values need to be enclosed in single quotes when used as constants in SQL commands. PostgreSQLでは利便性のために、表 8.13に示されているような特別な日付/時刻入力値をサポートしています。 infinity-infinityの値は、特別にシステム内部で表現され、変更されずに表示されます。 他のものは、単に簡略化された表記で、読み込まれるときに通常の日付/時刻値に変換されます。 (特にnowとその関連文字列は読み込まれるとすぐにその時点の値に変換されます。) これらの値はすべて、SQLコマンドで定数として使う場合は、単一引用符でくくらなければなりません。

表8.13 特別な日付/時刻定数

<title>Special Date/Time Inputs</title>
入力文字列有効な型説明
epochdate, timestamp1970-01-01 00:00:00+00(Unixシステム時間におけるゼロ)
infinitydate, timestamp他のすべてのタイムスタンプより将来
-infinitydate, timestamp他のすべてのタイムスタンプより過去
nowdate, time, timestamp現トランザクションの開始時刻
todaydate, timestamp今日の午前0時
tomorrowdate, timestamp明日の午前0時
yesterdaydate, timestamp昨日の午前0時
allballstime00:00:00.00 UTC

The following <acronym>SQL</acronym>-compatible functions can also be used to obtain the current time value for the corresponding data type: <literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>, <literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>, <literal>LOCALTIMESTAMP</literal>. (See <xref linkend="functions-datetime-current"/>.) Note that these are SQL functions and are <emphasis>not</emphasis> recognized in data input strings. SQL互換の関数である、CURRENT_DATECURRENT_TIMECURRENT_TIMESTAMPLOCALTIMELOCALTIMESTAMPも、対応するデータ型の現在の日付または時間の値を取得するために使用できます。 (9.9.5 を参照してください。) これらはSQL関数であり、データ入力文字列として認識されないことに注意してください。

注意

While the input strings <literal>now</literal>, <literal>today</literal>, <literal>tomorrow</literal>, and <literal>yesterday</literal> are fine to use in interactive SQL commands, they can have surprising behavior when the command is saved to be executed later, for example in prepared statements, views, and function definitions. The string can be converted to a specific time value that continues to be used long after it becomes stale. Use one of the SQL functions instead in such contexts. For example, <literal>CURRENT_DATE + 1</literal> is safer than <literal>'tomorrow'::date</literal>. 入力する文字列としてnowtodaytomorrow及びyesterdayはインタラクティブなSQLコマンドの中で利用する時は良いですが、コマンドが保存され後に実行されるような時には驚く挙動になることがあります。例えば、準備された文、ビューや関数の定義です。 文字列は特定の時間の値に変換され、その値が古くなってからもしばらく使い続けられることがあります。 このような状況では、代わりにSQL関数を使用してください。 例えば、'tomorrow'::dateよりもCURRENT_DATE + 1の方が安全です。

8.5.2. 日付/時刻の出力 #

<title>Date/Time Output</title>

The output format of the date/time types can be set to one of the four styles ISO 8601, <acronym>SQL</acronym> (Ingres), traditional <productname>POSTGRES</productname> (Unix <application>date</application> format), or German. The default is the <acronym>ISO</acronym> format. (The <acronym>SQL</acronym> standard requires the use of the ISO 8601 format. The name of the <quote>SQL</quote> output format is a historical accident.) <xref linkend="datatype-datetime-output-table"/> shows examples of each output style. The output of the <type>date</type> and <type>time</type> types is generally only the date or time part in accordance with the given examples. However, the <productname>POSTGRES</productname> style outputs date-only values in <acronym>ISO</acronym> format. 日付/時刻型の出力書式は、ISO 8601、SQL(Ingres)、伝統的なPOSTGRES(Unix date書式)またはGermanの4つのいずれかに設定できます。 デフォルトはISO書式です。 (標準SQLではISO 8601書式の使用が定められています。 SQLという出力書式名は歴史的な事故です。) 表 8.14に各出力書式の例を示します。 datetimeの書式は、例にあるとおり、それぞれ日付と時刻の部分です。しかし、POSTGRESではISO書式の日付部分のみを出力します。(YMDやMDYの場合12-17-1997を返し、DMYの場合17-12-1997を返します。) However以下が原文のみだとわかりにくかったため http://www.postgresql.org/message-id/2598E90D3D534989905115C5C1172679@maumau より補足を追加

表8.14 日付/時刻の出力形式

<title>Date/Time Output Styles</title>
様式指定説明
ISOISO 8601, 標準SQL1997-12-17 07:37:16-08
SQL伝統的な様式12/17/1997 07:37:16.00 PST
Postgres独自の様式Wed Dec 17 07:37:16 1997 PST
German地域限定様式17.12.1997 07:37:16.00 PST

注記

ISO 8601 specifies the use of uppercase letter <literal>T</literal> to separate the date and time. <productname>PostgreSQL</productname> accepts that format on input, but on output it uses a space rather than <literal>T</literal>, as shown above. This is for readability and for consistency with <ulink url="https://tools.ietf.org/html/rfc3339">RFC 3339</ulink> as well as some other database systems. ISO 8601の仕様では日付と時刻を区切るために大文字のTを使用します。 PostgreSQLは入力ではこの書式を受け付けますが、上記のように出力ではTではなく空白を使用します。 これは読みやすさのため、そしてRFC3339や他のデータベースシステムとの整合性を保つためです。

In the <acronym>SQL</acronym> and POSTGRES styles, day appears before month if DMY field ordering has been specified, otherwise month appears before day. (See <xref linkend="datatype-datetime-input"/> for how this setting also affects interpretation of input values.) <xref linkend="datatype-datetime-output2-table"/> shows examples. SQLとPOSTGRESでは、DMYフィールド順が指定された場合は月の前に日が現れます。 指定がなければ日の前に月が現れます。 (この設定が入力値の解釈にどう影響を与えるのかについては8.5.1を参考にしてください)。 表 8.15に例を示します。

表8.15 日付の順序の慣習

<title>Date Order Conventions</title>
datestyleの設定入力の順序出力例
SQL, DMYday(日)/month(月)/year(年)17/12/1997 15:37:16.00 CET
SQL, MDYmonth(月)/day(日)/year(年)12/17/1997 07:37:16.00 PST
Postgres, DMYday(日)/month(月)/year(年)Wed 17 Dec 07:37:16 1997 PST

In the <acronym>ISO</acronym> style, the time zone is always shown as a signed numeric offset from UTC, with positive sign used for zones east of Greenwich. The offset will be shown as <replaceable>hh</replaceable> (hours only) if it is an integral number of hours, else as <replaceable>hh</replaceable>:<replaceable>mm</replaceable> if it is an integral number of minutes, else as <replaceable>hh</replaceable>:<replaceable>mm</replaceable>:<replaceable>ss</replaceable>. (The third case is not possible with any modern time zone standard, but it can appear when working with timestamps that predate the adoption of standardized time zones.) In the other date styles, the time zone is shown as an alphabetic abbreviation if one is in common use in the current zone. Otherwise it appears as a signed numeric offset in ISO 8601 basic format (<replaceable>hh</replaceable> or <replaceable>hhmm</replaceable>). ISO形式ではタイムゾーンはUTCからの符号付きの数値で表現され、グリニッジより東の地域では正の符号が使用されます。 オフセットは時間の整数倍であればhh(時間のみ)、分の整数倍であればhh:mm、それ以外の場合は、hh:mm:ssで表現されます(三番目のケースは現代のどのタイムゾーンの標準でもありえませんが、タイムゾーンが標準化される前に適用されたタイムスタンプで動いている場合に現れる可能性があります)。 他の日付スタイルでは、現在の地域で一般的に使われている場合、タイムゾーンは省略形で表現されます。 それ以外の場合はISO 8601の基本フォーマット(hhhhmm)の符号付き数値のオフセットとして表示されます。

The date/time style can be selected by the user using the <command>SET datestyle</command> command, the <xref linkend="guc-datestyle"/> parameter in the <filename>postgresql.conf</filename> configuration file, or the <envar>PGDATESTYLE</envar> environment variable on the server or client. ユーザはSET DATESTYLEコマンド、postgresql.conf構成ファイルのDateStyleパラメータ、そしてサーバかクライアントのPGDATESTYLE環境変数を使用して、日付/時刻の様式を選択することができます。

The formatting function <function>to_char</function> (see <xref linkend="functions-formatting"/>) is also available as a more flexible way to format date/time output. 日付/時刻出力のより柔軟な書式設定方法として、書式設定関数to_char9.8を参照)を使用することもできます。

8.5.3. 時間帯 #

<title>Time Zones</title>

Time zones, and time-zone conventions, are influenced by political decisions, not just earth geometry. Time zones around the world became somewhat standardized during the 1900s, but continue to be prone to arbitrary changes, particularly with respect to daylight-savings rules. <productname>PostgreSQL</productname> uses the widely-used IANA (Olson) time zone database for information about historical time zone rules. For times in the future, the assumption is that the latest known rules for a given time zone will continue to be observed indefinitely far into the future. 時間帯および時間帯の取り決めは地球の幾何学的要素のみでなく政治的決定に影響されます。 世界にまたがる時間帯は1900年代に標準化されたようですが、特に夏時間規則の点で、勝手に変更する傾向が続いています。 PostgreSQLは歴史的な時間帯ルールについての情報に、広く使われているIANA時間帯データベースを使用します。 将来の時間は、ある与えられた時間帯に対する最新の既知のルールが、将来長きに渡りそのまま遵守が継続されるということを前提としています。

<productname>PostgreSQL</productname> endeavors to be compatible with the <acronym>SQL</acronym> standard definitions for typical usage. However, the <acronym>SQL</acronym> standard has an odd mix of date and time types and capabilities. Two obvious problems are: PostgreSQLは典型的な使用法については標準SQLへの互換性に対し最大限の努力をしています。 しかし、標準SQLには、日付と時刻のデータ型と機能に関する混乱が見受けられます。 2つの明らかな問題点を以下に示します。

  • Although the <type>date</type> type cannot have an associated time zone, the <type>time</type> type can. Time zones in the real world have little meaning unless associated with a date as well as a time, since the offset can vary through the year with daylight-saving time boundaries. date型にはそれに関連する時間帯を持てませんが、time型にはあります。 現実の世界において、時間帯のオフセットが夏時間への切り替えにより年間を通じて変化することから、時刻と同様に日付もそれに結び付けられていないと意味がありません。

  • The default time zone is specified as a constant numeric offset from <acronym>UTC</acronym>. It is therefore impossible to adapt to daylight-saving time when doing date/time arithmetic across <acronym>DST</acronym> boundaries. デフォルトの時間帯はUTCからの整数定数オフセットとして指定されています。 したがってDST(夏時間)への切り替えをまたいで日付/時刻演算を行う場合、夏時間を適用することは不可能です。

To address these difficulties, we recommend using date/time types that contain both date and time when using time zones. We do <emphasis>not</emphasis> recommend using the type <type>time with time zone</type> (though it is supported by <productname>PostgreSQL</productname> for legacy applications and for compliance with the <acronym>SQL</acronym> standard). <productname>PostgreSQL</productname> assumes your local time zone for any type containing only date or time. このような問題を解決するためには、時間帯を使用する際に日付と時刻の両方を保持できる日付/時刻データ型を使用することを勧めます。 time with time zone型の使用はお勧めしません (もっともPostgreSQLでは、旧式のアプリケーションや、標準SQLとの互換性のために、time with time zone型の使用をサポートしています)。 PostgreSQLは、日付または時刻のみを保持するデータ型のすべては使用中の時間帯であると前提しています。

All timezone-aware dates and times are stored internally in <acronym>UTC</acronym>. They are converted to local time in the zone specified by the <xref linkend="guc-timezone"/> configuration parameter before being displayed to the client. すべての時間帯付きの日付と時刻はUTCで内部的に保存されます。 これらはクライアントに表示される前にTimeZone設定パラメータで指定された時間帯におけるローカル時間に変換されます。

<productname>PostgreSQL</productname> allows you to specify time zones in three different forms: PostgreSQLでは、3つの形式で時間帯を指定することができます。

  • A full time zone name, for example <literal>America/New_York</literal>. The recognized time zone names are listed in the <literal>pg_timezone_names</literal> view (see <xref linkend="view-pg-timezone-names"/>). <productname>PostgreSQL</productname> uses the widely-used IANA time zone data for this purpose, so the same time zone names are also recognized by other software. America/New_Yorkなどの完全な時間帯名称。 認識できる時間帯名称はpg_timezone_namesビューに列挙されています(54.32を参照してください)。 PostgreSQLはこの目的のためによく使用されているIANA時間帯データを使用します。 したがって、他のソフトウェアでも同じ名前が認識されます。

  • A time zone abbreviation, for example <literal>PST</literal>. Such a specification merely defines a particular offset from UTC, in contrast to full time zone names which can imply a set of daylight savings transition rules as well. The recognized abbreviations are listed in the <literal>pg_timezone_abbrevs</literal> view (see <xref linkend="view-pg-timezone-abbrevs"/>). You cannot set the configuration parameters <xref linkend="guc-timezone"/> or <xref linkend="guc-log-timezone"/> to a time zone abbreviation, but you can use abbreviations in date/time input values and with the <literal>AT TIME ZONE</literal> operator. PSTなどの時間帯省略形。 こうした指定は、単に特定のUTCからのオフセットを定義します。 一方、完全な時間帯名称では夏時間遷移規則群も組み込まれます。 認識可能な省略形はpg_timezone_abbrevsビューに列挙されています(54.31を参照してください)。 TimeZone設定パラメータおよびlog_timezone設定パラメータを時間帯省略形に設定することはできませんが、日付時刻型の入力値やAT TIME ZONE演算子に省略形を使用することができます。

  • In addition to the timezone names and abbreviations, <productname>PostgreSQL</productname> will accept POSIX-style time zone specifications, as described in <xref linkend="datetime-posix-timezone-specs"/>. This option is not normally preferable to using a named time zone, but it may be necessary if no suitable IANA time zone entry is available. 時間帯名やその省略形に加え、PostgreSQLは、B.5に記載されているPOSIX様式の時間帯指定を受付けます。 このオプションは通常、名前付きのタイムゾーンを使用するよりも好ましくありませんが、適切なIANAタイムゾーンのエントリが利用できない場合は必要になるかもしれません。

In short, this is the difference between abbreviations and full names: abbreviations represent a specific offset from UTC, whereas many of the full names imply a local daylight-savings time rule, and so have two possible UTC offsets. As an example, <literal>2014-06-04 12:00 America/New_York</literal> represents noon local time in New York, which for this particular date was Eastern Daylight Time (UTC-4). So <literal>2014-06-04 12:00 EDT</literal> specifies that same time instant. But <literal>2014-06-04 12:00 EST</literal> specifies noon Eastern Standard Time (UTC-5), regardless of whether daylight savings was nominally in effect on that date. 一言で言うと、これは省略形と正式名称との差異です。 省略形はUTCから固定したオフセットを表わすのに対して、多くの正式名称はローカルの夏時間規定を暗示するので、2つのUTCオフセットがあるかもしれません。 例えば2014-06-04 12:00 America/New_Yorkはニューヨークの正午を示しますが、これはこの日について言えば東部夏時間(UTC-4)です。 つまり2014-06-04 12:00 EDTはこれと同時刻を示します。 しかし、2014-06-04 12:00 ESTは、その日に夏時間が使用されていたかどうかに関わらず、東部標準時間(UTC-5)での正午を示します。

To complicate matters, some jurisdictions have used the same timezone abbreviation to mean different UTC offsets at different times; for example, in Moscow <literal>MSK</literal> has meant UTC+3 in some years and UTC+4 in others. <productname>PostgreSQL</productname> interprets such abbreviations according to whatever they meant (or had most recently meant) on the specified date; but, as with the <literal>EST</literal> example above, this is not necessarily the same as local civil time on that date. 問題を更に複雑にしているのは、一部の管轄は同じ略号を使って、年によって異なるUTCオフセットを表していることです。 例えばモスクワではMSKはある年ではUTC+3を意味しますが、別の年ではUTC+4を意味します。 PostgreSQLではそのような略号について、指定の日に何を意味していたか(あるいは最も最近にどういう意味だったか)に従って解釈します。 しかし、ESTの例にあるように、必ずしもその日付における地方常用時を示しているとは限りません。

In all cases, timezone names and abbreviations are recognized case-insensitively. (This is a change from <productname>PostgreSQL</productname> versions prior to 8.2, which were case-sensitive in some contexts but not others.) すべての場合において、時間帯名や略号は大文字小文字の区別なく認識されます。 (これはPostgreSQLの8.2より前のバージョンからの変更です。 以前は、文脈によって大文字小文字が区別される場合と、されない場合がありました。)

Neither timezone names nor abbreviations are hard-wired into the server; they are obtained from configuration files stored under <filename>.../share/timezone/</filename> and <filename>.../share/timezonesets/</filename> of the installation directory (see <xref linkend="datetime-config-files"/>). 時間帯名、省略形のどちらもサーバ内に組み込まれるわけではありません。 インストールディレクトリの.../share/timezone/および.../share/timezonesets/の下に保存される構成ファイルから取得されます(B.4を参照ください)。

The <xref linkend="guc-timezone"/> configuration parameter can be set in the file <filename>postgresql.conf</filename>, or in any of the other standard ways described in <xref linkend="runtime-config"/>. There are also some special ways to set it: TimeZonepostgresql.confファイルや第20章で説明する他の標準的な方法で設定することができます。 以下に、いくつか特別な設定方法を示します。

  • The <acronym>SQL</acronym> command <command>SET TIME ZONE</command> sets the time zone for the session. This is an alternative spelling of <command>SET TIMEZONE TO</command> with a more SQL-spec-compatible syntax. SQLコマンドSET TIME ZONEはセッションの時間帯を設定します。 これはSET TIMEZONE TOの別名ですが、SQL仕様の構文へのより高い互換性があります。

  • The <envar>PGTZ</envar> environment variable is used by <application>libpq</application> clients to send a <command>SET TIME ZONE</command> command to the server upon connection. PGTZ環境変数は、libpqクライアントが接続時にサーバにSET TIME ZONEコマンドを送信するために用いられます。

8.5.4. 時間間隔の入力 #

<title>Interval Input</title>

<type>interval</type> values can be written using the following verbose syntax: interval値は以下の冗長な構文を使って記述されます。

[@] quantity unit [quantity unit...] [direction]

where <replaceable>quantity</replaceable> is a number (possibly signed); <replaceable>unit</replaceable> is <literal>microsecond</literal>, <literal>millisecond</literal>, <literal>second</literal>, <literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>, <literal>week</literal>, <literal>month</literal>, <literal>year</literal>, <literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>, or abbreviations or plurals of these units; <replaceable>direction</replaceable> can be <literal>ago</literal> or empty. The at sign (<literal>@</literal>) is optional noise. The amounts of the different units are implicitly added with appropriate sign accounting. <literal>ago</literal> negates all the fields. This syntax is also used for interval output, if <xref linkend="guc-intervalstyle"/> is set to <literal>postgres_verbose</literal>. ここで、quantityは(符号付き)時間量、unit(単位)はmicrosecondmillisecondsecond(秒)、minute(分)、hour(時)、day(日)、week(週)、month(月)、year(年)、decade(10年単位)、century(100年単位)、millennium(1000年単位)あるいはこれらの単位の簡略形または複数形です。 direction(方向)はagoもしくは空です。 アットマーク(@)はオプションで、付けても付けなくても構いません。 異なる単位における時間量は適切に符号を考慮して暗黙的に足されます。 agoはすべてのフィールドの正負を逆にします。 この構文はまた、IntervalStylepostgres_verboseに設定されている場合に時間間隔の出力でも使用されます。

Quantities of days, hours, minutes, and seconds can be specified without explicit unit markings. For example, <literal>'1 12:59:10'</literal> is read the same as <literal>'1 day 12 hours 59 min 10 sec'</literal>. Also, a combination of years and months can be specified with a dash; for example <literal>'200-10'</literal> is read the same as <literal>'200 years 10 months'</literal>. (These shorter forms are in fact the only ones allowed by the <acronym>SQL</acronym> standard, and are used for output when <varname>IntervalStyle</varname> is set to <literal>sql_standard</literal>.) 日、時、分、および秒の時間量は明示的に単位を指定しなくても構いません。 例えば、'1 12:59:10''1 day 12 hours 59 min 10 sec'(1日と12時間59分10秒)と解釈されます。 また年と月の組み合わせはダッシュを使って指定することができます。 例えば、'200-10''200 years 10 months'(200年と10か月)と解釈されます。 (実際のところ、標準SQLで許されている簡略形はこれらだけです。 そしてIntervalStylesql_standardに設定されている場合には、これらが出力で使用されます。)

Interval values can also be written as ISO 8601 time intervals, using either the <quote>format with designators</quote> of the standard's section 4.4.3.2 or the <quote>alternative format</quote> of section 4.4.3.3. The format with designators looks like this: 標準の4.4.3.2節の指定文字付書式または4.4.3.3節の代替書式のどちらかを使用して、時間間隔値はISO 8601時間間隔として書くこともできます。 指定文字付の書式は以下のようなものです。

P quantity unit [ quantity unit ...] [ T [ quantity unit ...]]

The string must start with a <literal>P</literal>, and may include a <literal>T</literal> that introduces the time-of-day units. The available unit abbreviations are given in <xref linkend="datatype-interval-iso8601-units"/>. Units may be omitted, and may be specified in any order, but units smaller than a day must appear after <literal>T</literal>. In particular, the meaning of <literal>M</literal> depends on whether it is before or after <literal>T</literal>. 文字列はPで始まらなければならず、また、日と時間を区切るTを含めることができます。 利用可能な単位の省略形を表 8.16に示します。 単位は省略しても構いませんし、任意の順番で指定できますが、1日より小さな単位はTの後に書かなければなりません。 特にMの意味はTの前にあるか後にあるかに依存します。

表8.16 ISO 8601における時間間隔単位の省略形

<title>ISO 8601 Interval Unit Abbreviations</title>
省略形意味
Y
M月(日付部分における)
W
D
H時間
M分(時刻部分における)
S

In the alternative format: 別の書式を示します。

P [ years-months-days ] [ T hours:minutes:seconds ]

the string must begin with <literal>P</literal>, and a <literal>T</literal> separates the date and time parts of the interval. The values are given as numbers similar to ISO 8601 dates. 上の代替書式では、文字列はPから始まらなければなりません。 そして、Tは時間間隔の日付部分と時刻部分とを分割します。 値はISO 8601日付と同様の数字で指定されます。

When writing an interval constant with a <replaceable>fields</replaceable> specification, or when assigning a string to an interval column that was defined with a <replaceable>fields</replaceable> specification, the interpretation of unmarked quantities depends on the <replaceable>fields</replaceable>. For example <literal>INTERVAL '1' YEAR</literal> is read as 1 year, whereas <literal>INTERVAL '1'</literal> means 1 second. Also, field values <quote>to the right</quote> of the least significant field allowed by the <replaceable>fields</replaceable> specification are silently discarded. For example, writing <literal>INTERVAL '1 day 2:03:04' HOUR TO MINUTE</literal> results in dropping the seconds field, but not the day field. fields指定を使って時間間隔定数を記述する場合、または、fields仕様で定義された時間間隔列に文字列を割り当てる場合、マークされていない時間量の解釈はfieldsに依存します。 例えばINTERVAL '1' YEARは1年と解釈され、一方でINTERVAL '1'は1秒と解釈されます。 同時に、fields仕様によって許可される最下位フィールドの右側のフィールド値は警告なしに破棄されます。 例えば、INTERVAL '1 day 2:03:04' HOUR TO MINUTEと書くことで、二番目のフィールドは削除されますが、日付フィールドは削除されません。

According to the <acronym>SQL</acronym> standard all fields of an interval value must have the same sign, so a leading negative sign applies to all fields; for example the negative sign in the interval literal <literal>'-1 2:03:04'</literal> applies to both the days and hour/minute/second parts. <productname>PostgreSQL</productname> allows the fields to have different signs, and traditionally treats each field in the textual representation as independently signed, so that the hour/minute/second part is considered positive in this example. If <varname>IntervalStyle</varname> is set to <literal>sql_standard</literal> then a leading sign is considered to apply to all fields (but only if no additional signs appear). Otherwise the traditional <productname>PostgreSQL</productname> interpretation is used. To avoid ambiguity, it's recommended to attach an explicit sign to each field if any field is negative. 標準SQLに従うと、時間間隔値のフィールドはすべて同じ符号を持たなければなりません。 このため、先頭の負の符号はすべてのフィールドに適用されます。 例えば時間間隔リテラル'-1 2:03:04'の負の符号は、日付部分にも時、分、秒部分にも適用されます。 PostgreSQLではフィールドに異なる符号を持たせることができます。 また伝統的にテキスト形式表現における各フィールドは独立した符号を持つものとして扱われます。 このため、この例では時、分、秒部分は正であるとみなされます。 IntervalStylesql_standardに設定されている場合、先頭の符号はすべてのフィールドに適用されるものとみなされます(ただし他に符号がない場合のみです)。 さもなくば、伝統的なPostgreSQLの解釈が使用されます。 あいまいさを防ぐために、負のフィールドがある場合には個別に明示的な符号を付けることを勧めます。

Field values can have fractional parts: for example, <literal>'1.5 weeks'</literal> or <literal>'01:02:03.45'</literal>. However, because interval internally stores only three integer units (months, days, microseconds), fractional units must be spilled to smaller units. Fractional parts of units greater than months are rounded to be an integer number of months, e.g. <literal>'1.5 years'</literal> becomes <literal>'1 year 6 mons'</literal>. Fractional parts of weeks and days are computed to be an integer number of days and microseconds, assuming 30 days per month and 24 hours per day, e.g., <literal>'1.75 months'</literal> becomes <literal>1 mon 22 days 12:00:00</literal>. Only seconds will ever be shown as fractional on output. フィールドには'1.5weeks''01:02:03.45'のような小数部分を持つことができます。 しかし、intervalは内部的に3つの整数型の位(月、日、マイクロ秒)でのみ格納されるため、小数の位はより小さな位で埋められていく必要があります。 月より大きな小数部分の位は月の整数に丸められます。例えば、'1.5 years''1 year 6 mons'になります。 週と日の小数部分は、1ヶ月は30日、1日は24時間と想定して整数の日数やマイクロ秒として計算されます。例えば'1.75 months'1 mon 22 days 12:00:00になります。 出力では秒だけが小数として表示されます。

<xref linkend="datatype-interval-input-examples"/> shows some examples of valid <type>interval</type> input. 表 8.17は有効なinterval入力のいくつかの例を示しています。

表8.17 時間間隔入力

<title>Interval Input</title>
説明
1-2標準SQL書式。1年2ヶ月
3 4:05:06標準SQL書式。3日4時間5分6秒
1 year 2 months 3 days 4 hours 5 minutes 6 seconds伝統的Postgres書式。1年2月3日4時間5分6秒
P1Y2M3DT4H5M6SISO 8601 指定文字付き書式。意味は上と同じ
P0001-02-03T04:05:06ISO 8601 代替書式。意味は上と同じ

Internally <type>interval</type> values are stored as months, days, and microseconds. This is done because the number of days in a month varies, and a day can have 23 or 25 hours if a daylight savings time adjustment is involved. The months and days fields are integers while the microseconds field can store fractional seconds. Because intervals are usually created from constant strings or <type>timestamp</type> subtraction, this storage method works well in most cases, but can cause unexpected results: 内部的にintervalの値は月、日、マイクロ秒として格納されます。 これはひと月の日数は変化し、一日も夏時間への切り替えで23時間もしくは25時間になりうるからです。 マイクロ秒の桁は小数で格納できますが、月、日の桁は整数です。 時間の間隔は通常、文字列定数もしくはtimestampの引き算で生成されるため、この格納方式はほとんどのケースではうまくいきますが、予期しない結果を返すこともあります。 例:

SELECT EXTRACT(hours from '80 minutes'::interval);
 date_part
-----------
         1

SELECT EXTRACT(days from '80 hours'::interval);
 date_part
-----------
         0

Functions <function>justify_days</function> and <function>justify_hours</function> are available for adjusting days and hours that overflow their normal ranges. justify_days関数とjustify_hours関数は範囲から溢れた日と時間の補正に役立ちます。

8.5.5. 時間間隔の出力 #

<title>Interval Output</title>

The output format of the interval type can be set to one of the four styles <literal>sql_standard</literal>, <literal>postgres</literal>, <literal>postgres_verbose</literal>, or <literal>iso_8601</literal>, using the command <literal>SET intervalstyle</literal>. The default is the <literal>postgres</literal> format. <xref linkend="interval-style-output-table"/> shows examples of each output style. 時間間隔型の出力書式は、SET intervalstyleコマンドを使用して、sql_standardpostgrespostgres_verboseまたはiso_8601の4つのうちの1つを設定できます。 デフォルトはpostgres書式です。 表 8.18はそれぞれの出力形式を示した例です。

The <literal>sql_standard</literal> style produces output that conforms to the SQL standard's specification for interval literal strings, if the interval value meets the standard's restrictions (either year-month only or day-time only, with no mixing of positive and negative components). Otherwise the output looks like a standard year-month literal string followed by a day-time literal string, with explicit signs added to disambiguate mixed-sign intervals. sql_standard形式は、時間間隔値が標準制約(構成要素に正負が混在していない年数と月数のみ、または日数と時間のみ)を満足する場合、時間間隔リテラル文字列に対し標準SQLに準拠する出力を作成します。 それ以外の場合、出力は、標準的な年数-月数のリテラル文字列の後に日数-時間のリテラル文字列が続いたものになり、正負混在した時間間隔のあいまいさを無くすために明示的な符号が付加されます。

The output of the <literal>postgres</literal> style matches the output of <productname>PostgreSQL</productname> releases prior to 8.4 when the <xref linkend="guc-datestyle"/> parameter was set to <literal>ISO</literal>. postgres書式の出力は、DateStyleパラメータがISOに設定されたとき、PostgreSQL 8.4より前のリリースと一致します。

The output of the <literal>postgres_verbose</literal> style matches the output of <productname>PostgreSQL</productname> releases prior to 8.4 when the <varname>DateStyle</varname> parameter was set to non-<literal>ISO</literal> output. postgres_verbose書式の出力は、DateStyleパラメータがISO以外に設定されたとき、PostgreSQL 8.4より前のリリースと一致します。

The output of the <literal>iso_8601</literal> style matches the <quote>format with designators</quote> described in section 4.4.3.2 of the ISO 8601 standard. iso_8601書式の出力はISO 8601 標準の4.4.3.2節に記述のformat with designators(指名付き書式)に一致します。

表8.18 時間間隔出力形式の例

<title>Interval Output Style Examples</title>
形式指定年-月時間間隔日-時刻時間間隔混在した時間間隔
sql_standard1-23 4:05:06-1-2 +3 -4:05:06
postgres1 year 2 mons3 days 04:05:06-1 year -2 mons +3 days -04:05:06
postgres_verbose@ 1 year 2 mons@ 3 days 4 hours 5 mins 6 secs@ 1 year 2 mons -3 days 4 hours 5 mins 6 secs ago
iso_8601P1Y2MP3DT4H5M6SP-1Y-2M3D​T-4H-5M-6S