Git Product home page Git Product logo

swiftcron's Introduction

SwiftCron

codecov CocoaPod Version

A cron expression parser that can take a cron string and give you the next run date and time specified in the string.


Installation

CocoaPods

Podfile:

pod 'SwiftCron'

Carthage

Cartfile:

github "thecodedself/swiftcron" >= 0.4.5

Swift Package Manager

Package.swift:

.Package(url: "https://github.com/TheCodedSelf/SwiftCron.git", majorVersion: 0)

Requirements

  • iOS 9.0 or greater
  • Xcode 8.0 or greater
  • Swift 3 or greater

Usage

Create a Cron Expression

Creating a cron expression is easy. Just invoke the initializer with the fields you want.

// Midnight every 8th day of the month
let myCronExpression = CronExpression(minute: "0", hour: "0", day: "8")
// Executes May 9th, 2024 at 11:30am
let anotherExpression = CronExpression(minute: "30", hour: "11", day: "9", month: "5", year: "2024") 
// Every tuesday at 6:00pm
let everyTuesday = CronExpression(minute: "0", hour: "18", weekday: "2")

Manually create an expression

If you'd like to manually write the expression yourself, The cron format is as follows:

* * * * * *
(Minute) (Hour) (Day) (Month) (Weekday) (Year)

Initialize an instance of CronExpression with a string specifying the format.

// Every 11th May at midnight
let every11May = CronExpression(cronString: "0 0 11 5 * *")

Get the next run date

Once you have your CronExpression, you can get the next time the cron will run. Call the getNextRunDate(_:) method and pass in the date to begin the search on.

// Every Friday 13th at midday
let myCronExpression = CronExpression(minute: "0", hour: "12", day: "13", weekday: "5")

let dateToStartSearchOn = NSDate()
let nextRunDate = myCronExpression.getNextRunDate(dateToStartSearchOn)
Custom timezones

By default SwiftCron uses the system's current timezone to calculate the next run date. To override this behavior, just pass the timezone you want into CronExpression.getNextRunDateFromNow(adjustingForTimeZone:) or CronExpression.getNextRunDate(_:adjustingForTimeZone:) methods.

// Every 12th September at 10 AM
let cronExpression = CronExpression(cronString: "0 10 12 9 * *")

// Calculate the next run date as if the cron expression was created in the
// America/Los_Angeles timezone
let losAngeles = TimeZone(identifier: "America/Los_Angeles")!
let nextRun = cronExpression.getNextRunDateFromNow(adjustingForTimeZone: losAngeles)

Contributing

  • Pull requests for bug fixes and new features are most welcome.
  • Pull requests will only be merged once the Travis CI build passes.

swiftcron's People

Contributors

angrymango avatar ducky-hong avatar dynamicy avatar objectivecharm avatar thecodedself avatar wilsonator5000 avatar zeeshankhan 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

Watchers

 avatar  avatar

swiftcron's Issues

Can a cron job be cancelled ?

Hi - I have tried another Swift Cron SPM and it worked mostly ok, but you could not cancel a schedule.

That SPM created a cron interval correctly, and it started a task, using

queue.asyncAfter(deadline: .now() + getInterval()) { () -> () in

But, this cannot be cancelled. Does your SPM allow for cancelling a started task ?

Can't import from Swift 5.3.2 / Xcode 12

Using Xcode 12 with Swift 5.3.2, I am unable to import this package.

package at 'https://github.com/TheCodedSelf/SwiftCron.git' @ d633279 is using Swift tools version 3.1.0 which is no longer supported; consider using '// swift-tools-version:5.3' to specify the current tools version

image

Steps to reproduce

  1. Create a new Swift project in Xcode 12
  2. File > Swift Packages > Add Package Dependency...
  3. Paste URL https://github.com/TheCodedSelf/SwiftCron.git
  4. Branch = master

Carthage availability

It would be good if this solid library can be used using carthage dependency manager.

*** Fetching SwiftCron
*** Checking out SwiftCron at "0.4.4"
*** xcodebuild output can be found in /var/folders/_f/xrm49x_j4fj7dmnnr1zrt3s40000gp/T/carthage-xcodebuild.M1de3y.log
*** Skipped building SwiftCron due to the error:
Dependency "SwiftCron" has no shared framework schemes

Add perfomBlockWithCron method

  • Schedules a block to run on the cron expression's next run date
  • Add a CronStorage class that will persist all cron blocks that the user can reload when the app starts up

Utilize CronField enum more

  • All CronField enum cases should take a string of the value
  • CronField should have methods to check if the field is satisfied by a date. e.g. FieldInterface.isSatisfiedBy
  • In getNextRunDate, when allPartsInExpression is instantiated, it is instantiated with the values from the CronRepresentation and then in the loop, look at the parts and call the enum isSatisfiedBy methods
  • The isSatisfiedBy methods of FieldCheckerInterface should be static and called by the enum
  • Should probably do the same with increment method of FieldCheckerInterface

getNextRunDate is always returning nil

Can you tell me what is wrong with this usage? you can try any of the below cronStrings.
And why it is always returning nil

//"30 9 ? * MON,WED,FRI *" //"30 15 ? * TUE,THU *" //"0 10 ? * MON-FRI *" //"30 18 ? * MON-FRI *" //"0 20 ? * * *" let cronString = "0 8 ? * * *" var calendar = Calendar.current calendar.timeZone = NSTimeZone.local let startOfDate = calendar.startOfDay(for: date) let cronExpression = CronExpression(cronString: cronString) if let nextRunDate = cronExpression?.getNextRunDate(startOfDate) { // should go the code to run on "nextRunDate" }

Better date matching

Don't use the brute force approach where possible. If the current run date doesn't match, use NSCalendar.nextDateAfterDate to get the date that will match the current field.

CronFieldTranslatable interface

Create a CronFieldTranslatable interface that specifies an object can be represented as a cron field in a cron string. Rather than have the CronExpression initializer take strings, take CronFieldTranslatables instead.

Extend Int and String to conform, so you can initialize a CronExpression with either Ints or Strings.

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.