我有一些代码可以获得今天和另一个时间字符串之间的时间差异。
使用NSTimer.scheduledTimerWithTimeInterval
每秒刷新一次显示。
除非计时器达到0,否则一切正常。0
显示两次。
示例:
3,2,1,0,0,-1,-2
以下是我在游乐场完成的代码:
//: Playground - Time Countdown
import UIKit
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely()
class MyClass {
var totalSecondsText = ""
init(classSecondsText: String){
totalSecondsText = classSecondsText
}
@objc func ontimer(timer:NSTimer!){
let total = determineTimeDifference(totalSecondsText)
let (hour,min,sec) = secondsConverter(total)
if hour >= 1 {
if min >= 1 {
NSLog(String(hour)+"h "+String(min)+"m")
}else {
NSLog(String(hour)+"h")
}
}else{
if min >= 1{
NSLog(String(min)+"m "+String(sec)+"s")
} else if min > -1 {
NSLog(String(sec)+"s")
} else if min <= -1 {
NSLog(String(min)+"m")
}
}
}
func determineTimeDifference(timeInputed: String) -> Int {
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let finalDate = dateFormat.dateFromString(raceTime)
let differenceTimeInSeconds = (finalDate?.timeIntervalSinceNow)!
return differenceInSeconds
}
func secondsConverter (seconds : Int) -> (Int , Int, Int){
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}
}
// Add +1 minute on time to test.
var dateString = "2016-08-31T6:49:00Z"
NSTimer.scheduledTimerWithTimeInterval(1, target: MyClass(classSecondsText: dateString), selector: #selector(MyClass.ontimer(_:)), userInfo: nil, repeats: true)
对于出了什么问题有什么想法吗?
更新
正如所承诺的,这是针对此问题的错误修复:
func determineTimeDifference(timeInputed: String) -> Int {
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let finalDate = dateFormat.dateFromString(raceTime)?.timeIntervalSince1970
let dateNow = NSDate().timeIntervalSince1970
let differenceTimeInSeconds = Int(finalDate) - Int(dateNow)
return differenceInSeconds
}
答案 0 :(得分:1)
您的代码提供良好,条件正确。
看起来Data.components
没有正确处理计数差异。它可以由秒组件组成。
如果在此刻之前计算,当前时间和最终时间相等,(达到0,0),结果会出现错误-1 。
当我使用其他打印更新您的代码时,会显示:
CURRENT date: 2016-08-31 13:10:57 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: 2s (BAD)
CURRENT unix: 1472649057.28685 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: -3.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
2s
CURRENT date: 2016-08-31 13:10:58 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: 1s (BAD)
CURRENT unix: 1472649058.28734 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: -2.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
1s
CURRENT date: 2016-08-31 13:10:59 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: 0s (BAD)
CURRENT unix: 1472649059.28731 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: -1.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
0s
CURRENT date: 2016-08-31 13:11:00 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: 0s (OK)
CURRENT unix: 1472649060.28731 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: 0.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
0s
CURRENT date: 2016-08-31 13:11:01 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: -1s (OK)
CURRENT unix: 1472649061.28731 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: 1.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
-1s
CURRENT date: 2016-08-31 13:11:02 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: -2s (OK)
CURRENT unix: 1472649062.28729 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: 2.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
-2s
因此,如果使用舍入的unix时间之间的差值进行计数,则结果是正确的。
结论:错误有时会出现在等式之前,有时会出现。这取决于该脚本的发布时间。如果按数据组件计数以错误开始,则在0,0后清除。如果计数以正确的结果开始,则结果开始带来错误结果。这就是为什么零出现两次 - 在结果改变其精确状态的那一刻。