Code Monkey home page Code Monkey logo

rruleswift's Introduction

RRuleSwift

Swift rrule library for working with recurrence rules of calendar dates.

Example

RRuleSwift is based on rrule.js.

How To Get Started

Carthage

Specify "RRuleSwift" in your Cartfile:

github "teambition/RRuleSwift"

Usage

Initialization

var recurrenceRule = RecurrenceRule(frequency: .daily)
recurrenceRule.calendar = ...
recurrenceRule.frequency = ...
recurrenceRule.interval = ...
recurrenceRule.firstDayOfWeek = ...
recurrenceRule.startDate = ...
recurrenceRule.recurrenceEnd = ...
recurrenceRule.bysetpos = ...
recurrenceRule.byyearday = ...
recurrenceRule.bymonth = ...
recurrenceRule.byweekno = ...
recurrenceRule.bymonthday = ...
recurrenceRule.byweekday = ...
recurrenceRule.byhour = ...
recurrenceRule.byminute = ...
recurrenceRule.bysecond = ...
recurrenceRule.rdate = ...
recurrenceRule.exdate = ...

Rule form string

let ruleString = "RRULE:FREQ=MONTHLY;DTSTART=20160404T021000Z;COUNT=5;INTERVAL=2;WKST=MO;BYDAY=MO,TU"
let rule = RecurrenceRule(rruleString: ruleString)

String form rule

let ruleString = rule.toRRuleString()
print(ruleString)
// RRULE:FREQ=MONTHLY;DTSTART=20160404T021000Z;COUNT=5;INTERVAL=2;WKST=MO;BYDAY=MO,TU

Inclusion date and Exclusion date

let rdateString = "RDATE:20180706T160000Z,20210706T160000Z"
if let inclusionDate = InclusionDate(rdateString: rdateString) {
    print(inclusionDate.toRDateString())
    // RDATE:20180706T160000Z,20210706T160000Z

    print(inclusionDate.dates)
    /*
    2018-07-07 00:00:00 Sun
    2021-07-07 00:00:00 Sun
    */
}

let exdateString = "EXDATE:20181231T160000Z,20201231T160000Z"
if let exclusionDate = ExclusionDate(exdateString: exdateString, granularity: .year) {
    print(exclusionDate.toExDateString())
    // EXDATE:20181231T160000Z,20201231T160000Z

    print(exclusionDate.dates)
    /*
    2019-01-01 00:00:00 Tue
    2021-01-01 00:00:00 Fri
    */
}

Occurrence iterator

let ruleString = "RRULE:FREQ=YEARLY;COUNT=11;WKST=MO"
if let rule = RecurrenceRule(rruleString: ruleString) {
    var rule = rule
    rule.rdate = inclusionDate // RDATE:20180706T160000Z,20210706T160000Z
    rule.exdate = exclusionDate // EXDATE:20181231T160000Z,20201231T160000Z
    let allDates = rule.allOccurrences()
    print(allDates)
    /*
    2016-04-14 14:22:30 Thu
    2017-04-14 14:22:30 Fri
    2018-04-14 14:22:30 Sat
    2018-07-07 00:00:00 Sat
    2020-04-14 14:22:30 Tue
    2022-04-14 14:22:30 Thu
    2023-04-14 14:22:30 Fri
    2024-04-14 14:22:30 Sun
    2025-04-14 14:22:30 Mon
    2026-04-14 14:22:30 Tue
    */

    let date = dateFormatter.date(from: "2018-01-01 00:00:00 Sun")!
    let otherDate = dateFormatter.date(from: "2024-01-01 00:00:00 Mon")!
    let betweenDates = rule.occurrences(between: date, and: otherDate)
    print(betweenDates)
    /*
    2018-04-14 14:22:30 Sat
    2018-07-07 00:00:00 Sat
    2020-04-14 14:22:30 Tue
    2022-04-14 14:22:30 Thu
    2023-04-14 14:22:30 Fri
    */
}

Minimum Requirement

iOS 8.0

Release Notes

License

RRuleSwift is released under the MIT license. See LICENSE for details.

More Info

Have a question? Please open an issue!

rruleswift's People

Contributors

hongxinhope avatar stormxx avatar suricforever avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rruleswift's Issues

Iterator.rruleContext?.evaluateScript hangs with certain device date/time regional/locale settings

During unrelated testing of formatting time with the 12 hour / 24 hour time switch in iOS' Date & Time settings I noticed my app was hanging indefinitely, and I narrowed it down to this line in Iterators.swift:

guard let betweenOccurrences = Iterator.rruleContext?.evaluateScript("rule.between(new Date('\(beginDateJSON)'), new Date('\(untilDateJSON)'))").toArray() as? [Date] else {
    return []
}

Typically I set my device's region to United States, but I changed it to United Kingdom. Even then it was fine, but if I turned the use 24 hour time switch to off (in the UK it defaults to on), the app has this problem with hanging.

Regardless of how big the gap between the two dates are, it seems to always hang. Memory doesn't build up, no error… I haven't really left it sitting to see if it ever escapes.

I think it can be easily resolved by setting a locale on the ISO8601DateFormatter in RRule.swift, e.g.:

dateFormatter.locale = Locale(identifier: "en_US_POSIX")

It already sets the time zone to 0 seconds away from GMT so it seems it might as well prevent the locale of the device affecting the output.

I'm not clear on why it's doing this, but it might be because date formats are overridden by this "use 24 hour time" switch such according to whatever the default is for that region. So in the US the default is 12 hour time, so if you have a date format like "hh:mm a" then it will replace it with "HH:mm" if the switch is on. If you have a date format like "HH:mm" it seems to keep it as 24 hour time if your region's default is 12 hour time. But if your region defaults to 24 hour time, it looks like it might be the other way around. So if your format is "hh:mm a" it will not replace it, but if it's "HH:mm" then it will if the switch is turned off. I haven't tested this theory fully, but it seems to make sense.

Performance Problems

Using this library to generate occurrences has generated significant performance problems for my app. It seems to get slowed down in the JS files and occasionally even crashes there.

Endless Recurrence Count

The issue is that if i had a repeating from 2017 daily and no endRule, and the window from someDate 2019 and otherDate in 2019 the endlessRecurrenceCount come in place that has a 500 value and doesn't display any values because it counts also the values that i don't need.

right now i changed the endlessRecurrenceCount but is there a better/nice way to do it right ?

Making DTSTART an optional while creating RRule

Google Calendar APIs don't accept DTSTART in RRule. Can we make it optional in the function
toRRuleString of RecurrenceRule. If you will agree to merge it into this repo, I can submit a pull request.

How to install using cocoapod.

How can install using Cocoapod.
I am unable to run by installing Manually dragging framework.
Getting error like 'No such Module RRuleSwift'.
Thanks in advance.

TimeZone difference issue

I the app has different timeZone from the device, the recurrence dates are incorrect, because javaScript uses the device Settings.

eg: device TimeZone: LosAngeles, app timeZone: Athena and recurrence every Friday i got dates on Thursday

carthage update doesn't work

I am getting the following error when trying to update rruleswift.

Parse error: expected submodule commit SHA in output of task (ls-tree -z HEAD RRuleSwift) but encountered:`

Does not parse rule correctly

I don't think this library parses RRULE:FREQ=YEARLY;INTERVAL=1;COUNT=6;BYMONTH=2;BYDAY=3MO

(president's day)
correctly. I tried it, and it gives me back
RRULE:FREQ=YEARLY;INTERVAL=1;WKST=MO;DTSTART=20190103T054718Z;COUNT=6;BYMONTH=2

which is incorrect. I think the issue is the BYDAY not parsing, but that's just a guess.

Example project won't run

RRuleSwiftExample throws an error when attempting to run. The error is: error: /Users/hongxin/Library/Developer/Xcode/DerivedData/RRuleSwift-bsfdvctfhfasceecxzjeffpridns/Build/Products/Debug-iphonesimulator/RRuleSwift.framework: No such file or directory

The RRuleSwift.framework is red in the project settings when checked out:

screen shot 2016-05-01 at 9 48 59 pm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.