Skip to content

Commit

Permalink
Better calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianValente committed Nov 22, 2018
1 parent 5d8cba6 commit fefe812
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
30 changes: 14 additions & 16 deletions Time Progress/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func getMonthProgressPercentage() -> Int {
let date = Date()

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

let monthSeconds = end - start
Expand All @@ -268,29 +268,27 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func getWeekProgressPercentage() -> Int {
let date = Date()
let monday = date.previous(.monday).timeIntervalSince1970
let sunday = date.next(.sunday).timeIntervalSince1970
let thisWeek = date.previous(.monday).midnight.timeIntervalSince1970
let nextWeek = date.next(.monday).midnight.timeIntervalSince1970
let now = date.timeIntervalSince1970

let percentage = ((now - monday) * 100) / (sunday - now)
let weekSeconds = nextWeek - thisWeek
let calcNow = now - thisWeek

let percentage = (calcNow * 100) / weekSeconds

return Int(percentage)
}

func getDayProgressPercentage() -> Int {
let date = Date()
let calendar = Calendar.current
let year = calendar.component(Calendar.Component.year, from: date)
let month = calendar.component(Calendar.Component.month, from: date)
let day = calendar.component(Calendar.Component.day, from: date)
let today = Date().midnight.timeIntervalSince1970
let tomorrow = Date.tomorrow.timeIntervalSince1970
let now = Date().timeIntervalSince1970

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let today = formatter.date(from: "\(year)-\(month)-\(day) 00:00:00")!.timeIntervalSince1970
let tomorrow = today + 86400
let now = date.timeIntervalSince1970
let daySeconds = tomorrow - today
let calcNow = now - today

let percentage = ((now - today) * 100) / (tomorrow - today)
let percentage = (calcNow * 100) / daySeconds

return Int(percentage)
}
Expand Down
11 changes: 7 additions & 4 deletions Time Progress/DateExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,23 @@ extension Date {

extension Date {
static var yesterday: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: Date().noon)!
return Calendar.current.date(byAdding: .day, value: -1, to: Date().midnight)!
}
static var tomorrow: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: Date().noon)!
return Calendar.current.date(byAdding: .day, value: 1, to: Date().midnight)!
}
var dayBefore: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: noon)!
return Calendar.current.date(byAdding: .day, value: -1, to: midnight)!
}
var dayAfter: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
return Calendar.current.date(byAdding: .day, value: 1, to: midnight)!
}
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
var midnight: Date {
return Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: self)!
}
var month: Int {
return Calendar.current.component(.month, from: self)
}
Expand Down

0 comments on commit fefe812

Please sign in to comment.