Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: time.Time in readable format #12

Open
trakhimenok opened this issue Nov 2, 2017 · 6 comments
Open

feature request: time.Time in readable format #12

trakhimenok opened this issue Nov 2, 2017 · 6 comments

Comments

@trakhimenok
Copy link

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
@atombender
Copy link
Member

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.

@trakhimenok
Copy link
Author

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.

@atombender
Copy link
Member

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.

@trakhimenok
Copy link
Author

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.

@simen
Copy link
Member

simen commented Nov 6, 2017

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.

@urandom
Copy link

urandom commented Jun 2, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants