Search code examples
swiftdatensdatenscalendarnstimeinterval

Test whether current time of day is between two TimeIntervals


I have 2 TimeIntervals, which just represent date-agnostic times of day (e.g. 8:00 AM and 5:00 PM). So 0 represents exactly midnight, in this case, and 29,040 represents 8:04 AM. I want to check if the phone's time of day is between the two TimeIntervals.

I found a few similar Stack Overflow questions, but none of them really dealt with TimeIntervals. It seems like just using start <= Date().timeIntervalSinceReferenceDate <= end or something wouldn't work, because it would return a huge value.

What's the best way to handle this type of situation in Swift 3?

Edit: To clarify, I don't need to worry about things like daylight savings. As an example, assume that the user only wants certain things in the app to happen between the hours of X and Y, where X and Y are given to me as TimeInterval values since midnight. So it should be sufficient to check if the phone's TimeInterval since midnight on a typical day is between X and Y before completing the action.


Solution

  • I ended up using DateComponents to calculate a TimeInterval.

    let components = Calendar.current.dateComponents(
      [.hour, .minute, .second], from: Date())
    guard let seconds = components.second,
      let minutes = components.minute,
      let hours = components.hour else
    {
      return false
    }
    let currentTime = Double(seconds + minutes * 60 + hours * 60 * 60)
    return startTime <= currentTime && currentTime <= endTime