Skip to content

Commit 359a811

Browse files
committed
Fix Month calculation
1 parent 4e5b191 commit 359a811

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

Time Progress/AppDelegate.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
253253

254254
func getMonthProgressPercentage() -> Int {
255255
let date = Date()
256+
256257
let start = date.startOfMonth().timeIntervalSince1970
257-
let end = date.endOfMonth().timeIntervalSince1970
258+
let end = date.endOfMonth().dayAfter.timeIntervalSince1970
258259
let now = date.timeIntervalSince1970
259-
print(start, end, now)
260-
261260

262-
let percentage = ((end - now) * 100) / (end - start)
261+
let monthSeconds = end - start
262+
let calcNow = now - start
263+
264+
let percentage = (calcNow * 100) / monthSeconds
263265

264266
return Int(percentage)
265267
}

Time Progress/DateExtensions.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,30 @@ extension Date {
100100
return Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth())!
101101
}
102102
}
103+
104+
105+
// https://stackoverflow.com/questions/44009804/swift-3-how-to-get-date-for-tomorrow-and-yesterday-take-care-special-case-ne
106+
107+
extension Date {
108+
static var yesterday: Date {
109+
return Calendar.current.date(byAdding: .day, value: -1, to: Date().noon)!
110+
}
111+
static var tomorrow: Date {
112+
return Calendar.current.date(byAdding: .day, value: 1, to: Date().noon)!
113+
}
114+
var dayBefore: Date {
115+
return Calendar.current.date(byAdding: .day, value: -1, to: noon)!
116+
}
117+
var dayAfter: Date {
118+
return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
119+
}
120+
var noon: Date {
121+
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
122+
}
123+
var month: Int {
124+
return Calendar.current.component(.month, from: self)
125+
}
126+
var isLastDayOfMonth: Bool {
127+
return dayAfter.month != month
128+
}
129+
}

Time Progress/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>0.3</string>
20+
<string>0.4</string>
2121
<key>CFBundleVersion</key>
22-
<string>3</string>
22+
<string>4</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.productivity</string>
2525
<key>LSMinimumSystemVersion</key>

0 commit comments

Comments
 (0)