Browse Source

Commit epoch

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 7 months ago
parent
commit
fcbcf885e9
Signed by: jolheiser GPG Key ID: B853ADA5DA7BBF7A
  1. 13
      Makefile
  2. 21
      epoch/epoch.go
  3. 24
      epoch/epoch_test.go
  4. 3
      go.mod

13
Makefile

@ -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

@ -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())
}

24
epoch/epoch_test.go

@ -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

@ -0,0 +1,3 @@
module go.jolheiser.com/go-common
go 1.16
Loading…
Cancel
Save