Skip to content

Commit 77f821e

Browse files
authored
Merge pull request #1200 from ddunn2/formatterWork
2 parents cffdab3 + 9d248ed commit 77f821e

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

Foundation/DateFormatter.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,23 @@ open class DateFormatter : Formatter {
142142
}
143143
}
144144

145-
open var dateStyle: Style = .none { willSet { _dateFormat = nil; _reset() } }
145+
open var dateStyle: Style = .none {
146+
willSet {
147+
_dateFormat = nil
148+
}
149+
didSet {
150+
_dateFormat = CFDateFormatterGetFormat(_cfObject)._swiftObject
151+
}
152+
}
146153

147-
open var timeStyle: Style = .none { willSet { _dateFormat = nil; _reset() } }
154+
open var timeStyle: Style = .none {
155+
willSet {
156+
_dateFormat = nil
157+
}
158+
didSet {
159+
_dateFormat = CFDateFormatterGetFormat(_cfObject)._swiftObject
160+
}
161+
}
148162

149163
/*@NSCopying*/ open var locale: Locale! = .current { willSet { _reset() } }
150164

TestFoundation/TestDateFormatter.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TestDateFormatter: XCTestCase {
2929
("test_dateStyleFull", test_dateStyleFull),
3030
("test_customDateFormat", test_customDateFormat),
3131
("test_setLocalizedDateFormatFromTemplate", test_setLocalizedDateFormatFromTemplate),
32+
("test_dateFormatString", test_dateFormatString),
3233
]
3334
}
3435

@@ -295,4 +296,45 @@ class TestDateFormatter: XCTestCase {
295296
XCTAssertEqual(f.dateFormat, dateFormat)
296297
}
297298

299+
func test_dateFormatString() {
300+
let f = DateFormatter()
301+
f.timeZone = TimeZone(abbreviation: DEFAULT_TIMEZONE)
302+
303+
//.full cases have been commented out as they're not working correctly on Linux
304+
let formats: [String: (DateFormatter.Style, DateFormatter.Style)] = [
305+
"": (.none, .none),
306+
"h:mm a": (.none, .short),
307+
"h:mm:ss a": (.none, .medium),
308+
"h:mm:ss a z": (.none, .long),
309+
// "h:mm:ss a zzzz": (.none, .full),
310+
"M/d/yy": (.short, .none),
311+
"M/d/yy, h:mm a": (.short, .short),
312+
"M/d/yy, h:mm:ss a": (.short, .medium),
313+
"M/d/yy, h:mm:ss a z": (.short, .long),
314+
// "M/d/yy, h:mm:ss a zzzz": (.short, .full),
315+
"MMM d, y": (.medium, .none),
316+
//These tests currently fail, there seems to be a difference in behavior in the CoreFoundation methods called to construct the format strings.
317+
// "MMM d, y 'at' h:mm a": (.medium, .short),
318+
// "MMM d, y 'at' h:mm:ss a": (.medium, .medium),
319+
// "MMM d, y 'at' h:mm:ss a z": (.medium, .long),
320+
// "MMM d, y 'at' h:mm:ss a zzzz": (.medium, .full),
321+
"MMMM d, y": (.long, .none),
322+
"MMMM d, y 'at' h:mm a": (.long, .short),
323+
"MMMM d, y 'at' h:mm:ss a": (.long, .medium),
324+
"MMMM d, y 'at' h:mm:ss a z": (.long, .long),
325+
// "MMMM d, y 'at' h:mm:ss a zzzz": (.long, .full),
326+
// "EEEE, MMMM d, y": (.full, .none),
327+
// "EEEE, MMMM d, y 'at' h:mm a": (.full, .short),
328+
// "EEEE, MMMM d, y 'at' h:mm:ss a": (.full, .medium),
329+
// "EEEE, MMMM d, y 'at' h:mm:ss a z": (.full, .long),
330+
// "EEEE, MMMM d, y 'at' h:mm:ss a zzzz": (.full, .full),
331+
]
332+
333+
for (dateFormat, styles) in formats {
334+
f.dateStyle = styles.0
335+
f.timeStyle = styles.1
336+
337+
XCTAssertEqual(f.dateFormat, dateFormat)
338+
}
339+
}
298340
}

0 commit comments

Comments
 (0)