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

fix(timepicker): switch to PM when hour > 12 #1684

Closed
wants to merge 1 commit into from

Conversation

dmytroyarmak
Copy link
Contributor

Fixes #1680

@@ -224,7 +224,11 @@ export class NgbTimepicker implements ControlValueAccessor,
updateHour(newVal: string) {
const isPM = this.model.hour >= 12;
const enteredHour = toInteger(newVal);
this.model.updateHour((this.meridian ? enteredHour % 12 : enteredHour) + (this.meridian && isPM ? 12 : 0));
if (this.meridian && (isPM && enteredHour < 12 || !isPM && enteredHour === 12)) {
this.model.updateHour(enteredHour + 12);

Choose a reason for hiding this comment

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

What would happen if I enter, say 30? I don't think that this logic here is enough...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if you enter 30 it will try to set it as an hour without any corrections and updateHour will do % 24, so the result will be 6, so it will switch to AM.

@pkozlowski-opensource
Copy link
Member

@dmytroyarmak thnx for the PR. I can see how it would solve the existing issue, but IMO if we want to fix it one and for all we need to decide what happens if a user enters any number > 12 for the meridian on and AM / PM being selected. As such we should have tests for all those cases.

Would you be willing to work more on this PR and add more tests that prove proper behaviour in all corner cases?

@dmytroyarmak
Copy link
Contributor Author

dmytroyarmak commented Jul 17, 2017

@pkozlowski-opensource yeah, I can do more work on this PR
So, how it works in my PR is:

if current mode is AM then
  if 0 <= entered value < 12 then
    save value as is in 24h format (no correction needed)
  else if entered value is 12 then
    store it as 0 in 24h format (in current implementation we add 12 to it and then `mod 24` transform it to 0)
  else if entered value > 12 then
    store it as is in 24h format by doing `mod 24`

else if current mode is PM then
  if 0 <= entered value < 12 then
    add 12 and save
  else if  entered value >= 12
    store it as is in 24h format by doing `mod 24`

So, in general, if value is <= 12 we store it as a correct value in current meridian by doing needed corrections to transform it to 24h format. But if value is > 12, we just save its remainder of division by 24 as a value in 24h format.

@pkozlowski-opensource If this logic is correct I can cover not-covered already cases with unit tests.

@pkozlowski-opensource
Copy link
Member

@dmytroyarmak Yup, your reasoning looks fine! If you could please add more tests for the listed cases it would be great. I will merge your PR as soon as we've got tests covering all the cases.

Thnx so much for helping it, this is much appreciated! 👍

@pkozlowski-opensource pkozlowski-opensource added this to the 1.0.0-alpha.29 milestone Jul 18, 2017
@dmytroyarmak
Copy link
Contributor Author

@pkozlowski-opensource I found that there were already some test about handling different values in meridian, I've moved them to 'meridian' section and added missed cases

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.

None yet

2 participants