-
Notifications
You must be signed in to change notification settings - Fork 814
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
Crash on specific time zones #323
Comments
Hi, i see the problem, but I am trying to understand why the code fails before I merge the fix. here is the code: let formatter = DateFormatter()
let timeZone = TimeZone(identifier: "Asia/Amman")!
let locale = Locale(identifier: "ar_JO")
formatter.timeZone = timeZone
formatter.locale = locale
formatter.dateFormat = "yyyy MM dd"
let date = formatter.date(from: "2017 03 31") In this code, the For you region in Jordan, this code above is supposed to give me a date right? |
When i do the exact code for my region, it works. timeZone = TimeZone(identifier: "America/Vancouver")!
locale = Locale(identifier: "en_US")
formatter.timeZone = timeZone
formatter.locale = locale
date = formatter.date(from: "2017 03 31") Date does not = nil. Silly question, but does is there a 31-March-2017 for your region? or is there something wrong with the code. |
Yes there is 31 of march :) I think I have figured out the problem It refers to daylight saving time. And this shift in most countries usually is being performed in march. I created following playground to check this Then I checked best practices for that. And it is advised to use UTC time zone instead of local. So now I think the best fix for this problem will be to use following |
UTC is a solution thats true, but I wanted to keep this calendar's generated dates friendly with the dates for the region they selected. Doing this allows this calendar to work well other libraries such as https://github.com/malcommac/SwiftDate without developers doing unnecessary conversions from UTC into the timezone of their region. After researching I see that DateFormatter's default is midnight (i think). But since in the buggy regions, midnight doesn't exist because of Daylight Saving Time, this causes the formatter to returns nil. Honestly, I think this feels like an apple bug. I think that it should default to the next legal time if midnight doesn't exist. I think it should only return nil if there are no legal times left for that date. While looking around i've seen people file bugs to Apple about this. But since its been filed for some years now, I doubt they will change this behaviour. The Fix: Here is the new PlayGround code: //: Playground - noun: a place where people can play
import Foundation
let formatter = DateFormatter()
let timeZone = TimeZone(identifier: "Asia/Amman")!
let locale = Locale(identifier: "ar_JO")
formatter.timeZone = timeZone
//formatter.locale = locale
formatter.dateFormat = "yyyy MM dd"
formatter.isLenient = true
let date1 = formatter.date(from: "2017 03 30") // The date works and starts from midnight (the default)
let date2 = formatter.date(from: "2017 03 31") // The date works and starts from the next legally availablel time
// We can check to see that the time look correct
formatter.dateFormat = "yyyy MM dd HH:MM:SS"
print(formatter.string(from: date1!)) // Starts from midnight
print(formatter.string(from: date2!)) // Starts from 1Am Here are the results: I think this might be a good solution to get the best out of the situation:
What do you think? |
Wow, you did a great research ! Yes I also think that it looks like an apple bug also (time for radar ? :) ) Solution with |
First , thanks for such nice tool!!!
It really save tones of tome
But there is one crash though
Crash could be reproduced in example project
steps to reproduce :
let startDate = formatter.date(from: "2017 02 01")! let endDate = formatter.date(from: "2017 12 01")!
App crashes on line 754
let lastDateOfCurrentMonth = calendar.endOfMonth(for: date)!
in file JTAppleCalendatView.swift
I now investigating this ,myself
will post some clue about fixing if I had one
Thanks,
Roman
====UPDATE=====
After investigation I have found that problem is in
File GlobalFunctionsAndExtensions.swift
in function endOfMonth
I replaced functions startOfMonth and endOfMonth with following functions from this answer on stack overflow
http://stackoverflow.com/questions/10717574/get-firstdate-lastdate-of-month
Could you, please review this suggestion and if it's appropriate can you please update code and cocoapod ?
Thanks in advance, Roman
The text was updated successfully, but these errors were encountered: