gpt4 book ai didi

ios - 为什么 9 月的月份会在我的时差方法中增加五个小时?

转载 作者:行者123 更新时间:2023-11-28 07:16:41 26 4
gpt4 key购买 nike

我正在制作一个简单的轮类记录器和薪水计算器来练习 Swift,但出于某种原因,这种方法会使除八月以外的任何日期增加五个小时。

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),有人能找出我方法中的错误吗?


编辑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

}
}
}

toDate 和 fromDate 来 self 文章中的第一个方法 getShift()。它们是从日期选择器轮中收集的日期。此处显示的代码。第一个方法中的 from 和 toDate 来 self 的 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)

最佳答案

问题不在于 9 月 - 8 月 24 日的值也增加了 5 小时。相反,问题在于你有半小时的时段。

这反过来又是因为这段代码:

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

小时是可以的(或者更确切地说,如果“长度”是一个整数而不是一个 double )——但是分钟的 5/3 因子到底是什么?然后你将分钟/10 添加到小时???例如。如果你有 50 分钟,你真的想在小时数上增加 5 分钟吗 ;)

相反,您可以执行以下任一操作:由于 'length' 是 double 的分钟数,因此只需:

    let billable = length/60.0

或者,如果你想按分钟计算,

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

我真的建议将长度设为整数 :)

关于ios - 为什么 9 月的月份会在我的时差方法中增加五个小时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25528589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com