summaryrefslogtreecommitdiffstats
path: root/vendor/xorm.io/xorm/dialects/time.go
blob: f0bbb7651875b6f224c307e682eea9ae8ce029ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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 (
	"strings"
	"time"

	"xorm.io/xorm/schemas"
)

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

	var tmZone = dbLocation
	if col.TimeZone != nil {
		tmZone = col.TimeZone
	}

	t = t.In(tmZone)

	switch col.SQLType.Name {
	case schemas.Date:
		return t.Format("2006-01-02"), nil
	case schemas.Time:
		var layout = "15:04:05"
		if col.Length > 0 {
			layout += "." + strings.Repeat("0", col.Length)
		}
		return t.Format(layout), nil
	case schemas.DateTime, schemas.TimeStamp:
		var layout = "2006-01-02 15:04:05"
		if col.Length > 0 {
			layout += "." + strings.Repeat("0", col.Length)
		}
		return t.Format(layout), nil
	case schemas.Varchar:
		return t.Format("2006-01-02 15:04:05"), nil
	case schemas.TimeStampz:
		if dialect.URI().DBType == schemas.MSSQL {
			return t.Format("2006-01-02T15:04:05.9999999Z07:00"), nil
		} else {
			return t.Format(time.RFC3339Nano), nil
		}
	case schemas.BigInt, schemas.Int:
		return t.Unix(), nil
	default:
		return t, nil
	}
}