3 ms·
Dealing with time on Go, whether simply unmarshalling string representations of time or handling timezone/locale converstions, is so incredibly convenient. Gone
by chimeracoder 13y ago
Dealing with time on Go, whether simply unmarshalling string representations of time or handling timezone/locale converstions, is so incredibly convenient. Gone are the days when I'd have to remember the difference between "%MM", "%mm", and "%m".
I have gotten incredibly spoiled in this regard, and it's incredibly painful now to deal with time in Python or (even worse) Javascript[0].
This isn't even getting at the synchronization/timing features, which Go provides amazing support for as well.
[0] My jaw hit the floor when I saw how difficult it is to get the name of a month from a Date object in Javascript: https://stackoverflow.com/questions/1643320/get-month-name-from-date https://stackoverflow.com/questions/1643320/get-month-name-f...
- enneff 13y agoInterestingly, the Parse and Format functions were inspired by COBOL's "picture statements".
- redbad 13y agoAgreed. In particular, I found the "reference value" approach that Go uses for time parsing incredibly quirky, but ultimately intuitive and unambiguous. http://golang.org/pkg/time/#Parse http://golang.org/pkg/time/#Parse
- awj 13y agoUsing a reference time to describe the layout is pretty brilliant. Out of curiosity, how would it handle non-time "noise" data that happened to be embedded in the string? Expecting "it'll throw an error, also why would you do that" as the answer, but just curious.
- aaronblohowiak 13y agoYou can trivially check on play.golang.org
- grosskur 13y ago"it's incredibly painful now to deal with time in Python" I suggest looking into Arrow: http://crsmithdev.com/arrow/ http://crsmithdev.com/arrow/ My experience is that Arrow fixes date/time handling for Python the same way Requests fixes HTTP handling.
- ivanca 13y agoIn (modern ES5) JavaScript is easy enough: new Date("10/11/2014").toLocaleString("en-us", { month: "long" });
- shultays 13y agoAre you talking about some other answer from top one? The one that takes index of current month and prints it? That looks pretty straight forward to me.
- chimeracoder 13y agoExplicitly and manually creating a (mutable!) array of strings for this purpose is rather ridiculous. Contrast with the equivalent code in Go: http://play.golang.org/p/lpYeBYGEv5 http://play.golang.org/p/lpYeBYGEv5 It's especially ridiculous in Javascript because the internal functions make use of it (try calling (new Date()).toString()), but they don't give direct access to it. A well-designed stdlib should handle all of these basic things without requiring the programmer to do any extra work. It's especially important when talking about time, because there are so many wacky edge cases that no programmer is ever going to be able to predict all of them if they have to roll it themselves.
- rquirk 13y agoIt was one of the things I hated about Go! I am entirely comfortable with the POSIX-style %Y, %m, etc. formatting and having to learn Go's silly way to do it annoyed me no end. The documentation should be much more explicit about what the "reference time" thing means for someone used to strftime. http://golang.org/pkg/time/#Time.Format http://golang.org/pkg/time/#Time.Format
- rwj 13y agoIt took me an unreasonably long time to figure out how that time parsing works. The explanation is not particularly clear. That said, once you understand the format, it is very easy to use.
- chimeracoder 13y ago> For more information about the formats and the definition of the reference time, see the documentation for ANSIC and the other constants defined by this package. I find the information at http://golang.org/pkg/time/#pkg-constants http://golang.org/pkg/time/#pkg-constants very explanatory. What do you think is unclear about the documentation?
- rquirk 13y agoI just didn't "get it" as I had no idea what it meant by a reference time. Normally a time is formatted like %Y-%m-%d and so on, these examples look nothing like that so I had no idea that 2006 is how they spell %Y, 01 is %m, 02 is %d, etc. If you have grown up with strptime and friends, the Go version and documentation makes literally no sense. Then it suddenly clicks ("ah! they do that?!") and it is perfectly obvious.