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

SR-7011: Fatal Error when Using Calendar.current.dateComponents(_:from:to:) #1658

Merged
merged 2 commits into from Aug 15, 2018

Conversation

spevans
Copy link
Collaborator

@spevans spevans commented Aug 10, 2018

  • _CFCalendarGetComponentDifferenceV() doesn't support a component
    character of 'l' (leap year) as it make no sense for the difference
    between two dates.

  • Dont add 'l' to the component string or parse the result into the
    .isLeapMonth property when called from
    NSCalendar.components(_:from:to:options)

…m:to:)

- _CFCalendarGetComponentDifferenceV() doesn't support a component
  character of 'l' (leap year) as it make no sense for the difference
  between two dates.

- Dont add 'l' to the component string or parse the result into the
  .isLeapMonth property when called from
  NSCalendar.components(_:from:to:options)
@spevans
Copy link
Collaborator Author

spevans commented Aug 10, 2018

@swift-ci please test

@phausler
Copy link
Member

@itaiferber probably would be the better one to review this.


// 1971-06-21
let date2 = Date(timeIntervalSince1970: 46310400)
let difference = Calendar.current.dateComponents([.month, .year, .day], from: date1, to: date2)
Copy link
Member

Choose a reason for hiding this comment

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

for leap months you probably need the Chinese calendar iirc.

Copy link
Contributor

Choose a reason for hiding this comment

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

On Darwin we actually almost always add "is leap month" to DateComponents in output, if that's what you're referring to. (Leap months are also applicable in the Hebrew calendar, and otherwise.)

Copy link
Contributor

Choose a reason for hiding this comment

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

(But yes, in this case, on Darwin we don't add "is leap month" here.)

Copy link
Contributor

@itaiferber itaiferber left a comment

Choose a reason for hiding this comment

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

Looks good otherwise! AFAICT, this will match the "is leap month" additions we make on Darwin (i.e. in components(from date: Date))

@@ -980,8 +985,13 @@ Boolean _CFCalendarGetComponentDifferenceV(CFCalendarRef calendar, CFAbsoluteTim
divide = alwaysDivide;
}
}
if (count < 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be moved further down (right after the --) so we don't go through the loop an additional time and do work just to discover we can't get the difference?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It should probably go at the top of the while loop in case count starts at 0. I will fix that

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yes — good catch

@itaiferber
Copy link
Contributor

I know the unit tests were non-existent before, but it'd be nice to expand them further if we can before we merge. A variety of dates (future and past relative to one another) and a variety of components would be great!

- _CFCalendarGetComponentDifferenceV: Add validation of datetime
  format character and bounds check multiple_table array lookup.

- Allow -1 as a component value.

- Reorder datetime fields from largest to smallest when creating
  datetime format and constructing components from response.

- Add tests validated against Darwin's Foundation.
@spevans
Copy link
Collaborator Author

spevans commented Aug 14, 2018

@swift-ci please test

@spevans
Copy link
Collaborator Author

spevans commented Aug 14, 2018

@itaiferber Ive added some extra tests and validated them against Darwin. There are probably some more cases to catch but this should cover a reasonable amount and does add some extra validation into the CF function.

Copy link
Contributor

@itaiferber itaiferber left a comment

Choose a reason for hiding this comment

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

Changes here look great! Thanks for taking the time to be so thorough here! :)

@spevans
Copy link
Collaborator Author

spevans commented Aug 14, 2018

@swift-ci please test and merge

2 similar comments
@millenomi
Copy link
Contributor

@swift-ci please test and merge

@spevans
Copy link
Collaborator Author

spevans commented Aug 14, 2018

@swift-ci please test and merge

@swift-ci swift-ci merged commit 4c95608 into apple:master Aug 15, 2018
spevans added a commit to spevans/swift-corelibs-foundation that referenced this pull request Apr 8, 2019
- Nanoseconds were originally disable in apple#1658 to fix SR-7011,
  however the CF/NSCalendar rewrite in apple#1755 fixed them to
  work correctly so they can now be re-enabled.
spevans added a commit to spevans/swift-corelibs-foundation that referenced this pull request Apr 16, 2019
- Nanoseconds were originally disable in apple#1658 to fix SR-7011,
  however the CF/NSCalendar rewrite in apple#1755 fixed them to
  work correctly so they can now be re-enabled.

(cherry picked from commit d8f09b6)
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.

None yet

5 participants