go-tools icon indicating copy to clipboard operation
go-tools copied to clipboard

time.Date versus people miusing octual notation

Open ccoVeille opened this issue 6 months ago • 0 comments

I'm often facing the following pattern when using gofumpt

People are using time.Date with leading 0 for days, months, hours, minutes, or seconds …

here is an example

        now := time.Date(2001, 02, 03, 04, 05, 06, 07, time.UTC)

A leading 0 is one of the octal notation

So gofumpt suggests fixing it to use the 0o literal octal notation

So it transforms this piece of code into

        now := time.Date(2001, 0o2, 0o3, 0o4, 0o5, 0o6, 0o7, time.UTC)

This pattern is very frequent.

https://github.com/search?q=+%22time.Date%2820%22+language%3Ago+%22%2C+02%2C+%22++&type=code&p=1

People should use

        now := time.Date(2001, 2, 3, 4, 5, 6, 7, time.UTC)

For records, I found examples of code where I'm sure gofumpt suggestions were applied, and where time.Date is now using the octal notation

https://github.com/search?q=+%22time.Date%2820%22+language%3Ago+%22%2C+0o1%2C+%22++&type=code

https://github.com/search?q=+%22time.Date%2820%22+language%3Ago+%22%2C+0o2%2C+%22++&type=code

https://github.com/search?q=+%22time.Date%2820%22+language%3Ago+%22%2C+0o4%2C+%22++&type=code

So the checker that could be written should look for 00-07 and 0o0-0o7

This would be very similar to https://staticcheck.dev/docs/checks/#SA9002, just in the opposite direction.

Originally posted by @dominikh in https://github.com/mvdan/gofumpt/issues/309#issuecomment-2266836614

ccoVeille avatar Aug 03 '24 16:08 ccoVeille