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

fix(os/gtime): fix gtime.StrToTime("00:00:00") is not zero time instant #3694

Closed
wants to merge 1 commit into from

Conversation

niluan304
Copy link
Contributor

@niluan304 niluan304 commented Jul 19, 2024

updated on 2024-07-27

in stdlib,

timer, _ := time.Parse("15:04:05", "00:00:00")
fmt.Println(timer.IsZero()) // false
fmt.Println(timer)          // 0000-01-01 00:00:00 +0000 UTC

so, is is ok that gtime.StrToTime("00:00:00")is not zero time instant.


origin

gtime.StrToTime parse string h:m:s like 00:00:00,the return is not zero time instant.

for example:

gtime.StrToTime("15:04:05")
// is equal to

date := time.Date(0, 1, 1, 15, 4, 5, 0, time.Local)
gtime.NewFromTime(date)

and in std, zero time instant is : time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC) not time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)

// IsZero reports whether t represents the zero time instant,
// January 1, year 1, 00:00:00 UTC.
func (t Time) IsZero() bool {
	return t.sec() == 0 && t.nsec() == 0
}

so, I think it is a bug.

the pr will Fixes #2012 , and make

gtime.New(0).Equal(gtime.New("00:00:00")) // true

@niluan304
Copy link
Contributor Author

If gtime.StrToTime should not be modified,

Perhaps should update

gf/os/gtime/gtime_sql.go

Lines 18 to 28 in ba322ba

// Value is the interface providing the Value method for package database/sql/driver
// for retrieving value from golang variable to database.
func (t *Time) Value() (driver.Value, error) {
if t == nil {
return nil, nil
}
if t.IsZero() {
return nil, nil
}
return t.Time, nil
}

// Value is the interface providing the Value method for package database/sql/driver
// for retrieving value from golang variable to database.
func (t *Time) Value() (driver.Value, error) {
	if t == nil {
		return nil, nil
	}
	if t.IsZero() {
		return nil, nil
	}

	if t.Year() == 0 {
		// Only time.
		return t.Format("15:04:05"), nil
	}

	return t.Time, nil
}

Copy link

sonarcloud bot commented Jul 22, 2024

@gqcn
Copy link
Member

gqcn commented Jul 24, 2024

If gtime.StrToTime should not be modified,

Perhaps should update

gf/os/gtime/gtime_sql.go

Lines 18 to 28 in ba322ba

// Value is the interface providing the Value method for package database/sql/driver
// for retrieving value from golang variable to database.
func (t *Time) Value() (driver.Value, error) {
if t == nil {
return nil, nil
}
if t.IsZero() {
return nil, nil
}
return t.Time, nil
}

// Value is the interface providing the Value method for package database/sql/driver
// for retrieving value from golang variable to database.
func (t *Time) Value() (driver.Value, error) {
	if t == nil {
		return nil, nil
	}
	if t.IsZero() {
		return nil, nil
	}

	if t.Year() == 0 {
		// Only time.
		return t.Format("15:04:05"), nil
	}

	return t.Time, nil
}

The ci failed. I think this change is ok:

// Value is the interface providing the Value method for package database/sql/driver
// for retrieving value from golang variable to database.
func (t *Time) Value() (driver.Value, error) {
	if t == nil {
		return nil, nil
	}
	if t.IsZero() {
		return nil, nil
	}

	if t.Year() == 0 {
		// Only time.
		return t.Format("15:04:05"), nil
	}

	return t.Time, nil
}

@niluan304
Copy link
Contributor Author

If gtime.StrToTime should not be modified,
Perhaps should update

gf/os/gtime/gtime_sql.go

Lines 18 to 28 in ba322ba

// Value is the interface providing the Value method for package database/sql/driver
// for retrieving value from golang variable to database.
func (t *Time) Value() (driver.Value, error) {
if t == nil {
return nil, nil
}
if t.IsZero() {
return nil, nil
}
return t.Time, nil
}

// Value is the interface providing the Value method for package database/sql/driver
// for retrieving value from golang variable to database.
func (t *Time) Value() (driver.Value, error) {
	if t == nil {
		return nil, nil
	}
	if t.IsZero() {
		return nil, nil
	}

	if t.Year() == 0 {
		// Only time.
		return t.Format("15:04:05"), nil
	}

	return t.Time, nil
}

The ci failed. I think this change is ok:

// Value is the interface providing the Value method for package database/sql/driver
// for retrieving value from golang variable to database.
func (t *Time) Value() (driver.Value, error) {
	if t == nil {
		return nil, nil
	}
	if t.IsZero() {
		return nil, nil
	}

	if t.Year() == 0 {
		// Only time.
		return t.Format("15:04:05"), nil
	}

	return t.Time, nil
}

Thanks for suggestion, I make a new pr #3714

@niluan304 niluan304 closed this Jul 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants