* Kurtis Rader <kra...@skepticism.us> [210505 23:35]:
> On Wed, May 5, 2021 at 8:23 PM Bagas Sanjaya <bagasdo...@gmail.com> wrote:
> 
> > On 06/05/21 04.37, Ian Lance Taylor wrote:
> >   > You wrote 2009 where you need to write 2006.
> >
> > Wouldn't it possible to define arbitrary reference time and format it
> > according to what it would like?
> >
> 
> If arbitrary literals for the various components were allowed it is certain
> to create ambiguities. Especially, when you allow for locale specific
> spellings and formatting of the date components.. The Go approach is easier
> to understand if you read/speak English but suggests you can replace exact
> literals (e.g., "2006") with what seems like an equivalent literal (e.g.,
> "2009") rather than the opaque POSIX `%Y`. This is the reason I am not a
> fan of the Go approach compared to the POSIX strftime approach (see
> https://pubs.opengroup.org/onlinepubs/009695399/functions/strftime.html)

The fact that this sort of confusion over the layout for Time.Format
occurs frequently is a good indication that the approach taken is not as
programmer-friendly as it ought to be.  However, Time.Format has what I
consider to be a much more egregious design flaw; one that should have
ruled it out as the design for the standard library:  the notation
cannot unambiguously represent any desired format containing literals.
If you want literal digits in the output, you are unlikely to be able
use a single call to Time.Format without additional string manipulation
afterward.

Try to code the Go equivalent of the Linux command «date +"%Y 4 %d"».
You either need two calls to Time.Format with string concatenation, or
you must manipulate the string result of Time.Format to insert the
literal "4".

The %x notation is used in Go's fmt package, so this syntax must already
be clearly understood by a Go programmer.  This notation allows any
literal to be unambiguously contained within the format string.  Why add
the cognitive overload of a completely different notation for time
formatting that many programmers struggle to understand?  Also, I would
be very surprised if there were many Go programmers who never have to
look up which reference digit to use for day and which for seconds; the
order is arbitrary and not logical.  It doesn't take much to remember
that %H %M %S is hours, minutes, and seconds, while %y %m %d is year,
month, and day.

I would be willing to bet that if a Time.Printf were added to the time
package, it would very quickly become the overwhelming favorite over
Time.Format.  This would be a compatible change that could easily be
added without violating the Compatibility Promise.

If I were designing this, however, I would not just copy the Linux date
format.  I would use format modifiers similar to the fmt package.  E.g.
instead of %y for two-digit year and %Y for four-digit year, year would
always be %y with a way to indicate how it should be formatted.

...Marvin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20210506125722.uye4s7ls4na6rie4%40basil.wdw.

Reply via email to