简体   繁体   English

如何在Swift上基于GMT获得时差

[英]How to get time difference based on GMT on Swift

I am trying to get a time difference based on a GMT time. 我正在尝试根据GMT时间设置时差。 where at the end of everyday the timer resets to zero. 在每天结束时,计时器重置为零。 I've tried the below code on the Xcode simulator and every time i change the time on the mac, the difference also changes. 我已经在Xcode模拟器上尝试了以下代码,并且每次我在Mac上更改时间时,差异也会改变。

how can i go about keeping the time to midnight set to a time zone, where the difference in timer is always the same, even if the time zone changes. 我如何才能将至午夜的时间设置为一个时区,即使时区发生变化,计时器中的差异也始终相同。

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

}

I think your problem is that you are using a calendar with an unset time zone. 我认为您的问题是您使用的时区未设置日历。

Your calculation of adding 60*60*24*2 to the current time does not account for the two days when some timezones change to and from daylight savings time. 您将当前时间增加60*60*24*2计算不考虑当某些时区更改为夏时制时间或从夏时制更改为两天时的两天。 Those days are 23 and 25 hours long. 那些日子分别是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)"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM