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

Change Time::DayOfWeek to ISO ordinal numbering based on Monday = 1 #6555

Merged

Conversation

straight-shoota
Copy link
Member

Fixes #6554

DayOfWeek.from_value also identifies 0 as Sunday making it work with both numbering schemes. I don't think this should pose a problem.
DayOfWeek.new still requires the proper value, meaning 0 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 use value % 7.

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
Copy link
Member

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?

Copy link
Contributor

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...

Copy link
Member Author

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
Copy link
Contributor

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...

@asterite
Copy link
Member

What's the reason it works for 0? I would just leave the default Enum implementation.

@straight-shoota
Copy link
Member Author

What's the reason it works for 0?

Sunday = 0 is also commonly used besides the standard numbering starting at Monday = 1. For example the time format directive %w uses this.

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 from_value, there could also just be a separate method for this (like from_value_sun0 or similar). I just figured it would be easier to also interpret from_value(0) as Sunday.

@straight-shoota straight-shoota force-pushed the jm/feature/time-day_of_week-iso branch 3 times, most recently from 1d555a1 to c9ed04d Compare September 8, 2018 11:46
Copy link
Member

@asterite asterite left a 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)

@straight-shoota
Copy link
Member Author

@asterite Sounds fair.

@straight-shoota straight-shoota force-pushed the jm/feature/time-day_of_week-iso branch from c9ed04d to d46294c Compare September 10, 2018 11:45
@RX14 RX14 requested a review from bcardiff October 3, 2018 11:49
@RX14 RX14 added this to the 0.27.0 milestone Oct 3, 2018
Copy link
Member

@sdogruyol sdogruyol left a 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 👍

@RX14 RX14 merged commit 698ebf6 into crystal-lang:master Oct 10, 2018
@straight-shoota straight-shoota deleted the jm/feature/time-day_of_week-iso branch October 10, 2018 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants