Convert epoch timestamps to human readable formats and vice versa.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
epoch
.gitignore
LICENSE
README.md
go.mod
go.sum
main.go

README.md

epoch

GoDoc

epoch converts unix timestamps to human readable formats and vice versa.

Why?
To convert timestamps to dates, you have to run different commands for Linux and macOS: date -d @1267619929 vs date -r 1267619929, and what about handling nanosecond timestamps? Seriously, I don't know how to do this with date.
Furthermore, have you ever tried converting a time formatted string such as "2019-01-25 21:51:38 +0100 CET" to a timestamp? Of course, you can do this somehow, but all ways I've found so far were too cumbersome. This tool tries to solve all this with ease:

$ epoch "2019-01-25 21:51:38 +0100 CET"
1548449498
$ epoch 1548449498
Fri Jan 25 21:51:38 CET 2019

The functionallity is implemented as a package and can be used in other programs.

Installation

go get -u github.com/sj14/epoch

Examples

Timestamps to human readable format

conversion based on given unit

$ epoch -unit s 1548449513
Fri Jan 25 21:51:53 CET 2019
$ epoch -unit ms 1548449513
Sun Jan 18 23:07:29 CET 1970
$ epoch -unit us 1548449513
Thu Jan  1 01:25:48 CET 1970
$ epoch -unit ns 1548449513
Thu Jan  1 01:00:01 CET 1970

set the output format

$ epoch -unit ms -format rfc850 1548449513
Sunday, 18-Jan-70 23:07:29 CET
$ epoch -unit ms -format ruby 1548449513
Sun Jan 18 23:07:29 +0100 1970
$ epoch -unit ms -format ansic 1548449513
Sun Jan 18 23:07:29 1970

guess the unit

Guess the unit. Internally, the guess is done by comparing the number of digits with the current epoch timestamps (in s, ms, us, ns) of your machine. The smallest difference wins.

seconds:

$ epoch 1548449513
guessed unit seconds
2019-01-25 21:51:53 +0100 CET

milliseconds:

$ epoch 1548449513940
guessed unit milliseconds
2019-01-25 21:51:53 +0100 CET

microseconds:

$ epoch 1548449513940562
guessed unit microseconds
2019-01-25 21:51:53.940562 +0100 CET

nanoseconds:

$ epoch 1548449513940562000
guessed unit nanoseconds
2019-01-25 21:51:53.940562 +0100 CET

negative timestamp

$ epoch -- -15484495
1969-07-05 19:45:05 +0100 CET

using the pipe

$ echo -15484495 | epoch
1969-07-05 19:45:05 +0100 CET

Formatted input to epoch timestamps

seconds (default when no unit flag given):

$ epoch -unit s "2019-01-25 21:51:38.272173 +0100 CET"
1548449498

milliseconds:

$ epoch -unit ms "2019-01-25 21:51:38.272173 +0100 CET"
1548449498272

microseconds:

$ epoch -unit us "2019-01-25 21:51:38.272173 +0100 CET"
1548449498272173

nanoseconds:

$ epoch -unit ns "2019-01-25 21:51:38.272173 +0100 CET"
1548449498272173000

Supported Formats

All current Go formats as of 2019-01-26 (https://golang.org/pkg/time/#pkg-constants):

ANSIC       = "Mon Jan _2 15:04:05 2006"
UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
RFC822      = "02 Jan 06 15:04 MST"
RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339     = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen     = "3:04PM"
// Handy time stamps.
Stamp      = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano  = "Jan _2 15:04:05.000000000"
// HTTP Timestamp time.RFC1123 but hard-codes GMT as the time zone.
HTTP = "Mon, 02 Jan 2006 15:04:05 GMT"