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: Parse of am/pm should be forgiving of spaces and case #51320

Open
spekary opened this issue Feb 22, 2022 · 4 comments
Open

time: Parse of am/pm should be forgiving of spaces and case #51320

spekary opened this issue Feb 22, 2022 · 4 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@spekary
Copy link

spekary commented Feb 22, 2022

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

1.17.5

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
Mac OS

What did you do?

import (
"testing"
"time"
)

func TestParse(t *testing.T) {
var tm time.Time
var err error

tm, err = time.Parse("3:04PM", "5:23 PM")
if err != nil {
	t.Error(err)
}
if tm.Hour() != 17 {
	t.Error("hour does not match")
}

tm, err = time.Parse("3:04pm", "5:23PM")
if err != nil {
	t.Error(err)
}
if tm.Hour() != 17 {
	t.Error("hour does not match")
}

}

What did you expect to see?

Tests pass

What did you see instead?

=== RUN TestParse
parse_test.go:14: parsing time "5:23 PM" as "3:04PM": cannot parse "M" as "PM"
parse_test.go:17: hour does not match
parse_test.go:22: parsing time "5:23PM" as "3:04pm": cannot parse "" as "pm"
parse_test.go:25: hour does not match
--- FAIL: TestParse (0.00s)

@spekary
Copy link
Author

spekary commented Feb 22, 2022

If this is too big a change, a possible solution would be an additional layout specifier: "Pm", or more wonky "pM". This could indicate a desire to be forgiving of spaces and case.

@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 23, 2022
@thanm
Copy link
Contributor

thanm commented Feb 23, 2022

@rsc per owners.

It seems as though it would be very easy to write a bit of Go code that would preprocess time values to put them in canonical form prior to the time.Parse() invocation. That is, if you want both "PM" and "pm" to be accepted, just pattern match the tail end of the value string and replace it if needed.

@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
@andlabs
Copy link
Contributor

andlabs commented Jul 27, 2023

Another example, including periods/dots:

parsing time "07/13/2023 9:34 A.M." as "01/02/2006 3:04 P.M.": cannot parse "A.M." as " P.M."

(error message includes all relevant parameters; just tried this in the 1.20 playground)

@ghost
Copy link

ghost commented Jul 27, 2023

The bottom of https://knowadays.com/blog/writing-the-time-am-and-pm-or-a-m-and-p-m/ has a summary of various style guides that suggests almost all combinations are possible and acceptable.

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

4 participants