-1

Original problem is encountered during my post, testing and code in the link below https://stackoverflow.com/questions/27339072/working-with-nsdate-components-in-swift/30822018#30822018

Here is the minimal test-case

  1. I am creating a date that is offset from a particular date (self in this case as I do it in NSDate() extension) using NSCalendar.dateByAddingUnit(..., value:x, ...)
  2. I then get a difference "fromDate" (created date or self) "toDate" (current time which is NSDate()) using NSCalendar.components(..., fromDate, toDate, ....).minute
  3. If x>0, the answer I get in the step above is x-1. If x<0, the answer I get is x. x is defined in step 1 above.
  4. This has the potential to cause bugs in applications that filter events using human readable attributes such as all events in past 3 hrs since 180 minutes is not three hours due to this behavior.
  5. This is not a bug in step 1 since the date prints correctly as seen in printout at the end of the post.

Is there an option in NSCalendar.components that I am missing or is this a bug?

Here is the code.

//
//  NSDateTest.swift
//  NSDateTestCase
//
//  Created by Jitendra Kulkarni on 6/13/15.
//

import Foundation
public extension NSDate {

        func xMins(x:Int) -> NSDate {

        return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitMinute, value: x, toDate: self, options: nil)!
    }

     var hoursFromToday: Int{

        var fromDate: NSDate = self
        var toDate: NSDate = NSDate()
        var sign: Int = -1
        (fromDate,toDate, sign) = self.sanitizedDates()

        return (sign * NSCalendar.currentCalendar().components(.CalendarUnitHour, fromDate: fromDate, toDate: toDate, options: nil).hour)
    }


     var minsFromToday: Int{


        var fromDate: NSDate = self
        var toDate: NSDate = NSDate()
        var sign: Int = 1
        (fromDate,toDate) = self.sanitizedDates()


        return ( sign * NSCalendar.currentCalendar().components(.CalendarUnitMinute, fromDate: fromDate, toDate: toDate, options: nil).minute)
    }


    //Date Comparisons
    //https://stackoverflow.com/questions/26198526/nsdate-comparison-using-swift


    func isGreaterThanDate(dateToCompare : NSDate) -> Bool
    {
        //Declare Variables
        var isGreater = false

        //Compare Values
        if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
        {
            isGreater = true
        }

        //Return Result
        return isGreater
    }


    // Date printing conversions



       var timeDayMonthYear: String {
        let dateMonthYearFormatter: NSDateFormatter = NSDateFormatter()
        let currentLocale: NSLocale = NSLocale.currentLocale()
        let dateMonthYearFormatString: NSString! = NSDateFormatter.dateFormatFromTemplate("EdMMMyyyy' ' HH':'mm",options: 0, locale: currentLocale)
        dateMonthYearFormatter.dateFormat = dateMonthYearFormatString as! String
        return dateMonthYearFormatter.stringFromDate(self)
    }



    private  func sanitizedDates() -> (fromDate: NSDate, toDate: NSDate) {
        var toDate: NSDate = self
        var fromDate: NSDate = NSDate()
        if(toDate.isGreaterThanDate(fromDate)) {
         //   toDate = toDate.xMins(1)  // Uncomment this to make the answer work
        }

             return (fromDate,toDate)

    }

    static func test (mins: Int = 0) {

        NSLog("****************** Start Testing of NSDate **************************")
        NSLog("Inputs: mins: \(mins)")
        var today = NSDate()
        NSLog("Today is: \(today.timeDayMonthYear)")

              var minsFromToday = today.xMins(mins)
        NSLog("** Date created \(mins) minutes from today is: \(minsFromToday.timeDayMonthYear)")
        NSLog("minsFromToday returns: \(minsFromToday.minsFromToday)");

        NSLog("hoursFromToday returns: \(minsFromToday.hoursFromToday)");


        NSLog("__________________ End Testing of NSDate    _________________________")

    }
}

Here is the result of NSDate(60) followed by NSDate(-60). Look for *ERROR. If you were remove comments from the one commented line in sanitizedDate(), you will get the expected result.

2015-06-13 14:56:44.877 NSDateTestCase[23136:1150501] ****************** Start Testing of NSDate **************************
2015-06-13 14:56:44.878 NSDateTestCase[23136:1150501] Inputs: mins: 60
2015-06-13 14:56:44.882 NSDateTestCase[23136:1150501] Today is: Sat, Jun 13, 2015, 14:56
2015-06-13 14:56:44.883 NSDateTestCase[23136:1150501] ** Date created 60 minutes from today is: Sat, Jun 13, 2015, 15:56
2015-06-13 14:56:44.883 NSDateTestCase[23136:1150501] minsFromToday returns: 59  // *ERROR should be 60
2015-06-13 14:56:44.884 NSDateTestCase[23136:1150501] hoursFromToday returns: 0 // *ERROR should be 1
2015-06-13 14:56:44.884 NSDateTestCase[23136:1150501] __________________ End Testing of NSDate    _________________________
2015-06-13 14:56:44.884 NSDateTestCase[23136:1150501] ****************** Start Testing of NSDate **************************
2015-06-13 14:56:44.884 NSDateTestCase[23136:1150501] Inputs: mins: -60
2015-06-13 14:56:44.885 NSDateTestCase[23136:1150501] Today is: Sat, Jun 13, 2015, 14:56
2015-06-13 14:56:44.885 NSDateTestCase[23136:1150501] ** Date created -60 minutes from today is: Sat, Jun 13, 2015, 13:56
2015-06-13 14:56:44.886 NSDateTestCase[23136:1150501] minsFromToday returns: -60
2015-06-13 14:56:44.886 NSDateTestCase[23136:1150501] hoursFromToday returns: -1
2015-06-13 14:56:44.893 NSDateTestCase[23136:1150501] __________________ End Testing of NSDate    _________________________

Here is the code based on Kurt's answer below. Notice that xFromToday API has to be changed to get an input about "today":

//
//  NSDateTest.swift
//  NSDateTestCase
//
//  Created by Jitendra Kulkarni on 6/13/15.

//

import Foundation
public extension NSDate {

        func xMins(x:Int) -> NSDate {

        return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitMinute, value: x, toDate: self, options: nil)!
    }



    func hoursFromToday(today: NSDate) -> Int {
        var fromDate: NSDate = self
        var toDate: NSDate = today

        return (-NSCalendar.currentCalendar().components(.CalendarUnitHour, fromDate: fromDate, toDate: toDate, options: nil).hour)

    }

    func minsFromToday(today: NSDate) -> Int {
        var fromDate: NSDate = self
        var toDate: NSDate = today

        return (-NSCalendar.currentCalendar().components(.CalendarUnitMinute, fromDate: fromDate, toDate: toDate, options: nil).minute)

    }


    //Date Comparisions
    //https://stackoverflow.com/questions/26198526/nsdate-comparison-using-swift


    func isGreaterThanDate(dateToCompare : NSDate) -> Bool
    {
        //Declare Variables
        var isGreater = false

        //Compare Values
        if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
        {
            isGreater = true
        }

        //Return Result
        return isGreater
    }


    // Date printing converstions



       var timeDayMonthYear: String {
        let dateMonthYearFormatter: NSDateFormatter = NSDateFormatter()
        let currentLocale: NSLocale = NSLocale.currentLocale()
        let dateMonthYearFormatString: NSString! = NSDateFormatter.dateFormatFromTemplate("EdMMMyyyy' ' HH':'mm",options: 0, locale: currentLocale)
        dateMonthYearFormatter.dateFormat = dateMonthYearFormatString as! String
        return dateMonthYearFormatter.stringFromDate(self)
    }



    static func test (mins: Int = 0) {

        NSLog("****************** Start Testing of NSDate **************************")
        NSLog("Inputs: mins: \(mins)")
        var today = NSDate()
        NSLog("Today is: \(today.timeDayMonthYear)")

              var minsFromToday = today.xMins(mins)
        NSLog("** Date created \(mins) minutes from today is: \(minsFromToday.timeDayMonthYear)")
        //NSLog("minsFromToday returns: \(minsFromToday.minsFromToday)")
        NSLog("minsFromToday(today) returns: \(minsFromToday.minsFromToday(today))");

       //NSLog("hoursFromToday returns: \(minsFromToday.hoursFromToday)")
        NSLog("hoursFromToday(today) returns: \(minsFromToday.hoursFromToday(today))");


        NSLog("__________________ End Testing of NSDate    _________________________")

    }

Here is the output produced:

2015-06-13 16:23:55.362 NSDateTestCase[23758:1196619] ****************** Start Testing of NSDate **************************
2015-06-13 16:23:55.363 NSDateTestCase[23758:1196619] Inputs: mins: 60
2015-06-13 16:23:55.366 NSDateTestCase[23758:1196619] Today is: Sat, Jun 13, 2015, 16:23
2015-06-13 16:23:55.367 NSDateTestCase[23758:1196619] ** Date created 60 minutes from today is: Sat, Jun 13, 2015, 17:23
2015-06-13 16:23:55.367 NSDateTestCase[23758:1196619] minsFromToday(today) returns: 60  // All good now.
2015-06-13 16:23:55.367 NSDateTestCase[23758:1196619] hoursFromToday(today) returns: 1 // All good now.
2015-06-13 16:23:55.367 NSDateTestCase[23758:1196619] __________________ End Testing of NSDate    _________________________
2015-06-13 16:23:55.368 NSDateTestCase[23758:1196619] ****************** Start Testing of NSDate **************************
2015-06-13 16:23:55.368 NSDateTestCase[23758:1196619] Inputs: mins: -60
2015-06-13 16:23:55.368 NSDateTestCase[23758:1196619] Today is: Sat, Jun 13, 2015, 16:23
2015-06-13 16:23:55.369 NSDateTestCase[23758:1196619] ** Date created -60 minutes from today is: Sat, Jun 13, 2015, 15:23
2015-06-13 16:23:55.370 NSDateTestCase[23758:1196619] minsFromToday(today) returns: -60
2015-06-13 16:23:55.380 NSDateTestCase[23758:1196619] hoursFromToday(today) returns: -1
2015-06-13 16:23:55.381 NSDateTestCase[23758:1196619] __________________ End Testing of NSDate    _________________________
Community
  • 1
  • 1
  • 2
    Please add the relevant information to the question itself. What is the input data, what output do you get and what do you expect? – Martin R Jun 13 '15 at 19:07
  • And how can we suggest a better method if you don't tell us your exact test cases in your answer (due to "confidentiality reasons")? Or did I overlook something? – Martin R Jun 13 '15 at 19:14
  • 2
    Even reading the linked post (the relevant parts of which should absolutely be included here), I'm not really clear on what the problem is. Please [edit] your question to explain better; sample input and output would probably be helpful here. – jscs Jun 13 '15 at 19:15
  • I edited the original post to create the minimal test case. I am not sure why I got down voted saying there is no research effort. I had extensive code, tests etc in my post on the thread that I referred to as well as here. – Jitendra Kulkarni Jun 13 '15 at 22:38

1 Answers1

1

You are calling NSDate() in 4 different places in your code.

Each one will return the current date and time when that code executes, so they will differ by fractions of a second.

I suspect you want to use one consistent value for "today" in your whole calculation. Call NSDate() once, at the start of your code, store it in a variable, and refer to that variable consistently.

Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
  • That exactly was it. Thanks a lot, it prevented future bugs and simplified my code to boot. I was not comfortable that I had to twiddle the inputs to make it work, great that it is sorted out now. I will post the accepted code here. – Jitendra Kulkarni Jun 13 '15 at 23:26