0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

H2 DatabaseでTIMESTAMP・DATEをUNIX時間に変換する

Posted at

概要

H2DataBaseで、TIMESTAMP・DATE型の値の、UNIXエポック、すなわち協定世界時 (UTC) での1970年1月1日真夜中(午前0時0分0秒)の時刻からの形式的な経過秒数(Wikipediaより引用)を求めます。

使用する関数

DATEDIFF関数
DATEDIFF(unitString, aTimestamp, bTimestamp)

  • 2つのタイムスタンプ間の差(bTimestamp - aTimestamp)を、指定された単位を基準にしてlong型で返します。
  • 単位を指定するunitStringの引数には、year month day hour minute second millisecond等が使えます。
  • aTimestamp > bTimestampの場合は負の数を返します。

詳しくは上のリンク先を参照してください。

実行例

UNIXエポックからの経過年数

コンソール
select DATEDIFF('year',timestamp '1970-01-01 00:00:00' ,  date '2012-05-05')
結果
42
(1 行, 0 ms)

UNIXエポックからの経過秒数

コンソール
select DATEDIFF('second',timestamp '1970-01-01 00:00:00' ,  date '2012-05-05')
結果
1336176000
(1 行, 13 ms)

UNIXエポックからの経過週数

コンソール
select DATEDIFF('day',timestamp '1970-01-01 00:00:00' ,  date '2012-05-05')/7;
結果
2209
(1 行, 2 ms)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?