我正在做一個簡單的記錄器和薪水計算器來練習Swift,但由於某種原因,這種方法除了八月份之外的任何日期都會增加五個小時。爲什麼9月份的時間差加法增加了5個小時?
func getShift() -> Shift{
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd w W HH:mm"
formatter.timeZone = NSTimeZone.localTimeZone()
let from:NSDate = formatter.dateFromString(self.fromDate)!
let to:NSDate = formatter.dateFromString(self.toDate)!
let length:Double = to.timeIntervalSinceDate(from) as Double/60
let newShift = Shift(from:from,to:to,length:length)
return newShift
}
我用的是長度可變的這種方法來計算工資
func calcSalary(ub: Bool){
if (ub){
} else {
let hours = length/60
let minutes = (length%60) * (5/3)
let billable = hours + minutes/10
self.salary = billable*hour
println("Got paid \(self.salary) for \(billable) hours times \(hour) from \(fromDate) to \(toDate)")
//Got paid 792.05 for 7.0 hours times 113.15 from 2014-08-21 13:00:00 +0000 to 2014-08-21 20:00:00 +0000
//Got paid 1018.35 for 9.0 hours times 113.15 from 2014-08-23 08:00:00 +0000 to 2014-08-23 17:00:00 +0000
//Got paid 961.775 for 8.5 hours times 113.15 from 2014-08-24 11:15:00 +0000 to 2014-08-24 14:45:00 +0000
//Got paid 1527.525 for 13.5 hours times 113.15 from 2014-09-04 13:00:00 +0000 to 2014-09-04 21:30:00 +0000
//Got paid 1074.925 for 9.5 hours times 113.15 from 2014-09-05 12:00:00 +0000 to 2014-09-05 16:30:00 +0000
}
}
正如你可以在底部的兩個日期看,它增加了五小時。什麼是更奇怪的,我在我的界面中使用相同的shift.length變量,並且它是正確的。
我在使用日期(和Swift)方面真的很糟糕,任何人都可以在我的方法中發現錯誤嗎?
EDIT FROM日期和TODATE駐留在我的移位類,如下所示:
class Shift {
var fromDate : NSDate
var toDate : NSDate
var length : Double //In Minutes
var salary : Double = Double()
let hour = 113.15
let etter18hverdag = 22
let etter21hverdag = 45
let helligdag = 90
let helgEtter13 = 45
let helgEtter16 = 90 //HUSK AT PAUSE FINNES
init (from : NSDate, to : NSDate, length:Double){
self.fromDate = from
self.toDate = to
self.length = length
}
func calcSalary(ub: Bool){
if (ub){
} else {
let hours = length/60
let minutes = (length%60) * (5/3)
let billable = hours + minutes/10
self.salary = billable*hour
println("Got paid \(self.salary) for \(billable) hours times \(hour) from \(fromDate) to \(toDate)")
//Got paid 792.05 for 7.0 hours times 113.15 from 2014-08-21 13:00:00 +0000 to 2014-08-21 20:00:00 +0000
//Got paid 1018.35 for 9.0 hours times 113.15 from 2014-08-23 08:00:00 +0000 to 2014-08-23 17:00:00 +0000
//Got paid 961.775 for 8.5 hours times 113.15 from 2014-08-24 11:15:00 +0000 to 2014-08-24 14:45:00 +0000
//Got paid 1527.525 for 13.5 hours times 113.15 from 2014-09-04 13:00:00 +0000 to 2014-09-04 21:30:00 +0000
//Got paid 1074.925 for 9.5 hours times 113.15 from 2014-09-05 12:00:00 +0000 to 2014-09-05 16:30:00 +0000
}
}
}
的TODATE和FROM日期來自第一方法在我的崗位,getShift()。它們是從日期選取器輪收集的日期。代碼如下所示。 from和toDate的第一個方法是從我的NSManagedObject類中,從下面的代碼保存。
@IBAction func donePressed(sender: AnyObject) {
let dateStringFormatter = NSDateFormatter()
//ÅR-MÅNED-DAG (2011-12-29) UKE I ÅR UKE I MND (27 3)
dateStringFormatter.dateFormat = "yyyy-MM-dd w W HH:mm"
dateStringFormatter.locale = NSLocale.currentLocale()
dateStringFormatter.timeZone = NSTimeZone.localTimeZone()
var appDel : AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
let newShift = NSEntityDescription.insertNewObjectForEntityForName("Shifts", inManagedObjectContext: context) as NSManagedObject
newShift.setValue("\(dateStringFormatter.stringFromDate(fromDate))", forKey: "fromDate")
newShift.setValue("\(dateStringFormatter.stringFromDate(toDate))", forKey: "toDate")
context.save(nil)
你有什麼self.fromD吃和self.toDate變量? – paiv 2014-08-27 13:49:23
@paiv看看更新後的帖子! – 2014-08-27 13:59:31