Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neilsardesai/chrono-swift
A Swift wrapper for the chrono.js natural language date parser
https://github.com/neilsardesai/chrono-swift
Last synced: 3 months ago
JSON representation
A Swift wrapper for the chrono.js natural language date parser
- Host: GitHub
- URL: https://github.com/neilsardesai/chrono-swift
- Owner: neilsardesai
- License: mit
- Created: 2016-11-18T03:59:37.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-10-20T02:34:32.000Z (about 5 years ago)
- Last Synced: 2024-04-04T18:34:09.409Z (7 months ago)
- Language: Swift
- Homepage:
- Size: 4.25 MB
- Stars: 73
- Watchers: 4
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# chrono-swift
chrono-swift is a Swift wrapper for [Wanasit Tanakitrungruang’s excellent chrono.js natural language date parser](https://github.com/wanasit/chrono "chrono.js"). chrono-swift lets you use chrono.js for your iOS, macOS, or tvOS apps, all while working with native Swift and Foundation data types. No need to deal with any of JavaScript’s silliness.
chrono-swift, (referred to as “Chrono” from here onwards), extracts date information from natural language phrases like **“Remind me in 2 days”** or **“Meet me this Saturday from 3-4 PM”** and returns Swift `Date` or `DateInterval` values.
# Setup
Just add the “Chrono” folder located at the root of the repo to your Xcode project.
# Usage
Using Chrono is easy. First, create an instance of the `Chrono` singleton:
```swift
let chrono = Chrono.shared
```### Extract Date
To extract a date from a natural language phrase, simply pass in the phrase to `Chrono`’s `dateFrom(naturalLanguageString:)` method. Here, the reference date is assumed to be the current system date.
```swift
let date = chrono.dateFrom(naturalLanguageString: "Remind me in 2 days")
// If today is November 17th, 2016, `date` will be November 19th, 2016
```If you want to use a different date as the reference date, use `dateFrom(naturalLanguageString:referenceDate:)`
### Extract Date Interval
To extract a date interval from a natural language phrase, pass in the phrase to `Chrono`’s `dateIntervalFrom(naturalLanguageString:)` method. Again, the reference date is assumed to be the current system date here.
```swift
let dateInterval = chrono.dateIntervalFrom(naturalLanguageString: "Meet me this Saturday from 3-4 PM")
// If today is November 17th, 2016, `dateInterval` will be November 19th, 2016 3:00 PM - November 19th, 2016 4:00 PM
```If you want to use a different date as the reference date, use `dateIntervalFrom(naturalLanguageString:referenceDate:)`
### Detailed Parsed Results
You can also get more detailed parsed results using `Chrono`’s `parsedResultsFrom(naturalLanguageString:)` method.
```swift
let result = chrono.parsedResultsFrom(naturalLanguageString: "I have an appointment tomorrow from 10 to 11 AM", referenceDate: nil) // If referenceDate is nil, it is assumed to be the current system date.
print(results)
````result` will be a special struct of type `ChronoParsedResult`. This struct contains several member variables that describe the parsed result. In this case, the struct will look like this:
```
inputString: I have an appointment tomorrow from 10 to 11 AM
indexOfStartingCharacterOfTimePhrase: 22
timePhrase: tomorrow from 10 to 11 AM
ignoredText: I have an appointment
referenceDate: November 17, 2016 at 8:18:14 PM CST
startDate: November 18, 2016 at 10:00:00 AM CST
endDate: November 18, 2016 at 11:00:00 AM CST
dateInterval: November 18, 2016, 10:00:00 AM CST - 11:00:00 AM CST
```# Sample Project
Under the “Sample Apps” folder is a sample Xcode project with iOS, macOS, and tvOS targets.
![Chrono iOS](Screenshots/iOS.png "Chrono iOS")
![Chrono macOS](Screenshots/macOS.png "Chrono macOS")
![Chrono tvOS](Screenshots/tvOS.png "Chrono tvOS")