如何根据Swift上的GMT获得时差

时间:2015-06-22 09:51:55

标签: ios swift

我正在尝试根据GMT时间获得时差。在每天结束时,计时器重置为零。我在Xcode模拟器上尝试了下面的代码,每当我改变mac的时间时,差异也会发生变化。

如何将时间设置为午夜设置为时区,即使时区发生变化,计时器的差异也始终相同。

func updateTimeGMT(){

    let date = NSDate()
    var formatter = NSDateFormatter()
    var addDay = date.dateByAddingTimeInterval(60*60*24*2)

    formatter.timeZone = NSTimeZone(abbreviation: "GMT+5:30")
    formatter.dateStyle = NSDateFormatterStyle.LongStyle
    formatter.timeStyle = NSDateFormatterStyle.LongStyle

    let localDate = formatter.stringFromDate(addDay)
    let datefrom = formatter.dateFromString(localDate)

    let currentDate = formatter.stringFromDate(date)
    let currentDateFrom = formatter.dateFromString(currentDate)

    let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
    let startOfToday = NSCalendar.currentCalendar().startOfDayForDate(datefrom!)

    let newDate = cal!.startOfDayForDate(addDay)

    var currentTime = currentDateFrom?.timeIntervalSinceReferenceDate
    var elapsedTime : NSTimeInterval = newDate.timeIntervalSinceReferenceDate - currentTime!

    println("\(newDate) elapsed : \(currentDateFrom)")

    let hours = UInt(elapsedTime/60/60)
    elapsedTime -= (NSTimeInterval(hours)*60*60)
    let minutes = UInt(elapsedTime/60.0)
    elapsedTime -= (NSTimeInterval(minutes)*60)
    let seconds = UInt(elapsedTime)
    elapsedTime -= NSTimeInterval(seconds)
    let fraction = UInt(elapsedTime * 100)

    let strHours = String(format: "%02d", hours)
    let strMinutes = String(format: "%02d", minutes)
    let strSeconds = String(format: "%02d", seconds)
    let strFraction = String(format: "%02d", fraction)

    countingLabel.text = "\(strHours) : \(strMinutes) : \(strSeconds) : \(strFraction)"

}

1 个答案:

答案 0 :(得分:1)

我认为您的问题是您正在使用未设置时区的日历。

60*60*24*2添加到当前时间的计算并不考虑某些时区更改为夏令时和来自夏令时的两天。那些日子是23和25小时。

let calendar = NSCalendar.autoupdatingCurrentCalendar()
let now = NSDate()
var startOfDay = NSDate?()
var lengthOfDaySeconds : NSTimeInterval = 0
if (calendar.rangeOfUnit(NSCalendarUnit.Day,
                         startDate:&startOfDay, 
                         interval:&lengthOfDaySeconds,
                         forDate: now)) {
    let endOfDay = startOfDay?.dateByAddingTimeInterval(lengthOfDaySeconds)
    if let secondsUntilEndOfDay = endOfDay?.timeIntervalSinceDate(now) {
        var timeLeft = secondsUntilEndOfDay
        let hours = floor(timeLeft / 3600.0)
        timeLeft -= (hours * 3600.0)
        let minutes = floor(timeLeft / 60.0)
        timeLeft -= (minutes * 60.0)
        let seconds = floor(timeLeft)
        timeLeft -= seconds
        let fraction = floor(timeLeft * 100)

        let strHours = String(format: "%02d", UInt(hours))
        let strMinutes = String(format: "%02d", UInt(minutes))
        let strSeconds = String(format: "%02d", UInt(seconds))
        let strFraction = String(format: "%02d", UInt(fraction))

        countingLabel.text = "\(strHours) : \(strMinutes) : \(strSeconds) : \(strFraction)"
    }
}