在我的用例中,我得到了一个Microsoft时区索引值。我需要这个索引来获得一个项目的时区。
为了在Swift中设置自定义时区,我找到了以下代码片段。
dateformatter.timeZone = NSTimeZone(abbreviation: "CEST") here我找到了一个包含所有缩写的列表
ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
[...]
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";Swift中的索引值和所有可能的时区。但是我不知道哪个时区是用来干嘛的。
例如,在Microsoft时区索引值是001“萨摩亚标准时间- GMT-11:00中途岛,萨摩亚”
dateformatter.timeZone = NSTimeZone(abbreviation: "GMT-11") //not found the abbreviation有人有一些想法或意见来解决问题吗?
发布于 2016-07-12 17:35:20
我试着用下面的代码来实现这一点
Swift
let calendar: NSCalendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(name: "Pacific/Midway")!Objective-C
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"Pacific/Midway"]];
NSLog(@"calendar timezone: %@", calendar.timeZone);
Output:
calendar timezone: Pacific/Midway (GMT-11) offset -39600
// You will set timeZone Pacific/Apia and get below output is GMT +13
calendar timezone: Pacific/Apia (GMT+13) offset 46800我已经从下面的链接中引用了。首先,您知道所有timeZone的特定缩写
1.http://www.unicode.org/cldr/charts/29/supplemental/zone_tzid.html
发布于 2016-07-12 17:43:41
我有一个可能会有帮助的解决方案,但它有点老生常谈,也不是最干净的。因为您可以从NSTimeZone获得缩写的值的字典,所以我们可以在for循环中遍历它。如果你有一个缩写,我们可以插入到NSTimeZone(abbreviation:)中,我们可以通过使用.secondsFromGMT找到格林尼治标准时间的秒数,然后创建一个if语句来检查它是否是我们正在寻找的格林尼治标准时间。她是一个很好的例子:
// Get the full name and abbreviation from the NSTimeZone dictionary
for (abbreviation, fullName) in NSTimeZone.abbreviationDictionary() {
// Plug the abbreviation into NSTimeZone and get the seconds from GMT and match it to the GMT we want (in this case, GMT+8)
if NSTimeZone(abbreviation: abbreviation)?.secondsFromGMT == 28800 {
// Print the result if a match was found.
print(fullName)
}
}发布于 2016-07-12 20:45:15
所以我找到了一个对我有效的解决方案。
当你想要一个secondsFromGMT,并且你有一个微软的价值指数:
func getTimeZoneShorthandSymbol(index: Int) -> Double {
var gmtnumber = 0.0
if index == 000 {
gmtnumber = 12
} else if index == 001 {
gmtnumber = 11
} else if index == 002 {
gmtnumber = 10
} else if index == 003 {
gmtnumber = 9
} else if index == 004 {
gmtnumber = 8
} else if index == 010 || index == 013 || index == 015 {
gmtnumber = 7
} else if index == 020 || index == 025 || index == 030 || index == 033 {
gmtnumber = 6
} else if index == 035 || index == 040 || index == 045 {
gmtnumber = 5
} else if index == 050 || index == 055 || index == 056 {
gmtnumber = 4
} else if index == 060 {
gmtnumber = 3.5
} else if index == 065 || index == 070 || index == 073 {
gmtnumber = 3
} else if index == 075 {
gmtnumber = 2
} else if index == 080 || index == 083 {
gmtnumber = 1
} else if index == 085 || index == 090 {
gmtnumber = 0
} else if index == 095 || index == 100 || index == 105 || index == 110 || index == 113 {
gmtnumber = 1
} else if index == 115 || index == 120 || index == 125 || index == 130 || index == 135 || index == 140 {
gmtnumber = 2
} else if index == 145 || index == 150 || index == 155 || index == 158 {
gmtnumber = 3
} else if index == 160 {
gmtnumber = 3.5
} else if index == 165 || index == 170 {
gmtnumber = 4
} else if index == 175 {
gmtnumber = 4.5
} else if index == 180 || index == 185 {
gmtnumber = 5
} else if index == 190 {
gmtnumber = 5.5
} else if index == 193 {
gmtnumber = 5.75
} else if index == 195 || index == 200 || index == 201 {
gmtnumber = 6
} else if index == 203 {
gmtnumber = 6.5
} else if index == 205 || index == 207 {
gmtnumber = 7
} else if index == 210 || index == 215 || index == 220 || index == 225 || index == 227 {
gmtnumber = 8
} else if index == 230 || index == 235 || index == 240 {
gmtnumber = 9
} else if index == 245 || index == 250 {
gmtnumber = 9.5
} else if index == 255 || index == 260 || index == 265 || index == 270 || index == 275 {
gmtnumber = 10
} else if index == 280 {
gmtnumber = 11
} else if index == 285 || index == 290 {
gmtnumber = 12
} else if index == 300 {
gmtnumber = 13
}
return gmtnumber*60*60
}请注意:这只显示了标准差异。没有处理夏季时间或其他任何事情。我没有用过它,但它有太多的打字...也许任何时候都有人需要它。
我的解决方案:我有三个日期: UTC,我的物品的时间(区域)和我的本地时间。
我接受了UTC和我的项目之间的差异。
var m = Model.sharedInstance().minutesFrom(dateFromUtc, sndDate: from) //get difference between two dates in minutes
if m < 0 {
m = m * (-1) //m should be always positiv
}
let secondsDiffernce = m * 60 //to seconds
func minutesFrom(date: NSDate, sndDate: NSDate) -> Int{
return NSCalendar.currentCalendar().components(.Minute, fromDate: date, toDate: sndDate, options: []).minute
}接下来,我需要utc和我的本地时区之间的差异
let secondsFromLocalTimeZone = NSTimeZone.localTimeZone().secondsFromGMT有了这些信息,我可以创建不同日期的日期,如下所示:
//utc time
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.day = 12
components.month = 01
components.year = 2016
components.hour = 11
components.minute = 53
components.timeZone = NSTimeZone(forSecondsFromGMT: 25200) //set here your seconds from item, localtime whatever...显示您选择的日期
let newDate = calendar.dateFromComponents(components)感谢您的帮助!
https://stackoverflow.com/questions/38324064
复制相似问题