71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
// Copyright 2017 The Xorm Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package xorm
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"xorm.io/xorm/internal/utils"
|
|
"xorm.io/xorm/schemas"
|
|
)
|
|
|
|
func (session *Session) str2Time(col *schemas.Column, data string) (outTime time.Time, outErr error) {
|
|
sdata := strings.TrimSpace(data)
|
|
var x time.Time
|
|
var err error
|
|
|
|
var parseLoc = session.engine.DatabaseTZ
|
|
if col.TimeZone != nil {
|
|
parseLoc = col.TimeZone
|
|
}
|
|
|
|
if sdata == utils.ZeroTime0 || sdata == utils.ZeroTime1 {
|
|
} else if !strings.ContainsAny(sdata, "- :") { // !nashtsai! has only found that mymysql driver is using this for time type column
|
|
// time stamp
|
|
sd, err := strconv.ParseInt(sdata, 10, 64)
|
|
if err == nil {
|
|
x = time.Unix(sd, 0)
|
|
}
|
|
} else if len(sdata) > 19 && strings.Contains(sdata, "-") {
|
|
x, err = time.ParseInLocation(time.RFC3339Nano, sdata, parseLoc)
|
|
session.engine.logger.Debugf("time(1) key[%v]: %+v | sdata: [%v]\n", col.Name, x, sdata)
|
|
if err != nil {
|
|
x, err = time.ParseInLocation("2006-01-02 15:04:05.999999999", sdata, parseLoc)
|
|
}
|
|
if err != nil {
|
|
x, err = time.ParseInLocation("2006-01-02 15:04:05.9999999 Z07:00", sdata, parseLoc)
|
|
}
|
|
} else if len(sdata) == 19 && strings.Contains(sdata, "-") {
|
|
x, err = time.ParseInLocation("2006-01-02 15:04:05", sdata, parseLoc)
|
|
} else if len(sdata) == 10 && sdata[4] == '-' && sdata[7] == '-' {
|
|
x, err = time.ParseInLocation("2006-01-02", sdata, parseLoc)
|
|
} else if col.SQLType.Name == schemas.Time {
|
|
if strings.Contains(sdata, " ") {
|
|
ssd := strings.Split(sdata, " ")
|
|
sdata = ssd[1]
|
|
}
|
|
|
|
sdata = strings.TrimSpace(sdata)
|
|
if session.engine.dialect.URI().DBType == schemas.MYSQL && len(sdata) > 8 {
|
|
sdata = sdata[len(sdata)-8:]
|
|
}
|
|
|
|
st := fmt.Sprintf("2006-01-02 %v", sdata)
|
|
x, err = time.ParseInLocation("2006-01-02 15:04:05", st, parseLoc)
|
|
} else {
|
|
outErr = fmt.Errorf("unsupported time format %v", sdata)
|
|
return
|
|
}
|
|
if err != nil {
|
|
outErr = fmt.Errorf("unsupported time format %v: %v", sdata, err)
|
|
return
|
|
}
|
|
outTime = x.In(session.engine.TZLocation)
|
|
return
|
|
}
|