parent
849fa010b0
commit
fcbcf885e9
|
@ -0,0 +1,13 @@
|
|||
GO ?= go
|
||||
|
||||
.PHONY: vet
|
||||
vet:
|
||||
$(GO) vet ./...
|
||||
|
||||
.PHONY: fmt
|
||||
fmt:
|
||||
$(GO) fmt ./...
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
$(GO) test -race ./...
|
|
@ -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())
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue