我正在制作一个简单的班次记录器和薪水计算器来练习Swift,但是由于某种原因,这种方法会在除8月之外的任何日期增加五个小时。
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 fromDate和toDate驻留在我的Shift类中,如下所示:
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
}
}
}
The toDate and fromDate comes from the first method in my post, getShift(). They are dates collected from a date picker wheel. Code for that shown here. from and toDate in the first method are from my NSManagedObject class, saved from the below code.
@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)
The problem is not with September - the value for August24th also has 5 hours added as well. Rather, the problem is where you have half-hour periods.
That in turn us because of this section of code :
let hours = length/60
let minutes = (length%60) * (5/3)
let billable = hours + minutes/10
Hours is OK (or rather would be if 'length' was an int rather than a double) - but what on earth is the 5/3 factor for minutes ? And then you add minutes/10 to hours ??? Eg. If You have 50 minutes, do you REALLY want to add 5 to the hours ;)
Rather, you could do either of the following : Since 'length' is the number of minutes as a double, then simply :
let billable = length/60.0
另外,如果您想通过分钟来计算,
let hours = length/60
let minutes = (length%60)
let billable = hours + minutes/60.0
我真的建议长度为整数:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句