time.go 528 B

123456789101112131415161718192021222324252627282930
  1. package utilx
  2. import (
  3. "database/sql"
  4. "time"
  5. )
  6. const TimeLayout = "2006-01-02 15:04:05"
  7. // ParseTime 文本转时间
  8. func ParseTime(str string) time.Time {
  9. t, _ := TryParseTime(str)
  10. return t
  11. }
  12. func TryParseTime(str string) (time.Time, error) {
  13. return time.ParseInLocation(TimeLayout, str, time.Local)
  14. }
  15. // NullTimeFormat 格式化sql.NullTime
  16. func NullTimeFormat(t sql.NullTime) string {
  17. v, _ := t.Value()
  18. if v == nil {
  19. return ""
  20. }
  21. if ts, ok := v.(time.Time); ok {
  22. return ts.Format(TimeLayout)
  23. }
  24. return ""
  25. }