See http://mmertsock.github.io/2015/01/23/undocumented-nscalendar/ for more info.
import XCTest | |
func NSCalendarForTesting() -> NSCalendar { | |
let calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)! | |
calendar.timeZone = NSTimeZone(name: "America/New_York")! | |
calendar.firstWeekday = 2 // Monday | |
return calendar | |
} | |
class NSCalendarTests: XCTestCase { | |
let calendar = NSCalendarForTesting() | |
var date20140703noon: NSDate! | |
var baseComps: NSDateComponents! | |
override func setUp() { | |
super.setUp() | |
baseComps = NSDateComponents() | |
baseComps.year = 2014 | |
baseComps.month = 7 | |
baseComps.day = 3 | |
baseComps.hour = 12 | |
baseComps.minute = 0 | |
baseComps.second = 0 | |
date20140703noon = calendar.dateFromComponents(baseComps)! | |
} | |
func testStartOfDayForDate() { | |
let newDate = calendar.startOfDayForDate(date20140703noon) | |
let comps = comparisonComponents(newDate) | |
XCTAssertEqual(comps.year, baseComps.year, "same year") | |
XCTAssertEqual(comps.month, baseComps.month, "same month") | |
XCTAssertEqual(comps.day, baseComps.day, "same day") | |
XCTAssertEqual(comps.hour, 0, "midnight (hour)") | |
XCTAssertEqual(comps.minute, 0, "midnight (minutes)") | |
XCTAssertEqual(comps.second, 0, "midnight (seconds)") | |
} | |
func testStartOfDayForDateEdgeCases() { | |
let c = NSDateComponents() | |
c.year = 2014 | |
c.month = 12 | |
c.day = 31 | |
c.hour = 23 | |
c.minute = 59 | |
c.second = 59 | |
let baseDate = calendar.dateFromComponents(c)! | |
var tomorrow = calendar.startOfNextDayForDate(baseDate) | |
var comps = comparisonComponents(tomorrow) | |
XCTAssertEqual(comps.year, c.year + 1, "Next year") | |
XCTAssertEqual(comps.month, 1, "January") | |
XCTAssertEqual(comps.day, 1, "First") | |
XCTAssertEqual(comps.hour, 0, "Midnight (hour)") | |
XCTAssertEqual(comps.minute, 0, "Midnight (minute)") | |
// DST: calendar day is less than 24 hours | |
c.year = 2015 | |
c.month = 3 | |
c.day = 8 | |
c.hour = 12 | |
c.minute = 0 | |
c.second = 0 | |
let noonOnDSTChangeDay = calendar.dateFromComponents(c)! | |
let morningOnDSTChangeDay = calendar.startOfDayForDate(noonOnDSTChangeDay) | |
comps = comparisonComponents(morningOnDSTChangeDay) | |
XCTAssertEqual(comps.year, 2015, "Same year") | |
XCTAssertEqual(comps.month, 3, "Same month") | |
XCTAssertEqual(comps.day, 8, "Same day") | |
XCTAssertEqual(comps.hour, 0, "Midnight (hour)") | |
XCTAssertEqual(comps.minute, 0, "Midnight (minute)") | |
c.hour = 1 | |
let justBeforeDSTChange = calendar.dateFromComponents(c)! | |
tomorrow = calendar.startOfNextDayForDate(justBeforeDSTChange) | |
comps = comparisonComponents(tomorrow) | |
XCTAssertEqual(comps.year, 2015, "Same year") | |
XCTAssertEqual(comps.month, 3, "Same month") | |
XCTAssertEqual(comps.day, 9, "Next day") | |
XCTAssertEqual(comps.hour, 0, "Midnight (hour)") | |
XCTAssertEqual(comps.minute, 0, "Midnight (minute)") | |
} | |
private func comparisonComponents(date: NSDate) -> NSDateComponents { | |
return calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay | .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate: date) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment