spf13/cast

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

Opened this issue · 0 comments

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}
}