From baccda91586719829be9b89b0f32689f4c443d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Olejn=C3=ADk?= Date: Wed, 13 Jun 2018 14:42:24 +0200 Subject: [PATCH] Add new `Date` extensions --- ACKategories.xcodeproj/project.pbxproj | 8 +++ ACKategories/DateExtensions.swift | 21 ++++++++ ACKategories/UIStackViewExtensions.swift | 1 + Tests/DateTests.swift | 64 ++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 ACKategories/DateExtensions.swift create mode 100644 Tests/DateTests.swift diff --git a/ACKategories.xcodeproj/project.pbxproj b/ACKategories.xcodeproj/project.pbxproj index 7641babf..518a2e36 100644 --- a/ACKategories.xcodeproj/project.pbxproj +++ b/ACKategories.xcodeproj/project.pbxproj @@ -26,6 +26,8 @@ 6925243020D1440800228289 /* TimeIntervalExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6925242F20D1440800228289 /* TimeIntervalExtensions.swift */; }; 6925243220D1455F00228289 /* UIStackViewExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6925243120D1455F00228289 /* UIStackViewExtensions.swift */; }; 6925243520D145F600228289 /* UIStackViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6925243320D145F200228289 /* UIStackViewTests.swift */; }; + 6925243720D1470B00228289 /* DateExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6925243620D1470B00228289 /* DateExtensions.swift */; }; + 6925243A20D1472300228289 /* DateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6925243820D1471F00228289 /* DateTests.swift */; }; 69306C512029C48300BBE8F0 /* UISearchBarExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69306C422029C48200BBE8F0 /* UISearchBarExtensions.swift */; }; 69306C522029C48300BBE8F0 /* UIColorExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69306C432029C48200BBE8F0 /* UIColorExtensions.swift */; }; 69306C532029C48300BBE8F0 /* UserDefaultsExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69306C442029C48200BBE8F0 /* UserDefaultsExtensions.swift */; }; @@ -110,6 +112,8 @@ 6925242F20D1440800228289 /* TimeIntervalExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeIntervalExtensions.swift; sourceTree = ""; }; 6925243120D1455F00228289 /* UIStackViewExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIStackViewExtensions.swift; sourceTree = ""; }; 6925243320D145F200228289 /* UIStackViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIStackViewTests.swift; sourceTree = ""; }; + 6925243620D1470B00228289 /* DateExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtensions.swift; sourceTree = ""; }; + 6925243820D1471F00228289 /* DateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateTests.swift; sourceTree = ""; }; 69306C422029C48200BBE8F0 /* UISearchBarExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UISearchBarExtensions.swift; sourceTree = ""; }; 69306C432029C48200BBE8F0 /* UIColorExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIColorExtensions.swift; sourceTree = ""; }; 69306C442029C48200BBE8F0 /* UserDefaultsExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserDefaultsExtensions.swift; sourceTree = ""; }; @@ -210,6 +214,7 @@ 6925242B20D1439300228289 /* BundleExtensions.swift */, 6925242920D1434A00228289 /* CollectionExtensions.swift */, 69306C492029C48200BBE8F0 /* ConditionalAssignment.swift */, + 6925243620D1470B00228289 /* DateExtensions.swift */, 6925242720D142E200228289 /* DictionaryExtensions.swift */, D2FF92C3202BA8DD00C1A129 /* NSAttributedStringExtensions.swift */, 6939359E2080AB2800EDA582 /* NSMutableParagraphStyleExtensions.swift */, @@ -248,6 +253,7 @@ 693935992080A34400EDA582 /* UIViewControllerChildrenTests.swift */, 691F85632029DE5500DA4FAD /* Supporting files */, 6925243320D145F200228289 /* UIStackViewTests.swift */, + 6925243820D1471F00228289 /* DateTests.swift */, ); name = UnitTests; path = Tests; @@ -435,6 +441,7 @@ 69306C832029C92B00BBE8F0 /* ColorTests.swift in Sources */, 6925243520D145F600228289 /* UIStackViewTests.swift in Sources */, 6939359B2080A34C00EDA582 /* UIViewControllerChildrenTests.swift in Sources */, + 6925243A20D1472300228289 /* DateTests.swift in Sources */, 691F858B202A00E700DA4FAD /* ConditionalAssignmentTests.swift in Sources */, 69306C872029C92B00BBE8F0 /* StringTests.swift in Sources */, 69306C842029C92B00BBE8F0 /* ControlBlocksTests.swift in Sources */, @@ -451,6 +458,7 @@ 6939359D2080AA6100EDA582 /* UIDeviceExtensions.swift in Sources */, 6939359820809F3D00EDA582 /* UIViewController+Children.swift in Sources */, 69306C5C2029C48300BBE8F0 /* UIViewController+SafeAreaCompat.swift in Sources */, + 6925243720D1470B00228289 /* DateExtensions.swift in Sources */, 6925242C20D1439300228289 /* BundleExtensions.swift in Sources */, 69306C522029C48300BBE8F0 /* UIColorExtensions.swift in Sources */, 69306C5B2029C48300BBE8F0 /* UIImageExtensions.swift in Sources */, diff --git a/ACKategories/DateExtensions.swift b/ACKategories/DateExtensions.swift new file mode 100644 index 00000000..a17522d5 --- /dev/null +++ b/ACKategories/DateExtensions.swift @@ -0,0 +1,21 @@ +import Foundation + +extension Date { + /// Seconds from date + public var second: Int { return Calendar.current.component(.second, from: self) } + + /// Minutes from date + public var minute: Int { return Calendar.current.component(.minute, from: self) } + + /// Hours from date in 24-hour format + public var hour: Int { return Calendar.current.component(.hour, from: self) } + + /// Days from date + public var day: Int { return Calendar.current.component(.day, from: self) } + + /// Months from date + public var month: Int { return Calendar.current.component(.month, from: self) } + + /// Year from date + public var year: Int { return Calendar.current.component(.year, from: self) } +} diff --git a/ACKategories/UIStackViewExtensions.swift b/ACKategories/UIStackViewExtensions.swift index 8a5ae82d..bbfa35fd 100644 --- a/ACKategories/UIStackViewExtensions.swift +++ b/ACKategories/UIStackViewExtensions.swift @@ -2,6 +2,7 @@ import UIKit @available(iOS 9.0, *) extension UIStackView { + /// Remove all arranged subviews public func removeAllArrangedSubviews() { arrangedSubviews.forEach { removeArrangedSubview($0); $0.removeFromSuperview() } } diff --git a/Tests/DateTests.swift b/Tests/DateTests.swift new file mode 100644 index 00000000..ba39592d --- /dev/null +++ b/Tests/DateTests.swift @@ -0,0 +1,64 @@ +// +// DateTests.swift +// ACKategories +// +// Created by Jakub Olejník on 13/06/2018. +// Copyright © 2018 Ackee, s.r.o. All rights reserved. +// + +import XCTest +import ACKategories + +final class DateTests: XCTestCase { + + var date: Date! + + override func setUp() { + super.setUp() + + date = Date() + } + + func testCorrectSecond() { + let df = DateFormatter() + df.dateFormat = "s" + + XCTAssertEqual(Int(df.string(from: date)), date.second) + } + + func testCorrectMinute() { + let df = DateFormatter() + df.dateFormat = "m" + + XCTAssertEqual(Int(df.string(from: date)), date.minute) + } + + func testCorrectHour() { + let df = DateFormatter() + df.dateFormat = "H" + + XCTAssertEqual(Int(df.string(from: date)), date.hour) + } + + func testCorrectDay() { + let df = DateFormatter() + df.dateFormat = "d" + + XCTAssertEqual(Int(df.string(from: date)), date.day) + } + + func testCorrectMonth() { + let df = DateFormatter() + df.dateFormat = "M" + + XCTAssertEqual(Int(df.string(from: date)), date.month) + } + + func testCorrectYear() { + let df = DateFormatter() + df.dateFormat = "YYYY" + + XCTAssertEqual(Int(df.string(from: date)), date.year) + } +} +