为什么九月一个月会增加我的时差方法五个小时?

奥斯卡·佩兰(Oscar Apeland)

我正在制作一个简单的班次记录器和薪水计算器来练习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)
racraman

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用jQuery增加一个月

来自分类Dev

将可变日期增加一个月

来自分类Dev

一个月递增/递减的安全方法

来自分类Dev

Python:一个月的第三个星期五

来自分类Dev

如何标记上周五或最后一天或一个月

来自分类Dev

一个月的每小时数据差距

来自分类Dev

JavaScript中的Date()问题-它增加了一个月?

来自分类Dev

新的Date(...)增加了一个月

来自分类Dev

猫鼬:将mongo集合的日期字段增加一个月

来自分类Dev

在日期中增加一个月会将时区从GMT更改为BST

来自分类Dev

如何在Excel日期值中增加一个月?

来自分类Dev

SQL-获得一个月的新事件计数并仅显示增加的值

来自分类Dev

如何找到一个月的最后一个星期三之后的星期五?

来自分类Dev

Qt 5.5 QDateTime :: addSec函数如果增加12小时(43200秒),则在一个月的最后一天返回了错误的值

来自分类Dev

前一个月的查询/表达

来自分类Dev

as3-加一个月

来自分类Dev

从即日起一个月

来自分类Dev

Rails:如何遍历一个月?

来自分类Dev

计算一个月的剩余天数

来自分类Dev

从Datetime.Today减去一个月

来自分类Dev

SimpleDateFormat在00时减少一个月

来自分类Dev

SAS从变量循环一个月

来自分类Dev

日期加上一个月的PHP

来自分类Dev

每周一个月的分组日期

来自分类Dev

Incremnet日期由一个月

来自分类Dev

在R中将日期偏移一个月

来自分类Dev

在MongoDB数组中查询一个月

来自分类Dev

PHP前一个月

来自分类Dev

最近一个月的子集数据