Calendar+GlucoseStatsChart.swift 728 B

123456789101112131415161718192021
  1. import Foundation
  2. extension Calendar {
  3. /// Converts an hour (0-23) to a Date object representing that hour on the current day.
  4. /// This is used to properly position marks on the chart's time axis.
  5. ///
  6. /// - Parameter hour: Integer representing the hour of day (0-23)
  7. /// - Returns: Date object set to the specified hour on the current day
  8. ///
  9. /// Example:
  10. /// ```
  11. /// calendar.dateForChartHour(14) // Returns today's date at 2:00 PM
  12. /// calendar.dateForChartHour(0) // Returns today's date at 12:00 AM
  13. /// ```
  14. func dateForChartHour(_ hour: Int) -> Date {
  15. let today = startOfDay(for: Date())
  16. return date(byAdding: .hour, value: hour, to: today) ?? today
  17. }
  18. }