如何将自定义时区从Microsoft时区索引值设置为Swift?

时间:2016-07-12 08:49:44

标签: swift datetime timezone

在我的用例中,我获得了Microsoft时区索引值。您找到的索引here。我需要这个索引来获取项目的时区。

要在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";

所以我掌握了所有信息。 UTC /格林威治标准时间Swift中的索引值和所有可能的时区。但我无法弄清楚哪个时区属于什么。

例如在微软时区索引值为001“萨摩亚标准时间 - 格林威治标准时间-11:00中途岛,萨摩亚”

但是这快速是什么?我不能做像

这样的事情
dateformatter.timeZone =  NSTimeZone(abbreviation: "GMT-11") //not found the abbreviation

有人有一些想法或意见来解决问题吗?

3 个答案:

答案 0 :(得分:0)

我尝试下面的代码来实现这个

<强>夫特

let calendar: NSCalendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(name: "Pacific/Midway")!

<强>目标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

2。Translate Time Zone Names to Time ID:s in Xcode

答案 1 :(得分:0)

我有一个可能会有所帮助的解决方案,但它有点hacky并且不是最干净的。因为您可以从NSTimeZone获取缩写值的字典,所以我们可以在for循环中迭代它。如果您有我们可以插入NSTimeZone(abbreviation:)的缩写,我们可以使用.secondsFromGMT找到GMT的秒数,然后创建一个if语句来检查它是否是我们正在寻找的GMT对于。她是一个有效的例子:

// 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)

    }

}

答案 2 :(得分:-1)

所以我找到了一个适合我的解决方案。

如果您需要secondsFromGMT并且您拥有Microsoft Value Index:

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)

感谢您的帮助!