-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from AckeeCZ/date_formatting
Add date formatting extensions
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
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 | ||
} | ||
} | ||
} |
This file contains 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
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) | ||
} | ||
} |