Parsing a Unix Epoch With Bash/Ruby on the Command-Line

Featured image for sharing metadata for article

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

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #command-line #ruby #shell.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.