-
Notifications
You must be signed in to change notification settings - Fork 56
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
Comments
Do you still get an empty |
Seems
I was under impression purpose of the library is primarily to provide human readable output. |
It's human-readable because it's Go. 🙂 While the time dumping is not ideal, it's how the 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 |
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. |
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. |
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 |
Thanks for the brilliant workaround, @urandom. I'm regenerating tests for an email parsing programme which requires second accuracy. The following works well for my use case without any further processing. 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)
t = t.In(time.UTC)
t.Truncate(time.Second) // only need accuracy to seconds for email parsing
fmt.Fprintf(
w,
`(time.Date(%d, %d, %d, %d, %d, %d, %d, time.UTC))`,
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(),
)
return true
} Output snippet: func TestParseEmailHeadersEnglishPlaintextAsciiOver7bit(t *testing.T) {
fp := "tests/test_english_plaintext_ascii_over_7bit.txt"
expectedEmail := &email.Email{
Headers: email.Headers{
Date: time.Time(time.Date(2019, 4, 1, 6, 55, 0, 0, time.UTC)),
Sender: &mail.Address{
Name: "Alice Sender",
Address: "[email protected]",
},
// ... |
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:
Would be great if by default it shows in ISO format, something like:
The text was updated successfully, but these errors were encountered: