Last active
March 13, 2018 10:45
-
-
Save jaderfeijo/eb95b71f5819391e99f72668e7e1353a to your computer and use it in GitHub Desktop.
Date+Components.swift
This file contains hidden or 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
// | |
// Date+Components.swift | |
// Created by: Jader Feijo | |
// | |
import Foundation | |
extension Date { | |
var hour: Int { | |
return (Calendar.current as NSCalendar).component(.hour, from: self) | |
} | |
var minute: Int { | |
return (Calendar.current as NSCalendar).component(.minute, from: self) | |
} | |
var second: Int { | |
return (Calendar.current as NSCalendar).component(.second, from: self) | |
} | |
var year: Int { | |
return (Calendar.current as NSCalendar).component(.year, from: self) | |
} | |
var month: Int { | |
return (Calendar.current as NSCalendar).component(.month, from: self) | |
} | |
var day: Int { | |
return (Calendar.current as NSCalendar).component(.day, from: self) | |
} | |
var weekday: Int { | |
return (Calendar.current as NSCalendar).component(.weekday, from: self) | |
} | |
} |
This file contains hidden or 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
// | |
// DateComponentsTests.swift | |
// Created by: Jader Feijo | |
// | |
import XCTest | |
class NSDateComponentsTests: XCTestCase { | |
// 2016-10-14 16:00:36 UTC | |
let date = Date(timeIntervalSinceReferenceDate: 498153636.50913697) | |
let oldTimeZone = TimeZone.current | |
override func setUp() { | |
NSTimeZone.default = TimeZone(abbreviation: "UTC")! | |
} | |
override func tearDown() { | |
NSTimeZone.default = oldTimeZone | |
} | |
func testHour() { | |
XCTAssert(date.hour == 16) | |
} | |
func testMinute() { | |
XCTAssert(date.minute == 0) | |
} | |
func testSecond() { | |
XCTAssert(date.second == 36) | |
} | |
func testYear() { | |
XCTAssert(date.year == 2016) | |
} | |
func testMonth() { | |
XCTAssert(date.month == 10) | |
} | |
func testDay() { | |
XCTAssert(date.day == 14) | |
} | |
func testWeekday() { | |
XCTAssert(date.weekday == 6) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment