litter icon indicating copy to clipboard operation
litter copied to clipboard

feature request: time.Time in readable format

Open trakhimenok opened this issue 8 years ago • 6 comments

The time.Time type is commonly and often used but when a struct with Time field is dumped it does not show any specifics.

For example I get:

DtCreated: time.Time{},

Would be great if by default it shows in ISO format, something like:

DtCreated: time.Time{2017-10-31T23:45.000},
DtUpdated: time.Time{0}, // for zero times

trakhimenok avatar Nov 02 '17 00:11 trakhimenok

Do you still get an empty Time{} if you set litter.Config.HidePrivateFields to false? It should emit the private fields. Which is less readable than your proposal, of course, but the point of Litter is to emit something that is, at least in principle, parseable back to Go.

atombender avatar Nov 02 '17 00:11 atombender

Seems litter.Config.HidePrivateFields = false can be partially suitable for reading though I'm not sure how it's parseable back to Go.

test.go:48: litter.Config.HidePrivateFields = true:  time.Time{}
test.go:50: litter.Config.HidePrivateFields = false:  time.Time{
	  sec: 63645179878,
	  nsec: 545561922,
	  loc: &time.Location{
	    name: "",
	    zone: nil,
	    tx: nil,
	    cacheStart: 0,
	    cacheEnd: 0,
	    cacheZone: nil,
	  },
	}

I was under impression purpose of the library is primarily to provide human readable output.

trakhimenok avatar Nov 02 '17 00:11 trakhimenok

It's human-readable because it's Go. 🙂

While the time dumping is not ideal, it's how the time package does things, so it's technically correct, and not that terrible for the purpose that Litter exists.

That said, you can provide a custom dumper for your struct. We would also entertain the idea of adding support for custom dumpers for specific types, such as time.Time.

atombender avatar Nov 02 '17 00:11 atombender

A map of formatters by type in Options would be handy. Also you can consider map of options by type so HidePrivateFields can be specified for a specific type.

trakhimenok avatar Nov 02 '17 02:11 trakhimenok

I like the idea of overriding dumpers for types that are not your own. Also we could consider having a way for dumpers to annotate the dumps so that a time would be dumped as:

DtCreated: time.Time{}, // 2017-10-31T23:45.000

or maybe

DtCreated: time.Time{/* 2017-10-31T23:45.000 */}, 

At the moment I have no specific idea how this would be expressed through the api.

simen avatar Nov 06 '17 10:11 simen

My current workaround for this is:

timeType := reflect.TypeOf(time.Time{})
litter.Config.DumpFunc = func(v reflect.Value, w io.Writer) bool {
		if v.Type() != timeType {
			return false
		}

		t := v.Interface().(time.Time)
		fmt.Fprintf(w, `{/* time.Unix(%d, 0) */}`, t.Unix())
		return true
	}

I would prefer to have the function directly instead of a comment, however the type time.Time is always printed, unfortunately.

urandom avatar Jun 02 '22 09:06 urandom