Skip to content

Commit

Permalink
fix issues with date formatter (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandon committed Nov 18, 2015
1 parent 866b1a7 commit 5cdf3ae
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
4 changes: 3 additions & 1 deletion ModelRocket/JSONTransformable.swift
Expand Up @@ -80,7 +80,9 @@ extension String: JSONTransformable {
extension NSDate: JSONTransformable {
private class var JSONTransformableDateFormatter: NSDateFormatter {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
return dateFormatter
}
public class func fromJSON(json: JSON) -> NSDate? {
Expand Down
58 changes: 53 additions & 5 deletions ModelRocketTests/ModelRocketTests.swift
Expand Up @@ -40,7 +40,7 @@ class ModelRocketTests: XCTestCase {
// Put setup code here. This method is called before the invocation of each test method in the class.

// Setup test model
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}"
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}"
var jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
var json = JSON(data: jsonData!)
testModel = TestModel(json: json)
Expand Down Expand Up @@ -76,7 +76,7 @@ class ModelRocketTests: XCTestCase {
testVeryComplexNestedModel = TestVeryComplexNestedModel(json: json)

// Setup test subclass model
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\"}"
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\"}"
jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
json = JSON(data: jsonData!)
testSubclassModel = TestSubclassModel(json: json)
Expand All @@ -101,11 +101,13 @@ class ModelRocketTests: XCTestCase {

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

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

let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)

let units: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second, .Nanosecond]
let components = calendar.components(units, fromDate: date)

Expand All @@ -147,6 +151,27 @@ class ModelRocketTests: XCTestCase {
}
}

func testLocalDate() {
if let date = testModel.localDate.value {

let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)

let units: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second, .Nanosecond]
let components = calendar.components(units, fromDate: date)

XCTAssertEqual(components.year, 2015, "Date: years not equal")
XCTAssertEqual(components.month, 2, "Date: months not equal")
XCTAssertEqual(components.day, 5, "Date: days not equal")
XCTAssertEqual(components.hour, 0, "Date: hours not equal")
XCTAssertEqual(components.minute, 30, "Date: minutes not equal")
XCTAssertEqual(components.second, 15, "Date: seconds not equal")
}
else {
XCTAssert(false, "Test date should not be nil")
}
}

func testColor() {
if let color = testModel.color.value {
XCTAssertEqual(color, UIColor.greenColor(), "Colors not equal")
Expand Down Expand Up @@ -222,7 +247,8 @@ class ModelRocketTests: XCTestCase {
func testJSON() {
if let json = testModel.json().json {
XCTAssertEqual(json["string"].stringValue, "Test string", "Strings not equal")
XCTAssertEqual(json["date"].stringValue, "2015-02-04T18:30:15.000Z", "Dates not equal")
XCTAssertEqual(json["date"].stringValue, "2015-02-04T18:30:15.000+0000", "Dates not equal")
XCTAssertEqual(json["local_date"].stringValue, "2015-02-05T00:30:15.000+0000", "Dates not equal")
XCTAssertEqual(json["color"].stringValue, "#00FF00", "Colors not equal")
XCTAssertEqual(json["bool"].boolValue, true, "Bools not equal")

Expand Down Expand Up @@ -254,6 +280,8 @@ class ModelRocketTests: XCTestCase {

if let date = unarchived.date.value {
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)

let units: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second, .Nanosecond]
let components = calendar.components(units, fromDate: date)

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

if let date = unarchived.localDate.value {
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)

let units: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second, .Nanosecond]
let components = calendar.components(units, fromDate: date)

XCTAssertEqual(components.year, 2015, "Date: years not equal")
XCTAssertEqual(components.month, 2, "Date: months not equal")
XCTAssertEqual(components.day, 5, "Date: days not equal")
XCTAssertEqual(components.hour, 0, "Date: hours not equal")
XCTAssertEqual(components.minute, 30, "Date: minutes not equal")
XCTAssertEqual(components.second, 15, "Date: seconds not equal")
}
else {
XCTAssert(false, "Coding: date should not be nil")
}

if let color = unarchived.color.value { XCTAssertEqual(color, UIColor.greenColor(), "Colors not equal") }
else { XCTAssert(false, "Coding: color should not be nil") }

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

Expand Down Expand Up @@ -566,6 +613,7 @@ class ModelRocketTests: XCTestCase {
class TestModel: Model {
let string = Property<String>(key: "string")
let date = Property<NSDate>(key: "date")
let localDate = Property<NSDate>(key: "local_date")
let color = Property<UIColor>(key: "color")
let bool = Property<Bool>(key: "bool")
let url = Property<NSURL>(key: "url")
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -91,7 +91,7 @@ class Vehicle: Model {
In addition to the core types above, ModelRocket also supports serialization for several other
classes out of the box:

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

0 comments on commit 5cdf3ae

Please sign in to comment.