すべてのプロダクト
Search
ドキュメントセンター

MaxCompute:FROM_UTC_TIMESTAMP

最終更新日:Jan 17, 2025

協定世界時 (UTC) から指定されたタイムゾーンに変換されたタイムスタンプを返します。 この関数は、MaxCompute V2.0の追加関数です。

使用上の注意

FROM_UTC_TIMESTAMP関数の戻り値は、プロジェクト用に構成したodps.sql.timezoneの値の影響も受けます。 odps.sql.timezoneをAsia/Shanghaiに設定した場合、UTC + 8のタイムゾーンが使用されます。これはUTCより8時間進んでいます。 この場合、FROM_UTC_TIMESTAMP関数の計算結果に8時間が加算されます。 たとえば、FROM_UTC_TIMESTAMP(0, 'Asia/Shanghai') 関数は、UTC + 0のタイムスタンプを北京時間 (UTC + 08:00) のタイムスタンプに変換し、計算結果は0 + 8 × 3600 = 28800になります。 odps.sql.timezone=Asia/Shanghai設定には、さらに8時間のオフセットが必要です。 その結果、戻り値は1970-01-01 16:00:00となる。

構文

timestamp from_utc_timestamp({any primitive type}*, string <timezone>)

パラメーター

  • {any primitive type}*: 必須です。 timestamp、DATETIME、TINYINT、SMALLINT、INT、またはBIGINTタイプのタイムスタンプ。 値がTINYINT、SMALLINT、INT、またはBIGINTタイプの場合、時間単位はミリ秒の精度です。

  • timezone: 必須です。 新しいタイムゾーン。

    説明

    検索エンジンを使用して、タイムゾーンリストを検索できます。 サポートされているタイムゾーンの詳細については、「タイムゾーン」をご参照ください。

戻り値

TIMESTAMP型の値が返されます。 戻り値はyyyy-mm-dd hh:mi:ss.ff3形式です。 戻り値は、次のルールによって異なります。

  • {any primitive type}* の値がTIMESTAMP、DATETIME、TINYINT、SMALLINT、INT、またはBIGINT型でない場合、エラーが返されます。

  • {any primitive type}* の値がnullの場合、エラーが返されます。

  • timezoneの値がnullの場合、戻り値はnullになります。

サンプルデータ

このセクションでは、日付関数の使用方法を理解するためのサンプルソースデータを提供します。 このトピックでは、mf_date_fun_tという名前のテーブルが作成され、データがテーブルに挿入されます。 サンプル文:

create table if not exists mf_date_fun_t(
    id      int,
    date1   date,
    datetime1   datetime,
    timestamp1 timestamp,
    date2   date,
    datetime2   datetime,
    timestamp2 timestamp,
    date3 string,
    date4 bigint);
insert into mf_date_fun_t values
(1,DATE'2021-11-29',DATETIME'2021-11-29 00:01:00',TIMESTAMP'2021-01-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-20',123456780),
(2,DATE'2021-11-28',DATETIME'2021-11-28 00:02:00',TIMESTAMP'2021-02-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-21',123456781),
(3,DATE'2021-11-27',DATETIME'2021-11-27 00:03:00',TIMESTAMP'2021-03-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-22',123456782),
(4,DATE'2021-11-26',DATETIME'2021-11-26 00:04:00',TIMESTAMP'2021-04-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-23',123456783),
(5,DATE'2021-11-25',DATETIME'2021-11-25 00:05:00',TIMESTAMP'2021-05-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-24',123456784),
(6,DATE'2021-11-24',DATETIME'2021-11-24 00:06:00',TIMESTAMP'2021-06-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-25',123456785),
(7,DATE'2021-11-23',DATETIME'2021-11-23 00:07:00',TIMESTAMP'2021-07-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-26',123456786),
(8,DATE'2021-11-22',DATETIME'2021-11-22 00:08:00',TIMESTAMP'2021-08-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-27',123456787),
(9,DATE'2021-11-21',DATETIME'2021-11-21 00:09:00',TIMESTAMP'2021-09-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-28',123456788),
(10,DATE'2021-11-20',DATETIME'2021-11-20 00:10:00',TIMESTAMP'2021-10-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-29',123456789);

mf_date_fun_tテーブルからデータを照会します。 例:

select * from mf_date_fun_t;
-- The following result is returned: 
+------+-------+------------+------------+-------+------------+------------+-------+------------+
| id   | date1 | datetime1  | timestamp1 | date2 | datetime2  | timestamp2 | date3 | date4      |
+------+-------+------------+------------+-------+------------+------------+-------+------------+
| 1    | 2021-11-29 | 2021-11-29 00:01:00 | 2021-01-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-20 | 123456780  |
| 2    | 2021-11-28 | 2021-11-28 00:02:00 | 2021-02-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-21 | 123456781  |
| 3    | 2021-11-27 | 2021-11-27 00:03:00 | 2021-03-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-22 | 123456782  |
| 4    | 2021-11-26 | 2021-11-26 00:04:00 | 2021-04-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-23 | 123456783  |
| 5    | 2021-11-25 | 2021-11-25 00:05:00 | 2021-05-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-24 | 123456784  |
| 6    | 2021-11-24 | 2021-11-24 00:06:00 | 2021-06-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-25 | 123456785  |
| 7    | 2021-11-23 | 2021-11-23 00:07:00 | 2021-07-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-26 | 123456786  |
| 8    | 2021-11-22 | 2021-11-22 00:08:00 | 2021-08-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-27 | 123456787  |
| 9    | 2021-11-21 | 2021-11-21 00:09:00 | 2021-09-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-28 | 123456788  |
| 10   | 2021-11-20 | 2021-11-20 00:10:00 | 2021-10-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-29 | 123456789  |
+------+-------+------------+------------+-------+------------+------------+-------+------------+

例: 静的データ

-- The time unit of the input value is accurate to the millisecond and the return value is 2017-08-01 04:24:00.0. 
select from_utc_timestamp(1501557840000, 'PST'); 
-- The return value is 1970-01-30 08:00:00.0. 
select from_utc_timestamp('1970-01-30 16:00:00','PST'); 
-- The return value is 1970-01-29 16:00:00.0. 
select from_utc_timestamp('1970-01-30','PST'); 
-- Enable the MaxCompute V2.0 data type edition. Commit the following SET statement along with the SQL statement. The return value is 2011-12-25 17:00:00:00.123. 
set odps.sql.type.system.odps2=true;
select from_utc_timestamp(timestamp '2011-12-25 09:00:00.123456', 'Asia/Shanghai');
-- Enable the MaxCompute V2.0 data type edition. Commit the following SET statement along with the SQL statement. The return value is 2011-12-25 01:55:00.0. 
set odps.sql.type.system.odps2=true;
select from_utc_timestamp(timestamp '2011-12-25 06:55:00', 'America/Toronto');
-- The return value is null. 
select from_utc_timestamp('1970-01-30',null);

例: テーブルデータ

datetime1列とtimestamp1列の日付値を指定したタイムゾーンのタイムスタンプに変換します。 この例では、サンプルデータのデータを使用します。 サンプル文:

-- Enable the MaxCompute V2.0 data type edition. Commit the following SET statement along with the SQL statement. 
set odps.sql.type.system.odps2=true;
select datetime1, from_utc_timestamp(datetime1,'PST') pst, timestamp1, from_utc_timestamp(timestamp1,'Asia/Shanghai') asia from mf_date_fun_t;

次の応答が返されます。

+---------------------+---------------------+-------------------------------+-------------------------------+
| datetime1           | pst                 | timestamp1                    | asia                          |
+---------------------+---------------------+-------------------------------+-------------------------------+
| 2021-11-29 00:01:00 | 2021-11-28 16:01:00 | 2021-01-11 00:00:00.123456789 | 2021-01-11 08:00:00.123456789 |
| 2021-11-28 00:02:00 | 2021-11-27 16:02:00 | 2021-02-11 00:00:00.123456789 | 2021-02-11 08:00:00.123456789 |
| 2021-11-27 00:03:00 | 2021-11-26 16:03:00 | 2021-03-11 00:00:00.123456789 | 2021-03-11 08:00:00.123456789 |
| 2021-11-26 00:04:00 | 2021-11-25 16:04:00 | 2021-04-11 00:00:00.123456789 | 2021-04-11 08:00:00.123456789 |
| 2021-11-25 00:05:00 | 2021-11-24 16:05:00 | 2021-05-11 00:00:00.123456789 | 2021-05-11 08:00:00.123456789 |
| 2021-11-24 00:06:00 | 2021-11-23 16:06:00 | 2021-06-11 00:00:00.123456789 | 2021-06-11 08:00:00.123456789 |
| 2021-11-23 00:07:00 | 2021-11-22 16:07:00 | 2021-07-11 00:00:00.123456789 | 2021-07-11 08:00:00.123456789 |
| 2021-11-22 00:08:00 | 2021-11-21 16:08:00 | 2021-08-11 00:00:00.123456789 | 2021-08-11 08:00:00.123456789 |
| 2021-11-21 00:09:00 | 2021-11-20 16:09:00 | 2021-09-11 00:00:00.123456789 | 2021-09-11 08:00:00.123456789 |
| 2021-11-20 00:10:00 | 2021-11-19 16:10:00 | 2021-10-11 00:00:00.123456789 | 2021-10-11 08:00:00.123456789 |
+---------------------+---------------------+-------------------------------+-------------------------------+

関連関数

FROM_UTC_TIMESTAMPは日付関数です。 日付の計算と変換に関連する関数の詳細については、日付関数.