time: error prone implementation of Time.Format

176 views
Skip to first unread message

Manlio Perillo

unread,
Mar 29, 2017, 5:03:48 PM3/29/17
to golang-nuts
In a program I have a function that formats the time in Italian date format: dd/mm/yyyy, but, due to an oversight, I wrote the layout string as "02/02/2006", instead of "02/01/2006".
This caused all the dates to be incorrectly formatted.

IMHO, this is a nasty behavior.  The layout is clearly incorrect and the implementation should report an error, instead of returning an incorrectly formatted time.


Thanks
Manlio

Ian Lance Taylor

unread,
Mar 29, 2017, 5:18:09 PM3/29/17
to Manlio Perillo, golang-nuts
I think it would be difficult to define "clearly incorrect" in a way
that does not introduce any false negatives while remaining useful.

Ian

Manlio Perillo

unread,
Mar 29, 2017, 5:29:19 PM3/29/17
to golang-nuts, manlio....@gmail.com
In my case I specified stdZeroDay twice.

The AppendFormat function can define some additional boolean variables:
    hasYear, hasMonth, hasDay, ...
and return an error if a variable is set to true more than one time.


Manlio 

Ian Lance Taylor

unread,
Mar 29, 2017, 7:21:14 PM3/29/17
to Manlio Perillo, golang-nuts
On Wed, Mar 29, 2017 at 2:29 PM, Manlio Perillo
<manlio....@gmail.com> wrote:
> Il giorno mercoledì 29 marzo 2017 23:18:09 UTC+2, Ian Lance Taylor ha
> scritto:
>>
>> On Wed, Mar 29, 2017 at 2:03 PM, Manlio Perillo
>> <manlio....@gmail.com> wrote:
>> > In a program I have a function that formats the time in Italian date
>> > format:
>> > dd/mm/yyyy, but, due to an oversight, I wrote the layout string as
>> > "02/02/2006", instead of "02/01/2006".
>> > This caused all the dates to be incorrectly formatted.
>> >
>> > IMHO, this is a nasty behavior. The layout is clearly incorrect and the
>> > implementation should report an error, instead of returning an
>> > incorrectly
>> > formatted time.
>>
>> I think it would be difficult to define "clearly incorrect" in a way
>> that does not introduce any false negatives while remaining useful.
>
>
> In my case I specified stdZeroDay twice.
>
> The AppendFormat function can define some additional boolean variables:
> hasYear, hasMonth, hasDay, ...
> and return an error if a variable is set to true more than one time.

But it's not clearly incorrect to write

"2006-01-02 or Jan 02, 2006"

Ian

John Souvestre

unread,
Mar 29, 2017, 10:02:21 PM3/29/17
to golan...@googlegroups.com
Granted, but is this a common occurrence? Also, the workaround is easy.

On the other hand, how common are the errors which would be caught by adding the test?

John

John Souvestre - New Orleans LA


-----Original Message-----
From: golan...@googlegroups.com [mailto:golan...@googlegroups.com] On Behalf Of Ian Lance Taylor
Sent: 2017 March 29, Wed 18:21
To: Manlio Perillo
Cc: golang-nuts
Subject: Re: [go-nuts] time: error prone implementation of Time.Format

On Wed, Mar 29, 2017 at 2:29 PM, Manlio Perillo
<manlio....@gmail.com> wrote:
> Il giorno mercoled� 29 marzo 2017 23:18:09 UTC+2, Ian Lance Taylor ha
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

roger peppe

unread,
Mar 30, 2017, 7:18:35 AM3/30/17
to Manlio Perillo, golang-nuts
Isn't this the kind of thing we write tests for?

--
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+unsubscribe@googlegroups.com.
Message has been deleted

David Collier-Brown

unread,
Mar 30, 2017, 9:44:14 AM3/30/17
to golang-nuts, manlio....@gmail.com
From your described implementation, I assume you're asking for something like

    As a programmer, if I ask for a date with month twice, I wish to revieve a warning"

If so, it would complain if I asked to print something like "01/01/2017 (1 Jan 2017)"

--dave

Manlio Perillo

unread,
Mar 30, 2017, 10:47:25 AM3/30/17
to golang-nuts, manlio....@gmail.com
Certainly this is a bug that can be found with unit test, however catching a bug early is always better, if this is possible.


Manlio



Il giorno giovedì 30 marzo 2017 13:18:35 UTC+2, rog ha scritto:
Isn't this the kind of thing we write tests for?
On 29 Mar 2017 22:03, "Manlio Perillo" <manlio....@gmail.com> wrote:
In a program I have a function that formats the time in Italian date format: dd/mm/yyyy, but, due to an oversight, I wrote the layout string as "02/02/2006", instead of "02/01/2006".
This caused all the dates to be incorrectly formatted.

IMHO, this is a nasty behavior.  The layout is clearly incorrect and the implementation should report an error, instead of returning an incorrectly formatted time.


Thanks
Manlio

--
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...@googlegroups.com.

Manlio Perillo

unread,
Mar 30, 2017, 10:49:29 AM3/30/17
to golang-nuts, manlio....@gmail.com
I'm probably missing something, but "2006-01-02" is a valid time layout string.

Manlio 

David Collier-Brown

unread,
Mar 30, 2017, 10:55:54 AM3/30/17
to golang-nuts, manlio....@gmail.com
I think he said "02/02/2006", and got two day-numbers instead of a day and a month.

--dave

Ian Lance Taylor

unread,
Mar 30, 2017, 11:28:47 AM3/30/17
to Manlio Perillo, golang-nuts
On Thu, Mar 30, 2017 at 7:49 AM, Manlio Perillo
I'm presenting that entire string as a valid time layout format. It
is valid today, but would be invalid under your proposed change.

My point is to repeat what I said before: it is difficult to define
"clearly incorrect."

Ian

Manlio Perillo

unread,
Mar 30, 2017, 2:22:57 PM3/30/17
to golang-nuts, manlio....@gmail.com
Il giorno giovedì 30 marzo 2017 17:28:47 UTC+2, Ian Lance Taylor ha scritto:
> [...]

>> But it's not clearly incorrect to write
>>
>> "2006-01-02 or Jan 02, 2006"
>>
>
> I'm probably missing something, but "2006-01-02" is a valid time layout
> string.

I'm presenting that entire string as a valid time layout format.  It
is valid today, but would be invalid under your proposed change.

My point is to repeat what I said before: it is difficult to define
"clearly incorrect."


Ok.
I missed the fact that "2006-01-02 or Jan 02, 2006"  was one layout string and not two different layout strings.


Thanks
Manlio
Reply all
Reply to author
Forward
0 new messages