Created
January 24, 2015 02:52
-
-
Save mmertsock/59a7673df76bc31b14e4 to your computer and use it in GitHub Desktop.
See http://mmertsock.github.io/2015/01/23/undocumented-nscalendar/ for more info.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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