Swift 3 Check Two times against each other help

Hi,


I'm trying to compare two times against each other for a checkTimer method. I've got the functionality right, it's the 24 hour issue that's causing a problem.

The functionality should be that if the current time is greater than the userDefault time then return true. The Bug exists where it's the next day. Am I better comparing two dates against each other or is there a simpler way.


My method

func checkTimer() -> Bool {
      
        let time = Int(currentTime())!
      
        print(time)
      
        let storedTime = Int(UserDefaults.standard.integer(forKey: "LastAccessTime"))
      
        if time >= storedTime {
            print("Enough time has passed \(storedTime)")
            return true
        }
      
        print("Not enough time has passed \(storedTime)")
        return false
      
    }
  
    func currentTime() -> String {
        let currentTime = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "Hmm"
        let time = formatter.string(from: currentTime)
        return "\(time)"
    }

Thanks!

You have to be more explicit about what you want — "time" is too ambiguous.


A Date value is actually a date/time. If you want to compare the current date/time with the date/time at which something was last accessed, you should use the Date values, and not try to convert to strings or Ints. (Note: You can store a Date value in user defaults.) Such Date values are independent of time zone, daylight savings time, etc.


If you mean a time of day, for example if you want to make a new backup of a file every day at a certain time, you're going to have to work a bit harder to deal with time zones, etc.

If you mean a time of day, for example if you want to make a new backup of a file every day at a certain time, you're going to have to work a bit harder to deal with time zones, etc.

Which we covered over in this thread.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hey,


Essentially I wish to minimise an API call to 1 hour intervals. If an hour has passed then allow the API call.

Hey,


I thought out the time process again and came to a much simpler way. I'm getting the current time from Date so this should solve any time issues. If the device travels back in time then I will catch it by < 1 hour period.


Thanks

So let's try a simpler version of your function:


func checkTimer() -> Bool { 
     let time = Date () 
     print(time) 
     if let storedTime = UserDefaults.standard.object(forKey: "LastAccessTime")) as? Date, time >= storedTime { 
          print("Enough time has passed \(storedTime)") 
          return true 
     } 
     print("Not enough time has passed \(storedTime)") 
     return false 
}

Note that I've used an "if let" construct to check if the last access time exists in user defaults, and a Swift 3 "if XXX, YYY {…}" construct to test for both conditions XXX (last access time exists) and YYY (it's in the past) simultaneously.

Is this the functionality you're looking for?

Maybe the Timer class will help - https://developer.apple.com/reference/foundation/timer

Swift 3 Check Two times against each other help
 
 
Q