This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[ios] duration to time interval fix #8276
Merged
fabian-guerra
merged 1 commit into
release-ios-v3.5.0-android-v5.0.0
from
fabian-duration-time-interval-fix
Mar 4, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
#import "MGLFoundation.h" | ||
#include <mbgl/util/chrono.hpp> | ||
|
||
@interface NSDate (MGLAdditions) | ||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
|
||
/// Converts from a duration in seconds to a duration object usable in mbgl. | ||
mbgl::Duration MGLDurationInSecondsFromTimeInterval(NSTimeInterval duration); | ||
MGL_EXPORT | ||
mbgl::Duration MGLDurationFromTimeInterval(NSTimeInterval duration); | ||
|
||
/// Converts from an mbgl duration object to a duration in seconds. | ||
NSTimeInterval MGLTimeIntervalFromDurationInSeconds(mbgl::Duration duration); | ||
MGL_EXPORT | ||
NSTimeInterval MGLTimeIntervalFromDuration(mbgl::Duration duration); | ||
|
||
@end | ||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
#import "NSDate+MGLAdditions.h" | ||
|
||
@implementation NSDate (MGLAdditions) | ||
|
||
mbgl::Duration MGLDurationInSecondsFromTimeInterval(NSTimeInterval duration) | ||
mbgl::Duration MGLDurationFromTimeInterval(NSTimeInterval duration) | ||
{ | ||
return std::chrono::duration_cast<mbgl::Duration>(std::chrono::duration<NSTimeInterval>(duration)); | ||
} | ||
|
||
NSTimeInterval MGLTimeIntervalFromDurationInSeconds(mbgl::Duration duration) | ||
NSTimeInterval MGLTimeIntervalFromDuration(mbgl::Duration duration) | ||
{ | ||
return duration.count(); | ||
std::chrono::nanoseconds nano(duration.count()); | ||
std::chrono::seconds sec = std::chrono::duration_cast<std::chrono::seconds>(nano); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
return sec.count(); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#import <XCTest/XCTest.h> | ||
|
||
#include <mbgl/util/chrono.hpp> | ||
#import "../../darwin/src/NSDate+MGLAdditions.h" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
@interface MGLNSDateAdditionsTests : XCTestCase | ||
@end | ||
|
||
@implementation MGLNSDateAdditionsTests | ||
|
||
- (void)testDurationToNSTimeInterval { | ||
|
||
NSTimeInterval timeInterval = 5; | ||
mbgl::Duration duration = MGLDurationFromTimeInterval(timeInterval); | ||
NSTimeInterval durationTimeInterval = MGLTimeIntervalFromDuration(duration); | ||
|
||
mbgl::Duration expectedDuration = 5s; | ||
|
||
XCTAssertEqual(timeInterval, durationTimeInterval); | ||
XCTAssertEqual(timeInterval, MGLTimeIntervalFromDuration(expectedDuration)); | ||
|
||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#import <XCTest/XCTest.h> | ||
|
||
#include <mbgl/util/chrono.hpp> | ||
#import "../../darwin/src/NSDate+MGLAdditions.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
|
||
using namespace std::chrono_literals; | ||
|
||
@interface MGLNSDateAdditionsTests : XCTestCase | ||
@end | ||
|
||
@implementation MGLNSDateAdditionsTests | ||
|
||
- (void)testDurationToNSTimeInterval { | ||
|
||
NSTimeInterval timeInterval = 5; | ||
mbgl::Duration duration = MGLDurationFromTimeInterval(timeInterval); | ||
NSTimeInterval durationTimeInterval = MGLTimeIntervalFromDuration(duration); | ||
|
||
mbgl::Duration expectedDuration = 5s; | ||
|
||
XCTAssertEqual(timeInterval, durationTimeInterval); | ||
XCTAssertEqual(timeInterval, MGLTimeIntervalFromDuration(expectedDuration)); | ||
|
||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 tostd::chrono::steady_clock::duration
, andstd::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 😄):