Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[ios] duration to time interval fix - #8276

Merged
fabian-guerra merged 1 commit into
release-ios-v3.5.0-android-v5.0.0from
fabian-duration-time-interval-fix
Mar 4, 2017
Merged

fabian-guerra merged 1 commit into
release-ios-v3.5.0-android-v5.0.0from
fabian-duration-time-interval-fix

Conversation

@fabian-guerra

Copy link
Copy Markdown
Contributor

Fixes #8264

@fabian-guerra fabian-guerra added the iOS Mapbox Maps SDK for iOS label Mar 3, 2017
@fabian-guerra fabian-guerra added this to the ios-v3.5.0 milestone Mar 3, 2017
@fabian-guerra fabian-guerra self-assigned this Mar 3, 2017
@mention-bot

Copy link
Copy Markdown

@fabian-guerra, thanks for your PR! By analyzing this pull request, we identified @incanus, @boundsj and @1ec5 to be potential reviewers.

@1ec5 1ec5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few optional changes, but this PR looks good.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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;.

@1ec5

1ec5 commented Mar 3, 2017

Copy link
Copy Markdown
Contributor

Actually, sharing the tests between iOS and macOS is pretty important, so if you could take care of that, that’d be 👌.

@incanus incanus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pending @1ec5's recommendations.

@fabian-guerra
fabian-guerra force-pushed the fabian-duration-time-interval-fix branch from 4d81add to fabad6f Compare March 4, 2017 00:36
@fabian-guerra
fabian-guerra merged commit 1f1cc13 into release-ios-v3.5.0-android-v5.0.0 Mar 4, 2017
@fabian-guerra
fabian-guerra deleted the fabian-duration-time-interval-fix branch March 4, 2017 01:11

@1ec5 1ec5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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();
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

iOS Mapbox Maps SDK for iOS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants