Skip to content

Commit 42b9a30

Browse files
authored
Merge pull request #671 from JGiola/implement-setLocalizedDateFormatFromTemplate
2 parents 6cda1f3 + 3a827f4 commit 42b9a30

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Foundation/NSDateFormatter.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ open class DateFormatter : Formatter {
8080
}
8181

8282
open func setLocalizedDateFormatFromTemplate(_ dateFormatTemplate: String) {
83-
NSUnimplemented()
83+
if let format = DateFormatter.dateFormat(fromTemplate: dateFormatTemplate, options: 0, locale: locale) {
84+
dateFormat = format
85+
}
8486
}
8587

8688
private func _reset() {

TestFoundation/TestNSDateFormatter.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class TestNSDateFormatter: XCTestCase {
2727
("test_dateStyleMedium", test_dateStyleMedium),
2828
("test_dateStyleLong", test_dateStyleLong),
2929
("test_dateStyleFull", test_dateStyleFull),
30-
("test_customDateFormat", test_customDateFormat)
30+
("test_customDateFormat", test_customDateFormat),
31+
("test_setLocalizedDateFormatFromTemplate", test_setLocalizedDateFormatFromTemplate),
3132
]
3233
}
3334

@@ -277,5 +278,17 @@ class TestNSDateFormatter: XCTestCase {
277278
XCTAssertEqual(f.string(from: testDate), "11-03-2016")
278279

279280
}
280-
281+
282+
func test_setLocalizedDateFormatFromTemplate() {
283+
let locale = Locale(identifier: DEFAULT_LOCALE)
284+
let template = "EEEE MMMM d y hhmmss a zzzz"
285+
286+
let f = DateFormatter()
287+
f.locale = locale
288+
f.setLocalizedDateFormatFromTemplate(template)
289+
290+
let dateFormat = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: locale)
291+
XCTAssertEqual(f.dateFormat, dateFormat)
292+
}
293+
281294
}

0 commit comments

Comments
 (0)