Skip to content

Commit

Permalink
time functions are deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Feb 9, 2025
1 parent 5e9fe18 commit f80ee5c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 53 deletions.
11 changes: 6 additions & 5 deletions defaults_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"strconv"
"time"

"github.com/xgfone/go-toolkit/timex"
)

var (
Expand Down Expand Up @@ -412,7 +414,7 @@ func toduration(src any) (dst time.Duration, err error) {
}

func totime(src any) (dst time.Time, err error) {
loc := TimeLocation.Get()
loc := timex.Location
switch src := src.(type) {
case nil:
dst = dst.In(loc)
Expand Down Expand Up @@ -467,19 +469,18 @@ func parseDuration(src string) (dst time.Duration, err error) {
}

func parseTime(value string) (time.Time, error) {
loc := TimeLocation.Get()

loc := timex.Location
switch value {
case "", "0000-00-00 00:00:00", "0000-00-00 00:00:00.000", "0000-00-00 00:00:00.000000":
return time.Time{}.In(loc), nil
}

if isIntegerString(value) {
i, err := strconv.ParseInt(value, 10, 64)
return time.Unix(i, 0).In(loc), err
return Unix(i, 0), err
}

for _, layout := range TimeFormats.Get() {
for _, layout := range timex.Formats {
if t, err := time.ParseInLocation(layout, value, loc); err == nil {
return t, nil
}
Expand Down
71 changes: 26 additions & 45 deletions defaults_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,43 @@
package defaults

import (
"errors"
"time"

"github.com/xgfone/go-toolkit/timex"
)

// Pre-define some global variables about time.
var (
TimeFormat = NewValueWithValidation(time.RFC3339Nano, validateTimeFormat)
TimeFormats = NewValueWithValidation([]string{time.RFC3339Nano, "2006-01-02 15:04:05"}, validateTimeFormats)
TimeLocation = NewValueWithValidation(time.UTC, validateTimeLocation)
TimeNowFunc = NewValueWithValidation(time.Now, validateTimeNow)
)
// DEPRECATED!!! Please use timex.Format instead.
TimeFormat = NewValue(timex.Format)

// Now returns the current time by using TimeNow and TimeLocation.
func Now() time.Time { return TimeNowFunc.Get()().In(TimeLocation.Get()) }
// DEPRECATED!!! Please use timex.Formats instead.
TimeFormats = NewValue(timex.Formats)

// Unix is the same as time.Unix, but set the location with TimeLocation.
func Unix(sec, nsec int64) time.Time {
return time.Unix(sec, nsec).In(TimeLocation.Get())
}
// DEPRECATED!!! Please use timex.Now instead.
TimeNowFunc = NewValue(timex.Now)

// Today returns the today time starting with 00:00:00.
func Today() time.Time {
return timex.ToToday(Now())
}
// DEPRECATED!!! Please use timex.Location instead.
TimeLocation = NewValue(timex.Location)
)

func validateTimeNow(f func() time.Time) error {
if f == nil {
return errors.New("TimeNow: the time now function must not be nil")
}
return nil
func init() {
TimeFormat.update = func(new string) { timex.Format = new }
TimeFormats.update = func(new []string) { timex.Formats = new }
TimeNowFunc.update = func(new func() time.Time) { timex.Now = new }
TimeLocation.update = func(new *time.Location) { timex.Location = new }
}

func validateTimeFormat(s string) error {
if s == "" {
return errors.New("TimeFormat: time format layout must not be empty")
}
return nil
}
// Now is eqaul to timex.Now.
//
// DEPRECATED!!! Please use timex.Now instead.
func Now() time.Time { return timex.Now() }

func validateTimeLocation(loc *time.Location) error {
if loc == nil {
return errors.New("TimeLocation: time location must not be nil")
}
return nil
}
// Unix is eqaul to timex.Unix.
//
// DEPRECATED!!! Please use timex.Unix instead.
func Unix(sec, nsec int64) time.Time { return timex.Unix(sec, nsec) }

func validateTimeFormats(ss []string) error {
if len(ss) == 0 {
return errors.New("TimeFormats: time format layouts must not be empty")
}
for _, s := range ss {
if s == "" {
return errors.New("TimeFormats: time format layouts must not be empty")
}
}
return nil
}
// Today is eqaul to timex.Today.
//
// DEPRECATED!!! Please use timex.Today instead.
func Today() time.Time { return timex.Today() }
11 changes: 11 additions & 0 deletions defaults_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@ import (
"fmt"
"testing"
"time"

"github.com/xgfone/go-toolkit/timex"
)

func TestLocation(t *testing.T) {
timex.Location = time.UTC
TimeLocation.Set(time.Local)

if timex.Location != time.Local {
t.Errorf("expect Location %s, but got %s", time.Local.String(), timex.Location.String())
}
}

func TestToday(t *testing.T) {
today := Today()
expect := fmt.Sprintf("%04d-%02d-%02d 00:00:00",
Expand Down
4 changes: 4 additions & 0 deletions defaults_value_notatomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package defaults
// Value represents a common value.
type Value[T any] struct {
verify func(T) error
update func(T)
value T
}

Expand Down Expand Up @@ -44,6 +45,9 @@ func (v *Value[T]) Set(new T) {
}
v.value = new
logset()
if v.update != nil {
v.update(v.value)
}
}

// Swap sets the value to new and returns the old value.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/xgfone/go-defaults

require github.com/xgfone/go-toolkit v0.3.0
require github.com/xgfone/go-toolkit v0.6.0

go 1.22
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/xgfone/go-toolkit v0.3.0 h1:siEHAiK0pkAMkkQ/LnGrXHrCE7c1ncSyfe2s8u9qPUc=
github.com/xgfone/go-toolkit v0.3.0/go.mod h1:eOWnIK/acAJOoqEOtWnvuY0Pbn6cZ0DP/Oeoyn17QHw=
github.com/xgfone/go-toolkit v0.6.0 h1:cGfgAVwYqKPLB+SmZ2Q7bo9YtXhXwfNlW+KKNNbE6Kg=
github.com/xgfone/go-toolkit v0.6.0/go.mod h1:eOWnIK/acAJOoqEOtWnvuY0Pbn6cZ0DP/Oeoyn17QHw=

0 comments on commit f80ee5c

Please sign in to comment.