Skip to content

feature: detect time/date out of bounds in time-date rule #1370

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

Merged
merged 2 commits into from
May 27, 2025

Conversation

ccoVeille
Copy link
Contributor

@ccoVeille ccoVeille commented May 22, 2025

dates are checked with the following rules:

  • no validation for year
  • month must be between 1 and 12
  • day must be between 1 and 31 for January, March, May, July, August, October, December
  • day must be between 1 and 30 for April, June, September, November
  • day must be between 1 and 28 for February, or 29 for leap years
  • hour must be between 0 and 23
  • minute must be between 0 and 59
  • second must be between 0 and 59 (and 60 for leap seconds)
  • nanosecond must be between 0 and 999999999, because otherwise you can use a second

Fixes #1369

@ccoVeille
Copy link
Contributor Author

This a naive approach that may require some iterations.

@ccoVeille
Copy link
Contributor Author

ccoVeille commented May 23, 2025

Switching back to draft to code the suggestion made during the code review.

It can be reviewed anyway

@ccoVeille
Copy link
Contributor Author

I'll wait for the #1377 #1378 to be merged and reviewed before coming back to this PR

@ccoVeille ccoVeille force-pushed the time-date branch 2 times, most recently from d4a70e2 to bffa778 Compare May 26, 2025 15:14
@ccoVeille ccoVeille requested a review from chavacava May 26, 2025 15:14
@ccoVeille ccoVeille marked this pull request as ready for review May 26, 2025 15:14
@ccoVeille ccoVeille force-pushed the time-date branch 6 times, most recently from ec1930a to b45d390 Compare May 26, 2025 15:38
@ccoVeille
Copy link
Contributor Author

Here are the change I made since the last time I worked on it:

I'm now validating arguments with multiple rules:

  • day and month equals to 0 are now reported differently
  • the day argument checks for the number of day in a month, even for a leap year
  • month and day inversion such as time.Date(2023, 22, 6, ...) when it should be time.Date(2023, 6, 22, ...)

There are some other changes, but they are the main ones.

Please review again, thanks

Let me know if you think I should adapt the confidence level of each error reporting.

Copy link
Collaborator

@chavacava chavacava left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've some remarks/questions but LGTM

@ccoVeille
Copy link
Contributor Author

I refactored code, it should be clearer I think, thanks @chavacava

@ccoVeille
Copy link
Contributor Author

I feel like, I screw up my PR by resolving conflicts 😅

@ccoVeille ccoVeille marked this pull request as draft May 27, 2025 07:25
dates are checked with the following rules:
- no validation for year
- arguments cannot be negative
- arguments cannot use an explicit plus sign
- month must be between 1 and 12
- day must be between 1 and 31 for January, March, May, July, August, October, December
- day must be between 1 and 30 for April, June, September, November
- day must be between 1 and 28 for February, or 29 for leap years
- hour must be between 0 and 23
- minute must be between 0 and 59
- second must be between 0 and 59 (and 60 for leap seconds)
- nanosecond must be between 0 and 999999999, because otherwise you can use a second
@ccoVeille ccoVeille marked this pull request as ready for review May 27, 2025 07:34
@ccoVeille
Copy link
Contributor Author

It can be reviewed again.

@alexandear alexandear requested a review from Copilot May 27, 2025 09:14
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds validation for out-of-bounds values in calls to time.Date, ensuring month, day, hour, minute, second, and nanosecond arguments fall within their legal ranges.

  • Introduces a new test file (time_date_out_of_bounds.go) with cases covering zero, negative, swapped, and extreme values.
  • Updates the TimeDateRule implementation to track each argument’s boundaries, refactors literal parsing to return int64, and adds unary-sign handling.
  • Adjusts existing decimal‐literal tests and updates documentation/examples in RULES_DESCRIPTIONS.md.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
testdata/time_date_out_of_bounds.go New var block with test cases for out‐of‐bounds dates
testdata/time_date_decimal_literal.go Replaces old examples with an expression case using i and j
test/time_date_test.go Registers the new out‐of‐bounds test suite
rule/time_date.go Implements argument boundary checks and refactors parsing
RULES_DESCRIPTIONS.md Expands rule examples and adds detailed <details>
Comments suppressed due to low confidence (1)

testdata/time_date_decimal_literal.go:91

  • The assignment i = 4 is invalid at package scope inside a var block. You need to declare these as var i = 4 (and var j = 1) or move them into a function scope.
i = 4

Copy link
Collaborator

@alexandear alexandear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Left a few minor comments.

@ccoVeille
Copy link
Contributor Author

Thanks @alexandear I have applied the changes you suggested

@alexandear alexandear merged commit 50dddb4 into mgechev:master May 27, 2025
8 checks passed
@alexandear alexandear deleted the time-date branch May 27, 2025 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

time-date: add out of bound detection
3 participants