Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 71fa951

Browse files
committed
Convert dateWithWordPressComJSONString: to Swift
This resulted in a change in the Swift API. I think this is acceptable because the method was not used outside WordPressKit. See: - WordPressAuthenticator https://github.com/search?q=repo%3Awordpress-mobile%2FWordPressAuthenticator-iOS%20dateWithWordPressComJSONString&type=code - WordPress-iOS https://github.com/search?q=repo%3Awordpress-mobile%2FWordPress-iOS+dateWithWordPressComJSONString&type=code
1 parent 51f0616 commit 71fa951

File tree

7 files changed

+25
-10
lines changed

7 files changed

+25
-10
lines changed

WordPressKit/NSDate+RFC3339.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ extension NSDate {
88
DateFormatter.rfc3339Formatter
99
}
1010

11+
/// Parses a date string
12+
///
13+
/// Dates in the format specified in http://www.w3.org/TR/NOTE-datetime should be OK.
14+
/// The kind of dates returned by the REST API should match that format, even if the doc promises ISO 8601.
15+
///
16+
/// Parsing the full ISO 8601, or even RFC 3339 is more complex than this, and makes no sense right now.
17+
///
18+
/// - Warning: This method doesn't support fractional seconds or dates with leap seconds (23:59:60 turns into 23:59:00)
19+
//
20+
// Needs to be `public` because of the usages in the Objective-C code.
21+
@objc(dateWithWordPressComJSONString:)
22+
public static func with(wordPressComJSONString jsonString: String) -> Date? {
23+
self.rfc3339DateFormatter().date(from: jsonString)
24+
}
25+
1126
@objc(WordPressComJSONString)
1227
public func wordPressComJSONString() -> String {
1328
NSDate.rfc3339DateFormatter().string(from: self as Date)

WordPressKit/NSDate+WordPressJSON.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
@warning this method doesn't support fractional seconds or dates with leap seconds (23:59:60 turns into 23:59:00)
1313
*/
1414

15-
+ (instancetype)dateWithWordPressComJSONString:(NSString *)string;
15+
//+ (instancetype)dateWithWordPressComJSONString:(NSString *)string;
1616

1717
@end

WordPressKit/RemoteCommentV2.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extension RemoteCommentV2: Decodable {
6565

6666
// since `date_gmt` is already in GMT timezone, manually add the timezone string to make the rfc3339 formatter happy (or it will throw otherwise).
6767
guard let dateString = try? container.decode(String.self, forKey: .date),
68-
let date = NSDate(wordPressComJSONString: dateString + "+00:00") as Date? else {
68+
let date = NSDate.with(wordPressComJSONString: dateString + "+00:00") else {
6969
throw DecodingError.dataCorruptedError(forKey: .date, in: container, debugDescription: "Date parsing failed")
7070
}
7171
self.date = date

WordPressKitTests/CommentServiceRemoteREST+APIv2Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class CommentServiceRemoteREST_APIv2Tests: RemoteTestCase, RESTTestable {
4141
XCTAssertEqual(firstComment.authorID, 135)
4242
XCTAssertEqual(firstComment.authorName, "John Doe")
4343
XCTAssertEqual(firstComment.authorURL, "https://example.com/john-doe")
44-
XCTAssertEqual(firstComment.date, NSDate(wordPressComJSONString: "2021-07-01T10:50:11+00:00") as Date)
44+
XCTAssertEqual(firstComment.date, NSDate.with(wordPressComJSONString: "2021-07-01T10:50:11+00:00"))
4545
XCTAssertEqual(firstComment.content, "<p>Some example comment.</p>\n")
4646
XCTAssertEqual(firstComment.link, "https://example.com/2021/05/25/example-post/comment-page-1/#comment-2")
4747
XCTAssertEqual(firstComment.status, "approve") // verify that it's converted correctly.

WordPressKitTests/CommentServiceRemoteRESTTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class CommentServiceRemoteRESTTests: RemoteTestCase, RESTTestable {
5050
XCTAssertEqual(comment.authorEmail, "[email protected]")
5151
XCTAssertEqual(comment.authorUrl, "author URL")
5252
XCTAssertEqual(comment.authorIP, "000.0.00.000")
53-
XCTAssertEqual(comment.date, NSDate(wordPressComJSONString: "2021-08-04T07:58:49+00:00") as Date)
53+
XCTAssertEqual(comment.date, NSDate.with(wordPressComJSONString: "2021-08-04T07:58:49+00:00"))
5454
XCTAssertEqual(comment.link, "comment URL")
5555
XCTAssertEqual(comment.parentID, nil)
5656
XCTAssertEqual(comment.postID, NSNumber(value: 1))
@@ -91,7 +91,7 @@ final class CommentServiceRemoteRESTTests: RemoteTestCase, RESTTestable {
9191
XCTAssertEqual(comment.authorEmail, "[email protected]")
9292
XCTAssertEqual(comment.authorUrl, "author URL")
9393
XCTAssertEqual(comment.authorIP, "000.0.00.000")
94-
XCTAssertEqual(comment.date, NSDate(wordPressComJSONString: "2021-08-04T07:58:49+00:00") as Date)
94+
XCTAssertEqual(comment.date, NSDate.with(wordPressComJSONString: "2021-08-04T07:58:49+00:00"))
9595
XCTAssertEqual(comment.link, "comment URL")
9696
XCTAssertEqual(comment.parentID, nil)
9797
XCTAssertEqual(comment.postID, NSNumber(value: 1))

WordPressKitTests/NSDate+RFC3339Tests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ class NSDateRFC3339Tests: XCTestCase {
1919

2020
func testValidRFC3339DateFromString() {
2121
XCTAssertEqual(
22-
NSDate(wordPressComJSONString: "2023-03-19T15:00:00Z"),
23-
NSDate(timeIntervalSince1970: 1_679_238_000)
22+
NSDate.with(wordPressComJSONString: "2023-03-19T15:00:00Z"),
23+
Date(timeIntervalSince1970: 1_679_238_000)
2424
)
2525
}
2626

2727
func testInvalidRFC3339DateFromString() {
28-
XCTAssertNil(NSDate(wordPressComJSONString: "2024-01-01"))
28+
XCTAssertNil(NSDate.with(wordPressComJSONString: "2024-01-01"))
2929
}
3030

3131
func testInvalidDateFromString() {
32-
XCTAssertNil(NSDate(wordPressComJSONString: "not a date"))
32+
XCTAssertNil(NSDate.with(wordPressComJSONString: "not a date"))
3333
}
3434

3535
func testValidRFC3339StringFromDate() {

WordPressKitTests/PostServiceRemoteRESTAutosaveTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PostServiceRemoteRESTAutosaveTests: RemoteTestCase, RESTTestable {
4141
XCTAssertEqual(remotePost.autosave.content, "<!-- wp:paragraph -->\n<p>Uno.</p>\n<!-- /wp:paragraph -->")
4242
XCTAssertEqual(remotePost.autosave.excerpt, "abc")
4343
XCTAssertEqual(remotePost.autosave.previewURL, "https://hello.wordpress.com/2019/10/28/hello-world/?preview=true&preview_nonce=07346f4e5d")
44-
XCTAssertEqual(remotePost.autosave.modifiedDate, NSDate(wordPressComJSONString: "2019-10-28T02:06:39+00:00") as Date?)
44+
XCTAssertEqual(remotePost.autosave.modifiedDate, NSDate.with(wordPressComJSONString: "2019-10-28T02:06:39+00:00"))
4545
expect.fulfill()
4646
}, failure: { _ in
4747
XCTFail("This callback shouldn't get called")

0 commit comments

Comments
 (0)