quire-io / swiftychrono Goto Github PK
View Code? Open in Web Editor NEWA natural language date parser in Swift (ported from chrono.js)
License: MIT License
A natural language date parser in Swift (ported from chrono.js)
License: MIT License
If you do:
let chrono = Chrono() let date = chrono.parseDate(text: "")
you get a crash
Example 1
input text: 2014-04-18 1pm ~ 2014-04-20 3pm
the end date it parsed is 2014-04-18
Example 2
input text: 2/25 ~ 2/28
the end date is nil
Would be nice to support this as I'm unable to use it in an Obj-C project.
Cool project. Thanks for your work on this. Dates entered like "June 6 2010" work great. "6 June 2010" however crashes.
as title
So I realized that when parsing and using a refDate such as now Date()
, the parsed result is in UTC timezone. So if i say 10pm today, it would parsed to 10pm today in UTC/GMT zone.
I tried to compensate this by calculating the different in my current time zone and UTC/GMT by using NSTimeZone.system.secondsFromGMT(for: currentDate)
, but when using options like forward date only, it creates problems. For example, its 4pm GMT, and 8AM PT, If I were in PT and say 10 AM today, it would mean in 2 hours, however since its parsed in UTC/GMT, it would be in 18 hours
*******sorry my mistake this issue is NON-EXISTENT ************
Current implementation will skip the third element accidentally if the both first and second one are merged. that's a bug.
reproduce case:
The morning of Monday, April 6 to Friday, April 10, the store will be closed.
how to change the time zone?
right now, I'm passing refdate as my local time
so in some scenarios it is showing right and some it shows according to different timezone.
import SwiftyChrono
extension Date {
// Convert local time to UTC (or GMT)
func toGlobalTime() -> Date {
let timezone = TimeZone.current
let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
// Convert UTC (or GMT) to local time
func toLocalTime() -> Date {
let timezone = TimeZone.current
let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
}
let chrono = Chrono()
chrono.parse(text: "Bring a book tomorrow")
let refDate = Date().toLocalTime()
let val = chrono.parse(text: "Bring a book tomorrow", refDate: refDate)
let date = chrono.parseDate(text: "tomorrow ",refDate: refDate)
let date1 = chrono.parseDate(text: "at 5 PM",refDate: refDate)
let date2 = chrono.parseDate(text: "in 2 minutes",refDate: refDate)
print(date)
print(date1)
print(date2)
Output-
Optional(2019-12-28 06:30:00 +0000) //different time zone
Optional(2019-12-27 11:30:00 +0000) //different time zone
Optional(2019-12-27 15:49:03 +0000) //right
I'm having an issue adding the pod to my project. I get the error "Unable to read the license file". Any suggestions?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.