Live data from Hacker News

Time in Go

bl.ocks.org

1–10 of 80 posts

Re: Time in Go

#2
Golang to me has the best time manipulation I ever seen, wannabe change the timezone, easy as eat pie, wanna add seconds? No prob, wanna compare dates? No biggie

Re: Time in Go

#3

Golang to me has the best time manipulation I ever seen, wannabe change the timezone, easy as eat pie, wanna add seconds? No prob, wanna compare dates? No biggie

True, and for most cases the parsing is great too, but once you get off the beaten path of standard time formats, parsing times can be annoying.

Re: Time in Go

#4

Golang to me has the best time manipulation I ever seen, wannabe change the timezone, easy as eat pie, wanna add seconds? No prob, wanna compare dates? No biggie

Agreed. Coming from JavaScript, I'm really happy with how intuitive it is to handle date and time with Go.

I'm new to the language but I'm really happy with the simplicity and the design decisions made by the core team.

Re: Time in Go

#5
More languages should copy Go's time library.

As someone who has written a lot of Go for years, I am always very quick to say that "time" is my favorite Go standard library. I love it.

The "time" library is to Go what the "requests" [non-standard] library is to Python, in terms of a great API and simplicity. For some reason in every other language I've used (important, I don't know every time library! :P) the time library has always been adequate, but not much else.

I think Python's "datetime" is a great example of a not great time library. At one point I was writing Python every day for a couple years. During that time, I could never ever remember the API for datetime, despite it being able to do what I needed to do well enough. I always had to open the docs. I think this is an example of an adequate library that just doesn't have a great API.

Or, take the more generic problem of: I have a time, I have a duration, how do I tell if that duration has passed? Most developers I know (including myself) stumble on this for some period of time (see what I did there?), remembering what do I add to what and is it a greater than or a less than or do I subtract, etc. It is an easy problem, but always seems to require a little bit of thinking.

Go's "time" library is nothing like this.

Go's "time" library you will remember the API, it is intuitive. You can even guess it after writing Go for some period of time. It is that easy (ignoring the constants and non-standard printf-like function in it).

Go's "time" comparison/arithmetic is incredible. Comparing times? Determining expiration? You'll almost never get this wrong the first time. (But write tests anyways)

What I'm trying to say is: even if you don't like Go, take a look at how Go's "time" library works. I think it would be valuable for other languages. Other languages can probably even do it better (imagining better type systems around units), but I've never seen an API that feels as right for manipulating time as Go's time stdlib.

Re: Time in Go

#6
post #3

Golang to me has the best time manipulation I ever seen, wannabe change the timezone, easy as eat pie, wanna add seconds? No prob, wanna compare dates? No biggie

True, and for most cases the parsing is great too, but once you get off the beaten path of standard time formats, parsing times can be annoying.

Do you have any examples of annoying to parse time formats?

Re: Time in Go

#7
post #6
post #3

Earlier quoted context omitted.

True, and for most cases the parsing is great too, but once you get off the beaten path of standard time formats, parsing times can be annoying.

Do you have any examples of annoying to parse time formats?

http://play.golang.org/p/hCoZ3tdeM_ am I doing this correctly?

Because it seems that golang does not recognize the abbreviations at all.

Re: Time in Go

#8

More languages should copy Go's time library. As someone who has written a lot of Go for years, I am always very quick to say that "time" is my favorite Go standard library. I love it. The "time" library is to Go what the "requests" [non-standard] library is to Python, in terms of a great API and simplicity. For some reason in every other language I've used (important, I don't know every time library! :P) the time li…

I think most language standard libraries just crib heavily off of the C interface, to their detriment.

Using a preset date (the 2006-01-02) instead of format strings was really inspired; it took me about 10 minutes of staring at the documentation to understand how it worked, but it's a lot more readable once you get it.

Re: Time in Go

#10
post #6
post #3

Earlier quoted context omitted.

True, and for most cases the parsing is great too, but once you get off the beaten path of standard time formats, parsing times can be annoying.

Do you have any examples of annoying to parse time formats?

I can find it later, but I just had this issue this week with timestamps sent from a browser, supposedly in RFC3339, but Go's RFC3339 format was not compliant with it. So naturally I made my own format template. Alas, the milliseconds part had arbitrary precision, which Go's parser doesn't like. So I had to revert to trying the timestamp with one, two ,three trailing digits, etc.

Still, this is the only issue I have with the time library, and even that has only bothered me a couple of times in 3 years of work, so no biggie. Other than that it's indeed the best time library I've ever worked with.

Post reply on HN