Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: one-digit hour is accepted for 15:04:05 template #29911

Open
tredoe opened this issue Jan 24, 2019 · 7 comments
Open

time: one-digit hour is accepted for 15:04:05 template #29911

tredoe opened this issue Jan 24, 2019 · 7 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@tredoe
Copy link

tredoe commented Jan 24, 2019

What version of Go are you using (go version)?

$ go version
go version go1.11.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

https://play.golang.org/p/EBxIIXatR1Y

What did you expect to see?

I expected to see an error at parsing the time "3:04:05" for layout "15:04:05", of the same way that it fails at parsing the minutes ("03:4:05") or the seconds ("03:04:5").

What did you see instead?

No error.

@mattn
Copy link
Member

mattn commented Jan 24, 2019

@tredoe
Copy link
Author

tredoe commented Jan 24, 2019

I want not to accept hours, minutes neither seconds with an only digit. And it is parsed correctly for both minutes and seconds with the layout "15:04:05".

@mattn
Copy link
Member

mattn commented Jan 24, 2019

Ah, I undestand just now. Sorry.

@FiloSottile FiloSottile changed the title time: fails at parsing the hour time: one-digit hour is accepted for 15:04:05 template Jan 24, 2019
@FiloSottile
Copy link
Contributor

I'm afraid there is no way to require 24-hour zero-padded hours with a time.Parse template.

/cc @rsc to confirm

@FiloSottile FiloSottile added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 24, 2019
@FiloSottile FiloSottile added this to the Unplanned milestone Jan 24, 2019
@FiloSottile
Copy link
Contributor

@Virepri You can open a proposal if you want to discuss such a big change, but in general we try not to provide two different ways of doing very similar things in the standard library.

This might be better as an external package.

@gopherbot
Copy link

Change https://go.dev/cl/425044 mentions this issue: time: fix Parse to reject single digit hour in RFC3339

@arp242
Copy link

arp242 commented Oct 3, 2023

This might be better as an external package.

Seems a bit much to maintain and/or import an external package for such a minor feature?

Anyway, this is what I came up with for the time being:

// time.Parse() will accept numbers without a leading zero; there isn't any way
// to require it. https://github.com/golang/go/issues/29911
//
// The separators (- and :) should always be at the same location for the layout
// and date string.
func missingLeadingZero(d, l string) bool {
	for i, c := range []byte(l) {
		if c == '.' || c == 'Z' {
			return false
		}
		if (c < '0' || c > '9') && d[i] != c {
			return true
		}
	}
	return false
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants