Skip to content

Commit

Permalink
Add new Date extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
olejnjak committed Jun 13, 2018
1 parent 87915f1 commit baccda9
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ACKategories.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -110,6 +112,8 @@
6925242F20D1440800228289 /* TimeIntervalExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeIntervalExtensions.swift; sourceTree = "<group>"; };
6925243120D1455F00228289 /* UIStackViewExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIStackViewExtensions.swift; sourceTree = "<group>"; };
6925243320D145F200228289 /* UIStackViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIStackViewTests.swift; sourceTree = "<group>"; };
6925243620D1470B00228289 /* DateExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtensions.swift; sourceTree = "<group>"; };
6925243820D1471F00228289 /* DateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateTests.swift; sourceTree = "<group>"; };
69306C422029C48200BBE8F0 /* UISearchBarExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UISearchBarExtensions.swift; sourceTree = "<group>"; };
69306C432029C48200BBE8F0 /* UIColorExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIColorExtensions.swift; sourceTree = "<group>"; };
69306C442029C48200BBE8F0 /* UserDefaultsExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserDefaultsExtensions.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -248,6 +253,7 @@
693935992080A34400EDA582 /* UIViewControllerChildrenTests.swift */,
691F85632029DE5500DA4FAD /* Supporting files */,
6925243320D145F200228289 /* UIStackViewTests.swift */,
6925243820D1471F00228289 /* DateTests.swift */,
);
name = UnitTests;
path = Tests;
Expand Down Expand Up @@ -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 */,
Expand All @@ -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 */,
Expand Down
21 changes: 21 additions & 0 deletions ACKategories/DateExtensions.swift
Original file line number Diff line number Diff line change
@@ -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) }
}
1 change: 1 addition & 0 deletions ACKategories/UIStackViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
Expand Down
64 changes: 64 additions & 0 deletions Tests/DateTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit baccda9

Please sign in to comment.