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

proposal: time: a better way to validate a time instance #66029

Closed
amarjeetanandsingh opened this issue Feb 29, 2024 · 1 comment
Closed

proposal: time: a better way to validate a time instance #66029

amarjeetanandsingh opened this issue Feb 29, 2024 · 1 comment
Labels
Milestone

Comments

@amarjeetanandsingh
Copy link
Contributor

amarjeetanandsingh commented Feb 29, 2024

Proposal Details

With current time pkg api, what is a good way to validate if a given input date is valid or not?

Suppose a scenario we have given three ints 2021, 02 and 29, for year,month,day and need to validate if it represents a valid date or not.

This can be achieved by writing a small helper function like

func isValidDate(year, month, day int) bool {
	t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
	y, m, d := t.Date()

	return y == year && m == time.Month(month) && d == day
}

Or maybe another way could be -

func isValidDate(year, month, day int) bool {
	dateStr := fmt.Sprintf("%d-%d-%d", year, month, day)
	_, err := time.Parse("2006-1-2", dateStr)
	return err == nil
}

What approach would u recommend for this? Or is this use case worthy enough to be added in the time pkg?

@gopherbot gopherbot added this to the Proposal milestone Feb 29, 2024
@amarjeetanandsingh amarjeetanandsingh changed the title proposal: time: a better way to validate if a time instance proposal: time: a better way to validate a time instance Feb 29, 2024
@seankhliao
Copy link
Member

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants