Skip to content

Commit

Permalink
Add private API for knowing if time should be used (#50)
Browse files Browse the repository at this point in the history
When parsing the natural language text like 'today', you get back a full
NSDate with a time set at 12:00. Previously to understand this case I
would remove the time if it was noon, which also broke you manually
specifying '12:00'. It turns out there's a private API for this instead,
so this now safely calls that, warning if it breaks. This at least works
on macOS 13 but based on class dumps I think it has been around since at
least 2018.

Fixes #33
  • Loading branch information
keith authored Dec 15, 2022
1 parent 348f5da commit 9254fe8
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Sources/RemindersLibrary/NaturalLanguage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ private func components(from string: String) -> DateComponents? {
return nil
}

var includeTime = true
if match.responds(to: NSSelectorFromString("timeIsSignificant")) {
includeTime = match.value(forKey: "timeIsSignificant") as? Bool ?? true
} else {
print("warning: timeIsSignificant is not available, please report this to keith/reminders-cli")
}

let timeZone = match.timeZone ?? .current
let parsedComponents = calendar.dateComponents(in: timeZone, from: date)
if let noon = calendar.date(bySettingHour: 12, minute: 0, second: 0, of: date),
calendar.compare(date, to: noon, toGranularity: .minute) == .orderedSame
{
if includeTime {
return parsedComponents
} else {
return calendar.dateComponents(calendarComponents(except: timeComponents), from: date)
}

return parsedComponents
}

extension DateComponents: ExpressibleByArgument {
Expand Down

0 comments on commit 9254fe8

Please sign in to comment.