Skip to content

Instantly share code, notes, and snippets.

@jaderfeijo
Last active March 13, 2018 10:45
Show Gist options
  • Save jaderfeijo/eb95b71f5819391e99f72668e7e1353a to your computer and use it in GitHub Desktop.
Save jaderfeijo/eb95b71f5819391e99f72668e7e1353a to your computer and use it in GitHub Desktop.
Date+Components.swift
//
// 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)
}
}
//
// 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