Skip to content

Commit

Permalink
fix a today time bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Dec 15, 2024
1 parent e5dc369 commit 5e9fe18
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
32 changes: 2 additions & 30 deletions defaults_clientip.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/netip"

"github.com/xgfone/go-toolkit/netx"
"github.com/xgfone/go-toolkit/netx/netipx"
)

var (
Expand Down Expand Up @@ -61,7 +62,7 @@ func getClientIP(ctx context.Context, req any) (addr netip.Addr) {
addr = v.RemoteAddr()

case interface{ RemoteAddr() net.Addr }:
addr = netaddr2netipaddr(v.RemoteAddr())
addr, _ = netipx.AddrFromNetAddr(v.RemoteAddr())

case interface{ RemoteAddr() string }:
host, _ := netx.SplitHostPort(v.RemoteAddr())
Expand All @@ -74,32 +75,3 @@ func getClientIP(ctx context.Context, req any) (addr netip.Addr) {

return
}

func netaddr2netipaddr(netaddr net.Addr) (addr netip.Addr) {
switch v := netaddr.(type) {
case *net.TCPAddr:
addr = ip2addr(v.IP)

case *net.UDPAddr:
addr = ip2addr(v.IP)

default:
host, _ := netx.SplitHostPort(v.String())
addr, _ = netip.ParseAddr(host)
}
return
}

func ip2addr(ip net.IP) (addr netip.Addr) {
switch len(ip) {
case net.IPv4len:
addr = netip.AddrFrom4([4]byte(ip))
case net.IPv6len:
if ipv4 := ip.To4(); ipv4 != nil {
addr = netip.AddrFrom4([4]byte(ipv4))
} else {
addr = netip.AddrFrom16([16]byte(ip))
}
}
return
}
2 changes: 1 addition & 1 deletion defaults_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Unix(sec, nsec int64) time.Time {

// Today returns the today time starting with 00:00:00.
func Today() time.Time {
return timex.Today().In(TimeLocation.Get())
return timex.ToToday(Now())
}

func validateTimeNow(f func() time.Time) error {
Expand Down
31 changes: 31 additions & 0 deletions defaults_time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package defaults

import (
"fmt"
"testing"
"time"
)

func TestToday(t *testing.T) {
today := Today()
expect := fmt.Sprintf("%04d-%02d-%02d 00:00:00",
today.Year(), today.Month(), today.Day())

if s := Today().Format(time.DateTime); s != expect {
t.Errorf("expect '%s', but got '%s'", expect, s)
}
}
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.1.1
require github.com/xgfone/go-toolkit v0.3.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.1.1 h1:eYBIgrIhfYHq6unbG0Yx0CM+aX/dvolPrJ5vbDk4bWM=
github.com/xgfone/go-toolkit v0.1.1/go.mod h1:eOWnIK/acAJOoqEOtWnvuY0Pbn6cZ0DP/Oeoyn17QHw=
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=

0 comments on commit 5e9fe18

Please sign in to comment.