Tuesday, January 27, 2015
A fine line indeed
In C (and Lua, PHP, Python and just about
any other language you'll find on Unix), you use the function
to format a timestamp based on a format string. I always have to look up
the format when I want to use it, but hey, it's not like I do it all the
time (otherwise, I wouldn't have to look this up).
Anyway, it seems that Go is … different (of course! Rob Pike & Co. know better and feel that replacing the entire development chain is the way to go, what with mandating a coding style in the language design and a static-only linking technology straight from the 70s and its own runtime and standard library … but I digress), and I'm still trying to decide if the approach is stupid or clever—to specify the format with an example! (link to this via Hacker News).
const ( ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" // Handy time stamps. Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000" )
Yes, you specify how you want the date formated by writing out January 2nd, 2006, at 3:05 PM in Mountain Standard Time (of course!) however you want it. Never mind that the 1-2-3 sequence is the American “month-then-day” format (Europe? Isn't that near New England somewhere? Across some pond or something).
Now, it's not the first language to specify output using an example. Some versions of Microsoft Basic had that feature to specify numeric output, but I've never seen anything quite like this.
And the justification for it?
I think the litmus test is that if a function's usage is so hard to remember that someone is motivated enough to create a domain and web site whose only purpose is to make it easier to remember where to find the documentation, then that function is broken. (http://strftime.org/)
Time.Format doesn't need to be the kitchen sink. If you need more control, there is always
fmt.Sprintf. Unlike C's
struct tm, Go's
time.Timedoes not require the addition of magic constants to turn struct fields into printable values.
I'm still unsure how I feel about this.