如何在Swift中使用pod TimeZoneLocate获得夏令时
我正在将本地时区转换为字符串以在屏幕上显示它。为此,我使用TimeZoneLocate库。问题:由于没有实施夏令时,我得到的日期结果比原来少了一个小时 我从sunrise-sunset.org获取JSON,并使用以下行:sunrise=“3:22:31 AM”;日落=“下午5:23:25” 我曾考虑将函数如何在Swift中使用pod TimeZoneLocate获得夏令时,swift,timezone,dst,Swift,Timezone,Dst,我正在将本地时区转换为字符串以在屏幕上显示它。为此,我使用TimeZoneLocate库。问题:由于没有实施夏令时,我得到的日期结果比原来少了一个小时 我从sunrise-sunset.org获取JSON,并使用以下行:sunrise=“3:22:31 AM”;日落=“下午5:23:25” 我曾考虑将函数isDaylightSavingTime()与if语句一起使用,但我不知道在何处添加这一小时 这就是神奇发生的地方: func UTCToLocal(incomingFormat: String
isDaylightSavingTime()
与if
语句一起使用,但我不知道在何处添加这一小时
这就是神奇发生的地方:
func UTCToLocal(incomingFormat: String, outgoingFormat: String, location: CLLocation?) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = incomingFormat
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: self)
let timeZone = location?.timeZone ?? TimeZone.current
dateFormatter.timeZone = timeZone
dateFormatter.dateFormat = outgoingFormat
return dateFormatter.string(from: dt ?? Date())
}
我使用CLLocation中的本地“location”,TimeZone.current由TimeZoneLocate库提供
这是我在代码中使用它的方式:
func parce(json: Data, location: CLLocation) {
let decoder = JSONDecoder()
if let sunriseData = try? decoder.decode(Results.self, from: json) {
self.sunriseLbl.text = sunriseData.results?.sunrise.UTCToLocal(incomingFormat: "h:mm:ss a",
outgoingFormat: "HH:mm",
location: location)
sunriseLbl正在从JSON打印当前位置默认情况下的sunrise数据,以及GooglePlaces为任何位置打印的sunrise数据。但是,在这两种情况下,我的日期都错了
此外,这里还有一个指向我在GitHub上的项目的链接,如果它可能对您有所帮助的话:
提前感谢我注意到了一件有趣的事情:
时区。当前
正在返回正确的时区,但是位置?。时区
并没有返回正确的时区。如果有一种方法可以实现TimeZone.current,即应用程序将始终使用用户的当前位置,那么我建议使用它。但是,如果用户可以输入自定义位置,则需要为location?.timeZone
返回的明显不正确的时区找到解决方法
我的解决方法如下。请注意,我们通过更改.secondsFromGMT()
属性手动调整所需时区的位置。这就是我调整你的代码的方式,它为我的个人位置返回了正确的时区
extension String {
func UTCToLocal(incomingFormat: String, outgoingFormat: String, location: CLLocation?) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = incomingFormat
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: self)
var timeZone = location?.timeZone ?? TimeZone.current
if timeZone.isDaylightSavingTime() {
timeZone = TimeZone(secondsFromGMT: timeZone.secondsFromGMT() - 7200)!
}
dateFormatter.timeZone = timeZone
dateFormatter.dateFormat = outgoingFormat
let output = dateFormatter.string(from: dt ?? Date())
return output
}
}
注意:
时区非常复杂,从一个地方到另一个地方以及从一年中的当前时间都会发生变化。就因为这一工作区在我当前所在地的这一天起作用,并不意味着这一工作区总是起作用但是,您可以根据需要查看返回的timeZone.isDaylightSavingTime()
值以及通过timeZone=timeZone(secondsFromGMT:timeZone.secondsFromGMT()-x
创建新时区的当前位置。这是实现
“我曾考虑将函数isDaylightSavingTime()与if语句一起使用,但我不知道在何处添加这一小时。”
你的想法
编辑:
作为记录,我使用的时区是CST或芝加哥时间。我编写此代码的日期是2019年4月19日。您能举一个输入格式的例子吗?您需要告诉我们
输入格式
,输出格式
,位置
,dt
,位置.时区
,和的准确值>TimeZone.current
。我正在获取JSON,并使用以下行:sunrise=“3:22:31 AM”sunset=“5:23:25 PM”。我使用CLLocation中的local,TimeZone.current是由TimeZoneLocate Library提供的。您是如何实现的?此函数是否被称为字符串类型的扩展?我已尝试重新创建此函数,但“self”不是字符串类型,因此让dt=dateFormatter.date(from:self)
导致错误。是的,它是StringStrange的扩展名。如果使用secondsFromGMT:timeZone.secondsFromGMT()-0,有效!如果我不使用它或键入其他数字,则显示错误time@ArtemBoordak这很好,但我会从多个不同的时区和多个不同的日期对其进行测试,以确保它不会碰巧适用于您当前的时区。您可能需要考虑国家是否使用了一些sor一天中的t灯节省时间,一年中的时间等。