

Amazon Timestream for LiveAnalytics に類似した機能をご希望の場合は Amazon Timestream for InfluxDB をご検討ください。リアルタイム分析に適した、シンプルなデータインジェストと 1 桁ミリ秒のクエリ応答時間を特徴としています。詳細については、[こちら](https://docs.aws.amazon.com//timestream/latest/developerguide/timestream-for-influxdb.html)を参照してください。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 日付/時間関数
<a name="date-time-functions"></a>

**注記**  
Timestream for LiveAnalytics は負の時間値をサポートしていません。負の時間になるオペレーションはエラーになります。

Timestream for LiveAnalytics は、日付と時刻に UTC タイムゾーンを使用します。Timestream は、次の日付と時刻の関数をサポートしています。

**Topics**
+ [一般および変換](#date-time-functions-general)
+ [間隔と期間](#date-time-functions-interval-duration)
+ [フォーマットと解析](#date-time-functions-formatting-parsing)
+ [抽出](#date-time-functions-extraction)

## 一般および変換
<a name="date-time-functions-general"></a>

Timestream for LiveAnalytics は、日付と時刻に関する次の一般的な関数と変換関数をサポートしています。


| 関数 | 出力データ型 | 説明 | 
| --- | --- | --- | 
|  current\$1date  |  date  |  UTC で現在の日付を返します。括弧は使用されません。 <pre>SELECT current_date</pre> 結果の例: `2022-07-07`  これは予約キーワードでもあります。予約キーワードの一覧については、「[予約済みキーワード](ts-limits.md#limits.reserved)」を参照してください。   | 
|  current\$1time  |  時間  |  UTC で現在の時刻を返します。括弧は使用されません。 <pre>SELECT current_time</pre> 結果の例: `17:41:52.827000000`  これは予約キーワードでもあります。予約キーワードの一覧については、「[予約済みキーワード](ts-limits.md#limits.reserved)」を参照してください。   | 
|  current\$1timestamp または now()  |  timestamp  |  UTC で現在のタイムスタンプを返します。 <pre>SELECT current_timestamp</pre> 結果の例: `2022-07-07 17:42:32.939000000`  これは予約キーワードでもあります。予約キーワードの一覧については、「[予約済みキーワード](ts-limits.md#limits.reserved)」を参照してください。   | 
|  current\$1timezone()  |  varchar 値は「UTC」になります。  |  Timestream は、日付と時刻に UTC タイムゾーンを使用します。 <pre>SELECT current_timezone()</pre> 結果の例: `UTC`  | 
|  date(varchar(x))、date(timestamp)  |  date  |  <pre>SELECT date(TIMESTAMP '2022-07-07 17:44:43.771000000')</pre> 結果の例: `2022-07-07`  | 
|  last\$1day\$1of\$1month(timestamp)、last\$1day\$1of\$1month(date)  |  date  |  <pre>SELECT last_day_of_month(TIMESTAMP '2022-07-07 17:44:43.771000000')</pre> 結果の例: `2022-07-31`  | 
|  from\$1iso8601\$1timestamp(string)  |  timestamp  |  ISO 8601 タイムスタンプを内部タイムスタンプ形式に解析します。 <pre>SELECT from_iso8601_timestamp('2022-06-17T08:04:05.000000000+05:00')</pre> 結果の例: `2022-06-17 03:04:05.000000000`  | 
|  from\$1iso8601\$1date(string)  |  date  |  ISO 8601 の日付文字列を、指定された日付の UTC 00:00:00 の内部タイムスタンプ形式に解析します。 <pre>SELECT from_iso8601_date('2022-07-17')</pre> 結果の例: `2022-07-17`  | 
|  to\$1iso8601(timestamp)、to\$1iso8601(date)  |  varchar  |  入力の ISO 8601 形式の文字列を返します。 <pre>SELECT to_iso8601(from_iso8601_date('2022-06-17'))</pre> 結果の例: `2022-06-17`  | 
|  from\$1milliseconds(bigint)  |  timestamp  |  <pre>SELECT from_milliseconds(1)</pre> 結果の例: `1970-01-01 00:00:00.001000000`  | 
|  from\$1nanoseconds(bigint)  |  timestamp  |  <pre>select from_nanoseconds(300000001)</pre> 結果の例: `1970-01-01 00:00:00.300000001`  | 
|  from\$1unixtime(double)  |  timestamp  |  指定された unixtime に対応するタイムスタンプを返します。 <pre>SELECT from_unixtime(1)</pre> 結果の例: `1970-01-01 00:00:01.000000000`  | 
|  localtime  |  時間  |  UTC で現在の時刻を返します。括弧は使用されません。 <pre>SELECT localtime</pre> 結果の例: `17:58:22.654000000`  これは予約キーワードでもあります。予約キーワードの一覧については、「[予約済みキーワード](ts-limits.md#limits.reserved)」を参照してください。   | 
|  localtimestamp  |  timestamp  |  UTC で現在のタイムスタンプを返します。括弧は使用されません。 <pre>SELECT localtimestamp</pre> 結果の例: `2022-07-07 17:59:04.368000000`  これは予約キーワードでもあります。予約キーワードの一覧については、「[予約済みキーワード](ts-limits.md#limits.reserved)」を参照してください。   | 
|  to\$1milliseconds(interval day to second)、to\$1milliseconds(timestamp)  |  bigint  |  <pre>SELECT to_milliseconds(INTERVAL '2' DAY + INTERVAL '3' HOUR)</pre> 結果の例: `183600000` <pre>SELECT to_milliseconds(TIMESTAMP '2022-06-17 17:44:43.771000000')</pre> 結果の例: `1655487883771`  | 
|  to\$1nanoseconds(interval day to second)、to\$1nanoseconds(timestamp)  |  bigint  |  <pre>SELECT to_nanoseconds(INTERVAL '2' DAY + INTERVAL '3' HOUR)</pre> 結果の例: `183600000000000` <pre>SELECT to_nanoseconds(TIMESTAMP '2022-06-17 17:44:43.771000678')</pre> 結果の例: `1655487883771000678`  | 
|  to\$1unixtime(timestamp)  |  double  |  指定されたタイムスタンプの unixtime を返します。 <pre>SELECT to_unixtime('2022-06-17 17:44:43.771000000')</pre> 結果の例: `1.6554878837710001E9`  | 
|  date\$1trunc(unit, timestamp)  |  timestamp  |  単位に切り捨てられたタイムスタンプを返します。単位は [秒、分、時間、日、週、月、四半期、年] のいずれかです。 <pre>SELECT date_trunc('minute', TIMESTAMP '2022-06-17 17:44:43.771000000')</pre> 結果の例: `2022-06-17 17:44:00.000000000`  | 

## 間隔と期間
<a name="date-time-functions-interval-duration"></a>

Timestream for LiveAnalytics は、日付と時刻に関する次の間隔関数と期間関数をサポートしています。


| 関数 | 出力データ型 | 説明 | 
| --- | --- | --- | 
|  date\$1add(unit, bigint, date)、date\$1add(unit, bigint, time)、date\$1add(varchar(x), bigint, timestamp)  |  timestamp  |  単位の bigint を追加します。単位は [秒、分、時間、日、週、月、四半期、年] のいずれかです。 <pre>SELECT date_add('hour', 9, TIMESTAMP '2022-06-17 00:00:00')</pre> 結果の例: `2022-06-17 09:00:00.000000000`  | 
|  date\$1diff(unit, date, date)、date\$1diff(unit, time, time)、date\$1diff(unit, timestamp, timestamp)  |  bigint  |  差を返します。単位は [秒、分、時間、日、週、月、四半期、年] のいずれかです。 <pre>SELECT date_diff('day', DATE '2020-03-01', DATE '2020-03-02')</pre> 結果の例: `1`  | 
|  parse\$1duration(string)  |  interval  |  入力文字列を解析して `interval` の同値を返します。 <pre>SELECT parse_duration('42.8ms')</pre> 結果の例: `0 00:00:00.042800000` <pre>SELECT typeof(parse_duration('42.8ms'))</pre> 結果の例: `interval day to second`  | 
| bin(timestamp, interval) | timestamp |  `timestamp` パラメータの整数値を、`interval` パラメータの整数値の最も近い倍数に切り下げます。 この戻り値の意味は明確でない場合があります。最初に整数演算を使用してタイムスタンプ整数を間隔整数で除算し、次にその結果を間隔整数で乗算して計算します。 タイムスタンプは、POSIX エポック (1970 年 1 月 1 日) から経過した 1 秒の小数点以下の数値として UTC ポイントを指定することに注意してください。戻り値が暦の単位と一致することはほとんどありません。例えば、30 日間の間隔を指定すると、エポックからのすべての日が増分で 30 日単位に分割され、暦月とは関係のない直近の増分の 30 日の開始値が返されます。 次に例を示します。 <pre>bin(TIMESTAMP '2022-06-17 10:15:20', 5m)     ==> 2022-06-17 10:15:00.000000000<br />bin(TIMESTAMP '2022-06-17 10:15:20', 1d)     ==> 2022-06-17 00:00:00.000000000<br />bin(TIMESTAMP '2022-06-17 10:15:20', 10day)  ==> 2022-06-17 00:00:00.000000000<br />bin(TIMESTAMP '2022-06-17 10:15:20', 30day)  ==> 2022-05-28 00:00:00.000000000</pre>  | 
|  ago(interval)  |  timestamp  |  current\$1timestamp `interval` に対応する値を返します。 <pre>SELECT ago(1d)</pre> 結果の例: `2022-07-06 21:08:53.245000000`  | 
|  1h、1d、30m などの間隔リテラル  |  interval  |  間隔リテラルは、parse\$1duration(string) の便利な機能です。例えば、`1d` は `parse_duration('1d')` と同じです。これにより、間隔が使用されるすべての場所でリテラルを使用できます。例えば、`ago(1d)` と `bin(<timestamp>, 1m)` です。  | 

一部の間隔リテラルは、parse\$1duration の簡易表現として機能します。例えば、`parse_duration('1day')`、`1day`、`parse_duration('1d')`、`1d` はそれぞれ、型が `interval day to second` である `1 00:00:00.000000000` を返します。スペースは、`parse_duration` の指定された形式で許可されます。例えば、`parse_duration('1day')` は `00:00:00.000000000` も返します。ただし、`1 day` は間隔リテラルではありません。

`interval day to second` に関連する単位は、ns、ナノ秒、us、マイクロ秒、ms、ミリ秒、s、秒、m、分、h、時間、d、日です。

`interval year to month` もあります。interval year to month に関連する単位は、y、年、月です。例えば、`SELECT 1year` は `1-0` を返します。`SELECT 12month` も `1-0` を返します。`SELECT 8month` は `0-8` を返します。

`quarter` の単位は、`date_trunc`、`date_add`、`quarter` などの一部の関数でも使用できますが、間隔リテラルの一部としては使用できません。

## フォーマットと解析
<a name="date-time-functions-formatting-parsing"></a>

Timestream for LiveAnalytics は、日付と時刻に関する次のフォーマット関数と解析関数をサポートしています。


| 関数 | 出力データ型 | 説明 | 
| --- | --- | --- | 
|  date\$1format(timestamp, varchar(x))  |  varchar  |  この関数で使用される書式指定子の詳細については、[https://trino.io/docs/current/functions/datetime.html\$1mysql-date-functions](https://trino.io/docs/current/functions/datetime.html#mysql-date-functions) を参照してください。 <pre>SELECT date_format(TIMESTAMP '2019-10-20 10:20:20', '%Y-%m-%d %H:%i:%s')</pre> 結果の例: `2019-10-20 10:20:20`  | 
|  date\$1parse(varchar(x), varchar(y))  |  timestamp  |  この関数で使用される書式指定子の詳細については、[https://trino.io/docs/current/functions/datetime.html\$1mysql-date-functions](https://trino.io/docs/current/functions/datetime.html#mysql-date-functions) を参照してください。 <pre>SELECT date_parse('2019-10-20 10:20:20', '%Y-%m-%d %H:%i:%s')</pre> 結果の例: `2019-10-20 10:20:20.000000000`  | 
|  format\$1datetime(timestamp, varchar(x))  |  varchar  |  この関数で使用される書式文字列の詳細については、[http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html) を参照してください。 <pre>SELECT format_datetime(parse_datetime('1968-01-13 12', 'yyyy-MM-dd HH'), 'yyyy-MM-dd HH')</pre> 結果の例: `1968-01-13 12`  | 
|  parse\$1datetime(varchar(x), varchar(y))  |  timestamp  |  この関数で使用される書式文字列の詳細については、[http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html) を参照してください。 <pre>SELECT parse_datetime('2019-12-29 10:10 PST', 'uuuu-LL-dd HH:mm z')</pre> 結果の例: `2019-12-29 18:10:00.000000000`  | 

## 抽出
<a name="date-time-functions-extraction"></a>

Timestream for LiveAnalytics は、日付と時刻に関する次の抽出関数をサポートしています。抽出関数は利便性が高く、残りの関数の基礎となります。


| 関数 | 出力データ型 | 説明 | 
| --- | --- | --- | 
|  extract  |  bigint  |  タイムスタンプからフィールドを抽出します。ここで、フィールドは [YEAR、QUARTER、MONTH、WEEK、DAY、DAY\$1OF\$1MONTH、DAY\$1OF\$1WEEK、DOW、DAY\$1OF\$1YEAR、DOY、YEAR\$1OF\$1WEEK、YOW、HOUR、MINUTE、SECOND] のいずれかです。 <pre>SELECT extract(YEAR FROM '2019-10-12 23:10:34.000000000')</pre> 結果の例: `2019`  | 
|  day(timestamp)、day(date)、day(interval day to second)  |  bigint  |  <pre>SELECT day('2019-10-12 23:10:34.000000000')</pre> 結果の例: `12`  | 
|  day\$1of\$1month(timestamp)、day\$1of\$1month(date)、day\$1of\$1month(interval day to second)  |  bigint  |  <pre>SELECT day_of_month('2019-10-12 23:10:34.000000000')</pre> 結果の例: `12`  | 
|  day\$1of\$1week(timestamp)、day\$1of\$1week(date)  |  bigint  |  <pre>SELECT day_of_week('2019-10-12 23:10:34.000000000')</pre> 結果の例: `6`  | 
|  day\$1of\$1year(timestamp)、day\$1of\$1year(date)  |  bigint  |  <pre>SELECT day_of_year('2019-10-12 23:10:34.000000000')</pre> 結果の例: `285`  | 
|  dow(timestamp)、dow(date)  |  bigint  |  day\$1of\$1week のエイリアス  | 
|  doy(timestamp)、doy(date)  |  bigint  |  day\$1of\$1year のエイリアス  | 
|  hour(timestamp)、hour(time)、hour(interval day to second)  |  bigint  |  <pre>SELECT hour('2019-10-12 23:10:34.000000000')</pre> 結果の例: `23`  | 
|  millisecond(timestamp)、millisecond(time)、millisecond(interval day to second)  |  bigint  |  <pre>SELECT millisecond('2019-10-12 23:10:34.000000000')</pre> 結果の例: `0`  | 
|  minute(timestamp)、minute(time)、minute(interval day to second)  |  bigint  |  <pre>SELECT minute('2019-10-12 23:10:34.000000000')</pre> 結果の例: `10`  | 
|  month(timestamp)、month(date)、month(interval year to month)  |  bigint  |  <pre>SELECT month('2019-10-12 23:10:34.000000000')</pre> 結果の例: `10`  | 
|  nanosecond(timestamp)、nanosecond(time)、nanosecond(interval day to second)  |  bigint  |  <pre>SELECT nanosecond(current_timestamp)</pre> 結果の例: `162000000`  | 
|  quarter(timestamp)、quarter(date)  |  bigint  |  <pre>SELECT quarter('2019-10-12 23:10:34.000000000')</pre> 結果の例: `4`  | 
|  second(timestamp)、second(time)、second(interval day to second)  |  bigint  |  <pre>SELECT second('2019-10-12 23:10:34.000000000')</pre> 結果の例: `34`  | 
|  week(timestamp)、week(date)  |  bigint  |  <pre>SELECT week('2019-10-12 23:10:34.000000000')</pre> 結果の例: `41`  | 
|  week\$1of\$1year(timestamp)、week\$1of\$1year(date)  |  bigint  |  週のエイリアス  | 
|  year(timestamp)、year(date)、year(interval year to month)  |  bigint  |  <pre>SELECT year('2019-10-12 23:10:34.000000000')</pre> 結果の例: `2019`  | 
|  year\$1of\$1week(timestamp)、year\$1of\$1week(date)  |  bigint  |  <pre>SELECT year_of_week('2019-10-12 23:10:34.000000000')</pre> 結果の例: `2019`  | 
|  yow(timestamp)、yow(date)  |  bigint  |  year\$1of\$1week のエイリアス  | 