开发者

iPhone - NSTimer - initWithFireDate and pass local time

In my app, I'm using NSTimer and using initWithFireDate. I want to fire the timer after one hour from now. "Now" means local time zone. When ever I'm trying to get 开发者_运维问答the add add one hour to it and trying to print it, its printing the time in GMT and not according to my timezone. How can I fix this bug? I want NSDate with 1 hour from now depending on local time zone.


This code would help you get the local time and you can take it forward from there accordingly:

- (NSDate *)currentLocalTime {
      NSDate *aTriggerDate = [NSDate date];

     NSTimeZone *aSourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
     NSTimeZone *aTriggerTimeZone = [NSTimeZone systemTimeZone];

     NSInteger aSourceGMTOffset = [aSourceTimeZone secondsFromGMTForDate:aTriggerDate];
     NSInteger aTriggerGMTOffset = [aTriggerTimeZone secondsFromGMTForDate:aTriggerDate];
     NSTimeInterval anInterval = aTriggerGMTOffset - aSourceGMTOffset;

     NSDate *aFinalDate = [[NSDate alloc] initWithTimeInterval:anInterval sinceDate:aTriggerDate];

    return aFinalDate;
}


NSDateFormatter *formater=[[NSDateFormatter alloc] init]; formater.timeZone=[NSTimeZone localTimeZone]; use this for to localized your time zone.


You can do it this with NSDateFormatter taking Different Formats of the Date String. Just try it out , You will ge your expected answer.


After getting the time, find the time zone and add the time difference accordingly.


This is my version of Bikramjit Singh's code in Swift (only worked for me by using "UTC" as the abbreviation). Hope it helps:

func testTimeZone(){

    let currentLocalTime = NSDate()

    let sourceTimeZone = NSTimeZone(abbreviation: "UTC")
    let triggerTimeZone = NSTimeZone.systemTimeZone()

    let sourceGTMOffset = sourceTimeZone?.secondsFromGMTForDate(currentLocalTime)
    let triggerGTMOffset = triggerTimeZone.secondsFromGMTForDate(currentLocalTime)

    let interval = triggerGTMOffset - sourceGTMOffset!

    let finalDate = NSDate(timeInterval: NSTimeInterval.init(interval), sinceDate: currentLocalTime)

    Swift.print("Current local time: \(currentLocalTime)")
    Swift.print("Source Time Zone (GTM): \(sourceTimeZone)")
    Swift.print("Trigger Time Zone (System Time Zone): \(triggerTimeZone)")
    Swift.print("Source GTM Offset: \(sourceGTMOffset)")
    Swift.print("Trigger GTM Offset: \(triggerGTMOffset)")
    Swift.print("Interval (Source - Trigger Offsets): \(interval)")
    Swift.print("Final Date (Current + interval): \(finalDate)")


}

UPDATE

I actually found a shorter way to get the date, this way has worked better for me:

    var currentDate: NSDate {
    let currentLocalTime = NSDate()

    let localTimeZone = NSTimeZone.systemTimeZone()
    let secondsFromGTM = NSTimeInterval.init(localTimeZone.secondsFromGMT)
    let resultDate = NSDate(timeInterval: secondsFromGTM, sinceDate: currentLocalTime)

    return resultDate
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜