新增 Default 结构体和 SetDefault 方法

This commit is contained in:
Peleus 2024-01-07 20:04:57 +08:00
parent 4541950424
commit 887b7d70a5
3 changed files with 68 additions and 0 deletions

37
default.go Normal file
View File

@ -0,0 +1,37 @@
package carbon
var (
// default layout
// 默认布局模板
defaultLayout = DateTimeLayout
// default timezone
// 默认时区
defaultTimezone = Local
// default locale
// 默认区域
defaultLocale = "en"
)
// Default defines a Default struct.
// 定义 Default 结构体
type Default struct {
Layout string
Timezone string
Locale string
}
// SetDefault sets default.
// 设置全局默认值
func SetDefault(d Default) {
if d.Layout != "" {
defaultLayout = d.Layout
}
if d.Timezone != "" {
defaultTimezone = d.Timezone
}
if d.Locale != "" {
defaultLocale = d.Locale
}
}

14
default_bench_test.go Executable file
View File

@ -0,0 +1,14 @@
package carbon
import "testing"
func BenchmarkCarbon_SetDefault(b *testing.B) {
d := Default{
Layout: DateTimeLayout,
Timezone: Local,
Locale: "en",
}
for n := 0; n < b.N; n++ {
SetDefault(d)
}
}

17
default_unit_test.go Normal file
View File

@ -0,0 +1,17 @@
package carbon
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestCarbon_SetDefault(t *testing.T) {
SetDefault(Default{
Layout: DateTimeLayout,
Timezone: Local,
Locale: "en",
})
assert.Equal(t, DateTimeLayout, defaultLayout)
assert.Equal(t, Local, defaultTimezone)
assert.Equal(t, "en", defaultLocale)
}