2

In my use case I get a Microsoft Time Zone Index Value. The index you find here. I need this index to get a timezone of an item.

To set a custom timezone in Swift I just found this snippet.

dateformatter.timeZone =  NSTimeZone(abbreviation: "CEST") 

And here I found a list with all abbreviations

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

So I have all information. A time in UTC/GMT. The index value and all possible timezones in Swift. But I can't figure out which timezone is for what.

For example in Microsoft Time Zone Index Values is 001 "Samoa Standard Time - GMT-11:00 Midway Island, Samoa"

But what is this in swift? I can't do something like

dateformatter.timeZone =  NSTimeZone(abbreviation: "GMT-11") //not found the abbreviation

Somebody has some ideas or input to solve the problem?

Community
  • 1
  • 1
kuzdu
  • 7,124
  • 1
  • 51
  • 69
  • 1
    Please note that unless you really are using Windows Embedded POS 1.1 from 2006, the article you linked to does not apply, and the numeric values like "001" are no longer in use. That particular article is often referenced, but hasn't been maintained in 10 years and does not match what is used on modern Windows OS. – Matt Johnson-Pint Jul 12 '16 at 20:22

3 Answers3

0

I try below code to achieve this

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

And I have refered from below links. First you know the specfic abbrevation for all timeZone

1.http://www.unicode.org/cldr/charts/29/supplemental/zone_tzid.html

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

Community
  • 1
  • 1
HariKrishnan.P
  • 1,204
  • 13
  • 23
0

I have a solution that might help, but it's a bit hacky and not the cleanest. Because you can get a dictionary of values of the abbreviations from NSTimeZone, we can iterate though that in a for loop. If you have an abbreviation that we can plug into NSTimeZone(abbreviation:) we can find the seconds for the GMT by using .secondsFromGMT and then create an if statement to check if it is the GMT we are looking for. Her is a working example:

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

    }

}
Camon
  • 1,003
  • 1
  • 10
  • 22
0

So I found a solution that worked for me.

When you want a the secondsFromGMT and you have an 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
    }

Be careful: This only show the standard difference. There is no handling of summer time or anything else. I didn't use it but it was so much typing... maybe someone need it anytime.

My solution: I had three dates: the UTC, the time(zone) of my item and my localtime.

I took the difference between UTC and my item.

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
}

Next I need the difference between utc and my local timezone

let secondsFromLocalTimeZone = NSTimeZone.localTimeZone().secondsFromGMT

With this information I can create different dates of a date like this:

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

Show the date of your choice

let newDate = calendar.dateFromComponents(components)

Thanks for help!

kuzdu
  • 7,124
  • 1
  • 51
  • 69