Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Change DateFormat to use the full ISO8601 date format by default #45

Merged
merged 8 commits into from
Jun 11, 2019

Conversation

MrMage
Copy link
Contributor

@MrMage MrMage commented Mar 22, 2019

No description provided.

@MrMage
Copy link
Contributor Author

MrMage commented Mar 27, 2019

@tanner0101 would you mind taking a look at this?

@tanner0101 tanner0101 added the enhancement New feature or request label Apr 12, 2019
Copy link
Member

@tanner0101 tanner0101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be considered a breaking change if someone is depending on that format. We should probably make this configurable instead.

@MrMage
Copy link
Contributor Author

MrMage commented Apr 15, 2019

Given that users can already provide any date format as an argument to the Leaf tag, I'm not feeling strongly about what exactly the default should be. However, I read somewhere (I think in the docs, but can't find it right now) that an ISO8601 date format will be used by default, so that documentation should probably be updated.

@tanner0101
Copy link
Member

I think it could make sense to move Vapor 4's Leaf to using 8601 by default. That seems like the better choice.

For 3 though, to avoid surprising anyone, we could make this configurable. Perhaps by passing a custom DateFormatter to the tag's init:

Then we could even provide some static methods for creating an 8601-default tag.

…er' of github.com:vapor/template-kit into patch-4.
@MrMage
Copy link
Contributor Author

MrMage commented Apr 26, 2019

@tanner0101 I have made this configurable now; please take another look.

Copy link
Member

@tanner0101 tanner0101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateFormatter is thread safe on new versions of macOS apparently:

In macOS 10.9 and later NSDateFormatter is thread safe so long as you are using the modern behavior in a 64-bit app.

But I'm not sure if this holds true for Linux. The safer option here would be to not share them between threads.

Unfortunately in Vapor 3, a single tag can be shared across threads, so if we store a DateFormatter there directly, it will be used from multiple threads. Storing a closure that creates a DateFormatter could solve this. Static methods of course will be shared across threads, too.

(Side note, in Vapor 4 all services are bound to an event loop. So it would be appropriate there to store the DateFormatter directly).

private let defaultDateFormatter: DateFormatter

private static let dateAndTimeFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateFormatter is a class and I don't think it's thread safe. This probably needs to be a factory method that produces a new formatter each time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spotted the same issue and checked; it is thread safe: https://developer.apple.com/documentation/foundation/dateformatter#1680059

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://bugs.swift.org/browse/SR-7745

That documentation references Darwin platforms. I'm not sure it holds true for Linux. Vapor has definitely had issues with DateFormatter and multi-threading in the past. Maybe they are resolved now? Vapor 3 must support Ubuntu 14.04 and Swift 4.1.x though. So even if multi-threading has been fixed recently, we can't really rely on that.

/// Creates a new `DateFormat` tag renderer.
/// - parameter defaultDateFormatter: The date formatter to use when the tag invocation
/// does not specify a date format.
public init(defaultDateFormatter: DateFormatter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also probably needs to be an escaping closure that creates a DateFormatter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

@MrMage
Copy link
Contributor Author

MrMage commented Apr 29, 2019

@tanner0101 thank you for the elaboration! And sorry, I hadn't seen your main review comment.

I've updated the PR to use a date formatter factory now.

@MrMage MrMage closed this Apr 29, 2019
@MrMage MrMage reopened this Apr 29, 2019
@MrMage
Copy link
Contributor Author

MrMage commented May 7, 2019

@tanner0101 could you please take another look at this?

@MrMage
Copy link
Contributor Author

MrMage commented Jun 6, 2019

Friendly ping @tanner0101 :-)

Copy link
Member

@tanner0101 tanner0101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM, although I'd like to make sure the tests run on Linux. Can you run swift test --generate-linuxmain here to ensure all of the new ones are running in CI?

It might also be useful to do a pressure test on DateFormat across two threads just to be sure this change won't introduce any seg faults on Linux.

@MrMage
Copy link
Contributor Author

MrMage commented Jun 11, 2019

Updated the test manifest and added a stress test; please take another look.

Copy link
Member

@tanner0101 tanner0101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks a ton!

@tanner0101 tanner0101 merged commit 51405c8 into vapor:master Jun 11, 2019
@penny-coin
Copy link

Hey @MrMage, you just merged a pull request, have a coin!

You now have 30 coins.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants