Skip to content

Commit

Permalink
Merge pull request #920 from sashamelentyev/revert
Browse files Browse the repository at this point in the history
Revert "refactor: use stdlib time layout"
  • Loading branch information
ernado authored May 26, 2023
2 parents d169b1e + 904ad91 commit 06e94a1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions conv/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ func ToBytes(s string) ([]byte, error) {
}

func ToTime(s string) (time.Time, error) {
return time.Parse(time.TimeOnly, s)
return time.Parse(timeLayout, s)
}

func ToDate(s string) (time.Time, error) {
return time.Parse(time.DateOnly, s)
return time.Parse(dateLayout, s)
}

func ToDateTime(s string) (time.Time, error) {
Expand Down
4 changes: 2 additions & 2 deletions conv/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func BoolToString(v bool) string { return strconv.FormatBool(v) }
func StringToString(v string) string { return v }
func BytesToString(v []byte) string { return string(v) }

func TimeToString(v time.Time) string { return v.Format(time.TimeOnly) }
func DateToString(v time.Time) string { return v.Format(time.DateOnly) }
func TimeToString(v time.Time) string { return v.Format(timeLayout) }
func DateToString(v time.Time) string { return v.Format(dateLayout) }
func DateTimeToString(v time.Time) string { return v.Format(time.RFC3339) }

func UnixSecondsToString(v time.Time) string { return StringInt64ToString(v.Unix()) }
Expand Down
5 changes: 5 additions & 0 deletions conv/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"time"
)

const (
dateLayout = "2006-01-02"
timeLayout = "15:04:05"
)

func Date(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
Expand Down
17 changes: 11 additions & 6 deletions json/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ import (
"github.com/go-faster/jx"
)

const (
dateLayout = "2006-01-02"
timeLayout = "15:04:05"
)

// DecodeDate decodes date from json.
func DecodeDate(i *jx.Decoder) (v time.Time, err error) {
s, err := i.Str()
if err != nil {
return v, err
}
return time.Parse(time.DateOnly, s)
return time.Parse(dateLayout, s)
}

// EncodeDate encodes date to json.
func EncodeDate(s *jx.Encoder, v time.Time) {
const (
roundTo = 8
length = len(time.DateOnly)
length = len(dateLayout)
allocate = ((length + roundTo - 1) / roundTo) * roundTo
)
b := make([]byte, allocate)
b = v.AppendFormat(b[:0], time.DateOnly)
b = v.AppendFormat(b[:0], dateLayout)
s.ByteStr(b)
}

Expand All @@ -33,18 +38,18 @@ func DecodeTime(i *jx.Decoder) (v time.Time, err error) {
if err != nil {
return v, err
}
return time.Parse(time.TimeOnly, s)
return time.Parse(timeLayout, s)
}

// EncodeTime encodes time to json.
func EncodeTime(s *jx.Encoder, v time.Time) {
const (
roundTo = 8
length = len(time.TimeOnly)
length = len(timeLayout)
allocate = ((length + roundTo - 1) / roundTo) * roundTo
)
b := make([]byte, allocate)
b = v.AppendFormat(b[:0], time.TimeOnly)
b = v.AppendFormat(b[:0], timeLayout)
s.ByteStr(b)
}

Expand Down

0 comments on commit 06e94a1

Please sign in to comment.