mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-20 23:41:23 +02:00
feat(serialization): add 'd', 'w',' 'M' units support for time duration
- Updated Makefile to include `-checklinkname=0` in LDFLAGS
This commit is contained in:
21
internal/serialization/time.go
Normal file
21
internal/serialization/time.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package serialization
|
||||
|
||||
import (
|
||||
"time"
|
||||
_ "unsafe"
|
||||
)
|
||||
|
||||
//go:linkname unitMap time.unitMap
|
||||
var unitMap map[string]uint64
|
||||
|
||||
const (
|
||||
unitDay uint64 = 24 * uint64(time.Hour)
|
||||
unitWeek uint64 = 7 * unitDay
|
||||
unitMonth uint64 = 30 * unitDay
|
||||
)
|
||||
|
||||
func init() {
|
||||
unitMap["d"] = unitDay
|
||||
unitMap["w"] = unitWeek
|
||||
unitMap["M"] = unitMonth
|
||||
}
|
||||
16
internal/serialization/time_test.go
Normal file
16
internal/serialization/time_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package serialization
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
expect "github.com/yusing/go-proxy/internal/utils/testing"
|
||||
)
|
||||
|
||||
// NOTE: -ldflags=-checklinkname=0 is required to test this function
|
||||
func TestParseDuration(t *testing.T) {
|
||||
require.Equal(t, 24*time.Hour, expect.Must(time.ParseDuration("1d")))
|
||||
require.Equal(t, 7*24*time.Hour, expect.Must(time.ParseDuration("1w")))
|
||||
require.Equal(t, 30*24*time.Hour, expect.Must(time.ParseDuration("1M")))
|
||||
}
|
||||
Reference in New Issue
Block a user