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:
parent
c8a128b657
commit
d3d0fb11d7
2 changed files with 48 additions and 0 deletions
22
src/utils/time.go
Normal file
22
src/utils/time.go
Normal 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
26
src/utils/time_test.go
Normal 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())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue