LoginSignup
2
0

More than 3 years have passed since last update.

MySQLで時差を補正(時刻の加算更新)したときのメモ

Last updated at Posted at 2020-02-27

米国山岳部標準時(MST)で記録されてしまったテーブルの日時フィールドを、SQLで日本標準時(JST)に書き替えた。
今後も度々ありそうなので、自分用にメモする。

SQLコマンド

MSTとJSTの時差である16時間を足す、という操作になる。

日時フィールドがDateTime型の場合

UPDATE t_rawdata SET TargetTime = (TargetTime + INTERVAL 16 HOUR) WHERE ID = xxx;

日時フィールドがUNIX timeの場合

日時フィールドがunsigned int型やtime_t型の UNIX time(協定世界時UTCの1970年1月1日からの経過秒数)なら秒換算する。

UPDATE t_rawdata SET TargetTime = (TargetTime + 16 * 60 * 60) WHERE ID = xxx;

UNIX time はfrom_unixtime()で整形できる。

SELECT
    ID,
    yyy,
    from_unixtime(min(TargetTime), "%Y/%m/%d %H:%i:%S") as start_time,
    from_unixtime(max(TargetTime), "%Y/%m/%d %H:%i:%S") as end_time
FROM t_rawdata
WHERE ID = xxx
GROUP BY yyy ORDER BY end_time;

シェルスクリプトからならdate -d '@1234567890'という感じ。

UNIX timeをExcelで表示するには

= ( UNIX time の列名 + 32400 ) / 86400 + 25569 でExcelのシリアル値に変換できる。

image.png

シリアル値は、[セルの書式設定] から任意の日付フォーマットで表示できる。
image.png

2
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
2
0