Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Summer time zone #57

Closed
cristibaluta opened this issue Feb 3, 2018 · 5 comments
Closed

Summer time zone #57

cristibaluta opened this issue Feb 3, 2018 · 5 comments

Comments

@cristibaluta
Copy link

Hi, I think there's a problem with datetime fields.
I store a summer date (+3 in my country) 2017-06-15 02:00:00 +0000 and if i read it in winter (+2), i get 2017-06-15 01:00:00 +0000. If i store a winter date i get it back correctly, so somewhere at reading or writing i lose 1hr. I don't fully understand timezones but working with coredata i didn't had this problem. Do you know what might be the problem?
Thanks.

@FahimF
Copy link
Owner

FahimF commented Feb 4, 2018

Dates are stored as GMT/UTC in the database and are restored back as such. I guess that would create an issue with daylight savings changes at the original location. Not quite sure how to fix for that when restoring dates but will look into it ...

@cristibaluta
Copy link
Author

I made this change in SqliteDB and seems to work but i guess i'll wait for the summer time to see if works the other way around.

let diff = TimeZone.current.secondsFromGMT()
let tz = TimeZone.current
var t = Double(mktime(&time) + diff)
var ti = TimeInterval(t)
var val = Date(timeIntervalSince1970: ti)
if tz.isDaylightSavingTime(for: val) {
t = t + tz.daylightSavingTimeOffset(for: val)
ti = TimeInterval(t)
val = Date(timeIntervalSince1970: ti)
}

@cristibaluta
Copy link
Author

Seems that my fix does not work for summer now.

@cristibaluta
Copy link
Author

I don't know why you used C, it seems to me that DateFormatter works perfectly. I also changed my computer's date, to simulate winter time, and my Dates are always built correctly. So as i said, the problem was that i was writing one date in summer and it was returning a different date in winter. I hope i'll not have surprises from now on.

let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
return dateFormatter.date(from: txt)!

@FahimF
Copy link
Owner

FahimF commented Mar 26, 2018

I didn't know what you meant by "I don't know why you used C", since I thought I was using DateFormatters all along. However, a peek at the code shows that you are right, and I have no idea why I did it that way since that bit of code appears to have been written in the early days of the framework. Of course, this would cause daylight savings issues since it hard codes daylight savings in. I'm still shaking my head at this and wondering what I was thinking ... Thank you for finding this.

@FahimF FahimF closed this as completed Mar 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants