Skip to content

Instantly share code, notes, and snippets.

@simonkim
Last active August 10, 2018 12:15
Show Gist options
  • Save simonkim/5c22b9c63be705b8de215a7e7b0e1794 to your computer and use it in GitHub Desktop.
Save simonkim/5c22b9c63be705b8de215a7e7b0e1794 to your computer and use it in GitHub Desktop.
NSDate comparison between original and converted through components and a calendar
//
// DateConversionThroughCalendarTests.m
// TestOnNSDateDateTests
//
// Created by Simon Kim on 10/08/2018.
// Copyright © 2018 SeoGiwon. All rights reserved.
//
#import <XCTest/XCTest.h>
@interface CalendarWash: NSObject
- (NSDate *) convertDate:(NSDate *) date calendarIdentifier:(NSCalendarIdentifier) calendarIdentifier unitFlags:(NSCalendarUnit) unitFlags;
@end
@implementation CalendarWash
- (NSDate *) convertDate:(NSDate *) date calendarIdentifier:(NSCalendarIdentifier) calendarIdentifier unitFlags:(NSCalendarUnit) unitFlags
{
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:calendarIdentifier];
NSDateComponents *components = [calendar components:unitFlags fromDate:date];
return [calendar dateFromComponents:components];
}
@end
@interface DateConversionThroughCalendarTests : XCTestCase
@end
@implementation DateConversionThroughCalendarTests
- (void)setUp {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testDayWeekday {
CalendarWash *wash = [[CalendarWash alloc] init];
NSDate *date = [NSDate date];
NSDate *greg = [wash convertDate:date calendarIdentifier:NSCalendarIdentifierGregorian unitFlags:(NSCalendarUnitDay | NSCalendarUnitWeekday)];
NSDate *budd = [wash convertDate:date calendarIdentifier:NSCalendarIdentifierBuddhist unitFlags:(NSCalendarUnitDay | NSCalendarUnitWeekday)];
double seconds = floor(date.timeIntervalSince1970);
double secondsGreg = floor(greg.timeIntervalSince1970);
double secondsBudd = floor(budd.timeIntervalSince1970);
NSLog(@"date: %.1f - %@", seconds, date);
NSLog(@"greg: %.1f - %@", secondsGreg, greg);
NSLog(@"budd: %.1f - %@", secondsBudd, budd);
XCTAssert(seconds == secondsGreg);
XCTAssert(seconds == secondsBudd);
XCTAssert(secondsGreg == secondsBudd);
}
- (void)testDateTime {
CalendarWash *wash = [[CalendarWash alloc] init];
NSCalendarUnit unitFlags = (NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond);
NSDate *date = [NSDate date];
NSDate *greg = [wash convertDate:date calendarIdentifier:NSCalendarIdentifierGregorian unitFlags:unitFlags];
NSDate *budd = [wash convertDate:date calendarIdentifier:NSCalendarIdentifierBuddhist unitFlags:unitFlags];
double seconds = floor(date.timeIntervalSince1970);
double secondsGreg = floor(greg.timeIntervalSince1970);
double secondsBudd = floor(budd.timeIntervalSince1970);
NSLog(@"date: %.1f - %@", seconds, date);
NSLog(@"greg: %.1f - %@", secondsGreg, greg);
NSLog(@"budd: %.1f - %@", secondsBudd, budd);
XCTAssert(seconds == secondsGreg);
XCTAssert(seconds == secondsBudd);
XCTAssert(secondsGreg == secondsBudd);
}
@end
Test: testDateTime in DateConversionThroughCalendarTests passed on iPhone X
Test: testDayWeekday in DateConversionThroughCalendarTests failed on iPhone X
Assertions: ((seconds == secondsGreg) is true) failed
File: DateConversionThroughCalendarTests.m:53
Assertions: ((seconds == secondsBudd) is true) failed
File: DateConversionThroughCalendarTests.m:54
Assertions: ((secondsGreg == secondsBudd) is true) failed
File: DateConversionThroughCalendarTests.m:55
UI Test Activity:
Assertion Failure: DateConversionThroughCalendarTests.m:53: ((seconds == secondsGreg) is true) failed
UI Test Activity:
Assertion Failure: DateConversionThroughCalendarTests.m:54: ((seconds == secondsBudd) is true) failed
UI Test Activity:
Assertion Failure: DateConversionThroughCalendarTests.m:55: ((secondsGreg == secondsBudd) is true) failed
Test: testDateTime() in TestOnNSDateDateTests passed on iPhone X
Test: testDayWeekday() in TestOnNSDateDateTests failed on iPhone X
Assertions: XCTAssertTrue failed -
File: TestOnNSDateDateTests.swift:42
Assertions: XCTAssertTrue failed -
File: TestOnNSDateDateTests.swift:43
Assertions: XCTAssertTrue failed -
File: TestOnNSDateDateTests.swift:44
UI Test Activity:
Assertion Failure: TestOnNSDateDateTests.swift:42: XCTAssertTrue failed -
UI Test Activity:
Assertion Failure: TestOnNSDateDateTests.swift:43: XCTAssertTrue failed -
UI Test Activity:
Assertion Failure: TestOnNSDateDateTests.swift:44: XCTAssertTrue failed -
Test: testYearMonthDay() in TestOnNSDateDateTests failed on iPhone X
Assertions: XCTAssertTrue failed -
File: TestOnNSDateDateTests.swift:59
Assertions: XCTAssertTrue failed -
File: TestOnNSDateDateTests.swift:60
UI Test Activity:
Assertion Failure: TestOnNSDateDateTests.swift:59: XCTAssertTrue failed -
UI Test Activity:
Assertion Failure: TestOnNSDateDateTests.swift:60: XCTAssertTrue failed -
//
// TestOnNSDateDateTests.swift
// TestOnNSDateDateTests
//
// Created by Simon Kim on 10/08/2018.
// Copyright © 2018 SeoGiwon. All rights reserved.
//
import XCTest
class CalendarWash {
func convert(date: Date, using calendarIdentifier: NSCalendar.Identifier, unitFlags: NSCalendar.Unit) -> Date {
let calendar = NSCalendar(identifier: calendarIdentifier)!
let components = calendar.components(unitFlags, from: date)
return calendar.date(from: components)!
}
}
class TestOnNSDateDateTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testDayWeekday() {
let wash = CalendarWash()
let date = Date()
let greg = wash.convert(date: date, using: .gregorian, unitFlags: [.day, .weekday])
let budd = wash.convert(date: date, using: .buddhist, unitFlags: [.day, .weekday])
let seconds = floor(date.timeIntervalSince1970)
let secondsGreg = floor(greg.timeIntervalSince1970)
let secondsBudd = floor(budd.timeIntervalSince1970)
print("date: \(seconds) - \(date)")
print("greg: \(secondsGreg) - \(greg)")
print("budd: \(secondsBudd) - \(budd)")
XCTAssert(seconds == secondsGreg)
XCTAssert(seconds == secondsBudd)
XCTAssert(secondsGreg == secondsBudd)
}
func testYearMonthDay() {
let wash = CalendarWash()
let date = Date()
let greg = wash.convert(date: date, using: .gregorian, unitFlags: [.day, .year, .month])
let budd = wash.convert(date: date, using: .buddhist, unitFlags: [.day, .year, .month])
let seconds = floor(date.timeIntervalSince1970)
let secondsGreg = floor(greg.timeIntervalSince1970)
let secondsBudd = floor(budd.timeIntervalSince1970)
print("date: \(seconds) - \(date)")
print("greg: \(secondsGreg) - \(greg)")
print("budd: \(secondsBudd) - \(budd)")
XCTAssert(seconds == secondsGreg)
XCTAssert(seconds == secondsBudd)
XCTAssert(secondsGreg == secondsBudd)
}
func testDateTime() {
let wash = CalendarWash()
let date = Date()
let greg = wash.convert(date: date, using: .gregorian, unitFlags: [.day, .year, .month, .hour, .minute, .second])
let budd = wash.convert(date: date, using: .buddhist, unitFlags: [.day, .year, .month, .hour, .minute, .second])
let seconds = floor(date.timeIntervalSince1970)
let secondsGreg = floor(greg.timeIntervalSince1970)
let secondsBudd = floor(budd.timeIntervalSince1970)
print("date: \(seconds) - \(date)")
print("greg: \(secondsGreg) - \(greg)")
print("budd: \(secondsBudd) - \(budd)")
XCTAssert(seconds == secondsGreg)
XCTAssert(seconds == secondsBudd)
XCTAssert(secondsGreg == secondsBudd)
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment