ようへいの日々精進XP

よかろうもん

ワンラインで Unixtime から Datetime に変換したいくさ

はじめに

ぽーんと unixtime を渡されても、これはいったい何時何分なんやって思うことがある。

そんな時にコマンドラインでちゃちゃっと変換出来たらカッコイイかな…ということで…

Ruby だと

$ ruby -v
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]

$ ruby -e 'p Time.at(1495248989)'
2017-05-20 02:56:29 +0000

Python だと

$ python -V
Python 2.7.12

$ python -c 'import datetime; print datetime.datetime.fromtimestamp(1495248989)'
2017-05-20 02:56:29

bash だと

ubuntu-xenial だと…

$ date -d @1495248989 もしくは date --date='@1495248989'
Sat May 20 02:56:29 UTC 2017

man date すると以下のように Example が提供されている。

...
EXAMPLES
       Convert seconds since the epoch (1970-01-01 UTC) to a date

              $ date --date='@2147483647'
...

ちなみに、MacOS X El Capitan だと…

$ date -r 1495248989
Sat May 20 11:56:29 JST 2017

こちらも man date すると以下のように記述されている。

     -r seconds
             Print the date and time represented by seconds, where seconds is the number of seconds since the Epoch (00:00:00 UTC, January 1, 1970; see time(3)), and can be specified in
             decimal, octal, or hex.

以上

メモでした。

BashRuby が少しばかりシンプルですな。