Skip to content

Commit

Permalink
Fix Month calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianValente committed Nov 21, 2018
1 parent 4e5b191 commit 359a811
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
10 changes: 6 additions & 4 deletions Time Progress/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func getMonthProgressPercentage() -> Int {
let date = Date()

let start = date.startOfMonth().timeIntervalSince1970
let end = date.endOfMonth().timeIntervalSince1970
let end = date.endOfMonth().dayAfter.timeIntervalSince1970
let now = date.timeIntervalSince1970
print(start, end, now)


let percentage = ((end - now) * 100) / (end - start)
let monthSeconds = end - start
let calcNow = now - start

let percentage = (calcNow * 100) / monthSeconds

return Int(percentage)
}
Expand Down
27 changes: 27 additions & 0 deletions Time Progress/DateExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,30 @@ extension Date {
return Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth())!
}
}


// https://stackoverflow.com/questions/44009804/swift-3-how-to-get-date-for-tomorrow-and-yesterday-take-care-special-case-ne

extension Date {
static var yesterday: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: Date().noon)!
}
static var tomorrow: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: Date().noon)!
}
var dayBefore: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: noon)!
}
var dayAfter: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
}
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
var month: Int {
return Calendar.current.component(.month, from: self)
}
var isLastDayOfMonth: Bool {
return dayAfter.month != month
}
}
4 changes: 2 additions & 2 deletions Time Progress/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.3</string>
<string>0.4</string>
<key>CFBundleVersion</key>
<string>3</string>
<string>4</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.productivity</string>
<key>LSMinimumSystemVersion</key>
Expand Down

0 comments on commit 359a811

Please sign in to comment.