我尝试让我的应用程序弹出viewControllers,每天到达9:00 UTC时间。我不希望它使用当地时间,因为它可以在不同地区改变,并且可以改变。我想过使用服务器时间,但是我在解决这个问题时遇到了问题。我有一个纪元时间戳并将其转换为日期。
let ref = FIRDatabase.database().reference()
let timestamp = FIRServerValue.timestamp()
ref.setValue(timestamp)
ref.observe(.value, with: {
snap in
if let t = snap.value as? TimeInterval {
// Cast the value to an NSTimeInterval
// and divide by 1000 to get seconds.
print("this is the time in seconds \(t)")
let date = Date(timeIntervalSince1970: t)
print("this is the time \(Date(timeIntervalSince1970: t/1000))")
打印出this is the time 2016-10-10 18:40:21 +0000
。
问题是弄清楚如何只获得小时分钟和秒数,这样我就可以比较时间日期。
答案 0 :(得分:1)
其中一个选项可能是:
Function getPowerPoints(eventID, resultTime) As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
strFile = ThisWorkbook.FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "SELECT max(POINTS) FROM PowerPoints WHERE EVENT = " & eventID & " and TIME >= " & resultTime ''Named range
rs.Open strSQL, cn
getPowerPoints = rs.GetString
cn.Close
Set rs = Nothing
Set cn = Nothing
End Function
),当差异过去时会触发。答案 1 :(得分:1)
您可以使用以下内容:
let calendar = Calendar.current
let components = NSDateComponents()
components.hour = 9
let nineOClock = calendar.nextDate(after: Date(), matching: components as DateComponents, matchingPolicy: .strict)
let timer = Timer(fireAt: nineOClock!, interval: 0, target: self, selector: #selector(doSomething), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)
func doSomething(){
print("Doing something")
}
答案 2 :(得分:1)
根据当前时间(1970年的秒数)计算到下一个上午9点的时间(以秒为单位)的方法。
func untilNineAmSeconds(now: Int) -> Int {
let todaySeconds = now % 86400
let hourSeconds = 3600
let nineAmSeconds = 9 * hourSeconds
let daySeconds = 24 * hourSeconds
if todaySeconds < nineAmSeconds {
return nineAmSeconds - todaySeconds
} else {
return (daySeconds - todaySeconds) + nineAmSeconds
}
}
现在你(1)从Firebase请求当前时间,(2)在上午9点之前获得一个时间间隔,以及(3)安排一个计时器。
let ref = FIRDatabase.database().reference()
let timestamp = FIRServerValue.timestamp()
ref.setValue(timestamp)
ref.observe(.value, with: { snap in
guard let ts = snap.value as? TimeInterval else {
return
}
let seconds = untilNineAmSeconds(now: Int(ts))
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(seconds), repeats: false) { _ in
// TODO: pop view controllers
}
// ...
}
答案 3 :(得分:0)
感谢Artem和他的帮助,我被指引在正确的道路上。花了很多时间研究日期转换后,我想出了这一点。最终目标是将两个值转换为整数,这样我就可以在几秒钟内对它们进行比较,并在计时器中使用它们来计算剩余的时间。
ref.observe(.value, with: {
snap in
if let t = snap.value as? TimeInterval {
let FinalTimeInterval = self.convertStringToDateToIntStartTime() - self.convertEpochTimeStampToDateToStringToDateToIntEndTime(timeInterval: t)
if FinalTimeInterval < 0 {
print("less than zero")
} else {
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(FinalTimeInterval), repeats: false) { _ in
// TODO: pop view controllers
print("i'm so happy this is working")
}
RunLoop.current.add(timer, forMode: RunLoopMode.commonModes)
}
}
})
func convertEpochTimeStampToDateToStringToDateToIntEndTime(timeInterval: TimeInterval) -> Int {
let date = Date(timeIntervalSince1970: timeInterval/1000)
var dateFormater = DateFormatter()
dateFormater.timeZone = TimeZone(identifier: "UTC")
dateFormater.dateFormat = "HH:mm:ss"
let dateString = dateFormater.string(from: date)
let endTime = dateFormater.date(from: dateString)
dateFormater.dateFormat = "HH"
let endHours = Int(dateFormater.string(from: endTime!))
dateFormater.dateFormat = "mm"
let endMinutes = Int(dateFormater.string(from: endTime!))
dateFormater.dateFormat = "ss"
let endSeconds = Int(dateFormater.string(from: endTime!))
return endSeconds! + endMinutes! * 60 + endHours! * 3600
}
func convertStringToDateToIntStartTime() -> Int {
var dateFormater = DateFormatter()
dateFormater.timeZone = TimeZone(identifier: "UTC")
dateFormater.dateFormat = "HH:mm:ss"
let startTime = dateFormater.date(from: "09:00:00")
dateFormater.dateFormat = "HH"
let startHours = Int(dateFormater.string(from: startTime!))
dateFormater.dateFormat = "mm"
let startMinutes = Int(dateFormater.string(from: startTime!))
dateFormater.dateFormat = "ss"
let startSeconds = Int(dateFormater.string(from: startTime!))
return startSeconds! + startMinutes! * 60 + startHours! * 3600
}