From ae3ca2981f991ba4772c59fd6d3b092fb26f65ec Mon Sep 17 00:00:00 2001 From: Sunny Chan Date: Tue, 19 Jun 2018 16:57:04 -0700 Subject: [PATCH] Fixed the timezone related freeze when using Britain's locale and 12 hours format. --- RRuleSwift.xcodeproj/project.pbxproj | 4 ++++ Sources/DateFormatter+RRule.swift | 27 +++++++++++++++++++++++++++ Sources/RRule.swift | 13 ++++--------- 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 Sources/DateFormatter+RRule.swift diff --git a/RRuleSwift.xcodeproj/project.pbxproj b/RRuleSwift.xcodeproj/project.pbxproj index a7989e7..90b50c0 100644 --- a/RRuleSwift.xcodeproj/project.pbxproj +++ b/RRuleSwift.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 6BD694AB20D9C6F40008B4C6 /* DateFormatter+RRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BD694AA20D9C6F40008B4C6 /* DateFormatter+RRule.swift */; }; EB48D1611D641D45001EE872 /* EKWeekday+RRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB48D1511D641D45001EE872 /* EKWeekday+RRule.swift */; }; EB48D1621D641D45001EE872 /* EKWeekday+RRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB48D1511D641D45001EE872 /* EKWeekday+RRule.swift */; }; EB48D1631D641D45001EE872 /* ExclusionDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB48D1521D641D45001EE872 /* ExclusionDate.swift */; }; @@ -30,6 +31,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 6BD694AA20D9C6F40008B4C6 /* DateFormatter+RRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DateFormatter+RRule.swift"; sourceTree = ""; }; D31B13971CA8E02E00D0B863 /* RRuleSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RRuleSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; }; EB48D1371D641AD6001EE872 /* RRuleSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RRuleSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; }; EB48D1511D641D45001EE872 /* EKWeekday+RRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "EKWeekday+RRule.swift"; sourceTree = ""; }; @@ -95,6 +97,7 @@ EB48D1551D641D45001EE872 /* JavaScriptBridge.swift */, EB48D1511D641D45001EE872 /* EKWeekday+RRule.swift */, EB48D1591D641D45001EE872 /* NSDate+Comparison.swift */, + 6BD694AA20D9C6F40008B4C6 /* DateFormatter+RRule.swift */, EB48D1561D641D45001EE872 /* lib */, EB48D15D1D641D45001EE872 /* Supporting Files */, ); @@ -248,6 +251,7 @@ EB48D1691D641D45001EE872 /* JavaScriptBridge.swift in Sources */, EB48D16F1D641D45001EE872 /* NSDate+Comparison.swift in Sources */, EB48D1751D641D45001EE872 /* RRule.swift in Sources */, + 6BD694AB20D9C6F40008B4C6 /* DateFormatter+RRule.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Sources/DateFormatter+RRule.swift b/Sources/DateFormatter+RRule.swift new file mode 100644 index 0000000..5870ae9 --- /dev/null +++ b/Sources/DateFormatter+RRule.swift @@ -0,0 +1,27 @@ +// +// DateFormatter+RRule.swift +// RRuleSwift-iOS +// +// Created by Sunny Chan on 6/19/18. +// Copyright © 2018 Teambition. All rights reserved. +// + +import Foundation + +extension DateFormatter { + convenience init(_ dateFormat: String, _ timeZone: TimeZone? = nil, _ safely: Bool = true) { + self.init() + self.dateFormat = dateFormat + + if timeZone == nil { + self.timeZone = TimeZone(secondsFromGMT: 0) + } + + if safely { + // NOTE: AM/PM on 12/24 hour switch is broken on some locale. + // https://stackoverflow.com/a/6735644 + // https://github.com/teambition/RRuleSwift/issues/12 + self.locale = Locale(identifier: "en_US_POSIX") + } + } +} diff --git a/Sources/RRule.swift b/Sources/RRule.swift index b3b97de..b691eed 100644 --- a/Sources/RRule.swift +++ b/Sources/RRule.swift @@ -11,22 +11,17 @@ import EventKit public struct RRule { public static let dateFormatter: DateFormatter = { - let dateFormatter = DateFormatter() - dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) - dateFormatter.dateFormat = "yyyyMMdd'T'HHmmss'Z'" + let dateFormatter = DateFormatter("yyyyMMdd'T'HHmmss'Z'") return dateFormatter }() + public static let ymdDateFormatter: DateFormatter = { - let dateFormatter = DateFormatter() - dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) - dateFormatter.dateFormat = "yyyyMMdd" + let dateFormatter = DateFormatter("yyyyMMdd") return dateFormatter }() internal static let ISO8601DateFormatter: DateFormatter = { - let dateFormatter = DateFormatter() - dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) - dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" + let dateFormatter = DateFormatter("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") return dateFormatter }()