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 3 years have passed since last update.

Splunk: 秒単位の値をHH:MM:SS形式に変換してみる

Posted at

ときどき必要となるのでメモ。

実施環境: Splunk Free 8.2.2

Splunk のジョブの実行時間ログなどは秒単位の値で出力されます。
それを HH : MM : SS 形式に変換する方法を考えてみます。

まず、元の秒単位の値を以下のように切り分けます。

  • 時 : 60で2回割り、小数点以下を切り下げる
  • 分 : 60で1回割り、小数点以下を切り下げ、60で割った余りをとる
  • 秒 : 60で割った余りをとり、小数点以下を切り下げる
  • ミリ秒 : 小数点以下を切り下げた値を元の値から引き、1000をかける
Splunk
| makeresults
| eval SECONDS = 7650.001
| eval TIME_H = floor(SECONDS / 60  / 60),
       TIME_M = floor(SECONDS / 60) % 60,
       TIME_S = floor(SECONDS % 60),
       TIME_Q = (SECONDS - floor(SECONDS)) * 1000

スクリーンショット 2021-12-19 20.48.00.png

これだけだと上記のように桁数が揃わないので、 printf 関数を使用して桁数を揃えます。

Splunk
| makeresults
| eval SECONDS = 7650.001
| eval TIME_H = printf("%02d", floor(SECONDS / 60  / 60)),
       TIME_M = printf("%02d", floor(SECONDS / 60) % 60),
       TIME_S = printf("%02d", floor(SECONDS % 60)),
       TIME_Q = printf("%03d", (SECONDS - floor(SECONDS)) * 1000)

スクリーンショット 2021-12-19 20.46.38.png

あとはこれらを文字列として結合すれば完了です。

Splunk
| makeresults
| eval SECONDS = 7650.001
| eval TIME_H = printf("%02d", floor(SECONDS / 60  / 60)),
       TIME_M = printf("%02d", floor(SECONDS / 60) % 60),
       TIME_S = printf("%02d", floor(SECONDS % 60)),
       TIME_Q = printf("%03d", (SECONDS - floor(SECONDS)) * 1000)
| eval TIME = TIME_H + ":" + TIME_M + ":" + TIME_S + "." + TIME_Q
| table TIME, SECONDS

スクリーンショット 2021-12-19 20.44.00.png

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?