0

I have searched StackOverflow for answers but I couldn't find a question that quite matches mine.

  • I am in GMT+2 time zone.

  • My Mac's time and timezone are correctly set and correctly displayed in 24-hour format.

  • My iPhone simulator's time matches my Mac's time but in 12-hour format, but that's not a problem.

  • Here's my Swift code. Comments in the code were copied from the print output in the Debug console:

      let tzSecsFromGMT = TimeZone.current.secondsFromGMT()
      print(tzSecsFromGMT) // 7200
    
      let nowDate = Date(timeIntervalSinceNow: TimeInterval(tzSecsFromGMT))
      print(nowDate)       // 2021-02-27 21:33:19 +0000  (21:33 matches my Mac's time)
    
      let triggerDate = nowDate + 30 // seconds
      print(triggerDate)   // 2021-02-27 21:33:49 +0000  (30 seconds after nowDate, it's correct)
    
      let dateComponents = Calendar.current.dateComponents([.timeZone, .year, .month, .day, .hour, .minute, .second], from: triggerDate)
      print(dateComponents)  // timeZone: Europe/Bucharest (current) year: 2021 month: 2 day: 27 hour: 23 minute: 33 second: 49 isLeapMonth: false  
                             // (NOTE THE HOUR is 23 instead of 21 !)
    
  • nowDate's hour is 21.

  • triggerDate's hour is 21.

  • But dateComponent's hour is 23 instead of 21.

What am I doing wrong?

Kaplan
  • 91
  • 1
  • 11
  • Are you sure that you have set right time zone in your mac? Cause it sounds to me that you havn’t set GMT+2 there. – Iman Nia Feb 27 '21 at 19:46
  • Yes, my Mac's preferences have been set with the right time zone (Bucharest), and the time/clock in the top-right corner of my Mac is showing the correct local (Bucharest) time. – Kaplan Feb 27 '21 at 21:07

1 Answers1

1

The issue there is that you are adding the secondsFromGMT to your current timeZone. A date is just a point in time. The date (now) is the same anywhere in the world.

let nowDate = Date()
print(nowDate.description(with: .current))       // Saturday, February 27, 2021 at 4:51:10 PM Brasilia Standard Time
print(nowDate)   // 2021-02-27 19:51:10 +0000   (+0000 means UTC time / zero seconds offset from GMT)

let triggerDate = nowDate + 30 // seconds
print(triggerDate)   // 2021-02-27 19:51:40 +0000 (30 seconds after nowDate at UTC (GMT)

let dateFromTriggerDate = Calendar.current.dateComponents([.calendar, .year, .month, .day, .hour, .minute, .second], from: triggerDate).date!
print(dateFromTriggerDate.description(with: .current))  // Saturday, February 27, 2021 at 4:51:40 PM Brasilia Standard Time
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Thanks. I changed my code and it looks like I am now setting the correct datetime for my local notifications triggerDate, meaning, it is printing the correct **local** date and time (Bucharest time). I printed out the notificationCenter's pending request and it also shows the correct, intended local datetime. However, the notification doesn't fire. – Kaplan Feb 27 '21 at 22:25
  • @Kaplan are you sure your application is at the background? 30 seconds from now doesn't make any sense. Check this [post](https://stackoverflow.com/q/39713605/2303865) – Leo Dabus Feb 27 '21 at 22:26
  • 1
    OMG!!! That was it...I was staring at my app in the foreground waiting for the notification to appear. Arrrgh! You're right. After I backgrounded my app, the notification appeared on the Home screen. The 30 seconds from now is just for testing. Now that I know it works, I will work on handling the situation when the notification happens when the app is in the foreground. I know I read in the docs that there is a way to handle it, if so desired. Thanks again!! – Kaplan Feb 27 '21 at 22:52