qrz/vendor/xorm.io/xorm/dialects/time.go

64 lines
1.5 KiB
Go
Raw Normal View History

2020-07-19 19:32:22 +02:00
// Copyright 2015 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 dialects
import (
2021-08-30 19:45:06 +02:00
"strings"
2020-07-19 19:32:22 +02:00
"time"
"xorm.io/xorm/schemas"
)
2021-08-30 19:45:06 +02:00
// FormatColumnTime format column time
func FormatColumnTime(dialect Dialect, dbLocation *time.Location, col *schemas.Column, t time.Time) (interface{}, error) {
if t.IsZero() {
if col.Nullable {
return nil, nil
}
if col.SQLType.IsNumeric() {
return 0, nil
}
}
2022-10-30 14:50:22 +01:00
tmZone := dbLocation
2021-08-30 19:45:06 +02:00
if col.TimeZone != nil {
tmZone = col.TimeZone
}
t = t.In(tmZone)
switch col.SQLType.Name {
2020-07-19 19:32:22 +02:00
case schemas.Date:
2021-08-30 19:45:06 +02:00
return t.Format("2006-01-02"), nil
case schemas.Time:
2022-10-30 14:50:22 +01:00
layout := "15:04:05"
2021-08-30 19:45:06 +02:00
if col.Length > 0 {
2022-10-30 14:50:22 +01:00
// we can use int(...) casting here as it's very unlikely to a huge sized field
layout += "." + strings.Repeat("0", int(col.Length))
2021-08-30 19:45:06 +02:00
}
return t.Format(layout), nil
case schemas.DateTime, schemas.TimeStamp:
2022-10-30 14:50:22 +01:00
layout := "2006-01-02 15:04:05"
2021-08-30 19:45:06 +02:00
if col.Length > 0 {
2022-10-30 14:50:22 +01:00
// we can use int(...) casting here as it's very unlikely to a huge sized field
layout += "." + strings.Repeat("0", int(col.Length))
2021-08-30 19:45:06 +02:00
}
return t.Format(layout), nil
case schemas.Varchar:
return t.Format("2006-01-02 15:04:05"), nil
2020-07-19 19:32:22 +02:00
case schemas.TimeStampz:
if dialect.URI().DBType == schemas.MSSQL {
2021-08-30 19:45:06 +02:00
return t.Format("2006-01-02T15:04:05.9999999Z07:00"), nil
2020-07-19 19:32:22 +02:00
} else {
2021-08-30 19:45:06 +02:00
return t.Format(time.RFC3339Nano), nil
2020-07-19 19:32:22 +02:00
}
case schemas.BigInt, schemas.Int:
2021-08-30 19:45:06 +02:00
return t.Unix(), nil
2020-07-19 19:32:22 +02:00
default:
2021-08-30 19:45:06 +02:00
return t, nil
2020-07-19 19:32:22 +02:00
}
}