Home · RSS · E-Mail · GitHub · GitLab · Twitter · LinkedIn

Introducing epoch

» Introduction

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:

1
2
$ epoch "2019-01-25 21:51:38 +0100 CET"
1548449498

Currently, it’s a quickly hacked tool, which solves my needs, but I thought it might be useful for other people, too.

» Installation

1
go get -u github.com/sj14/epoch

» Supported Formats

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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"

» Examples

» Timestamps to human readable format

seconds:

1
2
$ epoch 1548449513
2019-01-25 21:51:53 +0100 CET

nanoseconds:

1
2
$ epoch 1548449513940562000
2019-01-25 21:51:53.940562 +0100 CET

negative timestamp:

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

or using the pipe:

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

» Formatted input to epoch timestamps

seconds:

1
2
$ epoch "2019-01-25 21:51:38.272173 +0100 CET"
1548449498

nanoseconds:

1
2
$ epoch -nsec "2019-01-25 21:51:38.272173 +0100 CET"
1548449498272173000

Home · RSS · E-Mail · GitHub · GitLab · Twitter · LinkedIn