diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..d1d5deb --- /dev/null +++ b/Package.swift @@ -0,0 +1,29 @@ +// swift-tools-version:5.3 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "RRuleSwift", + platforms: [ + .iOS(.v10), + .macOS(.v10_12), + ], + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "RRuleSwift", + targets: ["RRuleSwift-iOS"]), + ], + dependencies: [ + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "RRuleSwift-iOS", + dependencies: [], + path: "Sources" + ), + ] +) diff --git a/RRuleSwiftExample/RRuleExampleViewController.swift b/RRuleSwiftExample/RRuleExampleViewController.swift index 7a26e74..feaca31 100644 --- a/RRuleSwiftExample/RRuleExampleViewController.swift +++ b/RRuleSwiftExample/RRuleExampleViewController.swift @@ -72,7 +72,60 @@ class RRuleExampleViewController: UIViewController { textView.delegate = self navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Reset", style: .plain, target: self, action: #selector(resetButtonTapped(_:))) } + + fileprivate func testRRuleIteratorMultipleThreads() { + DispatchQueue.global(qos: .background).async { + for n in 1...100 + { + self.testRRuleIterator(n) + } + } + + DispatchQueue.global(qos: .background).async { + for n in 1...100 + { + self.testRRuleIterator(-n) + } + } + } + + fileprivate func testRRuleIterator(_ n : Int) { + let ruleFormat = "RRULE:FREQ=WEEKLY;DTSTART=%@%@01T014500Z;INTERVAL=1;UNTIL=%@%@01T014500Z" + let calendar = NSCalendar.current + var startDateComponents = DateComponents() + startDateComponents.month = n; + let startDate = calendar.date(byAdding: startDateComponents, to: Date()) + startDateComponents = calendar.dateComponents([.year, .month], from: startDate!) + + var endDateComponents = DateComponents() + endDateComponents.month = 6 + let endDate = calendar.date(byAdding: endDateComponents, to: startDate!) + endDateComponents = calendar.dateComponents([.year, .month], from: endDate!) + var startDateMonth = String(startDateComponents.month!) + if (startDateComponents.month! < 10) + { + startDateMonth = "0".appending(startDateMonth) + } + var endDateMonth = String(endDateComponents.month!) + if (endDateComponents.month! < 10) + { + endDateMonth = "0".appending(endDateMonth) + } + + let rruleString = String(format: ruleFormat, String(startDateComponents.year!), startDateMonth, String(endDateComponents.year!), endDateMonth ) + + print ("rruleString: ", rruleString) + var rule = RecurrenceRule(rruleString: rruleString)! + + let occurrences = rule.allOccurrences() + print ("occurrences for rrule: ", rruleString, ", count:", occurrences.count, ", first:", occurrences.first) + if !calendar.isDate(startDate!, equalTo: occurrences.first!, toGranularity: .day) + { + print("dates mismatch, got: ", occurrences.first, ", expected: ", startDate) + } + } + fileprivate func testRRuleIterator() { let dateFormatter: DateFormatter = { let dateFormatter = DateFormatter() diff --git a/Sources/Iterators.swift b/Sources/Iterators.swift index 2216377..96993ee 100644 --- a/Sources/Iterators.swift +++ b/Sources/Iterators.swift @@ -31,8 +31,7 @@ public extension RecurrenceRule { } let ruleJSONString = toJSONString(endless: endlessRecurrenceCount) - let _ = Iterator.rruleContext?.evaluateScript("var rule = new RRule({ \(ruleJSONString) })") - guard let allOccurrences = Iterator.rruleContext?.evaluateScript("rule.all()").toArray() as? [Date] else { + guard let allOccurrences = Iterator.rruleContext?.evaluateScript("new RRule({ \(ruleJSONString) }).all()").toArray() as? [Date] else { return [] } @@ -67,8 +66,7 @@ public extension RecurrenceRule { let untilDateJSON = RRule.ISO8601DateFormatter.string(from: untilDate) let ruleJSONString = toJSONString(endless: endlessRecurrenceCount) - let _ = Iterator.rruleContext?.evaluateScript("var rule = new RRule({ \(ruleJSONString) })") - guard let betweenOccurrences = Iterator.rruleContext?.evaluateScript("rule.between(new Date('\(beginDateJSON)'), new Date('\(untilDateJSON)'))").toArray() as? [Date] else { + guard let betweenOccurrences = Iterator.rruleContext?.evaluateScript("new RRule({ \(ruleJSONString) }).between(new Date('\(beginDateJSON)'), new Date('\(untilDateJSON)'))").toArray() as? [Date] else { return [] }