Skip to content

Commit 47fa18a

Browse files
committed
Merge pull request #274 from tfrank64/dateFormatter_tests
Initial NSDateFormatter tests
2 parents 311f2dd + 8b7dd2a commit 47fa18a

File tree

4 files changed

+223
-16
lines changed

4 files changed

+223
-16
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
2EBE67A51C77BF0E006583D5 /* TestNSDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBE67A31C77BF05006583D5 /* TestNSDateFormatter.swift */; };
1011
528776141BF2629700CB0090 /* FoundationErrors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 522C253A1BF16E1600804FC6 /* FoundationErrors.swift */; };
1112
528776191BF27D9500CB0090 /* Test.plist in Resources */ = {isa = PBXBuildFile; fileRef = 528776181BF27D9500CB0090 /* Test.plist */; };
1213
555683BD1C1250E70041D4C6 /* TestNSUserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 555683BC1C1250E70041D4C6 /* TestNSUserDefaults.swift */; };
@@ -381,6 +382,7 @@
381382

382383
/* Begin PBXFileReference section */
383384
22B9C1E01C165D7A00DECFF9 /* TestNSDate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSDate.swift; sourceTree = "<group>"; };
385+
2EBE67A31C77BF05006583D5 /* TestNSDateFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSDateFormatter.swift; sourceTree = "<group>"; };
384386
400E22641C1A4E58007C5933 /* TestNSProcessInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSProcessInfo.swift; sourceTree = "<group>"; };
385387
4AE109261C17CCBF007367B5 /* TestNSIndexPath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSIndexPath.swift; sourceTree = "<group>"; };
386388
4DC1D07F1C12EEEF00B5948A /* TestNSPipe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSPipe.swift; sourceTree = "<group>"; };
@@ -1199,6 +1201,7 @@
11991201
A5A34B551C18C85D00FD972B /* TestNSByteCountFormatter.swift */,
12001202
D3047AEB1C38BC3300295652 /* TestNSValue.swift */,
12011203
E19E17DB1C2225930023AF4D /* TestNSXMLDocument.swift */,
1204+
2EBE67A31C77BF05006583D5 /* TestNSDateFormatter.swift */,
12021205
);
12031206
name = Tests;
12041207
sourceTree = "<group>";
@@ -1956,6 +1959,7 @@
19561959
5B13B3481C582D4C00651CE2 /* TestNSTimer.swift in Sources */,
19571960
5B13B32D1C582D4C00651CE2 /* TestNSDictionary.swift in Sources */,
19581961
5B13B3261C582D4C00651CE2 /* TestNSAffineTransform.swift in Sources */,
1962+
2EBE67A51C77BF0E006583D5 /* TestNSDateFormatter.swift in Sources */,
19591963
5B13B3291C582D4C00651CE2 /* TestNSCalendar.swift in Sources */,
19601964
5B13B34F1C582D4C00651CE2 /* TestNSXMLParser.swift in Sources */,
19611965
5B13B32F1C582D4C00651CE2 /* TestNSGeometry.swift in Sources */,

Foundation/NSDateFormatter.swift

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ public class NSDateFormatter : NSFormatter {
2121
let dateStyle = CFDateFormatterStyle(self.dateStyle.rawValue)
2222
let timeStyle = CFDateFormatterStyle(self.timeStyle.rawValue)
2323
#endif
24+
2425
let obj = CFDateFormatterCreate(kCFAllocatorSystemDefault, locale._cfObject, dateStyle, timeStyle)
25-
// TODO: Set up attributes here
26-
if calendar != nil {
27-
CFDateFormatterSetProperty(obj, kCFDateFormatterCalendar, calendar._cfObject)
28-
}
29-
CFDateFormatterSetProperty(obj, kCFDateFormatterTimeZone, timeZone._cfObject)
26+
_setFormatterAttributes(obj)
3027
if let dateFormat = _dateFormat {
3128
CFDateFormatterSetFormat(obj, dateFormat._cfObject)
3229
}
@@ -44,7 +41,7 @@ public class NSDateFormatter : NSFormatter {
4441
super.init(coder: coder)
4542
}
4643

47-
public var formattingContext: NSFormattingContext = .Unknown
44+
public var formattingContext: NSFormattingContext = .Unknown // default is NSFormattingContextUnknown
4845

4946
public func objectValue(string: String, range rangep: UnsafeMutablePointer<NSRange>) throws -> AnyObject? { NSUnimplemented() }
5047

@@ -90,6 +87,42 @@ public class NSDateFormatter : NSFormatter {
9087
__cfObject = nil
9188
}
9289

90+
internal func _setFormatterAttributes(formatter: CFDateFormatter) {
91+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterIsLenient, value: lenient._cfObject)
92+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterTimeZone, value: timeZone?._cfObject)
93+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterCalendarName, value: calendar?.calendarIdentifier._cfObject)
94+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterTwoDigitStartDate, value: twoDigitStartDate?._cfObject)
95+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterDefaultDate, value: defaultDate?._cfObject)
96+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterCalendar, value: calendar?._cfObject)
97+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterEraSymbols, value: eraSymbols?._cfObject)
98+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterMonthSymbols, value: monthSymbols?._cfObject)
99+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterShortMonthSymbols, value: shortMonthSymbols?._cfObject)
100+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterWeekdaySymbols, value: weekdaySymbols?._cfObject)
101+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterShortWeekdaySymbols, value: shortWeekdaySymbols?._cfObject)
102+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterAMSymbol, value: AMSymbol?._cfObject)
103+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterPMSymbol, value: PMSymbol?._cfObject)
104+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterLongEraSymbols, value: longEraSymbols?._cfObject)
105+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterVeryShortMonthSymbols, value: veryShortMonthSymbols?._cfObject)
106+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterStandaloneMonthSymbols, value: standaloneMonthSymbols?._cfObject)
107+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterShortStandaloneMonthSymbols, value: shortStandaloneMonthSymbols?._cfObject)
108+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterVeryShortStandaloneMonthSymbols, value: veryShortStandaloneMonthSymbols?._cfObject)
109+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterVeryShortWeekdaySymbols, value: veryShortWeekdaySymbols?._cfObject)
110+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterStandaloneWeekdaySymbols, value: standaloneWeekdaySymbols?._cfObject)
111+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterShortStandaloneWeekdaySymbols, value: shortStandaloneWeekdaySymbols?._cfObject)
112+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterVeryShortStandaloneWeekdaySymbols, value: veryShortStandaloneWeekdaySymbols?._cfObject)
113+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterQuarterSymbols, value: quarterSymbols?._cfObject)
114+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterShortQuarterSymbols, value: shortQuarterSymbols?._cfObject)
115+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterStandaloneQuarterSymbols, value: standaloneQuarterSymbols?._cfObject)
116+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterShortStandaloneQuarterSymbols, value: shortStandaloneQuarterSymbols?._cfObject)
117+
_setFormatterAttribute(formatter, attributeName: kCFDateFormatterGregorianStartDate, value: gregorianStartDate?._cfObject)
118+
}
119+
120+
internal func _setFormatterAttribute(formatter: CFDateFormatter, attributeName: CFString, value: AnyObject?) {
121+
if let value = value {
122+
CFDateFormatterSetProperty(formatter, attributeName, value)
123+
}
124+
}
125+
93126
public var dateFormat: String! {
94127
get {
95128
guard let format = _dateFormat else {
@@ -107,16 +140,7 @@ public class NSDateFormatter : NSFormatter {
107140

108141
public var timeStyle: NSDateFormatterStyle = .NoStyle { willSet { _reset() } }
109142

110-
internal var _locale: NSLocale = NSLocale.currentLocale()
111-
/*@NSCopying*/ public var locale: NSLocale! {
112-
get {
113-
return _locale
114-
}
115-
set {
116-
_reset()
117-
_locale = newValue
118-
}
119-
}
143+
/*@NSCopying*/ public var locale: NSLocale! = .currentLocale() { willSet { _reset() } }
120144

121145
public var generatesCalendarDates = false { willSet { _reset() } }
122146

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
11+
import Foundation
12+
import XCTest
13+
#else
14+
import SwiftFoundation
15+
import SwiftXCTest
16+
#endif
17+
18+
class TestNSDateFormatter: XCTestCase {
19+
20+
let DEFAULT_LOCALE = "en_US"
21+
let DEFAULT_TIMEZONE = "GMT"
22+
23+
static var allTests : [(String, TestNSDateFormatter -> () throws -> Void)] {
24+
return [
25+
("test_BasicConstruction", test_BasicConstruction),
26+
// ("test_customDateFormat", test_customDateFormat),
27+
("test_dateStyleShort", test_dateStyleShort),
28+
("test_dateStyleMedium", test_dateStyleMedium),
29+
("test_dateStyleLong", test_dateStyleLong),
30+
("test_dateStyleFull", test_dateStyleFull)
31+
]
32+
}
33+
34+
func test_BasicConstruction() {
35+
let f = NSDateFormatter()
36+
XCTAssertNotNil(f)
37+
}
38+
39+
func test_customDateFormat() {
40+
let dateFormatter = NSDateFormatter()
41+
dateFormatter.dateFormat = String("dd-MM-yyyy")
42+
let dateStr = dateFormatter.stringFromDate(NSDate())
43+
44+
print("With dateFormat '\(dateFormatter.dateFormat)': '\(dateStr)'")
45+
46+
}
47+
48+
// ShortStyle
49+
// locale stringFromDate example
50+
// ------ -------------- --------
51+
// en_US M/d/yy 12/25/15
52+
func test_dateStyleShort() {
53+
54+
let timestamps = [
55+
-31536000 : "1/1/69, 12:00 AM" , 0.0 : "1/1/70, 12:00 AM", 31536000 : "1/1/71, 12:00 AM",
56+
2145916800 : "1/1/38, 12:00 AM", 1456272000 : "2/24/16, 12:00 AM", 1456358399 : "2/24/16, 11:59 PM",
57+
1452574638 : "1/12/16, 4:57 AM", 1455685038 : "2/17/16, 4:57 AM", 1458622638 : "3/22/16, 4:57 AM",
58+
1459745838 : "4/4/16, 4:57 AM", 1462597038 : "5/7/16, 4:57 AM", 1465534638 : "6/10/16, 4:57 AM",
59+
1469854638 : "7/30/16, 4:57 AM", 1470718638 : "8/9/16, 4:57 AM", 1473915438 : "9/15/16, 4:57 AM",
60+
1477285038 : "10/24/16, 4:57 AM", 1478062638 : "11/2/16, 4:57 AM", 1482641838 : "12/25/16, 4:57 AM"
61+
]
62+
63+
let f = NSDateFormatter()
64+
f.dateStyle = .ShortStyle
65+
f.timeStyle = .ShortStyle
66+
67+
// ensure tests give consistent results by setting specific timeZone and locale
68+
f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE)
69+
f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE)
70+
71+
for (timestamp, stringResult) in timestamps {
72+
73+
let testDate = NSDate(timeIntervalSince1970: timestamp)
74+
let sf = f.stringFromDate(testDate)
75+
76+
XCTAssertEqual(sf, stringResult)
77+
}
78+
79+
}
80+
81+
// MediumStyle
82+
// locale stringFromDate example
83+
// ------ -------------- ------------
84+
// en_US MMM d, y Dec 25, 2015
85+
func test_dateStyleMedium() {
86+
87+
let timestamps = [
88+
-31536000 : "Jan 1, 1969, 12:00:00 AM" , 0.0 : "Jan 1, 1970, 12:00:00 AM", 31536000 : "Jan 1, 1971, 12:00:00 AM",
89+
2145916800 : "Jan 1, 2038, 12:00:00 AM", 1456272000 : "Feb 24, 2016, 12:00:00 AM", 1456358399 : "Feb 24, 2016, 11:59:59 PM",
90+
1452574638 : "Jan 12, 2016, 4:57:18 AM", 1455685038 : "Feb 17, 2016, 4:57:18 AM", 1458622638 : "Mar 22, 2016, 4:57:18 AM",
91+
1459745838 : "Apr 4, 2016, 4:57:18 AM", 1462597038 : "May 7, 2016, 4:57:18 AM", 1465534638 : "Jun 10, 2016, 4:57:18 AM",
92+
1469854638 : "Jul 30, 2016, 4:57:18 AM", 1470718638 : "Aug 9, 2016, 4:57:18 AM", 1473915438 : "Sep 15, 2016, 4:57:18 AM",
93+
1477285038 : "Oct 24, 2016, 4:57:18 AM", 1478062638 : "Nov 2, 2016, 4:57:18 AM", 1482641838 : "Dec 25, 2016, 4:57:18 AM"
94+
]
95+
96+
let f = NSDateFormatter()
97+
f.dateStyle = .MediumStyle
98+
f.timeStyle = .MediumStyle
99+
f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE)
100+
f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE)
101+
102+
for (timestamp, stringResult) in timestamps {
103+
104+
let testDate = NSDate(timeIntervalSince1970: timestamp)
105+
let sf = f.stringFromDate(testDate)
106+
107+
XCTAssertEqual(sf, stringResult)
108+
}
109+
110+
}
111+
112+
113+
// LongStyle
114+
// locale stringFromDate example
115+
// ------ -------------- -----------------
116+
// en_US MMMM d, y December 25, 2015
117+
func test_dateStyleLong() {
118+
119+
let timestamps = [
120+
-31536000 : "January 1, 1969 at 12:00:00 AM GMT" , 0.0 : "January 1, 1970 at 12:00:00 AM GMT", 31536000 : "January 1, 1971 at 12:00:00 AM GMT",
121+
2145916800 : "January 1, 2038 at 12:00:00 AM GMT", 1456272000 : "February 24, 2016 at 12:00:00 AM GMT", 1456358399 : "February 24, 2016 at 11:59:59 PM GMT",
122+
1452574638 : "January 12, 2016 at 4:57:18 AM GMT", 1455685038 : "February 17, 2016 at 4:57:18 AM GMT", 1458622638 : "March 22, 2016 at 4:57:18 AM GMT",
123+
1459745838 : "April 4, 2016 at 4:57:18 AM GMT", 1462597038 : "May 7, 2016 at 4:57:18 AM GMT", 1465534638 : "June 10, 2016 at 4:57:18 AM GMT",
124+
1469854638 : "July 30, 2016 at 4:57:18 AM GMT", 1470718638 : "August 9, 2016 at 4:57:18 AM GMT", 1473915438 : "September 15, 2016 at 4:57:18 AM GMT",
125+
1477285038 : "October 24, 2016 at 4:57:18 AM GMT", 1478062638 : "November 2, 2016 at 4:57:18 AM GMT", 1482641838 : "December 25, 2016 at 4:57:18 AM GMT"
126+
]
127+
128+
let f = NSDateFormatter()
129+
f.dateStyle = .LongStyle
130+
f.timeStyle = .LongStyle
131+
f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE)
132+
f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE)
133+
134+
for (timestamp, stringResult) in timestamps {
135+
136+
let testDate = NSDate(timeIntervalSince1970: timestamp)
137+
let sf = f.stringFromDate(testDate)
138+
139+
XCTAssertEqual(sf, stringResult)
140+
}
141+
142+
}
143+
144+
// FullStyle
145+
// locale stringFromDate example
146+
// ------ -------------- -------------------------
147+
// en_US EEEE, MMMM d, y Friday, December 25, 2015
148+
func test_dateStyleFull() {
149+
150+
let timestamps = [
151+
-31536000 : "Wednesday, January 1, 1969 at 12:00:00 AM GMT" , 0.0 : "Thursday, January 1, 1970 at 12:00:00 AM GMT",
152+
31536000 : "Friday, January 1, 1971 at 12:00:00 AM GMT", 2145916800 : "Friday, January 1, 2038 at 12:00:00 AM GMT",
153+
1456272000 : "Wednesday, February 24, 2016 at 12:00:00 AM GMT", 1456358399 : "Wednesday, February 24, 2016 at 11:59:59 PM GMT",
154+
1452574638 : "Tuesday, January 12, 2016 at 4:57:18 AM GMT", 1455685038 : "Wednesday, February 17, 2016 at 4:57:18 AM GMT",
155+
1458622638 : "Tuesday, March 22, 2016 at 4:57:18 AM GMT", 1459745838 : "Monday, April 4, 2016 at 4:57:18 AM GMT",
156+
1462597038 : "Saturday, May 7, 2016 at 4:57:18 AM GMT", 1465534638 : "Friday, June 10, 2016 at 4:57:18 AM GMT",
157+
1469854638 : "Saturday, July 30, 2016 at 4:57:18 AM GMT", 1470718638 : "Tuesday, August 9, 2016 at 4:57:18 AM GMT",
158+
1473915438 : "Thursday, September 15, 2016 at 4:57:18 AM GMT", 1477285038 : "Monday, October 24, 2016 at 4:57:18 AM GMT",
159+
1478062638 : "Wednesday, November 2, 2016 at 4:57:18 AM GMT", 1482641838 : "Sunday, December 25, 2016 at 4:57:18 AM GMT"
160+
]
161+
162+
let f = NSDateFormatter()
163+
f.dateStyle = .FullStyle
164+
f.timeStyle = .FullStyle
165+
f.timeZone = NSTimeZone(name: DEFAULT_TIMEZONE)
166+
f.locale = NSLocale(localeIdentifier: DEFAULT_LOCALE)
167+
168+
for (timestamp, stringResult) in timestamps {
169+
170+
let testDate = NSDate(timeIntervalSince1970: timestamp)
171+
let sf = f.stringFromDate(testDate)
172+
173+
XCTAssertEqual(sf, stringResult)
174+
}
175+
176+
}
177+
178+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ XCTMain([
2929
testCase(TestNSCharacterSet.allTests),
3030
testCase(TestNSData.allTests),
3131
testCase(TestNSDate.allTests),
32+
testCase(TestNSDateFormatter.allTests),
3233
testCase(TestNSDictionary.allTests),
3334
testCase(TestNSFileManger.allTests),
3435
testCase(TestNSGeometry.allTests),

0 commit comments

Comments
 (0)