[swift-corelibs-dev] Suggested DateInterval API improvement

Dave Lyon dave at davelyon.net
Fri Jun 17 16:01:11 CDT 2016


In attempting to use the new DateInterval value type to improve some
existing code, I ran in to an issue where DateIntervals cannot be "reverse"
intervals, which is contrary to how TimeInterval works, and somewhat
confusing.

I would propose that the DateInterval value type should be able to be
properly initialized with any TimeInterval, as a reference date and time
interval are all that is actually required in order to construct a
DateInterval properly.

The simplest solution might be to change the `startDate:interval:`
initializer to one allows for negative time intervals, such as:

public init(withInterval: TimeInterval, fromDate: Date) {
    self.start = Date(timeInterval: interval, since: fromDate)
    self.duration = abs(interval)
}

I believe in general it is preferable to avoid preconditions that require a
subset of a given input type (in this case, that TimeInterval be positive
or 0), and would prefer to see an API where invalid values are properly
converted from the given input to the documented output. E.g. the old “Be
generous with input, strict with output” idea.

I hope this is the right place to bring this up, but if not I’m happy to
move the conversation to Radar or elsewhere.

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20160617/1a31240c/attachment.html>


More information about the swift-corelibs-dev mailing list