Skip to content

Commit 5cdf3ae

Browse files
committed
fix issues with date formatter (#10)
1 parent 866b1a7 commit 5cdf3ae

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

ModelRocket/JSONTransformable.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ extension String: JSONTransformable {
8080
extension NSDate: JSONTransformable {
8181
private class var JSONTransformableDateFormatter: NSDateFormatter {
8282
let dateFormatter = NSDateFormatter()
83-
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
83+
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
84+
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
85+
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
8486
return dateFormatter
8587
}
8688
public class func fromJSON(json: JSON) -> NSDate? {

ModelRocketTests/ModelRocketTests.swift

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ModelRocketTests: XCTestCase {
4040
// Put setup code here. This method is called before the invocation of each test method in the class.
4141

4242
// Setup test model
43-
var jsonString = "{\"string\" : \"Test string\", \"date\" : \"2015-02-04T18:30:15.000Z\", \"color\" : \"#00FF00\", \"bool\" : true, \"url\" : \"http://ovenbits.com\", \"number\": 3, \"double\" : 7.5, \"float\" : 4.75, \"int\" : -23, \"u_int\" : 25, \"string_enum\" : \"String1\", \"int_enum\" : 0}"
43+
var jsonString = "{\"string\" : \"Test string\", \"date\" : \"2015-02-04T18:30:15.000Z\", \"local_date\" : \"2015-02-04T18:30:15.000-0600\", \"color\" : \"#00FF00\", \"bool\" : true, \"url\" : \"http://ovenbits.com\", \"number\": 3, \"double\" : 7.5, \"float\" : 4.75, \"int\" : -23, \"u_int\" : 25, \"string_enum\" : \"String1\", \"int_enum\" : 0}"
4444
var jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
4545
var json = JSON(data: jsonData!)
4646
testModel = TestModel(json: json)
@@ -76,7 +76,7 @@ class ModelRocketTests: XCTestCase {
7676
testVeryComplexNestedModel = TestVeryComplexNestedModel(json: json)
7777

7878
// Setup test subclass model
79-
jsonString = "{\"string\" : \"Test string\", \"date\" : \"2015-02-04T18:30:15.000Z\", \"color\" : \"#00FF00\", \"bool\" : true, \"url\" : \"http://ovenbits.com\", \"number\": 3, \"double\" : 7.5, \"float\" : 4.75, \"int\" : -23, \"u_int\" : 25, \"string2\" : \"Test string 2\"}"
79+
jsonString = "{\"string\" : \"Test string\", \"date\" : \"2015-02-04T18:30:15.000Z\", \"local_date\" : \"2015-02-04T18:30:15.000-0600\", \"color\" : \"#00FF00\", \"bool\" : true, \"url\" : \"http://ovenbits.com\", \"number\": 3, \"double\" : 7.5, \"float\" : 4.75, \"int\" : -23, \"u_int\" : 25, \"string2\" : \"Test string 2\"}"
8080
jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
8181
json = JSON(data: jsonData!)
8282
testSubclassModel = TestSubclassModel(json: json)
@@ -101,11 +101,13 @@ class ModelRocketTests: XCTestCase {
101101

102102
func testEquatableProperty() {
103103
let dateFormatter = NSDateFormatter()
104-
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
104+
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
105105
let date = dateFormatter.dateFromString("2015-02-04T18:30:15.000Z")
106+
let localDate = dateFormatter.dateFromString("2015-02-04T18:30:15.000-0600")
106107

107108
XCTAssertTrue(testModel.string == Property<String>(key: "string", defaultValue: "Test string"), "Properties not equal")
108109
XCTAssertTrue(testModel.date == Property<NSDate>(key: "date", defaultValue: date), "Properties not equal")
110+
XCTAssertTrue(testModel.localDate == Property<NSDate>(key: "local_date", defaultValue: localDate), "Properties not equal")
109111
XCTAssertTrue(testModel.color == Property<UIColor>(key: "color", defaultValue: .greenColor()), "Properties not equal")
110112
XCTAssertTrue(testModel.bool == Property<Bool>(key: "bool", defaultValue: true), "Properties not equal")
111113
XCTAssertTrue(testModel.url == Property<NSURL>(key: "url", defaultValue: NSURL(string: "http://ovenbits.com")), "Properties not equal")
@@ -132,6 +134,8 @@ class ModelRocketTests: XCTestCase {
132134
if let date = testModel.date.value {
133135

134136
let calendar = NSCalendar.currentCalendar()
137+
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
138+
135139
let units: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second, .Nanosecond]
136140
let components = calendar.components(units, fromDate: date)
137141

@@ -147,6 +151,27 @@ class ModelRocketTests: XCTestCase {
147151
}
148152
}
149153

154+
func testLocalDate() {
155+
if let date = testModel.localDate.value {
156+
157+
let calendar = NSCalendar.currentCalendar()
158+
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
159+
160+
let units: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second, .Nanosecond]
161+
let components = calendar.components(units, fromDate: date)
162+
163+
XCTAssertEqual(components.year, 2015, "Date: years not equal")
164+
XCTAssertEqual(components.month, 2, "Date: months not equal")
165+
XCTAssertEqual(components.day, 5, "Date: days not equal")
166+
XCTAssertEqual(components.hour, 0, "Date: hours not equal")
167+
XCTAssertEqual(components.minute, 30, "Date: minutes not equal")
168+
XCTAssertEqual(components.second, 15, "Date: seconds not equal")
169+
}
170+
else {
171+
XCTAssert(false, "Test date should not be nil")
172+
}
173+
}
174+
150175
func testColor() {
151176
if let color = testModel.color.value {
152177
XCTAssertEqual(color, UIColor.greenColor(), "Colors not equal")
@@ -222,7 +247,8 @@ class ModelRocketTests: XCTestCase {
222247
func testJSON() {
223248
if let json = testModel.json().json {
224249
XCTAssertEqual(json["string"].stringValue, "Test string", "Strings not equal")
225-
XCTAssertEqual(json["date"].stringValue, "2015-02-04T18:30:15.000Z", "Dates not equal")
250+
XCTAssertEqual(json["date"].stringValue, "2015-02-04T18:30:15.000+0000", "Dates not equal")
251+
XCTAssertEqual(json["local_date"].stringValue, "2015-02-05T00:30:15.000+0000", "Dates not equal")
226252
XCTAssertEqual(json["color"].stringValue, "#00FF00", "Colors not equal")
227253
XCTAssertEqual(json["bool"].boolValue, true, "Bools not equal")
228254

@@ -254,6 +280,8 @@ class ModelRocketTests: XCTestCase {
254280

255281
if let date = unarchived.date.value {
256282
let calendar = NSCalendar.currentCalendar()
283+
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
284+
257285
let units: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second, .Nanosecond]
258286
let components = calendar.components(units, fromDate: date)
259287

@@ -268,6 +296,24 @@ class ModelRocketTests: XCTestCase {
268296
XCTAssert(false, "Coding: date should not be nil")
269297
}
270298

299+
if let date = unarchived.localDate.value {
300+
let calendar = NSCalendar.currentCalendar()
301+
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
302+
303+
let units: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second, .Nanosecond]
304+
let components = calendar.components(units, fromDate: date)
305+
306+
XCTAssertEqual(components.year, 2015, "Date: years not equal")
307+
XCTAssertEqual(components.month, 2, "Date: months not equal")
308+
XCTAssertEqual(components.day, 5, "Date: days not equal")
309+
XCTAssertEqual(components.hour, 0, "Date: hours not equal")
310+
XCTAssertEqual(components.minute, 30, "Date: minutes not equal")
311+
XCTAssertEqual(components.second, 15, "Date: seconds not equal")
312+
}
313+
else {
314+
XCTAssert(false, "Coding: date should not be nil")
315+
}
316+
271317
if let color = unarchived.color.value { XCTAssertEqual(color, UIColor.greenColor(), "Colors not equal") }
272318
else { XCTAssert(false, "Coding: color should not be nil") }
273319

@@ -475,7 +521,8 @@ class ModelRocketTests: XCTestCase {
475521
func testSubclassJSON() {
476522
if let json = testSubclassModel.json().json {
477523
XCTAssertEqual(json["string"].stringValue, "Test string", "Strings not equal")
478-
XCTAssertEqual(json["date"].stringValue, "2015-02-04T18:30:15.000Z", "Dates not equal")
524+
XCTAssertEqual(json["date"].stringValue, "2015-02-04T18:30:15.000+0000", "Dates not equal")
525+
XCTAssertEqual(json["local_date"].stringValue, "2015-02-05T00:30:15.000+0000", "Dates not equal")
479526
XCTAssertEqual(json["color"].stringValue, "#00FF00", "Colors not equal")
480527
XCTAssertEqual(json["bool"].boolValue, true, "Bools not equal")
481528

@@ -566,6 +613,7 @@ class ModelRocketTests: XCTestCase {
566613
class TestModel: Model {
567614
let string = Property<String>(key: "string")
568615
let date = Property<NSDate>(key: "date")
616+
let localDate = Property<NSDate>(key: "local_date")
569617
let color = Property<UIColor>(key: "color")
570618
let bool = Property<Bool>(key: "bool")
571619
let url = Property<NSURL>(key: "url")

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Vehicle: Model {
9191
In addition to the core types above, ModelRocket also supports serialization for several other
9292
classes out of the box:
9393

94-
- `NSDate` &mdash; ISO8601-formatted string (`2015-05-31T19:00:17+00:00`)
94+
- `NSDate` &mdash; ISO8601-formatted string (`2015-05-31T19:00:17.000+0000`)
9595
- `UIColor` &mdash; hex-color string (`#f6c500`)
9696
- `NSURL` &mdash; any url string (`http://ovenbits.com`)
9797
- `NSNumber` &mdash; any number, can be used in place of `Double`, `Float`, `Int`, and `UInt`

0 commit comments

Comments
 (0)