1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 16:12:42 +08:00
This commit is contained in:
2022-11-09 17:21:30 +08:00
parent 9203c3719f
commit d49c02924c
6 changed files with 146 additions and 85 deletions

View File

@ -1,4 +1,4 @@
package dateconv package calendar
import "time" import "time"

View File

@ -1,12 +0,0 @@
package dateconv_test
import (
"testing"
"github.com/charlienet/go-mixed/dateconv"
)
func TestToday(t *testing.T) {
today := dateconv.Today()
t.Log(dateconv.TimeToString(&today))
}

View File

@ -1,44 +0,0 @@
package dateconv
import (
"time"
)
const (
layoutDate = "2006-01-02"
layoutTime = "2006-01-02 15:04:05"
layoutTimeMilli = "2006-01-02 15:04:05.000"
layoutChineseDate = "2006年01月02日"
layoutChineseTime = "2006年01月02日 15:04:05"
)
func Today() time.Time {
t := time.Now()
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
// 日期转换为整数(如:20211222
func DateToInt(date time.Time) int {
return date.Year()*10000 + int(date.Month())*100 + date.Day()
}
// 日期转换为字符串
func DateToString(date *time.Time) string { return formatTime(date, layoutDate) }
// 时间转换为字符串
func TimeToString(date *time.Time) string { return formatTime(date, layoutTime) }
// 日期转换为中文
func DateToChinese(t *time.Time) string { return formatTime(t, layoutChineseDate) }
// 时间转换为中文
func TimeToChinese(t *time.Time) string { return formatTime(t, layoutChineseTime) }
func formatTime(t *time.Time, f string) string {
if t == nil || t.IsZero() {
return ""
}
return t.Format(f)
}

View File

@ -1,24 +0,0 @@
package dateconv
import (
"testing"
"time"
)
func TestParseDuration(t *testing.T) {
t.Log(ParseDuration(""))
t.Log(ParseDuration("abc"))
}
func TestMonth(t *testing.T) {
month := time.Now()
offset := (int(month.Month()) - 3)
t.Log(offset)
month = month.AddDate(0, -3, 1)
t.Log(month)
tt := time.Date(month.Year(), month.Month(), 1, 0, 0, 0, 0, month.Location())
t.Log(tt)
}

View File

@ -1,9 +1,128 @@
package expr package expr
// 如为真返回参数一,否则返回参数二 // 如为真返回参数一,否则返回参数二
func If[T any](e bool, v1, v2 T) T { func Ternary[T any](e bool, v1, v2 T) T {
if e { if e {
return v1 return v1
} }
return v2 return v2
} }
func TernaryF[T any](condition bool, ifFunc func() T, elseFunc func() T) T {
if condition {
return ifFunc()
}
return elseFunc()
}
type ifElse[T any] struct {
result T
done bool
}
func If[T any](condition bool, result T) *ifElse[T] {
if condition {
return &ifElse[T]{result, true}
}
var t T
return &ifElse[T]{t, false}
}
func IfF[T any](condition bool, resultF func() T) *ifElse[T] {
if condition {
return &ifElse[T]{resultF(), true}
}
var t T
return &ifElse[T]{t, false}
}
func (i *ifElse[T]) ElseIf(condition bool, result T) *ifElse[T] {
if !i.done && condition {
i.result = result
i.done = true
}
return i
}
func (i *ifElse[T]) ElseIfF(condition bool, resultF func() T) *ifElse[T] {
if !i.done && condition {
i.result = resultF()
i.done = true
}
return i
}
func (i *ifElse[T]) Else(result T) T {
if i.done {
return i.result
}
return result
}
func (i *ifElse[T]) ElseF(resultF func() T) T {
if i.done {
return i.result
}
return resultF()
}
type switchCase[T comparable, R any] struct {
predicate T
result R
done bool
}
func Switch[T comparable, R any](predicate T) *switchCase[T, R] {
var result R
return &switchCase[T, R]{
predicate,
result,
false,
}
}
func SwitchF[T comparable, R any](predicate func() T) *switchCase[T, R] {
return Switch[T, R](predicate())
}
func (s *switchCase[T, R]) Case(val T, result R) *switchCase[T, R] {
if !s.done && s.predicate == val {
s.result = result
s.done = true
}
return s
}
func (s *switchCase[T, R]) CaseF(val T, cb func() R) *switchCase[T, R] {
if !s.done && s.predicate == val {
s.result = cb()
s.done = true
}
return s
}
func (s *switchCase[T, R]) Default(result R) R {
if !s.done {
s.result = result
}
return s.result
}
func (s *switchCase[T, R]) DefaultF(cb func() R) R {
if !s.done {
s.result = cb()
}
return s.result
}

View File

@ -1,9 +1,31 @@
package expr package expr
import "testing" import (
"testing"
func TestIf(t *testing.T) { "github.com/stretchr/testify/assert"
)
func TestTernary(t *testing.T) {
v1 := 10 v1 := 10
v2 := 4 v2 := 4
t.Log(If(v1 > v2, v1, v2)) t.Log(Ternary(v1 > v2, v1, v2))
}
func TestIf(t *testing.T) {
is := assert.New(t)
is.Equal(1, If(true, 1).ElseIf(false, 2).Else(3))
is.Equal(1, If(true, 1).ElseIf(true, 2).Else(3))
is.Equal(2, If(false, 1).ElseIf(true, 2).Else(3))
is.Equal(3, If(false, 1).ElseIf(false, 2).Else(3))
}
func TestSwitch(t *testing.T) {
is := assert.New(t)
is.Equal(1, Switch[int, int](42).Case(42, 1).Case(1, 2).Default(3))
is.Equal(1, Switch[int, int](42).Case(42, 1).Case(42, 2).Default(3))
is.Equal(1, Switch[int, int](42).Case(1, 1).Case(42, 2).Default(3))
is.Equal(1, Switch[int, int](42).Case(1, 1).Case(1, 2).Default(3))
} }