Skip to content

Instantly share code, notes, and snippets.

@SlappyAUS
Created November 11, 2020 10:56
Show Gist options
  • Select an option

  • Save SlappyAUS/6f53eaea69a299ac3e6f345505bcd0f5 to your computer and use it in GitHub Desktop.

Select an option

Save SlappyAUS/6f53eaea69a299ac3e6f345505bcd0f5 to your computer and use it in GitHub Desktop.
Date Aggregation #swift #dates
import Cocoa
struct entry {
let date: Date
let vol: Measurement<UnitVolume>
}
let entries = [
entry(date: Date(), vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date(), vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date(), vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date(), vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date(), vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 2*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 2*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 2*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 2*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 3*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 3*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 4*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 4*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 5*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 5*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 6*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 6*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 7*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 7*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 8*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
entry(date: Date() - 8*25*60*60, vol: Measurement(value: 100, unit: UnitVolume.milliliters)),
]
extension Date {
public func removeTimeStamp() -> Date {
guard let date = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: self)) else {
fatalError("Failed to strip time from Date object")
}
return date
}
}
let now = Date()
print("Now: \(now)")
let endOfToday = Calendar.current.date(byAdding: .day, value: 1, to: Date())?.removeTimeStamp().addingTimeInterval(TimeInterval(-1))
print("End of today: \(endOfToday!)")
let sevenDays = entries.filter{ $0.date > Calendar.current.date(byAdding: .day, value: -7, to: endOfToday!)!}
var history = [Date: Measurement<UnitVolume>]()
for entry in sevenDays {
let date = entry.date.removeTimeStamp()
if let _ = history[date] {
history[date]! = history[date]! + entry.vol
} else {
history[date] = entry.vol
}
}
for date in Array(history.keys.sorted().reversed()) {
print("\(date): \(history[date]!)")
}
print(Locale.current)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment