Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why ToTimeE() use UTC as the default instead of Local? #225

Open
mrzish opened this issue Sep 9, 2024 · 0 comments
Open

Why ToTimeE() use UTC as the default instead of Local? #225

mrzish opened this issue Sep 9, 2024 · 0 comments

Comments

@mrzish
Copy link

mrzish commented Sep 9, 2024

I used cast.ToTime () to convert '2006-01-02 15:04:05' format time string, and then compared with time.Now(), there is a problem. I would like to ask why cast.ToTime() uses UTC instead of Local to be consistent with time.Now(). Thanks.

  1. Use the following code snippet:

import (
	"fmt"
	"time"

	"github.com/spf13/cast"
)

func main() {
	timeStr := "2024-09-09 20:46:05"
	t1 := cast.ToTime(timeStr)
	fmt.Println("cast.ToTime -> ", t1)

	now := time.Now()
	fmt.Println("time.Now() -> ", now)

	fmt.Println("now.After", now.After(t1))
}

  1. The output is:
cast.ToTime ->  2024-09-09 20:46:05 +0000 UTC
time.Now() ->  2024-09-09 21:59:33.889463 +0800 CST m=+0.000234554
now.After false

Here are the definitions of caste and time

// caste.go
// ToTimeE casts an interface to a time.Time type.
func ToTimeE(i interface{}) (tim time.Time, err error) {
	return ToTimeInDefaultLocationE(i, time.UTC)
}

// time.go
// Now returns the current local time.
func Now() Time {
	sec, nsec, mono := now()
	mono -= startNano
	sec += unixToInternal - minWall
	if uint64(sec)>>33 != 0 {
		return Time{uint64(nsec), sec + minWall, Local}
	}
	return Time{hasMonotonic | uint64(sec)<<nsecShift | uint64(nsec), mono, Local}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant