Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[UWP] Prevent crash when using DateTime.MinValue for DatePicker #1056

Merged
merged 1 commit into from Sep 12, 2017

Conversation

jimmgarrido
Copy link
Contributor

@jimmgarrido jimmgarrido commented Jul 14, 2017

Description of Change

On UWP, if you try to set the DatePicker.MinimumDate to DateTime.MinValue when the UTC offset is positive an ArgumentOutOfRangeException is thrown because the time gets converted to UTC which makes it go out of range.

This fix catches the exception and specifies the date as UTC in order to prevent it from being converted.

Bugs Fixed

API Changes

None

Behavioral Changes

None

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Rebased on top of master at time of PR
  • Changes adhere to coding standard
  • Consolidate commits as makes sense

catch(ArgumentOutOfRangeException)
{
// This will be thrown if you try to set MinYear to DateTime.MinValue when the UTC offset is positive
// because the time is out range once it's converted. In that case, specify the Kind as UTC
Copy link
Member

Choose a reason for hiding this comment

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

time is out range

typo, might confuse future dev

// This will be thrown if you try to set MinYear to DateTime.MinValue when the UTC offset is positive
// because the time is out range once it's converted. In that case, specify the Kind as UTC
// so it doesn't get converted.
mindate = DateTime.SpecifyKind(mindate, DateTimeKind.Utc);
Copy link
Member

Choose a reason for hiding this comment

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

I'm sure there's a reason, but why don't we just always get the Date value and use that instead of adding a try catch and additional logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We actually do do that in UpdateMaximumDate() and that was the first thing I tried. But Date just returns another DateTime with the Time set to 0:00 so in this case the crash still happens.

I think what we should do is always specify the incoming value as Utc and avoid the extra object from getting the Date. This would eliminate the try/catch and also prevent similar cases where the original date might get converted to the day before (or day after for MaxDate).

@hartez hartez added the DO-NOT-MERGE-!!! 🛑 This is in progress and needs to be updated before it can be merged. label Jul 24, 2017
@@ -90,13 +90,25 @@ void UpdateDate(DateTime date)
void UpdateMaximumDate()
{
DateTime maxdate = Element.MaximumDate;
Control.MaxYear = new DateTimeOffset(maxdate.Date);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Date just returns another DateTime object with the same date so it's redundant.

{
Control.MinYear = new DateTimeOffset(mindate);
}
catch (ArgumentOutOfRangeException)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out a try/catch is really just the simplest fix because the exception should only be thrown in this one scenario.

@jimmgarrido
Copy link
Contributor Author

This should be okay to merge now. I didn't make any changes other than making the comment more clearer because this seems like an edge case so most people probably won't hit the logic inside the catch.

@rmarinho rmarinho merged commit 6ae407e into xamarin:master Sep 12, 2017
@samhouts samhouts added D15.5 and removed cla-already-signed DO-NOT-MERGE-!!! 🛑 This is in progress and needs to be updated before it can be merged. labels Oct 10, 2017
@samhouts samhouts added this to the 2.5.0 milestone May 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants