1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-06-16 04:44:55 +02:00

Adding src/utils/time.go

This commit is contained in:
Henrik Hautakoski 2022-08-18 12:01:14 +02:00
parent c8a128b657
commit d3d0fb11d7
No known key found for this signature in database
GPG key ID: 608414D93E862CCD
2 changed files with 48 additions and 0 deletions

22
src/utils/time.go Normal file
View file

@ -0,0 +1,22 @@
package utils
import (
"time"
)
type Time struct {
ts time.Time
}
func (t *Time) SetTime(value time.Time) {
t.ts = value
}
func (t Time) GetTime() time.Time {
if ! t.ts.IsZero() {
return t.ts
}
return time.Now().In(time.UTC)
}

26
src/utils/time_test.go Normal file
View file

@ -0,0 +1,26 @@
package utils
import (
"time"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTimeGetTimeWithDefaultValue(t *testing.T) {
var ts Time
// Assert that time is NOW (+-10 seconds)
assert.InDelta(t, ts.GetTime().Unix(), time.Now().In(time.UTC).Unix(), float64(10))
}
func TestTimeGetTimeWithSetTime(t *testing.T) {
var ts Time
expected := time.Unix(1048722042, 500)
ts.SetTime(expected)
assert.Equal(t, expected, ts.GetTime())
}