You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

time.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package dialects
  5. import (
  6. "strings"
  7. "time"
  8. "xorm.io/xorm/schemas"
  9. )
  10. // FormatColumnTime format column time
  11. func FormatColumnTime(dialect Dialect, dbLocation *time.Location, col *schemas.Column, t time.Time) (interface{}, error) {
  12. if t.IsZero() {
  13. if col.Nullable {
  14. return nil, nil
  15. }
  16. if col.SQLType.IsNumeric() {
  17. return 0, nil
  18. }
  19. }
  20. var tmZone = dbLocation
  21. if col.TimeZone != nil {
  22. tmZone = col.TimeZone
  23. }
  24. t = t.In(tmZone)
  25. switch col.SQLType.Name {
  26. case schemas.Date:
  27. return t.Format("2006-01-02"), nil
  28. case schemas.Time:
  29. var layout = "15:04:05"
  30. if col.Length > 0 {
  31. layout += "." + strings.Repeat("0", col.Length)
  32. }
  33. return t.Format(layout), nil
  34. case schemas.DateTime, schemas.TimeStamp:
  35. var layout = "2006-01-02 15:04:05"
  36. if col.Length > 0 {
  37. layout += "." + strings.Repeat("0", col.Length)
  38. }
  39. return t.Format(layout), nil
  40. case schemas.Varchar:
  41. return t.Format("2006-01-02 15:04:05"), nil
  42. case schemas.TimeStampz:
  43. if dialect.URI().DBType == schemas.MSSQL {
  44. return t.Format("2006-01-02T15:04:05.9999999Z07:00"), nil
  45. } else {
  46. return t.Format(time.RFC3339Nano), nil
  47. }
  48. case schemas.BigInt, schemas.Int:
  49. return t.Unix(), nil
  50. default:
  51. return t, nil
  52. }
  53. }