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

9.9. 日付/時刻関数と演算子 #

<title>Date/Time Functions and Operators</title>

<xref linkend="functions-datetime-table"/> shows the available functions for date/time value processing, with details appearing in the following subsections. <xref linkend="operators-datetime-table"/> illustrates the behaviors of the basic arithmetic operators (<literal>+</literal>, <literal>*</literal>, etc.). For formatting functions, refer to <xref linkend="functions-formatting"/>. You should be familiar with the background information on date/time data types from <xref linkend="datatype-datetime"/>. 表 9.33は、日付/時刻型の値の処理で使用可能な関数を示しています。詳細は、以下の副節で説明します。 表 9.32は、(+*等の)基本的な算術演算子の振舞いを説明しています。 書式設定関数については9.8を参照してください。 8.5を参照して、日付/時刻データ型についての背景となっている情報に精通していなければなりません。

In addition, the usual comparison operators shown in <xref linkend="functions-comparison-op-table"/> are available for the date/time types. Dates and timestamps (with or without time zone) are all comparable, while times (with or without time zone) and intervals can only be compared to other values of the same data type. When comparing a timestamp without time zone to a timestamp with time zone, the former value is assumed to be given in the time zone specified by the <xref linkend="guc-timezone"/> configuration parameter, and is rotated to UTC for comparison to the latter value (which is already in UTC internally). Similarly, a date value is assumed to represent midnight in the <varname>TimeZone</varname> zone when comparing it to a timestamp. 加えて表 9.1で示す通常の比較演算子が日付/時刻型で利用できます。 日付とタイムスタンプ(時間帯付きあるいは時間帯なし)はすべて互換性がありますが、時刻(時間帯付きあるいは時間帯なし)と時間間隔は同じデータ型の値同士だけが比較可能です。 時間帯なしのタイムスタンプと時間帯付きのタイムスタンプを比較する際には、前者の値はTimeZone設定パラメータで指定された時間帯にあるものと仮定され、後者の値(すでに内部的にはUTCです)と比較するためにUTCに変換されます。 同様に、タイムスタンプと比較する際には、日付の値はTimeZone時間帯の午前零時であると見なされます。

All the functions and operators described below that take <type>time</type> or <type>timestamp</type> inputs actually come in two variants: one that takes <type>time with time zone</type> or <type>timestamp with time zone</type>, and one that takes <type>time without time zone</type> or <type>timestamp without time zone</type>. For brevity, these variants are not shown separately. Also, the <literal>+</literal> and <literal>*</literal> operators come in commutative pairs (for example both <type>date</type> <literal>+</literal> <type>integer</type> and <type>integer</type> <literal>+</literal> <type>date</type>); we show only one of each such pair. 以下のtimeもしくはtimestamp型の入力を受け取る関数および演算子は全て、実際には2つの種類があります。1つはtime with time zone型またはtimestamp with time zone型を取るもので、もう1つはtime without time zone型もしくはtimestamp without time zone型を取るものです。 簡略化のため、これらの種類の違いは個別に示していません。 また、+*演算子は可換な2項をとります(例えばdate + integerinteger + date)。こうした組み合わせは片方のみ示します。

表9.32 日付/時刻演算子

<title>Date/Time Operators</title>

Operator 演算子

Description 説明

Example(s)

date + integerdate

Add a number of days to a date 日付に日数を加算

date '2001-09-28' + 72001-10-05

date + intervaltimestamp

Add an interval to a date 時刻間隔を日付に加算

date '2001-09-28' + interval '1 hour'2001-09-28 01:00:00

date + timetimestamp

Add a time-of-day to a date 日付に時刻を加算

date '2001-09-28' + time '03:00'2001-09-28 03:00:00

interval + intervalinterval

Add intervals 時間間隔を加算

interval '1 day' + interval '1 hour'1 day 01:00:00

timestamp + intervaltimestamp

Add an interval to a timestamp 時間間隔をタイムスタンプに加算

timestamp '2001-09-28 01:00' + interval '23 hours'2001-09-29 00:00:00

time + intervaltime

Add an interval to a time 時間間隔を時分に加算

time '01:00' + interval '3 hours'04:00:00

- intervalinterval

Negate an interval 時間間隔の符号を反転

- interval '23 hours'-23:00:00

date - dateinteger

Subtract dates, producing the number of days elapsed 日付を減算し、経過日数を返す

date '2001-10-01' - date '2001-09-28'3

date - integerdate

Subtract a number of days from a date 日付から日数を減算

date '2001-10-01' - 72001-09-24

date - intervaltimestamp

Subtract an interval from a date 日付から時間間隔を減算

date '2001-09-28' - interval '1 hour'2001-09-27 23:00:00

time - timeinterval

Subtract times 時分を減算

time '05:00' - time '03:00'02:00:00

time - intervaltime

Subtract an interval from a time 時分から時刻間隔を減算

time '05:00' - interval '2 hours'03:00:00

timestamp - intervaltimestamp

Subtract an interval from a timestamp タイムスタンプから時刻間隔を減算

timestamp '2001-09-28 23:00' - interval '23 hours'2001-09-28 00:00:00

interval - intervalinterval

Subtract intervals 時間間隔を減算

interval '1 day' - interval '1 hour'1 day -01:00:00

timestamp - timestampinterval

Subtract timestamps (converting 24-hour intervals into days, similarly to <function>justify_hours()</function>) タイムスタンプを減算(justify_hours()と同様に24時間間隔を日数に変換)

timestamp '2001-09-29 03:00' - timestamp '2001-07-27 12:00'63 days 15:00:00

interval * double precisioninterval

Multiply an interval by a scalar 時間間隔にスカラーを乗算

interval '1 second' * 90000:15:00

interval '1 day' * 2121 days

interval '1 hour' * 3.503:30:00

interval / double precisioninterval

Divide an interval by a scalar 時間間隔をスカラーで除算

interval '1 hour' / 1.500:40:00


表9.33 日付/時刻関数演算子

<title>Date/Time Functions</title>

Function 関数

Description 説明

Example(s)

age ( timestamp, timestamp ) → interval

Subtract arguments, producing a <quote>symbolic</quote> result that uses years and months, rather than just days 引数間の減算。日数だけでなく年と月を使用した言葉による結果を生成

age(timestamp '2001-04-10', timestamp '1957-06-13')43 years 9 mons 27 days

age ( timestamp ) → interval

Subtract argument from <function>current_date</function> (at midnight) current_date(午前零時時点)から引数を減算

age(timestamp '1957-06-13')62 years 6 mons 10 days

clock_timestamp ( ) → timestamp with time zone

Current date and time (changes during statement execution); see <xref linkend="functions-datetime-current"/> 現在の日付と時刻(文実行中に変化する)。9.9.5を参照。

clock_timestamp()2019-12-23 14:39:53.662522-05

current_datedate

Current date; see <xref linkend="functions-datetime-current"/> 現在の日付。9.9.5を参照

current_date2019-12-23

current_timetime with time zone

Current time of day; see <xref linkend="functions-datetime-current"/> 現在の時刻。9.9.5を参照。

current_time14:39:53.662522-05

current_time ( integer ) → time with time zone

Current time of day, with limited precision; see <xref linkend="functions-datetime-current"/> 精度を限定した現在の時刻。9.9.5を参照。

current_time(2)14:39:53.66-05

current_timestamptimestamp with time zone

Current date and time (start of current transaction); see <xref linkend="functions-datetime-current"/> 現在の日付と時刻(現在のトランザクションの開始時)。9.9.5を参照。

current_timestamp2019-12-23 14:39:53.662522-05

current_timestamp ( integer ) → timestamp with time zone

Current date and time (start of current transaction), with limited precision; see <xref linkend="functions-datetime-current"/> 精度を限定した現在の日付と時刻(現在のトランザクションの開始時)。9.9.5を参照。

current_timestamp(0)2019-12-23 14:39:53-05

date_add ( timestamp with time zone, interval [, text ] ) → timestamp with time zone

Add an <type>interval</type> to a <type>timestamp with time zone</type>, computing times of day and daylight-savings adjustments according to the time zone named by the third argument, or the current <xref linkend="guc-timezone"/> setting if that is omitted. The form with two arguments is equivalent to the <type>timestamp with time zone</type> <literal>+</literal> <type>interval</type> operator. intervaltimestamp with timezoneに加算し、3番目の引数が指定する名前を持つタイムゾーン、または省略されている場合は現在のTimeZone設定に従って、時刻と夏時間の調整を計算します。 2つの引数を持つ形式は、timestamp with timezone+interval演算子と同じです。

date_add('2021-10-31 00:00:00+02'::timestamptz, '1 day'::interval, 'Europe/Warsaw')2021-10-31 23:00:00+00

date_bin ( interval, timestamp, timestamp ) → timestamp

Bin input into specified interval aligned with specified origin; see <xref linkend="functions-datetime-bin"/> 指定した起源に合わせて指定の時間間隔に切り捨てます。9.9.3を参照してください。

date_bin('15 minutes', timestamp '2001-02-16 20:38:40', timestamp '2001-02-16 20:05:00')2001-02-16 20:35:00

date_part ( text, timestamp ) → double precision

Get timestamp subfield (equivalent to <function>extract</function>); see <xref linkend="functions-datetime-extract"/> タイムスタンプの部分フィールドの取得(extractと同じ)。9.9.1を参照。

date_part('hour', timestamp '2001-02-16 20:38:40')20

date_part ( text, interval ) → double precision

Get interval subfield (equivalent to <function>extract</function>); see <xref linkend="functions-datetime-extract"/> 時間間隔の部分フィールドの取得(extractと同じ)。9.9.1を参照。

date_part('month', interval '2 years 3 months')3

date_subtract ( timestamp with time zone, interval [, text ] ) → timestamp with time zone

Subtract an <type>interval</type> from a <type>timestamp with time zone</type>, computing times of day and daylight-savings adjustments according to the time zone named by the third argument, or the current <xref linkend="guc-timezone"/> setting if that is omitted. The form with two arguments is equivalent to the <type>timestamp with time zone</type> <literal>-</literal> <type>interval</type> operator. intervaltimestamp with timezoneから減算し、3番目の引数が指定する名前を持つタイムゾーン、または省略されている場合は現在のTimeZone設定に従って、時刻と夏時間の調整を計算します。 2つの引数を持つ形式は、timestamp with timezone-interval演算子と同じです。

date_subtract('2021-11-01 00:00:00+01'::timestamptz, '1 day'::interval, 'Europe/Warsaw')2021-10-30 22:00:00+00

date_trunc ( text, timestamp ) → timestamp

Truncate to specified precision; see <xref linkend="functions-datetime-trunc"/> 指定された精度で切り捨て。9.9.2参照。

date_trunc('hour', timestamp '2001-02-16 20:38:40')2001-02-16 20:00:00

date_trunc ( text, timestamp with time zone, text ) → timestamp with time zone

Truncate to specified precision in the specified time zone; see <xref linkend="functions-datetime-trunc"/> 指定された時間帯において指定された精度で切り捨て。9.9.2参照

date_trunc('day', timestamptz '2001-02-16 20:38:40+00', 'Australia/Sydney')2001-02-16 13:00:00+00

date_trunc ( text, interval ) → interval

Truncate to specified precision; see <xref linkend="functions-datetime-trunc"/> 指定された精度で切り捨て。9.9.2参照。

date_trunc('hour', interval '2 days 3 hours 40 minutes')2 days 03:00:00

extract ( field from timestamp ) → numeric

Get timestamp subfield; see <xref linkend="functions-datetime-extract"/> タイムスタンプの部分フィールドの取得。9.9.1を参照。

extract(hour from timestamp '2001-02-16 20:38:40')20

extract ( field from interval ) → numeric

Get interval subfield; see <xref linkend="functions-datetime-extract"/> 時間間隔の部分フィールドの取得。9.9.1を参照。

extract(month from interval '2 years 3 months')3

isfinite ( date ) → boolean

Test for finite date (not +/-infinity) 日付が有限(+/-無限でない)かどうかの検査

isfinite(date '2001-02-16')true

isfinite ( timestamp ) → boolean

Test for finite timestamp (not +/-infinity) タイムスタンプが有限(+/-無限でない)かどうかの検査

isfinite(timestamp 'infinity')false

isfinite ( interval ) → boolean

Test for finite interval (currently always true) 時間間隔が有限かどうかの検査(今の所常に真)

isfinite(interval '4 hours')true

justify_days ( interval ) → interval

Adjust interval so 30-day time periods are represented as months 30日周期が1月を表すように時間間隔を調整

justify_days(interval '35 days')1 mon 5 days

justify_hours ( interval ) → interval

Adjust interval so 24-hour time periods are represented as days 24時間を1日とする時間間隔の調整

justify_hours(interval '27 hours')1 day 03:00:00

justify_interval ( interval ) → interval

Adjust interval using <function>justify_days</function> and <function>justify_hours</function>, with additional sign adjustments justify_daysおよびjustify_hoursを使用し、さらに符号による調整を行っての時間間隔の調整

justify_interval(interval '1 mon -1 hour')29 days 23:00:00

localtimetime

Current time of day; see <xref linkend="functions-datetime-current"/> 現在の時刻。9.9.5を参照。

localtime14:39:53.662522

localtime ( integer ) → time

Current time of day, with limited precision; see <xref linkend="functions-datetime-current"/> 精度を限定した現在の時刻。9.9.5を参照。

localtime(0)14:39:53

localtimestamptimestamp

Current date and time (start of current transaction); see <xref linkend="functions-datetime-current"/> 現在の日付と時刻(現在のトランザクションの開始時)。9.9.5を参照。

localtimestamp2019-12-23 14:39:53.662522

localtimestamp ( integer ) → timestamp

Current date and time (start of current transaction), with limited precision; see <xref linkend="functions-datetime-current"/> 精度を限定した現在の日付と時刻(現在のトランザクションの開始時)。9.9.5を参照。

localtimestamp(2)2019-12-23 14:39:53.66

make_date ( year int, month int, day int ) → date

Create date from year, month and day fields (negative years signify BC) 年、月、日フィールドから日付を作成(負の年はBCを意味します)

make_date(2013, 7, 15)2013-07-15

make_interval ( [ years int [, months int [, weeks int [, days int [, hours int [, mins int [, secs double precision ]]]]]]] ) → interval

Create interval from years, months, weeks, days, hours, minutes and seconds fields, each of which can default to zero 年、月、週、日、時間、分、秒フィールドから時間間隔を作成

make_interval(days => 10)10 days

make_time ( hour int, min int, sec double precision ) → time

Create time from hour, minute and seconds fields 時、分、秒フィールドから時刻を作成

make_time(8, 15, 23.5)08:15:23.5

make_timestamp ( year int, month int, day int, hour int, min int, sec double precision ) → timestamp

Create timestamp from year, month, day, hour, minute and seconds fields (negative years signify BC) 年、月、日、時、分、秒フィールドから時刻を作成(負の年はBCを意味します)

make_timestamp(2013, 7, 15, 8, 15, 23.5)2013-07-15 08:15:23.5

make_timestamptz ( year int, month int, day int, hour int, min int, sec double precision [, timezone text ] ) → timestamp with time zone

Create timestamp with time zone from year, month, day, hour, minute and seconds fields (negative years signify BC). If <parameter>timezone</parameter> is not specified, the current time zone is used; the examples assume the session time zone is <literal>Europe/London</literal> 年、月、日、時、分、秒フィールドから時間帯付きの時刻を作成(負の年はBCを意味する)。 timezoneが指定されていなければ、現在の時間帯が使われる。 例ではセッションの時間帯がEurope/Londonであると仮定。

make_timestamptz(2013, 7, 15, 8, 15, 23.5)2013-07-15 08:15:23.5+01

make_timestamptz(2013, 7, 15, 8, 15, 23.5, 'America/New_York')2013-07-15 13:15:23.5+01

now ( ) → timestamp with time zone

Current date and time (start of current transaction); see <xref linkend="functions-datetime-current"/> 現在の日付と時刻(現在のトランザクションの開始時)。9.9.5を参照。

now()2019-12-23 14:39:53.662522-05

statement_timestamp ( ) → timestamp with time zone

Current date and time (start of current statement); see <xref linkend="functions-datetime-current"/> 現在の日付と時刻(現在の文の開始時)。9.9.5を参照。

statement_timestamp()2019-12-23 14:39:53.662522-05

timeofday ( ) → text

Current date and time (like <function>clock_timestamp</function>, but as a <type>text</type> string); see <xref linkend="functions-datetime-current"/> 現在の日付と時刻(clock_timestampと似ているが、text型文字列として返す)。9.9.5を参照。

timeofday()Mon Dec 23 14:39:53.662522 2019 EST

transaction_timestamp ( ) → timestamp with time zone

Current date and time (start of current transaction); see <xref linkend="functions-datetime-current"/> 現在の日付と時刻(現在のトランザクションの開始時)。9.9.5を参照。

transaction_timestamp()2019-12-23 14:39:53.662522-05

to_timestamp ( double precision ) → timestamp with time zone

Convert Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp with time zone Unixエポック時間(1970-01-01 00:00:00+00からの経過秒数)をtimestamp with time zoneに変換

to_timestamp(1284352323)2010-09-13 04:32:03+00


In addition to these functions, the SQL <literal>OVERLAPS</literal> operator is supported: これらの関数に加え、OVERLAPS SQL演算子がサポートされています。

(start1, end1) OVERLAPS (start2, end2)
(start1, length1) OVERLAPS (start2, length2)

This expression yields true when two time periods (defined by their endpoints) overlap, false when they do not overlap. The endpoints can be specified as pairs of dates, times, or time stamps; or as a date, time, or time stamp followed by an interval. When a pair of values is provided, either the start or the end can be written first; <literal>OVERLAPS</literal> automatically takes the earlier value of the pair as the start. Each time period is considered to represent the half-open interval <replaceable>start</replaceable> <literal>&lt;=</literal> <replaceable>time</replaceable> <literal>&lt;</literal> <replaceable>end</replaceable>, unless <replaceable>start</replaceable> and <replaceable>end</replaceable> are equal in which case it represents that single time instant. This means for instance that two time periods with only an endpoint in common do not overlap. この式は、2つの時間間隔(その両端で定義されます)が重なる時に真を返します。重ならない場合は偽を返します。 両端は2つの日付、時刻、タイムスタンプとして、もしくは、日付/時刻/タイムスタンプとそれに続く時間間隔として指定できます。 値の組み合わせで指定する場合、開始と終了のいずれを先に記述しても構いません。OVERLAPSは与えられた値のうち、早い方を開始として扱います。 各時間間隔は、start <= time < endという半開区間として見なされます。ただし、startendが同じ値の場合には単一の時間点となります。 これは、例えば端点のみが共通である2つの時間間隔は、重ならないということを意味します。

SELECT (DATE '2001-02-16', DATE '2001-12-21') OVERLAPS
       (DATE '2001-10-30', DATE '2002-10-30');
Result: true
SELECT (DATE '2001-02-16', INTERVAL '100 days') OVERLAPS
       (DATE '2001-10-30', DATE '2002-10-30');
Result: false
SELECT (DATE '2001-10-29', DATE '2001-10-30') OVERLAPS
       (DATE '2001-10-30', DATE '2001-10-31');
Result: false
SELECT (DATE '2001-10-30', DATE '2001-10-30') OVERLAPS
       (DATE '2001-10-30', DATE '2001-10-31');
Result: true

When adding an <type>interval</type> value to (or subtracting an <type>interval</type> value from) a <type>timestamp</type> or <type>timestamp with time zone</type> value, the months, days, and microseconds fields of the <type>interval</type> value are handled in turn. First, a nonzero months field advances or decrements the date of the timestamp by the indicated number of months, keeping the day of month the same unless it would be past the end of the new month, in which case the last day of that month is used. (For example, March 31 plus 1 month becomes April 30, but March 31 plus 2 months becomes May 31.) Then the days field advances or decrements the date of the timestamp by the indicated number of days. In both these steps the local time of day is kept the same. Finally, if there is a nonzero microseconds field, it is added or subtracted literally. When doing arithmetic on a <type>timestamp with time zone</type> value in a time zone that recognizes DST, this means that adding or subtracting (say) <literal>interval '1 day'</literal> does not necessarily have the same result as adding or subtracting <literal>interval '24 hours'</literal>. For example, with the session time zone set to <literal>America/Denver</literal>: timestampまたはtimestamp with time zoneの値にintervalの値を加える際には(あるいはintervalの値を引く際には)、interval値の月、日、マイクロ秒のフィールドが順に適用されます。 まず、非ゼロの月フィールドが示す日数の分だけtimestampの日付を先に進める、もしくは後に戻し、新しい月の最終日を超えてしまわない限り月内の日付を同じに保ちます。月の最後の日を超えてしまうようなら、その月の最終日が使われます。 (たちえば、3月31日に1ヶ月を加えると4月30日になりますが、3月31日に2ヶ月を加えると5月31日になります。) 次に、日フィールド分だけtimestampの日付を先に進める、もしくは後に戻します。 この両方の処理において、現地時刻は同じに保ちます。 最後に、非ゼロのマイクロ秒フィールドがあれば、そのまま加算、もしくは減算します。 DSTと認識される時間帯におけるtimestamp with time zone値の演算を行う際には、(たとえば)interval '1 day'を加算、もしくは減算することは、interval '24 hours'を加算、もしくは減算するのと同じ結果になるとは限りません。 例えば、セッションの時間帯が America/Denverに設定されている時には以下のようになります。

SELECT timestamp with time zone '2005-04-02 12:00:00-07' + interval '1 day';
Result: 2005-04-03 12:00:00-06
SELECT timestamp with time zone '2005-04-02 12:00:00-07' + interval '24 hours';
Result: 2005-04-03 13:00:00-06

This happens because an hour was skipped due to a change in daylight saving time at <literal>2005-04-03 02:00:00</literal> in time zone <literal>America/Denver</literal>. その理由はAmerica/Denver時間帯で2005-04-03 02:00:00に夏時間への変更され、1時間スキップされたためです。

Note there can be ambiguity in the <literal>months</literal> field returned by <function>age</function> because different months have different numbers of days. <productname>PostgreSQL</productname>'s approach uses the month from the earlier of the two dates when calculating partial months. For example, <literal>age('2004-06-01', '2004-04-30')</literal> uses April to yield <literal>1 mon 1 day</literal>, while using May would yield <literal>1 mon 2 days</literal> because May has 31 days, while April has only 30. 異なる月では日数が異なりますのでageで返されるmonthsフィールドにはあいまいさがあります。 PostgreSQLのやり方は月をまたがる2つの日付の計算において、日付の早いほうの月を使用します。 例えば、age('2004-06-01', '2004-04-30')は4月を使用して1 mon 1 dayを得ます。5月は31日あり、4月は30日のため、もし5月を使用するなら結果は1 mon 2 daysとなるでしょう。

Subtraction of dates and timestamps can also be complex. One conceptually simple way to perform subtraction is to convert each value to a number of seconds using <literal>EXTRACT(EPOCH FROM ...)</literal>, then subtract the results; this produces the number of <emphasis>seconds</emphasis> between the two values. This will adjust for the number of days in each month, timezone changes, and daylight saving time adjustments. Subtraction of date or timestamp values with the <quote><literal>-</literal></quote> operator returns the number of days (24-hours) and hours/minutes/seconds between the values, making the same adjustments. The <function>age</function> function returns years, months, days, and hours/minutes/seconds, performing field-by-field subtraction and then adjusting for negative field values. The following queries illustrate the differences in these approaches. The sample results were produced with <literal>timezone = 'US/Eastern'</literal>; there is a daylight saving time change between the two dates used: 日付とタイムスタンプの引き算は複雑になることがあります。 引き算をする概念的に単純な方法は、それぞれの値を秒数にEXTRACT(EPOCH FROM ...)で変換してから、結果を引き算する方法です。この結果は2つの値の間の数になります。 これは各月の日数、時間帯の変更、夏時間の調整に対して調整されるでしょう。 -演算子での日付やタイムスタンプの引き算は値の間の(24時間の)日数と時間/分/秒を、同様に調整して返します。 age関数は年、月、日、時間/分/秒をフィールド毎に引き算し、負のフィールドの値を調整します。 以下の問い合わせは上の各方法の違いを説明する例です。 例の結果はtimezone = 'US/Eastern'で生成されました。2つの日付の間には夏時間の変更があります。

SELECT EXTRACT(EPOCH FROM timestamptz '2013-07-01 12:00:00') -
       EXTRACT(EPOCH FROM timestamptz '2013-03-01 12:00:00');
Result: 10537200.000000
SELECT (EXTRACT(EPOCH FROM timestamptz '2013-07-01 12:00:00') -
        EXTRACT(EPOCH FROM timestamptz '2013-03-01 12:00:00'))
        / 60 / 60 / 24;
Result: 121.9583333333333333
SELECT timestamptz '2013-07-01 12:00:00' - timestamptz '2013-03-01 12:00:00';
Result: 121 days 23:00:00
SELECT age(timestamptz '2013-07-01 12:00:00', timestamptz '2013-03-01 12:00:00');
Result: 4 mons

9.9.1. EXTRACT, date_part #

EXTRACT(field FROM source)

The <function>extract</function> function retrieves subfields such as year or hour from date/time values. <replaceable>source</replaceable> must be a value expression of type <type>timestamp</type>, <type>time</type>, or <type>interval</type>. (Expressions of type <type>date</type> are cast to <type>timestamp</type> and can therefore be used as well.) <replaceable>field</replaceable> is an identifier or string that selects what field to extract from the source value. The <function>extract</function> function returns values of type <type>numeric</type>. The following are valid field names: extract関数は、日付/時刻の値から年や時などの部分フィールドを抽出します。 sourcetimestamp型、time型、またはinterval型の評価式でなければなりません(date型の式はtimestamp型にキャストされますので、同様に使用可能です)。 fieldはsourceの値からどのフィールドを抽出するかを選択する識別子もしくは文字列です。 extract関数はnumeric型の値を返します。 以下に有効なフィールド名を示します。 alphabetical

century

The century 世紀

SELECT EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13');
Result: 20
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 21

The first century starts at 0001-01-01 00:00:00 AD, although they did not know it at the time. This definition applies to all Gregorian calendar countries. There is no century number 0, you go from -1 century to 1 century. If you disagree with this, please write your complaint to: Pope, Cathedral Saint-Peter of Roma, Vatican. 当時の人々にはそのような意識はありませんでしたが、最初の世紀は0001-01-01 00:00:00 ADから始まります。 この定義は全てのグレゴリオ暦を使用する国で適用されています。 0という値の世紀はありません。-1世紀の次は1世紀です。 この定義に納得できなければ、苦情をバチカンローマ聖ペテロ大聖堂のローマ法王に伝えてください。

day

For <type>timestamp</type> values, the day (of the month) field (1&ndash;31) ; for <type>interval</type> values, the number of days timestamp値については、(月内の)日付フィールド(1–31)。interval値については日数。

SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 16

SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
Result: 40
decade

The year field divided by 10 年フィールドを10で割ったもの

SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 200
dow

The day of the week as Sunday (<literal>0</literal>) to Saturday (<literal>6</literal>) 日曜日(0)から土曜日(6)までの曜日

SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 5

Note that <function>extract</function>'s day of the week numbering differs from that of the <function>to_char(..., 'D')</function> function. extract関数の曜日番号はto_char(...,'D')関数のそれとは異なる点に注意してください。

doy

The day of the year (1&ndash;365/366) 年内での通算日数(1–365/366)

SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 47
epoch

For <type>timestamp with time zone</type> values, the number of seconds since 1970-01-01 00:00:00 UTC (negative for timestamps before that); for <type>date</type> and <type>timestamp</type> values, the nominal number of seconds since 1970-01-01 00:00:00, without regard to timezone or daylight-savings rules; for <type>interval</type> values, the total number of seconds in the interval timestamp with time zone型の値においては、1970-01-01 00:00:00 UTCからの秒数(負の数はその前)。datetimestamp型の値においては、時間帯と夏時間を考慮しないローカルタイムの1970-01-01 00:00:00からの秒数。interval型の値ではその時間間隔における合計の秒数。

SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
Result: 982384720.120000

SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40.12');
Result: 982355920.120000

SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
Result: 442800.000000

You can convert an epoch value back to a <type>timestamp with time zone</type> with <function>to_timestamp</function>: to_timestampで経過秒数をtimestamp with time zoneに変換することができます。

SELECT to_timestamp(982384720.12);
Result: 2001-02-17 04:38:40.12+00

Beware that applying <function>to_timestamp</function> to an epoch extracted from a <type>date</type> or <type>timestamp</type> value could produce a misleading result: the result will effectively assume that the original value had been given in UTC, which might not be the case. to_timestampdateあるいはtimestampの値から取り出したエポックに適用すると、誤解を招く結果が得られるかもしれないことに注意してください。結果は実質的に元の値がUTCで与えられていると見なしますが、実際は違うかもしれません。

hour

The hour field (0&ndash;23) 時フィールド(0–23)

SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 20
isodow

The day of the week as Monday (<literal>1</literal>) to Sunday (<literal>7</literal>) 月曜日(1)から日曜日(7)までの曜日

SELECT EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40');
Result: 7

This is identical to <literal>dow</literal> except for Sunday. This matches the <acronym>ISO</acronym> 8601 day of the week numbering. 日曜日を除きdowと同一です。 これはISO 8601曜日番号付けに一致します。

isoyear

The <acronym>ISO</acronym> 8601 week-numbering year that the date falls in (not applicable to intervals) 日付に当てはまるISO 8601週番号年(intervalには適用できない)。

SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-01');
Result: 2005
SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
Result: 2006

Each <acronym>ISO</acronym> 8601 week-numbering year begins with the Monday of the week containing the 4th of January, so in early January or late December the <acronym>ISO</acronym> year may be different from the Gregorian year. See the <literal>week</literal> field for more information. すべてのISO 8601週番号年は1月4日を含む週の月曜日から開始されます。従って、1月上旬、または12月下旬でISO年がグレゴリオ年と異なる可能性があります。 より詳細はweekフィールドを参照してください。

This field is not available in PostgreSQL releases prior to 8.3. このフィールドは8.3より前のPostgreSQLリリースでは有効でありません。

julian

The <firstterm>Julian Date</firstterm> corresponding to the date or timestamp (not applicable to intervals). Timestamps that are not local midnight result in a fractional value. See <xref linkend="datetime-julian-dates"/> for more information. 日付あるいタイムスタンプに関連するユリウス日(Julian Date)(時間間隔には適用されない)。 ローカル午前零時ではないタイムスタンプは小数となります。 詳細はB.7をご覧ください。

SELECT EXTRACT(JULIAN FROM DATE '2006-01-01');
Result: 2453737
SELECT EXTRACT(JULIAN FROM TIMESTAMP '2006-01-01 12:00');
Result: 2453737.50000000000000000000
microseconds

The seconds field, including fractional parts, multiplied by 1 000 000; note that this includes full seconds 端数部分も含む秒フィールドに、1,000,000を乗じた値。秒の整数部を含むことに注意。

SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
Result: 28500000
millennium

The millennium ミレニアム(千年期)

SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 3

Years in the 1900s are in the second millennium. The third millennium started January 1, 2001. 1900年代の年は第2ミレニアムです。第3ミレニアムは2001年1月1日から始まりました。

milliseconds

The seconds field, including fractional parts, multiplied by 1000. Note that this includes full seconds. 端数部分も含む秒フィールドに、1000を乗た値。秒の整数部を含むことに注意してください。

SELECT EXTRACT(MILLISECONDS FROM TIME '17:12:28.5');
Result: 28500.000
minute

The minutes field (0&ndash;59) 分フィールド (0–59)

SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 38
month

For <type>timestamp</type> values, the number of the month within the year (1&ndash;12) ; for <type>interval</type> values, the number of months, modulo 12 (0&ndash;11) timestamp型の値に対しては年内の月番号(1–12)。interval型の値に対しては月数を12で割った余り(0–11)。

SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 2

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
Result: 3

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
Result: 1
quarter

The quarter of the year (1&ndash;4) that the date is in その日付が含まれる年の四半期(1–4)

SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 1
second

The seconds field, including any fractional seconds 端数を含んだ秒フィールド

SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 40.000000

SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
Result: 28.500000
timezone

The time zone offset from UTC, measured in seconds. Positive values correspond to time zones east of UTC, negative values to zones west of UTC. (Technically, <productname>PostgreSQL</productname> does not use UTC because leap seconds are not handled.) 秒単位のUTCからの時間帯オフセット。正の値はUTCより東の時間帯に対応し、負の値はUTCより西の時間帯に対応。 (技術的に言えば、PostgreSQLはうるう秒を制御しないためUTCを使用していない。)

timezone_hour

The hour component of the time zone offset 時間帯オフセットの時の成分。

timezone_minute

The minute component of the time zone offset 時間帯オフセットの分の成分。

week

The number of the <acronym>ISO</acronym> 8601 week-numbering week of the year. By definition, ISO weeks start on Mondays and the first week of a year contains January 4 of that year. In other words, the first Thursday of a year is in week 1 of that year. ISO 8601週番号。 定義ではISO週は月曜日から始まり、その年の1月4日を含む週をその年の第1週としています。 つまり、年の最初の木曜日がある週がその年の第1週となります。

In the ISO week-numbering system, it is possible for early-January dates to be part of the 52nd or 53rd week of the previous year, and for late-December dates to be part of the first week of the next year. For example, <literal>2005-01-01</literal> is part of the 53rd week of year 2004, and <literal>2006-01-01</literal> is part of the 52nd week of year 2005, while <literal>2012-12-31</literal> is part of the first week of 2013. It's recommended to use the <literal>isoyear</literal> field together with <literal>week</literal> to get consistent results. ISO週番号システムでは、1月の早い日にちは前年の第52週もしくは第53週となることがあり、12月の遅い日にちには次年の最初の週の一部となることがあります。 例えば、2005-01-01は2004年の第53週であり、2006-01-01は2005年の第52週の一部です、一方2012-12-31は2013年の第1週の一部となります。 整合性のある結果を得るため、isoyearフィールドとweekを併用することを推奨します。

SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 7
year

The year field. Keep in mind there is no <literal>0 AD</literal>, so subtracting <literal>BC</literal> years from <literal>AD</literal> years should be done with care. 年フィールド。AD零年が存在しないことは忘れないでください。このためADの年からBCの年を減ずる時には注意が必要です。

SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 2001

注記

When the input value is +/-Infinity, <function>extract</function> returns +/-Infinity for monotonically-increasing fields (<literal>epoch</literal>, <literal>julian</literal>, <literal>year</literal>, <literal>isoyear</literal>, <literal>decade</literal>, <literal>century</literal>, and <literal>millennium</literal>). For other fields, NULL is returned. <productname>PostgreSQL</productname> versions before 9.6 returned zero for all cases of infinite input. 入力値が+/-無限大の場合、extractは単調増加するフィールド(epochjulianyearisoyeardecadecenturymillennium)に対し、+/-無限大を返します。 その他のフィールドに対してはNULLが返されます。 PostgreSQLの9.6より前のバージョンでは、入力が無限大のすべての場合に対してゼロを返していました。

The <function>extract</function> function is primarily intended for computational processing. For formatting date/time values for display, see <xref linkend="functions-formatting"/>. extract関数は主に演算処理を意図しています。 日付/時刻の値を表示する目的での書式については9.8を参照してください。

The <function>date_part</function> function is modeled on the traditional <productname>Ingres</productname> equivalent to the <acronym>SQL</acronym>-standard function <function>extract</function>: date_part関数は伝統的なIngres上で設計されたもので、標準SQLextract関数と等価です。

date_part('field', source)

Note that here the <replaceable>field</replaceable> parameter needs to be a string value, not a name. The valid field names for <function>date_part</function> are the same as for <function>extract</function>. For historical reasons, the <function>date_part</function> function returns values of type <type>double precision</type>. This can result in a loss of precision in certain uses. Using <function>extract</function> is recommended instead. ここでfieldパラメータが名前ではなく文字列値である必要があることに注意してください。 date_partで有効なフィールド名はextractと同じです。 歴史的な理由により、date_part関数はdouble precision型の結果を返します。 場合によってはこれによって精度が失われることがあります。 extractを代わりに使うことをお勧めします。

SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
Result: 16

SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
Result: 4

9.9.2. date_trunc #

The function <function>date_trunc</function> is conceptually similar to the <function>trunc</function> function for numbers. date_trunc関数は概念的に数値に対するtrunc関数と類似しています。

date_trunc(field, source [, time_zone ])

<replaceable>source</replaceable> is a value expression of type <type>timestamp</type>, <type>timestamp with time zone</type>, or <type>interval</type>. (Values of type <type>date</type> and <type>time</type> are cast automatically to <type>timestamp</type> or <type>interval</type>, respectively.) <replaceable>field</replaceable> selects to which precision to truncate the input value. The return value is likewise of type <type>timestamp</type>, <type>timestamp with time zone</type>, or <type>interval</type>, and it has all fields that are less significant than the selected one set to zero (or one, for day and month). sourceは、データ型timestamptimestamp with time zoneもしくはintervalの評価式です。 (date型とtime型の値はそれぞれ自動的にtimestampもしくはintervalにキャストされます。) fieldは、入力値の値をどの精度で切り捨てるかを選択します。 同様に戻り値はtimestamptimestamp with time zoneもしくはinterval型で、指定した精度より下のすべてのフィールドがゼロに設定(日と月については1に設定)されます。

Valid values for <replaceable>field</replaceable> are: fieldの有効値には次のものがあります。

microseconds
milliseconds
second
minute
hour
day
week
month
quarter
year
decade
century
millennium

When the input value is of type <type>timestamp with time zone</type>, the truncation is performed with respect to a particular time zone; for example, truncation to <literal>day</literal> produces a value that is midnight in that zone. By default, truncation is done with respect to the current <xref linkend="guc-timezone"/> setting, but the optional <replaceable>time_zone</replaceable> argument can be provided to specify a different time zone. The time zone name can be specified in any of the ways described in <xref linkend="datatype-timezones"/>. 入力値がtimestamp with time zone型の値なら、特定の時間帯を考慮して切り捨てが行われます。たとえば、を切り捨てると値はその時間帯での真夜中になります。 デフォルトでは切り捨ては現在のTimeZoneの設定に従いますが、別の時間帯を指定することができるようにオプションのtime_zone引数が提供されています。 時間帯名は8.5.3に記述されている方法で指定できます。

A time zone cannot be specified when processing <type>timestamp without time zone</type> or <type>interval</type> inputs. These are always taken at face value. timestamp without time zoneあるいはintervalの入力を処理している間は時間帯は指定できません。 これらは額面通りの値で扱われます。

Examples (assuming the local time zone is <literal>America/New_York</literal>): 例(現地タイムゾーンはAmerica/New_Yorkと仮定します):

SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00

SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-01-01 00:00:00

SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00');
Result: 2001-02-16 00:00:00-05

SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00', 'Australia/Sydney');
Result: 2001-02-16 08:00:00-05

SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
Result: 3 days 02:00:00

9.9.3. date_bin #

The function <function>date_bin</function> <quote>bins</quote> the input timestamp into the specified interval (the <firstterm>stride</firstterm>) aligned with a specified origin. 関数date_binは、指定した原点に揃えて入力のタイムスタンプを指定した時間間隔(stride)に分類します。

date_bin(stride, source, origin)

<replaceable>source</replaceable> is a value expression of type <type>timestamp</type> or <type>timestamp with time zone</type>. (Values of type <type>date</type> are cast automatically to <type>timestamp</type>.) <replaceable>stride</replaceable> is a value expression of type <type>interval</type>. The return value is likewise of type <type>timestamp</type> or <type>timestamp with time zone</type>, and it marks the beginning of the bin into which the <replaceable>source</replaceable> is placed. sourcetimestampあるいはtimestamp with time zone型の値式です。 (date型の値はtimestampに自動キャストされます。) strideinterval型の値式です。 戻り値は同様に、timestampあるいはtimestamp with time zone型で、sourceが置かれた箱の開始位置に印を付けています。

Examples: 例を示します。

SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01');
Result: 2020-02-11 15:30:00

SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01 00:02:30');
Result: 2020-02-11 15:32:30

In the case of full units (1 minute, 1 hour, etc.), it gives the same result as the analogous <function>date_trunc</function> call, but the difference is that <function>date_bin</function> can truncate to an arbitrary interval. 完全な単位(1分、1時間など)の場合は、これは類似のdate_truncの呼び出しと同じ結果を与えます。 違いは、date_binは任意の間隔へと切り捨てられることです。

The <parameter>stride</parameter> interval must be greater than zero and cannot contain units of month or larger. stride間隔はゼロより大きくなければならず、かつ月単位あるいはそれよりも大きくてはいけません。

9.9.4. AT TIME ZONE #

The <literal>AT TIME ZONE</literal> operator converts time stamp <emphasis>without</emphasis> time zone to/from time stamp <emphasis>with</emphasis> time zone, and <type>time with time zone</type> values to different time zones. <xref linkend="functions-datetime-zoneconvert-table"/> shows its variants. AT TIME ZONE構文を使用することにより、time stamp without time zoneからtime stamp with time zoneへ、あるいはtime with time zoneの値を異なる時間帯に変換することができます。 表 9.34にその種類を示します。

表9.34 AT TIME ZONEの種類

<title><literal>AT TIME ZONE</literal> Variants</title>

Operator 演算子

Description 説明

Example(s)

timestamp without time zone AT TIME ZONE zonetimestamp with time zone

Converts given time stamp <emphasis>without</emphasis> time zone to time stamp <emphasis>with</emphasis> time zone, assuming the given value is in the named time zone. 与えられた時間帯なしタイムスタンプを指定された時間帯にあるとして時間帯ありタイムスタンプに変換します。

timestamp '2001-02-16 20:38:40' at time zone 'America/Denver'2001-02-17 03:38:40+00

timestamp with time zone AT TIME ZONE zonetimestamp without time zone

Converts given time stamp <emphasis>with</emphasis> time zone to time stamp <emphasis>without</emphasis> time zone, as the time would appear in that zone. 与えられた時間帯付き時刻を、時刻がその時間帯にあるものとして時間帯なしタイムスタンプに変換します。

timestamp with time zone '2001-02-16 20:38:40-05' at time zone 'America/Denver'2001-02-16 18:38:40

time with time zone AT TIME ZONE zonetime with time zone

Converts given time <emphasis>with</emphasis> time zone to a new time zone. Since no date is supplied, this uses the currently active UTC offset for the named destination zone. 与えられた時刻with time zoneを新しい時間帯に変換します。 判断するためのデータがないので、現在の有効なUTCオフセットを目的の時間帯のために使用します。

time with time zone '05:34:17-05' at time zone 'UTC'10:34:17+00


In these expressions, the desired time zone <replaceable>zone</replaceable> can be specified either as a text value (e.g., <literal>'America/Los_Angeles'</literal>) or as an interval (e.g., <literal>INTERVAL '-08:00'</literal>). In the text case, a time zone name can be specified in any of the ways described in <xref linkend="datatype-timezones"/>. The interval case is only useful for zones that have fixed offsets from UTC, so it is not very common in practice. これらの式では、設定する時間帯zoneは、('America/Los_Angeles'のような)テキスト値、または(INTERVAL '-08:00'のような)時間間隔で指定することができます。 テキストの場合、8.5.3に示した方法で時間帯名称を指定することができます。 時間間隔を使うのはUTCからの固定のオフセットを持つ時間帯でのみ有用なので、一般的に非常に有用であるとは言えません。

Examples (assuming the current <xref linkend="guc-timezone"/> setting is <literal>America/Los_Angeles</literal>): 以下に例を示します(現在の時間帯(TimeZone)をAmerica/Los_Angelesと想定しています)。

SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'America/Denver';
Result: 2001-02-16 19:38:40-08

SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'America/Denver';
Result: 2001-02-16 18:38:40

SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'Asia/Tokyo' AT TIME ZONE 'America/Chicago';
Result: 2001-02-16 05:38:40

The first example adds a time zone to a value that lacks it, and displays the value using the current <varname>TimeZone</varname> setting. The second example shifts the time stamp with time zone value to the specified time zone, and returns the value without a time zone. This allows storage and display of values different from the current <varname>TimeZone</varname> setting. The third example converts Tokyo time to Chicago time. 最初の例は、時間帯のない値に時間帯を追加し、現在のTimeZone設定を使ってその値を表示します。 2番目の例は、time stamp with time zone値を指定した時間帯に変換し、その値をwithout a time zoneで返しています。 これは、TimeZone設定とは異なる値の格納と表示を可能にします。 3番目の例は、東京時間をシカゴ時間に変換します。

The function <literal><function>timezone</function>(<replaceable>zone</replaceable>, <replaceable>timestamp</replaceable>)</literal> is equivalent to the SQL-conforming construct <literal><replaceable>timestamp</replaceable> AT TIME ZONE <replaceable>zone</replaceable></literal>. 関数timezone(zone, timestamp)は、SQL準拠の構文timestamp AT TIME ZONE zoneと等価です。

9.9.5. 現在の日付/時刻 #

<title>Current Date/Time</title>

<productname>PostgreSQL</productname> provides a number of functions that return values related to the current date and time. These SQL-standard functions all return values based on the start time of the current transaction: PostgreSQLは、現在の日付時刻に関した値を返す多くの関数を提供します。 これらの標準SQL関数はすべて、現在のトランザクションの開始時刻に基づいた値を返します。

CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
CURRENT_TIME(precision)
CURRENT_TIMESTAMP(precision)
LOCALTIME
LOCALTIMESTAMP
LOCALTIME(precision)
LOCALTIMESTAMP(precision)

<function>CURRENT_TIME</function> and <function>CURRENT_TIMESTAMP</function> deliver values with time zone; <function>LOCALTIME</function> and <function>LOCALTIMESTAMP</function> deliver values without time zone. CURRENT_TIMEおよびCURRENT_TIMESTAMP関数では、時間帯を伴う値を扱います。一方、LOCALTIMEおよびLOCALTIMESTAMP関数では、時間帯を伴わない値を扱います。

<function>CURRENT_TIME</function>, <function>CURRENT_TIMESTAMP</function>, <function>LOCALTIME</function>, and <function>LOCALTIMESTAMP</function> can optionally take a precision parameter, which causes the result to be rounded to that many fractional digits in the seconds field. Without a precision parameter, the result is given to the full available precision. CURRENT_TIMECURRENT_TIMESTAMPLOCALTIME、およびLOCALTIMESTAMP関数では、精度のパラメータをオプションで取ることができ、それに合わせて秒フィールドの端数桁を丸める結果をもたらします。 精度のパラメータがない場合、結果は使用可能な最大精度で出力されます。

Some examples: 例:

SELECT CURRENT_TIME;
Result: 14:39:53.662522-05

SELECT CURRENT_DATE;
Result: 2019-12-23

SELECT CURRENT_TIMESTAMP;
Result: 2019-12-23 14:39:53.662522-05

SELECT CURRENT_TIMESTAMP(2);
Result: 2019-12-23 14:39:53.66-05

SELECT LOCALTIMESTAMP;
Result: 2019-12-23 14:39:53.662522

Since these functions return the start time of the current transaction, their values do not change during the transaction. This is considered a feature: the intent is to allow a single transaction to have a consistent notion of the <quote>current</quote> time, so that multiple modifications within the same transaction bear the same time stamp. これらの関数は現在のトランザクションの開始時刻を返すため、その値はトランザクションが実行されている間は変化しません。 これは仕様であると考えられており、その意図は、単一のトランザクションが一貫性のある現在時刻の概念を持ち、同一トランザクション内の複数の変更が同一のタイムスタンプを持つようにすることにあります。

注記

Other database systems might advance these values more frequently. 他のデータベースシステムでは、これらの値をより頻繁に増加させることがあります。

<productname>PostgreSQL</productname> also provides functions that return the start time of the current statement, as well as the actual current time at the instant the function is called. The complete list of non-SQL-standard time functions is: PostgreSQLはまた、関数を呼び出した時の実際の現在時刻や現在の文の開始時刻を返す関数も提供します。 非標準SQLの時間関数の全一覧を以下に示します。

transaction_timestamp()
statement_timestamp()
clock_timestamp()
timeofday()
now()

<function>transaction_timestamp()</function> is equivalent to <function>CURRENT_TIMESTAMP</function>, but is named to clearly reflect what it returns. <function>statement_timestamp()</function> returns the start time of the current statement (more specifically, the time of receipt of the latest command message from the client). <function>statement_timestamp()</function> and <function>transaction_timestamp()</function> return the same value during the first command of a transaction, but might differ during subsequent commands. <function>clock_timestamp()</function> returns the actual current time, and therefore its value changes even within a single SQL command. <function>timeofday()</function> is a historical <productname>PostgreSQL</productname> function. Like <function>clock_timestamp()</function>, it returns the actual current time, but as a formatted <type>text</type> string rather than a <type>timestamp with time zone</type> value. <function>now()</function> is a traditional <productname>PostgreSQL</productname> equivalent to <function>transaction_timestamp()</function>. transaction_timestamp()CURRENT_TIMESTAMPと等価ですが、明確に何を返すかを反映する名前になっています。 statement_timestamp()は現在の文の実行開始時刻を返すものです(より具体的にいうと、直前のコマンドメッセージをクライアントから受け取った時刻です)。 statement_timestamp()およびtransaction_timestamp()はトランザクションの最初のコマンドでは同じ値を返しますが、その後に引き続くコマンドでは異なる可能性があります。 clock_timestamp()は実際の現在時刻を返しますので、その値は単一のSQLコマンドであっても異なります。 timeofday()PostgreSQLの歴史的な関数です。 clock_timestamp()同様、実際の現在時刻を返しますが、timestamp with time zone型の値ではなく、整形されたtext文字列を返します。now()transaction_timestamp()と同じもので、伝統的なPostgreSQL関数です。

All the date/time data types also accept the special literal value <literal>now</literal> to specify the current date and time (again, interpreted as the transaction start time). Thus, the following three all return the same result: すべての日付/時刻型はまた、特殊なリテラル値 nowを受け付け、これは現在の日付と時刻(ここでも、トランザクションの開始時刻として解釈されます)を表します。 したがって、下記の3つの実行結果は全て同じものとなります。

SELECT CURRENT_TIMESTAMP;
SELECT now();
SELECT TIMESTAMP 'now';  -- but see tip below

ヒント

Do not use the third form when specifying a value to be evaluated later, for example in a <literal>DEFAULT</literal> clause for a table column. The system will convert <literal>now</literal> to a <type>timestamp</type> as soon as the constant is parsed, so that when the default value is needed, the time of the table creation would be used! The first two forms will not be evaluated until the default value is used, because they are function calls. Thus they will give the desired behavior of defaulting to the time of row insertion. (See also <xref linkend="datatype-datetime-special-values"/>.) たとえばテーブルの列にDEFAULT句を指定するのに、後から評価される値を指定する際に3番目の形式は使わないでください。 システムはnowという定数を解析すると、すぐにそれをtimestampに変換するので、デフォルト値が必要が時には、テーブルが作成された時刻が使われます。 最初の2つの形式は関数呼び出しなので、デフォルト値が使用されるまで評価されません。 ですから、これらの関数は列の挿入時間をデフォルトとする、望ましい振舞いをします。 (8.5.1.4も見てください。)

9.9.6. 遅延実行 #

<title>Delaying Execution</title>

The following functions are available to delay execution of the server process: 以下の関数は、サーバプロセスの実行を遅延させるために使用可能です。

pg_sleep ( double precision )
pg_sleep_for ( interval )
pg_sleep_until ( timestamp with time zone )

<function>pg_sleep</function> makes the current session's process sleep until the given number of seconds have elapsed. Fractional-second delays can be specified. <function>pg_sleep_for</function> is a convenience function to allow the sleep time to be specified as an <type>interval</type>. <function>pg_sleep_until</function> is a convenience function for when a specific wake-up time is desired. For example: pg_sleepは、指定された秒数が経過するまで、現在のセッションのプロセスを休止させます。 pg_sleep_forintervalでより長い休止時間を指定する便利な関数です。 pg_sleep_untilは特定の起床時刻が望まれる場合に便利な関数です。 以下に例を示します。

SELECT pg_sleep(1.5);
SELECT pg_sleep_for('5 minutes');
SELECT pg_sleep_until('tomorrow 03:00');

注記

The effective resolution of the sleep interval is platform-specific; 0.01 seconds is a common value. The sleep delay will be at least as long as specified. It might be longer depending on factors such as server load. In particular, <function>pg_sleep_until</function> is not guaranteed to wake up exactly at the specified time, but it will not wake up any earlier. 休止時間の有効な分解能はプラットフォームに依存します。0.01秒が一般的な値です。 休止による遅延は最短で指定した時間と同じになります。 サーバの負荷などが要因となり、より長くなる可能性があります。 特に、pg_sleep_untilは指定した時刻ちょうどに起床する保証はありませんが、それより早く起床することはありません。

警告

Make sure that your session does not hold more locks than necessary when calling <function>pg_sleep</function> or its variants. Otherwise other sessions might have to wait for your sleeping process, slowing down the entire system. pg_sleepまたはその亜種を呼び出す時、セッションが必要以上のロックを保持していないことを確実にしてください。 さもないと、他のセッションが休止中のプロセスを待機しなければならないかもしれません。そのためシステム全体の速度が低下することになるかもしれません。