Home > Net >  Calculate timeInterval between two dates with different time zone in swift
Calculate timeInterval between two dates with different time zone in swift

Time:01-18

I'm trying to protect game from abusing time changing. The game save current time and if you enter game soon it check if timeInterval is greater than 0 (if not you can't play). But what about changing timeZone?

If player moved from France to e.g. United Kingdom timeZone changes. Let's say first point was 10:07AM in France and the second in UK after 30 minutes (-1 hour difference). The second point is gonna be 09:37AM? How Date() works when devise TimeZone changes? Do timeInterval() function takes into account that changes? Or it possible to just ignore changing timeZone?

Game should working offline.

Update

How that works

  1. The Date() is saved at the very first entry in app. (In CoreData)
  2. The next time the app enters, it will check the new and old dates. Using TimeInterval()
  3. Case1 - result is greater than 0. It means the entry in "Future". Entry allows. Save the new Date instead of the old.
  4. Case2 - result is less than 0. It means the entry in "Past". That impossible without date changing. Entry is prohibited until the new date is greater than old.

CodePudding user response:

From the Date() documentation:

A Date value encapsulate a single point in time, independent of any particular calendrical system or time zone.

I would therefore expect this to be a non-issue - the first point in time would still be 30 minutes before the second point in time, and I'd expect that to be respected by timeInterval. I'd expect the same Date() to be returned by now at a given point in time, regardless of the time zone of the system running the code.

  • Related