render_time.go 484 B

1234567891011121314151617181920212223242526
  1. package render
  2. import (
  3. "reflect"
  4. "time"
  5. )
  6. func renderTime(value reflect.Value) (string, bool) {
  7. if instant, ok := convertTime(value); !ok {
  8. return "", false
  9. } else if instant.IsZero() {
  10. return "0", true
  11. } else {
  12. return instant.String(), true
  13. }
  14. }
  15. func convertTime(value reflect.Value) (t time.Time, ok bool) {
  16. if value.Type() == timeType {
  17. defer func() { recover() }()
  18. t, ok = value.Interface().(time.Time)
  19. }
  20. return
  21. }
  22. var timeType = reflect.TypeOf(time.Time{})