A fine line indeed

In C (and Lua [1], PHP [2], Python [3] and just about any other language you'll find on Unix), you use the function strftime() [4] 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 [5] is … different [6] (of course! Rob Pike [7] & 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 [8]—to specify the format with an example! (link to this via Hacker News [9]).

>
```
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 2^nd, 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 [10], 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/ [11])
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.Time does not require the addition of magic constants to turn struct fields into printable values.

“Time formatting - Google Groups [12]”

I'm still unsure how I feel about this.

[1] http://www.lua.org/manual/5.3/manual.html#pdf-os.date

[2] http://php.net/manual/en/function.strftime.php

[3] https://docs.python.org/2/library/time.html

[4] http://pubs.opengroup.org/onlinepubs/009695399/functions/strftime.html

[5] http://golang.org/

[6] http://golang.org/pkg/time/#pkg-

[7] http://en.wikipedia.org/wiki/Rob_Pike

[8] https://www.youtube.com/watch?v=P6Fc_5slG_Q&t=1m28s

[9] https://news.ycombinator.com/item?id=8944578

[10] http://www.antonis.de/qbebooks/gwbasman/printusing.html

[11] http://strftime.org/

[12] https://groups.google.com/forum/#!msg/golang-

Gemini Mention this post

Contact the author