Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Mar 14, 2023
1 parent f0a9019 commit 58e9c29
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 91 deletions.
73 changes: 59 additions & 14 deletions pkg/lakego-pkg/go-cryptobin/tool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tool

import (
"time"
"sync"
"encoding/json"
)

Expand All @@ -18,24 +19,47 @@ func NewConfig() *Config {
* @author deatil
*/
type Config struct {
// 锁定
mu sync.RWMutex

// 数据
data map[string]any
}

// 设置
func (this *Config) WithData(data map[string]any) *Config {
this.mu.Lock()
defer this.mu.Unlock()

this.data = data

return this
}

// 设置
func (this *Config) Set(name string, data any) *Config {
this.mu.Lock()
defer this.mu.Unlock()

this.data[name] = data

return this
}

// 删除
func (this *Config) Remove(name string) *Config {
this.mu.Lock()
defer this.mu.Unlock()

delete(this.data, name)

return this
}

func (this *Config) Has(name string) bool {
this.mu.RLock()
defer this.mu.RUnlock()

if _, ok := this.data[name]; ok {
return true
}
Expand All @@ -44,13 +68,48 @@ func (this *Config) Has(name string) bool {
}

func (this *Config) Get(name string) any {
this.mu.RLock()
defer this.mu.RUnlock()

if data, ok := this.data[name]; ok {
return data
}

return nil
}

func (this *Config) All() map[string]any {
return this.data
}

func (this *Config) Names() []string {
names := make([]string, 0)
for name, _ := range this.data {
names = append(names, name)
}

return names
}

func (this *Config) Len() int {
return len(this.data)
}

func (this *Config) Clean() {
this.mu.Lock()
defer this.mu.Unlock()

for name, _ := range this.data {
delete(this.data, name)
}
}

func (this *Config) String() string {
data, _ := json.Marshal(this.data)

return string(data)
}

func (this *Config) GetString(name string) string {
if data, ok := this.Get(name).(string); ok {
return data
Expand Down Expand Up @@ -274,17 +333,3 @@ func (this *Config) GetDurationSlice(name string) []time.Duration {

return make([]time.Duration, 0)
}

func (this *Config) All() map[string]any {
return this.data
}

func (this *Config) String() string {
data, _ := json.Marshal(this.data)

return string(data)
}

func (this *Config) Reset() {
this.data = make(map[string]any)
}
4 changes: 2 additions & 2 deletions pkg/lakego-pkg/go-datebin/datebin/datebin.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ func (this Datebin) GetLocationString() string {

// 设置时区
func (this Datebin) WithTimezone(timezone string) Datebin {
location, err := this.GetLocationByTimezone(timezone)
loc, err := this.GetLocationByTimezone(timezone)
if err == nil {
this.loc = location
this.loc = loc
}

return this
Expand Down
28 changes: 4 additions & 24 deletions pkg/lakego-pkg/go-datebin/datebin/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import (
// 当前
func (this Datebin) Now(timezone ...string) Datebin {
if len(timezone) > 0 {
loc, err := this.GetLocationByTimezone(timezone[0])
if err == nil {
this.loc = loc
}

this.AppendError(err)
this = this.WithTimezone(timezone[0])
}

this.time = time.Now().In(this.loc)
Expand All @@ -22,12 +17,7 @@ func (this Datebin) Now(timezone ...string) Datebin {
// 今天
func (this Datebin) Today(timezone ...string) Datebin {
if len(timezone) > 0 {
loc, err := this.GetLocationByTimezone(timezone[0])
if err == nil {
this.loc = loc
}

this.AppendError(err)
this = this.WithTimezone(timezone[0])
}

var datetime Datebin
Expand All @@ -45,12 +35,7 @@ func (this Datebin) Today(timezone ...string) Datebin {
// 明天
func (this Datebin) Tomorrow(timezone ...string) Datebin {
if len(timezone) > 0 {
loc, err := this.GetLocationByTimezone(timezone[0])
if err == nil {
this.loc = loc
}

this.AppendError(err)
this = this.WithTimezone(timezone[0])
}

var datetime Datebin
Expand All @@ -68,12 +53,7 @@ func (this Datebin) Tomorrow(timezone ...string) Datebin {
// 昨天
func (this Datebin) Yesterday(timezone ...string) Datebin {
if len(timezone) > 0 {
loc, err := this.GetLocationByTimezone(timezone[0])
if err == nil {
this.loc = loc
}

this.AppendError(err)
this = this.WithTimezone(timezone[0])
}

var datetime Datebin
Expand Down
11 changes: 3 additions & 8 deletions pkg/lakego-pkg/go-datebin/datebin/offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import (
// 间隔
func (this Datebin) Offset(field string, offset int, timezone ...string) Datebin {
if len(timezone) > 0 {
loc, error := this.GetLocationByTimezone(timezone[0])
if error == nil {
this.loc = loc
}

this.AppendError(error)
this = this.WithTimezone(timezone[0])
}

// 设置时区
Expand Down Expand Up @@ -104,10 +99,10 @@ func (this Datebin) OffsetMonthsNoOverflow(months int) Datebin {
// 按照持续时长字符串增加时间
func (this Datebin) AddDuration(duration string) Datebin {
td, err := this.ParseDuration(duration)

this.time = this.time.In(this.loc).Add(td)
this.AppendError(err)

return this
return this.AppendError(err)
}

// 按照持续时长字符串减少时间
Expand Down
17 changes: 8 additions & 9 deletions pkg/lakego-pkg/go-datebin/datebin/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func (this Datebin) Parse(date string) Datebin {
}

time, err := time.Parse(layout, date)
this.AppendError(err)
if err != nil {
return this.AppendError(err)
}

this.time = time

Expand All @@ -53,17 +55,14 @@ func (this Datebin) Parse(date string) Datebin {
// 用布局字符解析时间字符
func (this Datebin) ParseWithLayout(date string, layout string, timezone ...string) Datebin {
if len(timezone) > 0 {
loc, err := this.GetLocationByTimezone(timezone[0])
if err == nil {
this.loc = loc
}

this.AppendError(err)
this = this.WithTimezone(timezone[0])
}

time, err := time.ParseInLocation(layout, date, this.loc)
this.AppendError(err)

if err != nil {
return this.AppendError(err)
}

this.time = time

return this
Expand Down
40 changes: 6 additions & 34 deletions pkg/lakego-pkg/go-datebin/datebin/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ func (this Datebin) String() string {
// 返回字符
func (this Datebin) ToString(timezone ...string) string {
if len(timezone) > 0 {
loc, error := this.GetLocationByTimezone(timezone[0])
if error == nil {
this.loc = loc
}

this.AppendError(error)
this = this.WithTimezone(timezone[0])
}

if this.IsInvalid() {
Expand All @@ -32,12 +27,7 @@ func (this Datebin) ToString(timezone ...string) string {
// 返回星座名称
func (this Datebin) ToStarString(timezone ...string) string {
if len(timezone) > 0 {
loc, error := this.GetLocationByTimezone(timezone[0])
if error == nil {
this.loc = loc
}

this.AppendError(error)
this = this.WithTimezone(timezone[0])
}

if this.IsInvalid() {
Expand Down Expand Up @@ -95,12 +85,7 @@ func (this Datebin) ToStarString(timezone ...string) string {
// 返回当前季节,以气象划分
func (this Datebin) ToSeasonString(timezone ...string) string {
if len(timezone) > 0 {
loc, error := this.GetLocationByTimezone(timezone[0])
if error == nil {
this.loc = loc
}

this.AppendError(error)
this = this.WithTimezone(timezone[0])
}

if this.IsInvalid() {
Expand Down Expand Up @@ -132,12 +117,7 @@ func (this Datebin) ToSeasonString(timezone ...string) string {
// 周几
func (this Datebin) ToWeekdayString(timezone ...string) string {
if len(timezone) > 0 {
loc, error := this.GetLocationByTimezone(timezone[0])
if error == nil {
this.loc = loc
}

this.AppendError(error)
this = this.WithTimezone(timezone[0])
}

if this.IsInvalid() {
Expand All @@ -152,12 +132,7 @@ func (this Datebin) ToWeekdayString(timezone ...string) string {
// 原始格式
func (this Datebin) Layout(layout string, timezone ...string) string {
if len(timezone) > 0 {
loc, error := this.GetLocationByTimezone(timezone[0])
if error == nil {
this.loc = loc
}

this.AppendError(error)
this = this.WithTimezone(timezone[0])
}

if this.IsInvalid() {
Expand All @@ -175,10 +150,7 @@ func (this Datebin) ToLayoutString(layout string, timezone ...string) string {
// 输出指定布局的时间字符串
func (this Datebin) Format(layout string, timezone ...string) string {
if len(timezone) > 0 {
loc, error := this.GetLocationByTimezone(timezone[0])

this.loc = loc
this.AppendError(error)
this = this.WithTimezone(timezone[0])
}

if this.IsInvalid() {
Expand Down

0 comments on commit 58e9c29

Please sign in to comment.