2014-08-27 31 views
1

我正在做一個簡單的記錄器和薪水計算器來練習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變量,並且它是正確的。 enter image description here

我在使用日期(和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) 
+0

你有什麼self.fromD吃和self.toDate變量? – paiv 2014-08-27 13:49:23

+0

@paiv看看更新後的帖子! – 2014-08-27 13:59:31

回答

1

問題不在於9月--8月24日的值還添加了5小時。相反,問題在於你有半小時的時間。

這反過來我們,因爲這部分代碼的:

let hours = length/60 
    let minutes = (length%60) * (5/3) 
    let billable = hours + minutes/10 

小時OK(或者更確切地說是,如果「長」是一個整數,而不是雙) - 但究竟什麼是5/3分鐘的因素?然後你添加分鐘/ 10到幾個小時?例如。如果你有50分鐘,你真的想要添加5個小時;)

相反,你可以做以下任一: 由於'長度'是分鐘數爲雙倍,那麼只需:

let billable = length/60.0 

或者,如果你想通過分鐘來計算,

let hours = length/60 
    let minutes = (length%60) 
    let billable = hours + minutes/60.0 

而且我真的建議其長度爲整數:)

+0

MacOS X和iOS _always_使用雙含義秒的存儲時間。如果存儲時間的含義與秒不同,則應始終在單位中使用變量名稱。像lengthInMinutes或billableHours。 – gnasher729 2014-08-27 14:37:09

+0

AHHHH當然!我有多愚蠢,我在這裏亂搞,在我去吃午飯之前把時間轉換爲10。非常感謝您在我不得不面對更多問題之前挽救我的大腦細胞。 – 2014-08-27 14:48:00