Skip to content

Commit

Permalink
Merge pull request #28 from AckeeCZ/date_formatting
Browse files Browse the repository at this point in the history
Add date formatting extensions
  • Loading branch information
olejnjak authored Aug 2, 2018
2 parents 3f249a0 + d0d4af7 commit a7b5dbe
Show file tree
Hide file tree
Showing 4 changed files with 72 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 @@ -49,6 +49,8 @@
6939359D2080AA6100EDA582 /* UIDeviceExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6939359C2080AA6100EDA582 /* UIDeviceExtensions.swift */; };
6939359F2080AB2800EDA582 /* NSMutableParagraphStyleExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6939359E2080AB2800EDA582 /* NSMutableParagraphStyleExtensions.swift */; };
693935A12080AB5A00EDA582 /* UILabelExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693935A02080AB5A00EDA582 /* UILabelExtensions.swift */; };
881A82EC21131CEC00267D2E /* DateFormatting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 881A82EB21131CEC00267D2E /* DateFormatting.swift */; };
881A82EF21131D8D00267D2E /* DateFormattingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 881A82ED21131D0F00267D2E /* DateFormattingTests.swift */; };
D2FF92C4202BA8DE00C1A129 /* NSAttributedStringExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2FF92C3202BA8DD00C1A129 /* NSAttributedStringExtensions.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -137,6 +139,8 @@
6939359C2080AA6100EDA582 /* UIDeviceExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIDeviceExtensions.swift; sourceTree = "<group>"; };
6939359E2080AB2800EDA582 /* NSMutableParagraphStyleExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSMutableParagraphStyleExtensions.swift; sourceTree = "<group>"; };
693935A02080AB5A00EDA582 /* UILabelExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UILabelExtensions.swift; sourceTree = "<group>"; };
881A82EB21131CEC00267D2E /* DateFormatting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateFormatting.swift; sourceTree = "<group>"; };
881A82ED21131D0F00267D2E /* DateFormattingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateFormattingTests.swift; sourceTree = "<group>"; };
D253FF781F0A65610079215C /* ACKategories.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ACKategories.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D2FF92C3202BA8DD00C1A129 /* NSAttributedStringExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSAttributedStringExtensions.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -213,6 +217,7 @@
6925242920D1434A00228289 /* CollectionExtensions.swift */,
69306C492029C48200BBE8F0 /* ConditionalAssignment.swift */,
6925243620D1470B00228289 /* DateExtensions.swift */,
881A82EB21131CEC00267D2E /* DateFormatting.swift */,
6925242720D142E200228289 /* DictionaryExtensions.swift */,
D2FF92C3202BA8DD00C1A129 /* NSAttributedStringExtensions.swift */,
6939359E2080AB2800EDA582 /* NSMutableParagraphStyleExtensions.swift */,
Expand Down Expand Up @@ -251,6 +256,7 @@
691F85632029DE5500DA4FAD /* Supporting files */,
6925243320D145F200228289 /* UIStackViewTests.swift */,
6925243820D1471F00228289 /* DateTests.swift */,
881A82ED21131D0F00267D2E /* DateFormattingTests.swift */,
);
name = UnitTests;
path = Tests;
Expand Down Expand Up @@ -441,6 +447,7 @@
6925243A20D1472300228289 /* DateTests.swift in Sources */,
691F858B202A00E700DA4FAD /* ConditionalAssignmentTests.swift in Sources */,
69306C872029C92B00BBE8F0 /* StringTests.swift in Sources */,
881A82EF21131D8D00267D2E /* DateFormattingTests.swift in Sources */,
69306C842029C92B00BBE8F0 /* ControlBlocksTests.swift in Sources */,
69306C852029C92B00BBE8F0 /* FoundationTests.swift in Sources */,
);
Expand All @@ -460,6 +467,7 @@
69306C522029C48300BBE8F0 /* UIColorExtensions.swift in Sources */,
69306C5B2029C48300BBE8F0 /* UIImageExtensions.swift in Sources */,
69306C592029C48300BBE8F0 /* UIControlEvents.swift in Sources */,
881A82EC21131CEC00267D2E /* DateFormatting.swift in Sources */,
69306C5A2029C48300BBE8F0 /* StringExtensions.swift in Sources */,
69306C512029C48300BBE8F0 /* UISearchBarExtensions.swift in Sources */,
6925243220D1455F00228289 /* UIStackViewExtensions.swift in Sources */,
Expand Down
7 changes: 7 additions & 0 deletions ACKategories/DateExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ extension Date {

/// Year from date
public var year: Int { return Calendar.current.component(.year, from: self) }

/// String from date formatted with given template
///
/// Uses cached date formatter, so no need to worry about performance. See `DateFormatter.cached(for template:)`
public func toString(with template: String) -> String {
return DateFormatter.cached(for: template).string(from: self)
}
}
26 changes: 26 additions & 0 deletions ACKategories/DateFormatting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// DateFormatting.swift
// Skeleton
//
// Created by Jan Misar on 02.08.18.
//

import Foundation

/// Date formatters cached for future use
fileprivate var dateFormattersCache = [String: DateFormatter]()

extension DateFormatter {

/// Returns existing DateFormatter from cache or creates the new one
public static func cached(for template: String) -> DateFormatter {
if let cachedFormatter = dateFormattersCache[template] {
return cachedFormatter
} else {
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate(template)
dateFormattersCache[template] = dateFormatter
return dateFormatter
}
}
}
31 changes: 31 additions & 0 deletions Tests/DateFormattingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// DateFormattingTests.swift
// ACKategories-Example
//
// Created by Jan Misar on 02.08.18.
// Copyright © 2018 Ackee, s.r.o. All rights reserved.
//

import XCTest
import ACKategories

final class DateFormattingTests: XCTestCase {

func testCachedDateFormatterWorks() {
let date = Date()

let df1 = DateFormatter.cached(for: "DDMMYYYY")

let df2 = DateFormatter()
df2.setLocalizedDateFormatFromTemplate("DDMMYYYY")

XCTAssertEqual(df1.string(from: date), df2.string(from: date))
}

func testCachedDateFormatter() {
let df1 = DateFormatter.cached(for: "DDMMYYYY")
let df2 = DateFormatter.cached(for: "DDMMYYYY")

XCTAssertEqual(df1, df2)
}
}

0 comments on commit a7b5dbe

Please sign in to comment.