我的帖子,测试和代码在下面的链接中遇到了原始问题 https://stackoverflow.com/questions/27339072/working-with-nsdate-components-in-swift/30822018#30822018
这是最小的测试用例
我缺少的NSCalendar.components中是否有选项或者这是一个错误?
这是代码。
//
// 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 _________________________")
}
}
这是NSDate(60)的结果,然后是NSDate(-60)。寻找*错误。如果您从sanitizedDate()中的一条注释行中删除注释,您将获得预期结果。
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 _________________________
以下是基于Kurt的答案的代码。请注意,必须更改xFromToday API以获取有关"今天"的输入:
//
// 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 _________________________")
}
以下是产生的输出:
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 _________________________
答案 0 :(得分:1)
您在代码的4个不同位置呼叫NSDate()
。
当代码执行时,每个人都会返回的当前日期和时间,因此它们会在几分之一秒内有所不同。
我怀疑你想在整个计算中使用“今天”的一个一致值。在代码开头调用NSDate()
一次,将其存储在变量中,并一致地引用该变量。