Parsing a Unix Epoch With Bash/Ruby on the Command-Line
Every so often I have to work with Unix Epochs, and am forever annoyed I can't convert them in my head (a little sarcasm).
I wanted an easy scripted alternative for how to do it myself, and have two options (so far):
Let's say that we have the epoch date 1516239022
.
With date
Using the coreutils, we can use the date
command-line tool:
$ date --date='@1516239022'
Thu 18 Jan 01:30:22 GMT 2018
# alternatively, using the BSD `date` command, i.e. on MacOS
$ date -r 1516239022
Thu 18 Jan 01:30:22 GMT 2018
Which we can convert to a handy function:
function epoch() {
date --date="@$1"
}
With Ruby
Alternatively we can do this with Ruby, my preferred scripting language:
$ ruby -rtime -e 'puts Time.at(ARGF.read.to_i)' <<< 1516239022
# or by using the clipboard
$ xsel | ruby -rtime -e 'puts Time.at(ARGF.read.to_i)'
# or just as an argument
$ ruby -rtime -e 'puts Time.at(ARGV[0].to_i)' 1516239022