2017-10-28 72 views
1

我有一个应用程序来计算两个位置之间的时间差,我只是意识到选择“欧洲/伦敦”和“GMT”都返回相同的字符串“格林尼治标准时间”NSTimeZone从两个不同的时区返回相同的本地化名称

buttonTimeZone.setTitle(timeZone?.localizedName(for: .standard, locale: locale), for: .normal) 
    print("timeZone:\(String(describing: timeZone?.description))") 

该按钮显示“格林威治标准时间”,但我相信GMT和欧洲/伦敦有1小时的时差。

我添加了第二行print,以确保在测试时我选择了两个不同的区域。测试时间为2017年10月28日。

感谢您的输入!

+0

格林威治位于伦敦郊外,因此格林威治时间(GMT)*是*伦敦时间。你提到的是英国夏令时,伦敦在每年的3月和10月之间使用 –

+1

我刚刚意识到伦敦在夏令时,因为时钟在今天改变了。我所要做的就是将风格从.standard改为.daylightsaving。 https://developer.apple.com/documentation/foundation/nstimezone.namestyle感谢您的评论! – yoshitech

+0

您应该将您的解释和解决方案发布为答案。 – clemens

回答

1

我刚刚意识到伦敦在夏令时,因为时钟在今天改变了。我所要做的就是在DST时将风格从.standard改为.daylightsaving。

var title = timeZone?.localizedName(for: .standard, locale: locale) 
    if (timeZone?.isDaylightSavingTime(for: dateTimeComplete!))! { 
     title = timeZone?.localizedName(for: .daylightSaving, locale: locale) 
    } 
    buttonTimeZone.setTitle(title, for: .normal) 
相关问题