Skip to content

SR-5837: Fix dateFormat bug in DateFormatter #1200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Foundation/DateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,23 @@ open class DateFormatter : Formatter {
}
}

open var dateStyle: Style = .none { willSet { _dateFormat = nil; _reset() } }
open var dateStyle: Style = .none {
willSet {
_dateFormat = nil
}
didSet {
_dateFormat = CFDateFormatterGetFormat(_cfObject)._swiftObject
}
}

open var timeStyle: Style = .none { willSet { _dateFormat = nil; _reset() } }
open var timeStyle: Style = .none {
willSet {
_dateFormat = nil
}
didSet {
_dateFormat = CFDateFormatterGetFormat(_cfObject)._swiftObject
}
}

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

Expand Down
42 changes: 42 additions & 0 deletions TestFoundation/TestDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TestDateFormatter: XCTestCase {
("test_dateStyleFull", test_dateStyleFull),
("test_customDateFormat", test_customDateFormat),
("test_setLocalizedDateFormatFromTemplate", test_setLocalizedDateFormatFromTemplate),
("test_dateFormatString", test_dateFormatString),
]
}

Expand Down Expand Up @@ -295,4 +296,45 @@ class TestDateFormatter: XCTestCase {
XCTAssertEqual(f.dateFormat, dateFormat)
}

func test_dateFormatString() {
let f = DateFormatter()
f.timeZone = TimeZone(abbreviation: DEFAULT_TIMEZONE)

//.full cases have been commented out as they're not working correctly on Linux
let formats: [String: (DateFormatter.Style, DateFormatter.Style)] = [
"": (.none, .none),
"h:mm a": (.none, .short),
"h:mm:ss a": (.none, .medium),
"h:mm:ss a z": (.none, .long),
// "h:mm:ss a zzzz": (.none, .full),
"M/d/yy": (.short, .none),
"M/d/yy, h:mm a": (.short, .short),
"M/d/yy, h:mm:ss a": (.short, .medium),
"M/d/yy, h:mm:ss a z": (.short, .long),
// "M/d/yy, h:mm:ss a zzzz": (.short, .full),
"MMM d, y": (.medium, .none),
//These tests currently fail, there seems to be a difference in behavior in the CoreFoundation methods called to construct the format strings.
// "MMM d, y 'at' h:mm a": (.medium, .short),
// "MMM d, y 'at' h:mm:ss a": (.medium, .medium),
// "MMM d, y 'at' h:mm:ss a z": (.medium, .long),
// "MMM d, y 'at' h:mm:ss a zzzz": (.medium, .full),
"MMMM d, y": (.long, .none),
"MMMM d, y 'at' h:mm a": (.long, .short),
"MMMM d, y 'at' h:mm:ss a": (.long, .medium),
"MMMM d, y 'at' h:mm:ss a z": (.long, .long),
// "MMMM d, y 'at' h:mm:ss a zzzz": (.long, .full),
// "EEEE, MMMM d, y": (.full, .none),
// "EEEE, MMMM d, y 'at' h:mm a": (.full, .short),
// "EEEE, MMMM d, y 'at' h:mm:ss a": (.full, .medium),
// "EEEE, MMMM d, y 'at' h:mm:ss a z": (.full, .long),
// "EEEE, MMMM d, y 'at' h:mm:ss a zzzz": (.full, .full),
]

for (dateFormat, styles) in formats {
f.dateStyle = styles.0
f.timeStyle = styles.1

XCTAssertEqual(f.dateFormat, dateFormat)
}
}
}