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

proposal: time: add Time.GoString method to help %#v #39034

Open
kevinburkemeter opened this issue May 13, 2020 · 5 comments
Open

proposal: time: add Time.GoString method to help %#v #39034

kevinburkemeter opened this issue May 13, 2020 · 5 comments
Labels
Projects
Milestone

Comments

@kevinburkemeter
Copy link

@kevinburkemeter kevinburkemeter commented May 13, 2020

I have an array of time.Time objects that I retrieved from the database, and I would like to place them in a Go source file. As I understand it, there's not an easy way to do this without a helper method.

  • fmt.Sprintf("%#v\n", t) prints out something like:

    time.Time{wall:0x0, ext:63724924180, loc:(*time.Location)(nil)}

    which not only makes it difficult to determine what time is represented - I can't do math on 63724924180 in my head to determine whether this is yesterday or today, but also can't be embedded directly in a Go program because wall and ext and loc are private variables.

  • I could use t.Format(time.RFC3339) on each item to get an array of strings, but then these need to be re-Parse'd to get a time.Time back, with appropriate error handling.

It could be nice to have either a (time.Time) GoString() string method or a GoString format constant which would print out a format that could be embedded in a Go source file.

For example

fmt.Sprintf(t.Format(time.GoString))

could yield

"time.Date(2020, time.February, 31, 23, 59, 59, 0, nil)"

or similar. I am not sure whether having time.Time implement the GoStringer interface - and change the format printed by %#v - would violate the Go compatibility promise, though I doubt the current wall, ext, loc format is very useful for most people.

The trickiest part of this would be printing Location, which could be implemented by printing out the pointer address for non-nil, non-UTC, non-Local locations - it's no worse than what's currently done.

I suppose we'd also lose some data about the monotonic-ness of the time measurement in question but if you're trying to put this in source code you likely don't care about that bit.

@gopherbot gopherbot added this to the Proposal milestone May 13, 2020
@gopherbot gopherbot added the Proposal label May 13, 2020
@kevinburkemeter
Copy link
Author

@kevinburkemeter kevinburkemeter commented May 13, 2020

Ah, t.Format(time.GoString) would not work since we can only do string formatting there and that would likely be insufficient for printing the Location part so it does not need error handling. Still we could satisfy the GoStringer interface.

@OneOfOne
Copy link
Contributor

@OneOfOne OneOfOne commented May 13, 2020

I'm not really sure that needs to be in the stdlib, it's trivial to implement without t.Format

https://play.golang.org/p/Lzzbd55VBeH

@kevinburkemeter
Copy link
Author

@kevinburkemeter kevinburkemeter commented May 13, 2020

If your struct is

type A struct {
     B time.Time
}

a := &A{B: time.Now()}
fmt.Printf("%#v\n", a)

Suddenly you need to either implement your custom format string for everything, just so you can get at the time.Time, or hack up something like Printf("&A{B: %s}\n", toGoTime(a.B)) to rebuild the struct formatting around your custom element formatting.

More to the point this is why the GoStringer interface exists.

@OneOfOne
Copy link
Contributor

@OneOfOne OneOfOne commented May 13, 2020

I see your point, you can still easily wrap it but yeah it'd be nice.

https://play.golang.org/p/OhAGAR6bPs4

@rsc rsc changed the title proposal: easier formatting of time.Time for placement in Go source code proposal: time: add Time.GoString method to help %#v Jun 10, 2020
@rsc
Copy link
Contributor

@rsc rsc commented Jun 10, 2020

This mostly makes sense, but it would have to hide the monotonic time if any.

@rsc rsc added this to Incoming in Proposals Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Proposals
Incoming
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.