I want to store some data as a dictionary in my app relative to the date this data was created. It should look something like this:
// hours
[ 17:00 - 18:00 : 34.5,
18:00 - 19:00 : 41.2,
... ]
// days
[ 2017-11-19 - 2017-11-20 : 37.9,
2017-11-20 - 2017-11-21 : 31.1,
... ]
A DateInterval
structure seems to be a perfect candidate for a key in this dictionary. But as I ran some basic tests I found that while date created with Date()
init represents correct information on a given moment in time, the DateInterval
structure from Calendar.current
appears to be defaulting to GMT+0 as a timezone, so I'm getting following output:
let date = Date() //outputs "Nov 20, 2017 at 10:32 PM"
let hourInterval = Calendar.current.dateInterval(of: .hour, for: date)
//outputs: 2017-11-20 20:00:00 +0000 to 2017-11-20 21:00:00 +0000
So it's 2 hours off my/user's timezone. What is the correct way to use DateInterval
so it doesn't shift in timezones?