Giter Site home page Giter Site logo

swiftychrono's Introduction

SwiftyChrono

A natural language date parser in Swift, designed to extract date information from any given text.

When we were integrating Siri with our iOS app, we found that JavascriptCore runtime is running on resource-limited extensions and will crash due to memory limitation. So, we created a Swift version. Thanks to Wanasit Tanakitrungruang's chrono.js.

SwiftyChrono supports most date and time formats, such as:

  • Today, Tomorrow, Yesterday, Last Friday, etc
  • 17 August 2013 - 19 August 2013
  • This Friday from 13:00 - 16.00
  • 5 days ago
  • Sat Aug 17 2013 18:40:39 GMT+0900 (JST)
  • 2014-11-30T08:15:30-05:30

There are more available patterns. You can simply download it and try.

Status

This project is currently being used in the Quire iOS app.

Requirements

  • iOS 9.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+
  • Xcode 8
  • Swift 3.0
  • 64 bit device (didn't test on 32 bit device)

Install

CocoaPods

use_frameworks!

target 'MyApp' do
	pod 'SwiftyChrono'
end

Usage

Initialization

import SwiftyChrono
let chrono = Chrono()

Parse

chrono.parse(text: "Bring a book tomorrow")
// [
// 	SwiftyChrono.ParsedResult(ref: 2017-02-22 08:33:33 +0000,
// 	index: 13,
// 	text: "tomorrow",
// 	tags: [
// 		SwiftyChrono.TagUnit.enCasualDateParser: true
// 	],
// 	start: SwiftyChrono.ParsedComponents(
// 		knownValues: [
// 			SwiftyChrono.ComponentUnit.day: 23,
// 			SwiftyChrono.ComponentUnit.year: 2017,
// 			SwiftyChrono.ComponentUnit.month: 2],
// 		impliedValues: [
// 			SwiftyChrono.ComponentUnit.minute: 0,
// 			SwiftyChrono.ComponentUnit.second: 0,
// 			SwiftyChrono.ComponentUnit.millisecond: 0,
// 			SwiftyChrono.ComponentUnit.hour: 12
// 		]),
// 	end: nil,
// 	isMoveIndexMode: false)
// ]

// refDate (1485921600000) is 2017/2/1 12:00:00.0000
let refDate = Date(timeIntervalSince1970: 1485921600)
// you can add a reference date
chrono.parse(text: "Bring a book tomorrow", refDate: refDate)

Quick Date Parse

chrono.parseDate(text: "Bring a book tomorrow", refDate: refDate)
// "Feb 2, 2017, 12:00 PM"

Other Options

// options: .forwardDate - the match date is always later than refDate
chrono.parseDate(text: "Bring a book on December 1", refDate: refDate)
// "Dec 1, 2016, 12:00 PM"
chrono.parseDate(text: "Bring a book on December 1", refDate: refDate, opt: [.forwardDate: 1])
// "Dec 1, 2017, 12:00 PM"

// you can assignee which hour in 
// morning, afternoon, evening, noon
chrono.parseDate(text: "Bring a book tomorrow morning", refDate: refDate, opt: [.morning: 10])
// "Feb 2, 2017, 10:00 AM"

/// specify the preferred language will let the answer more acurate
chrono.parse(text: "you can do it tomorrow", refDate: refDate).map{ $0.text }
// ["do", "tomorrow"]
Chrono.preferredLanguage = .english
chrono.parse(text: "you can do it tomorrow", refDate: refDate).map{ $0.text }
// ["tomorrow"]


/// specify sixMinutesFixBefore1900 to true, if the date before 1900 is in your use case
Chrono.sixMinutesFixBefore1900 = true
chrono.parseDate(text: "you can do it 1970/1/1")


/// override defaut hour, minute, second, millisecond
// the default implied hour is 12 pm if the given text doesn't specify
Chrono.defaultImpliedHour = 1
Chrono.defaultImpliedMinute = 1
Chrono.defaultImpliedSecond = 1
Chrono.defaultImpliedMillisecond = 1
chrono.parseDate(text: "you can do it tomorrow", refDate: refDate)?.timeIntervalSince1970
// 1485968461.001, 2017/2/1 01:01:01.001

Demo Video

Everything Is AWESOME

swiftychrono's People

Contributors

jerrywell avatar harryworld avatar leotumwattana avatar tomyeh avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar

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.