2020-06-26 12:16:13 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2022-10-03 22:59:29 +02:00
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-06-26 12:16:13 +02:00
|
|
|
)
|
|
|
|
|
2022-10-03 22:59:29 +02:00
|
|
|
func TestUTC(t *testing.T) {
|
|
|
|
t.Run("Zone", func(t *testing.T) {
|
|
|
|
utc := UTC()
|
|
|
|
|
|
|
|
if zone, offset := utc.Zone(); zone != time.UTC.String() {
|
|
|
|
t.Error("should be utc")
|
|
|
|
} else if offset != 0 {
|
|
|
|
t.Error("offset should be 0")
|
|
|
|
}
|
|
|
|
})
|
2022-10-15 21:54:11 +02:00
|
|
|
t.Run("RunGorm", func(t *testing.T) {
|
2022-10-03 22:59:29 +02:00
|
|
|
utc := UTC()
|
|
|
|
utcGorm := gorm.NowFunc()
|
|
|
|
|
|
|
|
t.Logf("NOW: %s, %s", utc.String(), utcGorm.String())
|
|
|
|
|
|
|
|
assert.True(t, utcGorm.After(utc))
|
|
|
|
|
|
|
|
if zone, offset := utcGorm.Zone(); zone != time.UTC.String() {
|
|
|
|
t.Error("gorm time should be utc")
|
|
|
|
} else if offset != 0 {
|
|
|
|
t.Error("gorm time offset should be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.InEpsilon(t, utc.Unix(), utcGorm.Unix(), 2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
func TestTimeStamp(t *testing.T) {
|
2022-04-13 22:17:59 +02:00
|
|
|
t.Run("UTC", func(t *testing.T) {
|
|
|
|
if TimeStamp().Location() != time.UTC {
|
|
|
|
t.Fatal("timestamp zone must be utc")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Past", func(t *testing.T) {
|
|
|
|
if TimeStamp().After(time.Now().Add(time.Second)) {
|
|
|
|
t.Fatal("timestamp should be in the past from now")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("JSON", func(t *testing.T) {
|
|
|
|
t1 := TimeStamp().Add(time.Nanosecond * 123456)
|
2020-06-26 12:16:13 +02:00
|
|
|
|
2022-04-13 22:17:59 +02:00
|
|
|
if b, err := t1.MarshalJSON(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
t.Logf("JSON: %s", b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("UnixMicro", func(t *testing.T) {
|
|
|
|
t1 := time.Date(-3000, 1, 1, 1, 1, 1, 0, time.UTC)
|
|
|
|
t2 := TimeStamp().Add(time.Nanosecond * 123456)
|
|
|
|
t3 := time.Date(3000, 1, 1, 1, 1, 1, 0, time.UTC)
|
2020-06-26 12:16:13 +02:00
|
|
|
|
2022-04-13 22:17:59 +02:00
|
|
|
ms1 := t1.UnixMilli()
|
|
|
|
ms2 := t2.UnixMilli()
|
|
|
|
ms3 := t3.UnixMilli()
|
|
|
|
|
|
|
|
m1 := t1.UnixMicro()
|
|
|
|
m2 := t2.UnixMicro()
|
|
|
|
m3 := t3.UnixMicro()
|
|
|
|
|
|
|
|
t.Logf("MS1: %20d", ms1)
|
|
|
|
t.Logf("MS2: %20d", ms2)
|
|
|
|
t.Logf("MS3: %20d", ms3)
|
|
|
|
|
|
|
|
t.Logf("U1: %20d", m1)
|
|
|
|
t.Logf("U2: %20d", m2)
|
|
|
|
t.Logf("U3: %20d", m3)
|
|
|
|
|
|
|
|
i1, i2, i3 := 1e18-m1, 1e18-m2, 1e18-m3
|
|
|
|
|
|
|
|
t.Logf("ZZ: %20d", 9223372036854775807)
|
|
|
|
t.Logf("I1: %20d", i1)
|
|
|
|
t.Logf("I2: %20d", i2)
|
|
|
|
t.Logf("I3: %20d", i3)
|
|
|
|
|
|
|
|
t.Logf("T1: %20d", 1e18-i1)
|
|
|
|
t.Logf("T2: %20d", 1e18-i2)
|
|
|
|
t.Logf("T3: %20d", 1e18-i3)
|
|
|
|
|
|
|
|
t.Logf("D1: %s", time.UnixMicro(1e18-i1).String())
|
|
|
|
t.Logf("D2: %s", time.UnixMicro(1e18-i2).String())
|
|
|
|
t.Logf("D3: %s", time.UnixMicro(1e18-i3).String())
|
|
|
|
})
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|
2021-01-27 15:17:45 +01:00
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
func TestTimePointer(t *testing.T) {
|
|
|
|
result := TimePointer()
|
2021-08-23 16:22:01 +02:00
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result must not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Location() != time.UTC {
|
|
|
|
t.Fatal("timestamp zone must be utc")
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.After(time.Now().Add(time.Second)) {
|
|
|
|
t.Fatal("timestamp should be in the past from now")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:17:45 +01:00
|
|
|
func TestSeconds(t *testing.T) {
|
|
|
|
result := Seconds(23)
|
|
|
|
|
|
|
|
if result != 23*time.Second {
|
|
|
|
t.Error("must be 23 seconds")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestYesterday(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
result := Yesterday()
|
|
|
|
|
|
|
|
t.Logf("yesterday: %s", result)
|
|
|
|
|
|
|
|
if result.After(now) {
|
|
|
|
t.Error("yesterday is not before now")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !result.Before(now) {
|
|
|
|
t.Error("yesterday is before now")
|
|
|
|
}
|
|
|
|
}
|