unsupported conversion from 15:04:05 to time #2073

Closed
opened 2021-11-18 13:01:41 +00:00 by arturwwl · 3 comments
Contributor

struct:

//Example
type Example struct {
	ID 		uint64 		`xorm:"'id' pk autoincr"`
	TimeStart       time.Time 	`xorm:"TIME not null"`
	TimeEnd         time.Time 	`xorm:"TIME not null"`
	TimesType       uint8     	`xorm:"not null"`
	CreatedAt       time.Time 	`xorm:"created not null"`
	UpdatedAt       time.Time 	`xorm:"updated not null"`
}

query:

examples := []Example
err := Engine.Find(&examples) //unsupported conversion from 15:04:05 to time

Code worked correctly (no error) in xorm.io/xorm v1.0.5 and xorm.io/xorm v1.1.2, but does not work in xorm.io/xorm v1.2.0 - xorm.io/xorm v1.2.5

go env:

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\username\AppData\Local\go-build
set GOENV=C:\Users\username\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=Q:\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=Q:\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=//project location
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\username\AppData\Local\Temp\go-build908889130=/tmp/go-build -gno-record-gcc-switches

struct: ```golang //Example type Example struct { ID uint64 `xorm:"'id' pk autoincr"` TimeStart time.Time `xorm:"TIME not null"` TimeEnd time.Time `xorm:"TIME not null"` TimesType uint8 `xorm:"not null"` CreatedAt time.Time `xorm:"created not null"` UpdatedAt time.Time `xorm:"updated not null"` } ``` query: ```golang examples := []Example err := Engine.Find(&examples) //unsupported conversion from 15:04:05 to time ``` Code worked correctly (no error) in `xorm.io/xorm v1.0.5` and `xorm.io/xorm v1.1.2`, but does not work in `xorm.io/xorm v1.2.0` - `xorm.io/xorm v1.2.5` go env: ``` set GO111MODULE=on set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\username\AppData\Local\go-build set GOENV=C:\Users\username\AppData\Roaming\go\env set GOEXE=.exe set GOEXPERIMENT= set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=Q:\go\pkg\mod set GONOPROXY= set GONOSUMDB= set GOOS=windows set GOPATH=Q:\go set GOPRIVATE= set GOPROXY=https://proxy.golang.org,direct set GOROOT=C:\Go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64 set GOVCS= set GOVERSION=go1.17.3 set GCCGO=gccgo set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=//project location set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\username\AppData\Local\Temp\go-build908889130=/tmp/go-build -gno-record-gcc-switches ```
Author
Contributor

This would fix the problem

/convert/time.go

// String2Time converts a string to time with original location
func String2Time(s string, originalLocation *time.Location, convertedLocation *time.Location) (*time.Time, error) {
	if len(s) == 19 {
		if s == utils.ZeroTime0 || s == utils.ZeroTime1 {
			return &time.Time{}, nil
		}
		dt, err := time.ParseInLocation("2006-01-02 15:04:05", s, originalLocation)
		if err != nil {
			return nil, err
		}
		dt = dt.In(convertedLocation)
		return &dt, nil
	} else if len(s) == 20 && s[10] == 'T' && s[19] == 'Z' {
		dt, err := time.ParseInLocation("2006-01-02T15:04:05", s[:19], originalLocation)
		if err != nil {
			return nil, err
		}
		dt = dt.In(convertedLocation)
		return &dt, nil
	} else if len(s) == 25 && s[10] == 'T' && s[19] == '+' && s[22] == ':' {
		dt, err := time.Parse(time.RFC3339, s)
		if err != nil {
			return nil, err
		}
		dt = dt.In(convertedLocation)
		return &dt, nil
	} else if len(s) >= 21 && s[19] == '.' {
		var layout = "2006-01-02 15:04:05." + strings.Repeat("0", len(s)-20)
		dt, err := time.ParseInLocation(layout, s, originalLocation)
		if err != nil {
			return nil, err
		}
		dt = dt.In(convertedLocation)
		return &dt, nil
	} else if len(s) == 8 { // new else if for time hour:minutes:seconds string
		var layout = "15:04:05"
		dt, err := time.ParseInLocation(layout, s, originalLocation)
		if err != nil {
			return nil, err
		}
		dt = dt.In(convertedLocation)
		return &dt, nil
	} else {
		i, err := strconv.ParseInt(s, 10, 64)
		if err == nil {
			tm := time.Unix(i, 0).In(convertedLocation)
			return &tm, nil
		}
	}
	return nil, fmt.Errorf("unsupported conversion from %s to time", s)
}
This would fix the problem /convert/time.go ```golang // String2Time converts a string to time with original location func String2Time(s string, originalLocation *time.Location, convertedLocation *time.Location) (*time.Time, error) { if len(s) == 19 { if s == utils.ZeroTime0 || s == utils.ZeroTime1 { return &time.Time{}, nil } dt, err := time.ParseInLocation("2006-01-02 15:04:05", s, originalLocation) if err != nil { return nil, err } dt = dt.In(convertedLocation) return &dt, nil } else if len(s) == 20 && s[10] == 'T' && s[19] == 'Z' { dt, err := time.ParseInLocation("2006-01-02T15:04:05", s[:19], originalLocation) if err != nil { return nil, err } dt = dt.In(convertedLocation) return &dt, nil } else if len(s) == 25 && s[10] == 'T' && s[19] == '+' && s[22] == ':' { dt, err := time.Parse(time.RFC3339, s) if err != nil { return nil, err } dt = dt.In(convertedLocation) return &dt, nil } else if len(s) >= 21 && s[19] == '.' { var layout = "2006-01-02 15:04:05." + strings.Repeat("0", len(s)-20) dt, err := time.ParseInLocation(layout, s, originalLocation) if err != nil { return nil, err } dt = dt.In(convertedLocation) return &dt, nil } else if len(s) == 8 { // new else if for time hour:minutes:seconds string var layout = "15:04:05" dt, err := time.ParseInLocation(layout, s, originalLocation) if err != nil { return nil, err } dt = dt.In(convertedLocation) return &dt, nil } else { i, err := strconv.ParseInt(s, 10, 64) if err == nil { tm := time.Unix(i, 0).In(convertedLocation) return &tm, nil } } return nil, fmt.Errorf("unsupported conversion from %s to time", s) } ```
Owner

Could you send a PR to fix that?

Could you send a PR to fix that?
Author
Contributor

Pull request sent: !2074

Pull request sent: !2074
lunny closed this issue 2023-07-23 02:34:17 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#2073
No description provided.