-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Change Time::DayOfWeek to ISO ordinal numbering based on Monday = 1 #6555
Change Time::DayOfWeek to ISO ordinal numbering based on Monday = 1 #6555
Conversation
src/time.cr
Outdated
# This method also accepts `0` to identify `Sunday` in order to be compliant | ||
# with the `Sunday = 0` numbering. All other days are equal in both formats. | ||
def self.from_value(value : Int32) | ||
value = (value + 6) % 7 + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
surely return Sunday if value == 0
would be better here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should happen when the value is greater than 7
?
The current implementation pass for the test below, is it the expected behavior?
Time::DayOfWeek.from_value(8).should eq Time::DayOfWeek::Monday
Time::DayOfWeek.from_value(9).should eq Time::DayOfWeek::Tuesday
# and goes on...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think either makes some kind of sense. But we can make it more strict, no problem.
src/time.cr
Outdated
# This method also accepts `0` to identify `Sunday` in order to be compliant | ||
# with the `Sunday = 0` numbering. All other days are equal in both formats. | ||
def self.from_value(value : Int32) | ||
value = (value + 6) % 7 + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should happen when the value is greater than 7
?
The current implementation pass for the test below, is it the expected behavior?
Time::DayOfWeek.from_value(8).should eq Time::DayOfWeek::Monday
Time::DayOfWeek.from_value(9).should eq Time::DayOfWeek::Tuesday
# and goes on...
What's the reason it works for 0? I would just leave the default Enum implementation. |
That why there should be a way to retrieve the week day from a 0-numbered index. As expressed in #6554, this doesn't need to be an override of |
1d555a1
to
c9ed04d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think from_value(8)
and others should work, just from_value(0)
..from_value(7)
@asterite Sounds fair. |
c9ed04d
to
d46294c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @straight-shoota 👍
Fixes #6554
DayOfWeek.from_value
also identifies0
asSunday
making it work with both numbering schemes. I don't think this should pose a problem.DayOfWeek.new
still requires the proper value, meaning0
is not recognized.I did not add a method to retrieve the value based on sunday = 0 (initially proposed as
#value_sun0
) because I think it is much easier and maybe even better understandable to just usevalue % 7
.