Skip to content

Commit

Permalink
Fix Crash With Japenese Calendar
Browse files Browse the repository at this point in the history
The Japenese calendar repeats years so
splitting the date by components and then
recreating it from the components will not work.
Instead use the new interval start and end.
  • Loading branch information
Kamin, Grant authored and patchthecode committed Apr 10, 2019
1 parent 62501f9 commit c06f0ee
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Sources/GlobalFunctionsAndExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,28 @@ extension Calendar {


func startOfMonth(for date: Date) -> Date? {
guard let comp = dateFormatterComponents(from: date) else { return nil }
return Calendar.formatter.date(from: "\(comp.year) \(comp.month) 01")
if #available(iOS 10.0, *) {
guard let interval = self.dateInterval(of: .month, for: date) else { return nil }
return interval.start
} else {
guard let comp = dateFormatterComponents(from: date) else { return nil }
return Calendar.formatter.date(from: "\(comp.year) \(comp.month) 01")
}
}

func endOfMonth(for date: Date) -> Date? {
guard
let comp = dateFormatterComponents(from: date),
let day = self.range(of: .day, in: .month, for: date)?.count,
let retVal = Calendar.formatter.date(from: "\(comp.year) \(comp.month) \(day)") else {
return nil
if #available(iOS 10.0, *) {
guard let interval = self.dateInterval(of: .month, for: date) else { return nil }
return interval.end
} else {
guard
let comp = dateFormatterComponents(from: date),
let day = self.range(of: .day, in: .month, for: date)?.count,
let retVal = Calendar.formatter.date(from: "\(comp.year) \(comp.month) \(day)") else {
return nil
}
return retVal
}
return retVal
}

private func dateFormatterComponents(from date: Date) -> (month: Int, year: Int)? {
Expand Down

4 comments on commit c06f0ee

@zl00
Copy link

@zl00 zl00 commented on c06f0ee Apr 22, 2020

Choose a reason for hiding this comment

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

@patchthecode

I am a macOS developer. Recently we found a crash, which leads our application to crash occasionlly when calling API of Calendar.

The crash log as follow:
Calendar.dateInterval(of:for:) (in libswiftFoundation.dylib) + 512

In our codes:

extension Date {
    var isToday: Bool {
        return Calendar.current.isDateInToday(self)
    }
    
    var isYesterday: Bool {
        return Calendar.current.isDateInYesterday(self)
    }
}

We guess maybe isDateInToday/isDateInYesterday leads to crash.

Can you give us some suggestion?

@patchthecode
Copy link
Owner

Choose a reason for hiding this comment

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

Since you are posting this under this merge request,
Are you able to tell me which iOS version crashed?

@zl00
Copy link

@zl00 zl00 commented on c06f0ee Apr 23, 2020

Choose a reason for hiding this comment

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

@patchthecode

Sorry, I didn't explain my problem clearly.
I encountered a strange crash when calling calendar API in my own application.
So, I found your commit. I think maybe Japanese calendar caused our application crash.
Can you share the reproduce steps of this issue to us?

@patchthecode
Copy link
Owner

Choose a reason for hiding this comment

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

just to clarify, you want the steps that happened to reproduce the Japanese calendar crash?

If this is what you want then i do not know the steps. The crash was experienced by another user.
And the fix suggester by a developer other than me.

Please sign in to comment.