Repository navigation
[ios] duration to time interval fix - #8276
fabian-guerra merged 1 commit into
Conversation
|
@fabian-guerra, thanks for your PR! By analyzing this pull request, we identified @incanus, @boundsj and @1ec5 to be potential reviewers. |
1ec5
left a comment
There was a problem hiding this comment.
A few optional changes, but this PR looks good.
There was a problem hiding this comment.
FYI, since this is Objective-C++ code, you could condense these lines somewhat by passing duration.count() directly into the duration_cast. You can also use auto instead of declaring the local variable to be of type std::chrono::seconds. What you’ve got is totally reasonable, though.
There was a problem hiding this comment.
Since NSDate+MGLAdditions.h is shared between iOS and macOS, the tests should be shared as well. Can you move this file to platform/darwin/test/ and add it to macos.xcodeproj?
There was a problem hiding this comment.
Since chrono.hpp imports <chrono>, you can write using namespace std::chrono_literals; at the top, then use auto expectedDuration = 5000000000ns; or even mbgl::Duration expectedDuration = 5s;.
|
Actually, sharing the tests between iOS and macOS is pretty important, so if you could take care of that, that’d be 👌. |
4d81add to
fabad6f
Compare
1ec5
left a comment
There was a problem hiding this comment.
This change appears to have added three identical MGLNSDateAdditionsTests.mm files, each in a different directory. There should only be one, under platform/darwin/test/.
| NSTimeInterval MGLTimeIntervalFromDuration(mbgl::Duration duration) | ||
| { | ||
| return duration.count(); | ||
| std::chrono::nanoseconds nano(duration.count()); |
There was a problem hiding this comment.
This implementation makes the unsafe assumption that mbgl::Duration's count is in nanoseconds. mbgl::Duration is a typedef to std::chrono::steady_clock::duration, and std::chrono::steady_clock does not specify a particular tick period.
In addition, this implementation truncates any fractional seconds in the duration. NSTimeInterval is a floating-point type, and so the conversion should be expected to preserve fractional seconds.
I believe the correct conversion is as follows (but don't trust me -- write some more tests 😄):
NSTimeInterval MGLTimeIntervalFromDuration(mbgl::Duration duration)
{
std::chrono::duration<NSTimeInterval, std::ratio<1>>(duration).count();
}
Fixes #8264