From 359a811b5a09ca335b5178546a2e8dd139b00140 Mon Sep 17 00:00:00 2001 From: Brian Valente Date: Wed, 21 Nov 2018 00:17:20 -0300 Subject: [PATCH] Fix Month calculation --- Time Progress/AppDelegate.swift | 10 ++++++---- Time Progress/DateExtensions.swift | 27 +++++++++++++++++++++++++++ Time Progress/Info.plist | 4 ++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Time Progress/AppDelegate.swift b/Time Progress/AppDelegate.swift index 09fdd26..0f232eb 100644 --- a/Time Progress/AppDelegate.swift +++ b/Time Progress/AppDelegate.swift @@ -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) } diff --git a/Time Progress/DateExtensions.swift b/Time Progress/DateExtensions.swift index 1d1874e..0998ca4 100644 --- a/Time Progress/DateExtensions.swift +++ b/Time Progress/DateExtensions.swift @@ -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 + } +} diff --git a/Time Progress/Info.plist b/Time Progress/Info.plist index 43f6bee..b530f79 100644 --- a/Time Progress/Info.plist +++ b/Time Progress/Info.plist @@ -17,9 +17,9 @@ CFBundlePackageType APPL CFBundleShortVersionString - 0.3 + 0.4 CFBundleVersion - 3 + 4 LSApplicationCategoryType public.app-category.productivity LSMinimumSystemVersion