在我们的应用程序中,存在从字符串创建日期的问题,但只能通过非常特定的组合进行复制.不幸的是,无法从遇到问题的用户那里获得它,因此我决定try 各种可能的组合:
import Foundation
var dateOnlyDateFormatter: (String, String) -> DateFormatter = { timeZoneS, localeS in
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.timeZone = TimeZone(identifier: timeZoneS)
formatter.locale = Locale(identifier: localeS)
return formatter
}
let date = "2022-05-27"
let time = "06:15"
for timeZone in TimeZone.knownTimeZoneIdentifiers {
for locale in Locale.availableIdentifiers {
let dateFormatter = dateOnlyDateFormatter(timeZone, locale)
let printDate = dateFormatter.date(from: date)
if printDate == nil {
print("TimeZone: \(timeZone), Locale: \(locale)")
}
}
}
结果:
TimeZone: America/Asuncion, Locale: ar_SA
TimeZone: America/Asuncion, Locale: en_SA
我不太确定处理这个问题的最佳方式是什么.很明显,我们的BE可以使用一个特定的区域设置返回日期,比如en_US_POSIX
,但我几乎无法控制它,因为它是一个更大的旧系统的一部分.有没有人经历过这样的问题?