Commit epoch

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2021-03-17 22:30:56 -05:00
parent 849fa010b0
commit fcbcf885e9
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
4 changed files with 61 additions and 0 deletions

13
Makefile 100644
View File

@ -0,0 +1,13 @@
GO ?= go
.PHONY: vet
vet:
$(GO) vet ./...
.PHONY: fmt
fmt:
$(GO) fmt ./...
.PHONY: test
test:
$(GO) test -race ./...

21
epoch/epoch.go 100644
View File

@ -0,0 +1,21 @@
package epoch
import "time"
// Zero passes time.Time.IsZero
const Zero = Epoch(-62135596800)
// Epoch is a unix timestamp (seconds)
//
// Any time-specific calculations should be done with Epoch.Time
type Epoch int64
// Time returns the corresponding time.Time from this Epoch
func (e Epoch) Time() time.Time {
return time.Unix(int64(e), 0)
}
// Now returns the current Unix Epoch
func Now() Epoch {
return Epoch(time.Now().Unix())
}

View File

@ -0,0 +1,24 @@
package epoch
import (
"testing"
"time"
)
const secs = 1616025600
func TestEpoch(t *testing.T) {
e := Epoch(secs)
u := time.Unix(secs, 0)
if e.Time() != u {
t.Log("epoch did not match time with matching seconds")
t.FailNow()
}
}
func TestEpochZero(t *testing.T) {
if !Zero.Time().IsZero() {
t.Log("epoch did not match zero-time")
t.FailNow()
}
}

3
go.mod 100644
View File

@ -0,0 +1,3 @@
module go.jolheiser.com/go-common
go 1.16