mirror of
https://github.com/yusing/godoxy.git
synced 2026-01-11 22:30:47 +01: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:
2
Makefile
2
Makefile
@@ -6,7 +6,7 @@ export GOOS = linux
|
||||
WEBUI_DIR ?= ../godoxy-frontend
|
||||
DOCS_DIR ?= ../godoxy-wiki
|
||||
|
||||
LDFLAGS = -X github.com/yusing/go-proxy/pkg.version=${VERSION}
|
||||
LDFLAGS = -X github.com/yusing/go-proxy/pkg.version=${VERSION} -checklinkname=0
|
||||
|
||||
ifeq ($(agent), 1)
|
||||
NAME = godoxy-agent
|
||||
|
||||
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