Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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"
),
]
)
53 changes: 53 additions & 0 deletions RRuleSwiftExample/RRuleExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions Sources/Iterators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}

Expand Down Expand Up @@ -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 []
}

Expand Down