davedelong / time

Robust and type-safe date and time calculations for Swift
MIT License
2.32k stars 77 forks source link

Add forcedCopy() method to `Region` and `TimePeriod` #65

Closed iKenndac closed 1 year ago

iKenndac commented 1 year ago

Calendar/NSCalendar aren't thread-safe on Linux, which makes working with Time a bit challenging on that platform - many non-mutating methods end up calling into Foundation code that makes temporary mutations during the call. This ends up manifesting as confusing crashes, such as:

let range = minute..<minute.adding(days: 1) // Fatal error: Range requires lowerBound <= upperBound

This PR adds forcedCopy() methods to Region and TimePeriod (as well as documentation comments and a unit test), allowing clients to make thread-local deep copies of these objects in order to manually manage threading.

iKenndac commented 1 year ago

The failing tests on Linux are an existing issue where the Codable roundtrip for Region doesn't give a region object that is equal to the one prior to encoding (it's equivalent, but doesn't return true for ==). I plan to look at this in due time.