Skip to content

Commit

Permalink
[NSDate] Fix description and implement descriptionWithLocale
Browse files Browse the repository at this point in the history
  • Loading branch information
thii committed Dec 16, 2015
1 parent fbd3466 commit 9e465d0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
44 changes: 40 additions & 4 deletions Foundation/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,51 @@ public class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
public func encodeWithCoder(aCoder: NSCoder) {

}


/**
A string representation of the date object (read-only).
The representation is useful for debugging only.
There are a number of options to acquire a formatted string for a date
including: date formatters (see
[NSDateFormatter](//apple_ref/occ/cl/NSDateFormatter) and
[Data Formatting Guide](//apple_ref/doc/uid/10000029i)),
and the `NSDate` methods `descriptionWithLocale:`,
`dateWithCalendarFormat:timeZone:`, and
`descriptionWithCalendarFormat:timeZone:locale:`.
*/
public override var description: String {
get {
return CFCopyDescription(_cfObject)._swiftObject
let dateFormatterRef = CFDateFormatterCreate(kCFAllocatorSystemDefault, nil, kCFDateFormatterFullStyle, kCFDateFormatterFullStyle)
let timeZone = CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorSystemDefault, 0.0)
CFDateFormatterSetProperty(dateFormatterRef, kCFDateFormatterTimeZoneKey, timeZone)
CFDateFormatterSetFormat(dateFormatterRef, "uuuu-MM-dd HH:mm:ss '+0000'"._cfObject)

return CFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, dateFormatterRef, _cfObject)._swiftObject
}
}


/**
Returns a string representation of the receiver using the given locale.
- Parameter locale: An NSLocale object.
If you pass `nil`, NSDate formats the date in the same way as the
`description` property.
- Returns: A string representation of the receiver, using the given locale,
or if the locale argument is `nil`, in the international format
`YYYY-MM-DD HH:MM:SS ±HHMM`, where `±HHMM` represents the time zone
offset in hours and minutes from UTC (for example,
"2001-03-24 10:45:32 +0600")
*/
public func descriptionWithLocale(locale: AnyObject?) -> String {
return description
guard let aLocale = locale else { return description }
let dateFormatterRef = CFDateFormatterCreate(kCFAllocatorSystemDefault, (aLocale as! NSLocale)._cfObject, kCFDateFormatterFullStyle, kCFDateFormatterFullStyle)
CFDateFormatterSetProperty(dateFormatterRef, kCFDateFormatterTimeZoneKey, CFTimeZoneCopySystem())

return CFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, dateFormatterRef, _cfObject)._swiftObject
}

override internal var _cfTypeID: CFTypeID {
Expand Down
7 changes: 7 additions & 0 deletions TestFoundation/TestNSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TestNSDate : XCTestCase {
("test_InitTimeIntervalSince1970", test_InitTimeIntervalSince1970),
("test_InitTimeIntervalSinceSinceDate", test_InitTimeIntervalSinceSinceDate),
("test_TimeIntervalSinceSinceDate", test_TimeIntervalSinceSinceDate),
("descriptionWithLocale", descriptionWithLocale),
("test_DistantFuture", test_DistantFuture),
("test_DistantPast", test_DistantPast),
("test_DateByAddingTimeInterval", test_DateByAddingTimeInterval),
Expand All @@ -41,6 +42,12 @@ class TestNSDate : XCTestCase {
let d = NSDate()
XCTAssertNotNil(d)
}

func descriptionWithLocale() {
let d = NSDate(timeIntervalSince1970: 0)
XCTAssertEqual(d.descriptionWithLocale(nil), "1970-01-01 00:00:00 +0000")
XCTAssertNotNil(d.descriptionWithLocale(NSLocale(localeIdentifier: "ja_JP")))
}

func test_InitTimeIntervalSince1970() {
let ti: NSTimeInterval = 1
Expand Down

0 comments on commit 9e465d0

Please sign in to comment.