Skip to content

Commit c38806b

Browse files
committed
Merge pull request #145 from thii/nsdate-descriptionWithLocale
[NSDate] Fix descriptionWithLocale always returns the default description
2 parents c74b405 + 306e853 commit c38806b

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

Foundation/NSDate.swift

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,51 @@ public class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
7272
public func encodeWithCoder(aCoder: NSCoder) {
7373

7474
}
75-
75+
76+
/**
77+
A string representation of the date object (read-only).
78+
79+
The representation is useful for debugging only.
80+
81+
There are a number of options to acquire a formatted string for a date
82+
including: date formatters (see
83+
[NSDateFormatter](//apple_ref/occ/cl/NSDateFormatter) and
84+
[Data Formatting Guide](//apple_ref/doc/uid/10000029i)),
85+
and the `NSDate` methods `descriptionWithLocale:`,
86+
`dateWithCalendarFormat:timeZone:`, and
87+
`descriptionWithCalendarFormat:timeZone:locale:`.
88+
*/
7689
public override var description: String {
7790
get {
78-
return CFCopyDescription(_cfObject)._swiftObject
91+
let dateFormatterRef = CFDateFormatterCreate(kCFAllocatorSystemDefault, nil, kCFDateFormatterFullStyle, kCFDateFormatterFullStyle)
92+
let timeZone = CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorSystemDefault, 0.0)
93+
CFDateFormatterSetProperty(dateFormatterRef, kCFDateFormatterTimeZoneKey, timeZone)
94+
CFDateFormatterSetFormat(dateFormatterRef, "uuuu-MM-dd HH:mm:ss '+0000'"._cfObject)
95+
96+
return CFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, dateFormatterRef, _cfObject)._swiftObject
7997
}
8098
}
81-
99+
100+
/**
101+
Returns a string representation of the receiver using the given locale.
102+
103+
- Parameter locale: An `NSLocale` object.
104+
105+
If you pass `nil`, `NSDate` formats the date in the same way as the
106+
`description` property.
107+
108+
- Returns: A string representation of the receiver, using the given locale,
109+
or if the locale argument is `nil`, in the international format
110+
`YYYY-MM-DD HH:MM:SS ±HHMM`, where `±HHMM` represents the time zone
111+
offset in hours and minutes from UTC (for example,
112+
"2001-03-24 10:45:32 +0600")
113+
*/
82114
public func descriptionWithLocale(locale: AnyObject?) -> String {
83-
return description
115+
guard let aLocale = locale else { return description }
116+
let dateFormatterRef = CFDateFormatterCreate(kCFAllocatorSystemDefault, (aLocale as! NSLocale)._cfObject, kCFDateFormatterFullStyle, kCFDateFormatterFullStyle)
117+
CFDateFormatterSetProperty(dateFormatterRef, kCFDateFormatterTimeZoneKey, CFTimeZoneCopySystem())
118+
119+
return CFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, dateFormatterRef, _cfObject)._swiftObject
84120
}
85121

86122
override internal var _cfTypeID: CFTypeID {

TestFoundation/TestNSDate.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class TestNSDate : XCTestCase {
2727
("test_InitTimeIntervalSince1970", test_InitTimeIntervalSince1970),
2828
("test_InitTimeIntervalSinceSinceDate", test_InitTimeIntervalSinceSinceDate),
2929
("test_TimeIntervalSinceSinceDate", test_TimeIntervalSinceSinceDate),
30+
("test_descriptionWithLocale", test_descriptionWithLocale),
3031
("test_DistantFuture", test_DistantFuture),
3132
("test_DistantPast", test_DistantPast),
3233
("test_DateByAddingTimeInterval", test_DateByAddingTimeInterval),
@@ -41,6 +42,12 @@ class TestNSDate : XCTestCase {
4142
let d = NSDate()
4243
XCTAssertNotNil(d)
4344
}
45+
46+
func test_descriptionWithLocale() {
47+
let d = NSDate(timeIntervalSince1970: 0)
48+
XCTAssertEqual(d.descriptionWithLocale(nil), "1970-01-01 00:00:00 +0000")
49+
XCTAssertNotNil(d.descriptionWithLocale(NSLocale(localeIdentifier: "ja_JP")))
50+
}
4451

4552
func test_InitTimeIntervalSince1970() {
4653
let ti: NSTimeInterval = 1

0 commit comments

Comments
 (0)